 | Code: #define NUM_NOTES 89 #define DURATION_SCALE 8
string g_NoteNames[NUM_NOTES] = { "R", "A0", "A#0", "B0", "C1", "C#1", "D1", "D#1", "E1", "F1", "F#1", "G1", "G#1", "A1", "A#1", "B1", "C2", "C#2", "D2", "D#2", "E2", "F2", "F#2", "G2", "G#2", "A2", "A#2", "B2", "C3", "C#3", "D3", "D#3", "E3", "F3", "F#3", "G3", "G#3", "A3", "A#3", "B3", "C4", "C#4", "D4", "D#4", "E4", "F4", "F#4", "G4", "G#4", "A4", "A#4", "B4", "C5", "C#5", "D5", "D#5", "E5", "F5", "F#5", "G5", "G#5", "A5", "A#5", "B5", "C6", "C#6", "D6", "D#6", "E6", "F6", "F#6", "G6", "G#6", "A6", "A#6", "B6", "C7", "C#7", "D7", "D#7", "E7", "F7", "F#7", "G7", "G#7", "A7", "A#7", "B7", "C8" };
int g_NoteFreq[NUM_NOTES] = { 0, 28, 29, 31, 33, 35, 37, 39, 41, 44, 46, 49, 52, 55, 58, 62, 65, 69, 73, 78, 82, 87, 92, 98, 104, 110, 117, 123, 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247, 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494, 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988, 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1976, 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951, 4186 };
char g_StarWars[436] = "G4.2,G4.2,G4.2," "C5.12,G5.12," "F5.2,E5.2,D5.2,C6.12,G5.6," "F5.2,E5.2,D5.2,C6.12,G5.6," "F5.2,E5.2,F5.2,D5.12,G4.3,G4.3," "C5.12,G5.12," "F5.2,E5.2,D5.2,C6.12,G5.6," "F5.2,E5.2,D5.2,C6.12,G5.6," "F5.2,E5.2,F5.2,D5.12,G4.3,G4.3," "A4.9,A4.3,F5.3,E5.3,D5.3,C5.3," "C5.2,D5.2,E5.2,D5.6,B4.6,G4.3,G4.3," "A4.9,A4.3,F5.3,E5.3,D5.3,C5.3," "G5.6,D5.12,G4.3,G4.3," "A4.9,A4.3,F5.3,E5.3,D5.3,C5.3," "C5.2,D5.2,E5.2,D5.6,B4.6,G5.3,G5.3," "C6.3,A#5.3,G#5.3,G5.3,F5.3,D#5.3,D5.3,C5.3," "G5.18,";
int FindNoteFreq(char *pszNote, int len) { int freq = -1;
for (int i = 0; i < NUM_NOTES; i++) { if (strncmp(pszNote, g_NoteNames[i], len) == 0) { freq = g_NoteFreq[i]; break; } }
return freq; } //FindNoteFreq
int ParseNote(char *pszNote, int *freq, int *duration) { int n;
n = StringFind(pszNote, "."); if (n <= 0) { n = -1; } else { long data;
*freq = FindNoteFreq(pszNote, n); writeDebugStreamLine("%p: F=%d, %s", pszNote, *freq, pszNote); pszNote += n + 1; if (*freq == -1) { nxtDisplayTextLine(2, "Note not found!"); n = -1; } else if (!sscanf(pszNote, "%d", &data)) { nxtDisplayTextLine(2, "Invalid duration!"); n = -1; } else { int n1; *duration = data; n1 = StringFind(pszNote, ","); if (n1 > 0) { n += n1 + 2; } else { nxtDisplayTextLine(2, "No next note!"); n = -1; } } }
return n; } //ParseNote
task main() { char *pszSong = g_StarWars;
clearDebugStream(); while (true) { if (!bSoundActive) { int freq, duration; int n = ParseNote(pszSong, &freq, &duration);
if (n == -1) { nxtDisplayTextLine(0, "Parse Error!"); writeDebugStreamLine(pszSong); break; } else { nxtDisplayTextLine(1, "F=%04d,Dur=%d", freq, duration); PlayTone(freq, duration*DURATION_SCALE); pszSong += n; if (*pszSong == '\0') { pszSong = &g_StarWars[0]; } } }
EndTimeSlice(); } }
|  |