/* * Ln Sets the duration (length) of the notes. The variable n does not indicate an actual duration * amount but rather a note type; L1 - whole note, L2 - half note, L4 - quarter note, etc. * (L8, L16, L32, L64, ...). By default, n = 4. * For triplets and quintets, use L3, L6, L12, ... and L5, L10, L20, ... series respectively. * The shorthand notation of length is also provided for a note. For example, "L4 CDE L8 FG L4 AB" * can be shortened to "L4 CDE F8G8 AB". F and G play as eighth notes while others play as quarter notes. * On Sets the current octave. Valid values for n are 0 through 6. An octave begins with C and ends with B. * Remember that C- is equivalent to B. * < > Changes the current octave respectively down or up one level. * Nn Plays a specified note in the seven-octave range. Valid values are from 0 to 84. (0 is a pause.) * Cannot use with sharp and flat. Cannot use with the shorthand notation neither. * MN Stand for Music Normal. Note duration is 7/8ths of the length indicated by Ln. It is the default mode. * ML Stand for Music Legato. Note duration is full length of that indicated by Ln. * MS Stand for Music Staccato. Note duration is 3/4ths of the length indicated by Ln. * Pn Causes a silence (pause) for the length of note indicated (same as Ln). * Tn Sets the number of "L4"s per minute (tempo). Valid values are from 32 to 255. The default value is T120. * . When placed after a note, it causes the duration of the note to be 3/2 of the set duration. * This is how to get "dotted" notes. "L4 C#." would play C sharp as a dotted quarter note. * It can be used for a pause as well. * MB MF Stand for Music Background and Music Foreground. MB places a maximum of 32 notes in the music buffer * and plays them while executing other statements. Works very well for games. * MF switches the PLAY mode back to normal. Default is MF. */ public STR2WAV(string snd) { snd = snd.ToUpper(); int i = 0; while (i < snd.Length) { char c = snd[i]; switch (c) { case 'L': { i++; string val = ""; while (i < snd.Length && char.IsDigit(snd[i])) { val += snd[i]; i++; } int duration = int.Parse(val); commands.Add(new PlayNoteLength(duration)); i--; } break; case 'T': { i++; string val = ""; while (i < snd.Length && char.IsDigit(snd[i])) { val += snd[i]; i++; } int duration = int.Parse(val); commands.Add(new PlayTempo(duration)); i--; } break; case 'M': { i++; switch (snd[i]) { case 'L': commands.Add(new PlayLegato()); break; case 'N': commands.Add(new PlayNormal()); break; case 'S': commands.Add(new PlayStaccato()); break; case 'B': commands.Add(new PlayMusicBackground()); break; case 'F': commands.Add(new PlayMusicForeground()); break; default: throw new Exception("Invalid duration " + snd[i] + "!"); } } break; case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': { bool flat = false; bool sharp = false; bool dotted = false; bool doubledotted = false; PlayNote n = new PlayNote(); i++; if (i < snd.Length && snd[i] == '+') { sharp = true; i++; } if (i < snd.Length && snd[i] == '-') { flat = true; i++; } if (i < snd.Length && snd[i] == '.') { dotted = true; i++; if (snd[i] == '.') { doubledotted = true; i++; } } if (i < snd.Length && char.IsDigit(snd[i])) { string val = ""; while (i < snd.Length && char.IsDigit(snd[i])) { val += snd[i]; i++; } int duration = int.Parse(val); if (i < snd.Length && snd[i] == '+') { sharp = true; i++; } if (i < snd.Length && snd[i] == '-') { flat = true; i++; } if (i < snd.Length && snd[i] == '.') { dotted = true; i++; if (snd[i] == '.') { doubledotted = true; i++; } } n.Note = c; n.Length = duration; } else { n.Note = c; } n.Flat = flat; n.Sharp = sharp; n.Dotted = dotted; n.DoubleDotted = doubledotted; commands.Add(n); i--; } break; case 'O': { i++; string val = ""; while (i < snd.Length && char.IsDigit(snd[i])) { val += snd[i]; i++; } int octave = int.Parse(val); commands.Add(new PlayOctave(octave)); i--; } break; case 'P': { i++; string val = ""; while (i < snd.Length && char.IsDigit(snd[i])) { val += snd[i]; i++; } int pause = int.Parse(val); commands.Add(new PlayPause(pause)); i--; } break; default: throw new Exception("Command " + c + " not implemented!"); } i++; } }
public void generate() { foreach (Command c in commands) { if (c as PlayTempo != null) { tempo = ((PlayTempo)c).Tempo; } else if (c as PlayNoteLength != null) { notelength = ((PlayNoteLength)c).Length; } else if (c as PlayMusicBackground != null) { MusicBackground = true; } else if (c as PlayMusicForeground != null) { MusicBackground = false; } else if (c as PlayNormal != null) { NoteDuration = NoteDurations.Normal; } else if (c as PlayLegato != null) { NoteDuration = NoteDurations.Legato; } else if (c as PlayStaccato != null) { NoteDuration = NoteDurations.Staccato; } else if (c as PlayNote != null) { PlayNote n = c as PlayNote; int noteno = n.getNumber(); double herz = FREQ[(12 * (octave + 2)) + noteno]; int thisnotelength = notelength; if (n.Length > 0) { thisnotelength = n.Length; } double totalseconds = (1.0 / (tempo / 60.0)) * (4.0 / thisnotelength); double noteseconds = totalseconds; double pause = 0.0; if (NoteDuration == NoteDurations.Normal) { noteseconds = noteseconds * (7.0 / 8.0); pause = totalseconds - noteseconds; } else if (NoteDuration == NoteDurations.Staccato) { noteseconds = noteseconds * (3.0 / 4.0); pause = totalseconds - noteseconds; } if (n.DoubleDotted) { noteseconds += (noteseconds / 2.0) + (noteseconds / 4.0); } else if (n.Dotted) { noteseconds += (noteseconds / 2.0); } Tone(herz, noteseconds); if (pause > 0.0) { Tone(0, pause); } } else if (c as PlayPause != null) { PlayPause pp = c as PlayPause; double totalseconds = (1.0 / (tempo / 60.0)) * (4.0 / pp.Pause); Tone(0, totalseconds); } else if (c as PlayOctave != null) { octave = ((PlayOctave)c).Octave; } else { throw new Exception("Not implemented " + c.GetType().ToString()); } } }