Well, the encoder is only used to make a .sav file that contains data to be used by the player. The only reason I made it a NES programm is that I didn't have enough skill in java back then, now I'd make a java programm instead since I've taken course for it last year and it's great.
The player that you'll need in your code is like that.
Code:
ldy #$00
-- ldx #$27 ;This values changes playback rate
- dex
bne -
lda [DMCPointerL],Y
lsr A
lsr A
lsr A
lsr A ;High nybble
tax
lda DPCMTable.w,X
clc
adc Last_DMCByte
sta Last_DMCByte
sta $4011
ldx #$28
- dex
bne -
lda [DMCPointerL],Y
and #$0f ;Low nybble
tax
lda DPCMTable.w,X
clc
adc Last_DMCByte
sta Last_DMCByte
sta $4011
iny
bne --
inc DMCPointerH
lda DMCPointerH
cmp #$c0 ;Stupid way to check the end of the sample
bcc --
- jmp -
DPCMTable ;Square table
.db 0, 1, 4, 9, 16, 25, 36, 49, -64, -49, -36, -25, -16, -9, -4, -1
The encoder should look liks that (sorry I'm just pasting it):
Code:
ldy #$00
- lda [DMCPointerL],Y
jsr PCMtoDPCM
asl A
asl A
asl A
asl A
sta Temp
inc DMCPointerL
lda [DMCPointerL],Y
jsr PCMtoDPCM
ora Temp
sta [DMCPointer2L],Y
inc DMCPointer2L
bne +
inc DMCPointer2H
+ inc DMCPointerL
bne -
inc DMCPointerH
lda DMCPointerH
bpl -
lda #$5a
sta [DMCPointer2L],Y
iny
lda Last_DMCByte
sta [DMCPointer2L],Y
iny
lda #$ff
sta [DMCPointer2L],Y
- jmp -
PCMtoDPCM ;Actual conversion routine
lsr A ;7-bit unsigned format
ldx #$00
sec
sbc Last_DMCByte ;Differentiate with last sample
- cmp DPCMTable2.w,X
bcc ++
inx
cpx #$0f
bne -
++ lda Last_DMCByte
clc
adc DPCMTable.w,X
sta Last_DMCByte
txa
rts
DPCMTable
.db 0, 1, 4, 9, 16, 25, 36, 49, -64, -49, -36, -25, -16, -9, -4, -1
DPCMTable2
.db 1, 2, 6, 12, 20, 30, 42, 72, -57, -43, -31, -21, -13, -7, -3, -1
Basically it computes the delta between each sample, and the square root of the (un-signed) delta is being encoded (with the original sign). For example if the delta is -5, you'll encode -2 because the square root of 5 is slightly above 2, and it's rounded down to 2, and the original sign is preserved. Each sample is coded on 4-bit.
When playing, you just use a signed square table to play back the samples. For some reason that square/root technique sound a whole lot better than the linear version of the same 4-bit dela encoding (I've tried it), and tepples found that.