//-- Play a note by number (0-127), specifying a velocity value (0-127) public void PlayNote(byte Note, byte Velocity) { byte Msg = 0; Int32 MidiMsg = default(Int32); if (_Engaged == false) { return; } Msg = StuffByte(GetByte(MIDIStatusMessages.NoteOn), _OutputChannel); MidiMsg = StuffInt32(Msg, GetByte(Note + _Transpose), Velocity, 0); try { MIDI.midiOutShortMsg(hMidiOUT, MidiMsg); if (_NoteDuration > 0) { NotesOff no = default(NotesOff); no.Note = GetByte(Note + _Transpose); no.ShutoffTime = DateAndTime.DateAdd(DateInterval.Second, _NoteDuration, DateAndTime.Now); lock (notesToTurnOff) { notesToTurnOff.Add(no); } } } catch (Exception ex) { throw ex; } }
//-- This is called by the thread to shut off a note after it has played. // Only called when NoteDuration is set to non-zero protected void ShutoffNoteCallback() { NotesOff no = default(NotesOff); bool there = false; byte Msg = 0; Int32 MidiMsg = default(Int32); while (!(_Engaged == false)) { lock (notesToTurnOff) { if (notesToTurnOff.Count > 0) { no = (NotesOff)notesToTurnOff[0]; there = true; } else { there = false; } } if (there) { while (!(DateAndTime.Now >= no.ShutoffTime)) { System.Threading.Thread.Sleep(1); if (_Engaged == false) { notesToTurnOff.Clear(); ev.Set(); return; } } Msg = StuffByte(GetByte(MIDIStatusMessages.NoteOff), _OutputChannel); MidiMsg = StuffInt32(Msg, no.Note, 64, 0); try { MIDI.midiOutShortMsg(hMidiOUT, MidiMsg); } catch (Exception ex) { break; // TODO: might not be correct. Was : Exit Do } lock (notesToTurnOff) { notesToTurnOff.RemoveAt(0); } } System.Threading.Thread.Sleep(1); } notesToTurnOff.Clear(); ev.Set(); }