dither audio

This commit is contained in:
Brooke Vibber 2024-08-31 10:58:15 -07:00
parent 34df2b8fce
commit ad42390cdd
2 changed files with 36 additions and 20 deletions

View file

@ -129,17 +129,9 @@ wait_loop:
ldy VCOUNT ; 4 cycles ldy VCOUNT ; 4 cycles
bne wait_loop ; 2 cycles bne wait_loop ; 2 cycles
.macro audio_prep
; Y is VCOUNT at entry
lda (sample_ptr),y ; 5/6 cyc
sta audiotemp ; 3 cyc
.endmacro
.macro inner_scanline frame_offset, line_offset .macro inner_scanline frame_offset, line_offset
; Y should be VCOUNT at entry ; Y should be VCOUNT at entry
; it'll fire on unused lines, but harmlessly ; it'll fire on unused lines, but harmlessly
;ldy scanline ; 3 cyc
;inc scanline ; 5 cyc
; 23-26 cycles before break ; 23-26 cycles before break
; Leisurely memory fetches ; Leisurely memory fetches
@ -149,6 +141,7 @@ wait_loop:
lda frame1_palette3_even + frame_offset + line_offset - scanline_offset / 2,y ; 4/5 lda frame1_palette3_even + frame_offset + line_offset - scanline_offset / 2,y ; 4/5
tay ; 2 tay ; 2
pla ; 3 pla ; 3
; Wait for horizontal blank ; Wait for horizontal blank
sta WSYNC ; 4 sta WSYNC ; 4
@ -165,13 +158,21 @@ wait_loop:
sta AUDC1 ; 4 cyc sta AUDC1 ; 4 cyc
.endmacro .endmacro
.macro audio_prep
; Y is VCOUNT at entry
lda (sample_ptr),y ; 5/6 cyc
sta audiotemp ; 3 cyc
.endmacro
; call with A pre-loaded to audiotemp
.macro audio_play_lo .macro audio_play_lo
lda audiotemp ; 3 cyc ;lda audiotemp ; 3 cyc
and #$0f ; 2 cyc and #$0f ; 2 cyc
ora #$10 ; 2 cyc ora #$10 ; 2 cyc
sta AUDC1 ; 4 cyc sta AUDC1 ; 4 cyc
.endmacro .endmacro
; clobbers Y
.macro audio_play_hi ; 12 cycles .macro audio_play_hi ; 12 cycles
ldy audiotemp ; 3 cyc ldy audiotemp ; 3 cyc
lda audio_high_byte,y ; 5 cyc lda audio_high_byte,y ; 5 cyc
@ -212,11 +213,13 @@ wait_loop:
each_scanline_pair: each_scanline_pair:
sty scanline ; 3 cycles sty scanline ; 3 cycles
audio_prep
audio_play_lo
inner_scanline frame_offset, 0 ; 23-26 cycles before break, 12 cycles after inner_scanline frame_offset, 0 ; 23-26 cycles before break, 12 cycles after
ldy scanline ; 3 cycles audio_play_hi
audio_play_raw ; 8-10 cycles
ldy scanline ; 3 cycles
inner_scanline frame_offset, 128 ; 23-26 cycles before break, 12 cycles after inner_scanline frame_offset, 128 ; 23-26 cycles before break, 12 cycles after
; pair cleanup: 6 cycles ; pair cleanup: 6 cycles

View file

@ -14,31 +14,44 @@ function byte2byte(arr) {
return lines.join('\n'); return lines.join('\n');
} }
function to4bit(val8) { class Dither {
let val = (val8 + 7) >> 4; constructor() {
if (val > 15) { this.err = 0;
return 15; }
to4bit(val8) {
let val = (val8 / 255) - this.err;
if (val < 0) {
val = 0;
}
if (val > 1) {
val = 1;
}
let val4 = Math.round(val * 15);
let dithered = (val4 / 15);
this.err = (dithered - val);
return val4;
} }
return val;
} }
function pack(audio) { function pack(audio) {
let packed = []; let packed = [];
/* let dither = new Dither();
for (let i = 0; i < audio.length; i += 2) { for (let i = 0; i < audio.length; i += 2) {
// little-endian 4-bit samples // little-endian 4-bit samples
let low = to4bit(audio[i]); let low = dither.to4bit(audio[i]);
let high = to4bit(audio[i + 1]); let high = dither.to4bit(audio[i + 1]);
let byte = low | (high << 4); let byte = low | (high << 4);
packed.push(byte); packed.push(byte);
} }
*/ /*
// raw push bytes // raw push bytes
for (let i = 0; i < audio.length; i += 2) { for (let i = 0; i < audio.length; i += 2) {
let val = to4bit(audio[i]); let val = to4bit(audio[i]);
let byte = val | 0x10; let byte = val | 0x10;
packed.push(byte); packed.push(byte);
} }
*/
return packed; return packed;
} }