-
Notifications
You must be signed in to change notification settings - Fork 1
/
MusicWithMoreFeaturesYourTurn.bs2
48 lines (41 loc) · 2.38 KB
/
MusicWithMoreFeaturesYourTurn.bs2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
' {$STAMP BS2}
' {$PBASIC 2.5}
' MusicWithMoreFeaturesYourTurn.bs2: Changing data isn't exactly writing a new program....
notes DATA "C","C","A","G","E","G","D","P","C","C","A","G","E","G","Q"
octaves DATA 6, 7, 6, 6, 6, 6, 6, 6, 6, 7, 6, 6, 6, 6
times DATA 2, 4, 4, 4, 4, 2, 2, 4, 2, 4, 4, 4, 4, 2
dots DATA 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1
bpm CON 240
index VAR Byte
offset VAR Nib
noteName VAR Byte
noteFreq VAR Word
noteTime VAR Word
noteOctave VAR Nib
noteDot VAR Bit
wholeNote VAR Word
wholeNote=(60000/bpm)*4
DO UNTIL noteName="Q"
READ notes+index, noteName
LOOKDOWN noteName, ["C", "d", "D", "e", "E", "F", "g", "G", "a", "A", "b", "B", "P", "Q"], offset 'Lowercase letters are flat
LOOKUP offset, [4186, 4435, 4699, 4978, 5274, 5588, 5920, 6272, 6645, 7040, 7459, 7902, 0, 0], noteFreq
READ octaves+index, noteOctave
noteOctave=8-noteOctave 'Smart-by dividing the 8th octave frequency and not multiplying the 1st octave frequency (with rounding errors),
noteFreq=noteFreq/(DCD noteOctave)'we achieve better frequency precision at the expense of not supporting the 9th octave or above.
'However, these weird math operators like DCD are quite annoying-was Parallax really that tight on firmware
'space to not implement general-purpose exponent operators? It's possible! (Just look at any old C compiler!)
READ times+index, noteTime
noteTime=WholeNote/noteTime
READ dots+index, noteDot
'IF notedot=1 THEN noteTime=(noteTime*3)/2
IF notedot=1 THEN noteTime=noteTime+(noteTime/2) 'Many microcontrollers (like the PIC16 series in the Stamp)
'do not have a hardware multiplier/divider (this must be done
'in software). However, most microcontrollers at least have a
'hardware integer adder. By replacing a multiply by (3/2) with an "add one-half",
'we use only one multiply/divide operation instead of two, which gives a
'speed improvement. However, I'm not sure if the overhead of software
'multiplication is more or less than the overhead of the PBASIC interpreter.
FREQOUT 9, noteTime, noteFreq
index=index+1
LOOP
END