private static void PlayTrack(int trackIndex) { if (zeOutput == null) { zeOutput = OutputDevice.InstalledDevices[0]; zeOutput.Open(); /* * zeOutput.SendProgramChange(Channel.Channel1, Instrument.Lead1Square); * zeOutput.SendProgramChange(Channel.Channel2, Instrument.Lead1Square); */ } Track track = mTrackLibrary.Tracks[trackIndex]; ChannelPlayState[] playState = new ChannelPlayState[track.Channels.Count]; for (int i = 0; i < playState.Length; i++) { playState[i] = new ChannelPlayState(i, track.Channels[i]); } mBPM = track.BPM; mBarLength = 0.0; Console.WriteLine("Playing {0} at {1} bpm", track.Name, track.BPM); int lastTick = Environment.TickCount; while (!playState.All(x => x.Finished)) { int elapsedms = Environment.TickCount - lastTick; lastTick = Environment.TickCount; Array.ForEach(playState, x => x.WaitTime -= elapsedms); Array.ForEach(playState, x => PlayNextNote(x)); } System.Threading.Thread.Sleep(1000); }
private static void PlayNextNote(ChannelPlayState playState) { if (playState.WaitTime > 0) { return; } zeOutput.SendNoteOff(Channel.Channel1 + playState.Index, NoteToPitch(playState.CurrentNote), 127); if (playState.Finished) { return; } int noteseqindex = mTrackLibrary.GetNoteSequenceIndex(playState.Channel[playState.SequenceIndex]); Note[] noteseq = mTrackLibrary.NoteSequences[noteseqindex]; Note note = noteseq[playState.NoteIndex]; if (playState.NoteIndex == 0 && playState.Index == 0) { if (mBarLength != 0.0) { Console.WriteLine(); } Console.WriteLine("{0}:", mTrackLibrary.GetNoteSequenceKey(noteseqindex)); } if (note.Key == Key.Rest) { if (playState.Index == 0) { Console.Write("~ "); } } else { if (playState.Index == 0) { Console.Write("{0,2}{1} ", Note.GetStringFromKey(note.Key), note.Octave); } zeOutput.SendNoteOn(Channel.Channel1 + playState.Index, NoteToPitch(note), 127); } playState.WaitTime = GetNoteTime(mBPM, note); playState.CurrentNote = note; playState.NoteIndex++; if (playState.NoteIndex >= noteseq.Length) { playState.SequenceIndex++; playState.NoteIndex = 0; if (playState.SequenceIndex >= playState.Channel.Length) { playState.Finished = true; } } if (playState.Index == 0) { mBarLength += note.BarLength; if (mBarLength >= 1.0) { Console.WriteLine(); mBarLength = 0.0; } } }