Here is some spc700 code I've been working on that could generate BRR samples for simple wave shapes stored as line segment data. It computes an 8-bit PCM wave, and then truncates it to 4-bit BRR. Line segments are allowed to wrap when passing 255 or 0.
Quote:
generate_brr_sample_library:
ldx #$00
-;
ldy #$00
lda ({source}),y
beq +
phx
jsr polygon_synth
plx
lda {brr_buffer}
sta {directory},x
sta {directory}+2,x
lda {brr_buffer}+1
sta {directory}+1,x
sta {directory}+3,x
inx
inx
inx
inx
tya
ldy #$00
adw {brr_buffer}
stw {brr_buffer}
bra -
+;
rts
polygon_synth:
ldx #$00
ldy #$00
str {temp}=#$00
lda ({source}),y
sta {countdown}
iny
lda ({source}),y
sta {header_byte}
-;
iny
lda ({source}),y
sta {temp2}
lda {temp}
iny
-;
clc
adc ({source}),y
sta {wave_buffer},x
inx
dec {temp2}
bne -
sta {temp}
dec {countdown}
bne --
txa
xcn
and #$0f
sta {#_of_blocks}
iny
tya
ldy #$00
adw {source}
stw {source}
jsr convert_wav_to_brr
rts
convert_wav_to_brr:
ldx #$00
ldy #$00
-;
lda {header_byte}
cmp {#_of_blocks}=#$01
bne +
inc
+;
sta ({brr_buffer}),y
str {temp2}=#$08
-;
lda {wave_buffer},x
and #$f0
sta {temp}
inx
lda {wave_buffer},x
and #$f0
xcn
ora {temp}
inx
iny
sta ({brr_buffer}),y
dec {temp2}
bne -
iny
dec {#_of_blocks}
bne --
rts
...and for the format:
Code:
byte 0: number of line segments in wave shape (0 means no more BRR samples)
byte 1: BRR header byte
repeats for "byte 0" amount of times
byte 2n+2: length in samples of line segment
byte 2n+3: 8-bit slope
Then it repeats for the next BRR sample until "byte 0" is 0.
Waves that are not divisible by 16, get clipped to the nearest multiple of 16.
Some examples:
Code:
square_wave:
db $04,$92 //4 line segments, with BRR header of $92
db $01,$7f //first line, 1 sample long, with slope of 127
db $1f,$00 //second line, 31 samples long, with no slope
db $01,$01 //third line, 1 sample long, slope is 1 but wraps around to -128
db $1f,$00 //forth line, 31 samples long, with no slope
saw_wave:
db $01,$92 //only 1 line segment needed, with BRR header of $92
db $40,$04 //64 samples long with a slope of 4, wraps around