spc700 music playing code

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
spc700 music playing code
by on (#147228)
I'm trying to come up with a code to play music, and here's what I came up with so far. Any suggestions?

Code:
define sfx_timer($00)
define pitch_slide($01)
define sfx_number($03)
define frame_number($04)
define tempo_delay($05)

str $fa=#$80
str $f1=#$11

str $f2=#$0f
str $f3=#$7f   //FIR filter is all pass
str $f2=#$0c
str $f3=#$7f   //left volume 100%
str $f2=#$1c
str $f3=#$7f   //right volume 100%
str $f2=#$2c
str $f3=#$00   //echo volume 0%
str $f2=#$3c
str $f3=#$00   //echo volume 0%
str $f2=#$6c
str $f3=#$00
str $f2=#$0d
str $f3=#$00   //feedback 0%
str $f2=#$4d
str $f3=#$00   //no channel with echo
str $f2=#$5d
str $f3=#$80   //directory
str $f2=#$6d
str $f3=#$ff   //echo buffer
str $f2=#$7d
str $f3=#$00   //shortest delay possible

str $f2=#$00
str $f3=#$7f   //channel 0 left volume
str $f2=#$01
str $f3=#$7f   //channel 0 right volume
str $f2=#$02
str $f3=#$00
str $f2=#$03
str $f3=#$10   //original pitch
str $f2=#$04
str $f3=#$00   //square
str $f2=#$07
str $f3=#$7f   //gain up



str {tempo_delay}=#$08


spc_loop:
lda $fd
beq spc_loop

dec {tempo_delay}
bne spc_loop




ldx {frame_number}
txa
clc
adc #$04
sta {frame_number}
lda track,x
beq +
lda track+1,x
tay
lda note_scale,y
pha
lda track+2,x
tay
lda octave_scale,y
ply
mul
str $f2=#$02
sta $f3
str $f2=#$03
sty $f3
str $f2=#$4c
str $f3=#$01   //key on
+;

ldx {frame_number}
txa
clc
adc #$04
sta {frame_number}
lda track,x
beq +
lda track+1,x
tay
lda note_scale,y
pha
lda track+2,x
tay
lda octave_scale,y
ply
mul
str $f2=#$12
sta $f3
str $f2=#$13
sty $f3
str $f2=#$4c
str $f3=#$02   //key on
+;

ldx {frame_number}
txa
clc
adc #$04
sta {frame_number}
lda track,x
beq +
lda track+1,x
tay
lda note_scale,y
pha
lda track+2,x
tay
lda octave_scale,y
ply
mul
str $f2=#$22
sta $f3
str $f2=#$23
sty $f3
str $f2=#$4c
str $f3=#$04   //key on
+;

ldx {frame_number}
txa
clc
adc #$04
sta {frame_number}
lda track,x
beq +
lda track+1,x
tay
lda note_scale,y
pha
lda track+2,x
tay
lda octave_scale,y
ply
mul
str $f2=#$32
sta $f3
str $f2=#$33
sty $f3
str $f2=#$4c
str $f3=#$08   //key on
+;



ldx {frame_number}
txa
clc
adc #$04
sta {frame_number}
lda track,x
beq +
lda track+1,x
tay
lda note_scale,y
pha
lda track+2,x
tay
lda octave_scale,y
ply
mul
str $f2=#$42
sta $f3
str $f2=#$43
sty $f3
str $f2=#$4c
str $f3=#$10   //key on
+;

ldx {frame_number}
txa
clc
adc #$04
sta {frame_number}
lda track,x
beq +
lda track+1,x
tay
lda note_scale,y
pha
lda track+2,x
tay
lda octave_scale,y
ply
mul
str $f2=#$52
sta $f3
str $f2=#$53
sty $f3
str $f2=#$4c
str $f3=#$20   //key on
+;

ldx {frame_number}
txa
clc
adc #$04
sta {frame_number}
lda track,x
beq +
lda track+1,x
tay
lda note_scale,y
pha
lda track+2,x
tay
lda octave_scale,y
ply
mul
str $f2=#$62
sta $f3
str $f2=#$63
sty $f3
str $f2=#$4c
str $f3=#$40   //key on
+;

ldx {frame_number}
txa
clc
adc #$04
sta {frame_number}
lda track,x
beq +
lda track+1,x
tay
lda note_scale,y
pha
lda track+2,x
tay
lda octave_scale,y
ply
mul
str $f2=#$72
sta $f3
str $f2=#$73
sty $f3
str $f2=#$4c
str $f3=#$80   //key on
+;


str {tempo_delay}=#$08




jmp spc_loop


//track format: key on, pitch, octave, sample


track:
db $01,$00,$04,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
db $01,$02,$04,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
db $01,$04,$04,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
db $01,$05,$04,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
db $01,$07,$04,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
db $01,$09,$04,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
db $01,$0b,$04,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
db $01,$00,$05,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00






note_scale:
db 64,68,72,76,81,85,91,96,102,108,114,121


octave_scale:
db 1,2,4,8,16,32,64,128



seek($838000)
directory:
dw square
dw square


square:
db $83,$77,$77,$77,$77,$88,$88,$88,$88
Re: spc700 music playing code
by on (#147634)
I'm surprised nobody responded yet. When I get done with this does anybody want to compose music for my game?
Re: spc700 music playing code
by on (#147635)
psycopathicteen wrote:
I'm surprised nobody responded yet. When I get done with this does anybody want to compose music for my game?

Not many people have worked, or have even really tried, if at all, to work with the SPC 700. That's literally my last concern. Abount composing music, What kind of music are you even looking for? I probably won't really be able to help. I'm a sucker for crappy sounding guitar music like this: https://www.youtube.com/watch?v=S_6ChXTP9TA, so that's probably what you'll end up with if you ask me. I'm going to start making music whenever the thing bazz is making is done.
Re: spc700 music playing code
by on (#147637)
The very least you want to make a program that converts music from some exiting format to what your driver uses. Making music "blindly" by entering numbers isn't gonna attract a whole lot of people.
Re: spc700 music playing code
by on (#147661)
Is the ADSR supposed to stay at the sustain level until the note is keyed off?, because it's not doing it.
Re: spc700 music playing code
by on (#147690)
Quote:
Is the ADSR supposed to stay at the sustain level until the note is keyed off?, because it's not doing it.

Nope, sustain on the SPC700 is actually what would be called decay2 on other synths. The envelope continues to decay exponentially at sustain rate, except in the particular case where sustain rate is #0, then it actually sustains.
Re: spc700 music playing code
by on (#147693)
Very few instruments sustain at "0" though, organ is probably the biggest example.
How many db is one ADSR step ?
Re: spc700 music playing code
by on (#147694)
What are you refering to by "ADSR step" ?

According to Anomie's doc, Internally the ADSR uses 11-bit values internally, but the register for sustain level is only 3-bit, and manipulation via the GAIN modes is 7-bit. In all cases, the volume correction is linear internally so there's no such thing as an "ADSR step dB", so your question can't be answered.
Re: spc700 music playing code
by on (#147695)
I figured the ADSR envelope uses logarithmic attenuation scale, but if it is linear all the way then yes, there's no answer. That is so odd, linear scale and envelopes don't mix so well from my experience but now looking at the design of the device it makes sense, anything else would be fair bit extra complexity.
Re: spc700 music playing code
by on (#147701)
No, you didn't understand and/or I worded this badly.

The decrease of volume enveloppe is indeed exponential (although the attack is linear but that's another story). But there is not such a thing as a "step" as you seem to be describing in your question, making your question unanswerable.
Re: spc700 music playing code
by on (#147726)
There is a counter or such for the envelope generator, and each count is a "step" (and the steps aren't in log scale as I thought).
I got my answers when I looked through the SPC section in nocash's docs.
Re: spc700 music playing code
by on (#155917)
Does this sound like a good idea of organizing parameters?

Instrument parameters:
-sample number
-stereo panning
-pitch modulation enable
-noise enable
-echo enable
-ADSR and GAIN envelopes
-vibrato
-detuning amount
-channel joining

Track parameters:
-note
-instrument
-duration
-volume
-sweep
-key offs

I want to keep the less frequently changed parameters as "instrument parameters," and the more frequently changed parameters as "track parameters," to keep things neat and organized.
Re: spc700 music playing code
by on (#155919)
That seems like a good way to organize the parameters.

If it weren't for the fact that I have college work to attend to (which can seriously hamper my musical production at times), I would jump right in on the condition that the converter accepts IT files. That, and perhaps a nice quick way for me to test the music so I can do proper prototyping (whether it be by making a SNES ROM or a SPC file).