2 * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include "winmmemidi_input_device.h"
24 #include "pbd/compose.h"
26 #include "win_utils.h"
27 #include "midi_util.h"
33 static const uint32_t MIDI_BUFFER_SIZE = 32768;
34 static const uint32_t SYSEX_BUFFER_SIZE = 32768;
38 WinMMEMidiInputDevice::WinMMEMidiInputDevice (int index)
41 , m_midi_buffer(new RingBuffer<uint8_t>(MIDI_BUFFER_SIZE))
42 , m_sysex_buffer(new uint8_t[SYSEX_BUFFER_SIZE])
44 DEBUG_MIDI (string_compose ("Creating midi input device index: %1\n", index));
46 std::string error_msg;
48 if (!open (index, error_msg)) {
49 DEBUG_MIDI (error_msg);
50 throw std::runtime_error (error_msg);
53 // perhaps this should be called in open
54 if (!add_sysex_buffer (error_msg)) {
55 DEBUG_MIDI (error_msg);
56 std::string close_error;
57 if (!close (close_error)) {
58 DEBUG_MIDI (close_error);
60 throw std::runtime_error (error_msg);
63 set_device_name (index);
66 WinMMEMidiInputDevice::~WinMMEMidiInputDevice ()
68 std::string error_msg;
69 if (!close (error_msg)) {
70 DEBUG_MIDI (error_msg);
75 WinMMEMidiInputDevice::open (UINT index, std::string& error_msg)
77 MMRESULT result = midiInOpen (&m_handle,
79 (DWORD_PTR) winmm_input_callback,
81 CALLBACK_FUNCTION | MIDI_IO_STATUS);
82 if (result != MMSYSERR_NOERROR) {
83 error_msg = get_error_string (result);
86 DEBUG_MIDI (string_compose ("Opened MIDI device index %1\n", index));
91 WinMMEMidiInputDevice::close (std::string& error_msg)
93 // return error message for first error encountered?
96 MMRESULT result = midiInReset (m_handle);
97 if (result != MMSYSERR_NOERROR) {
98 error_msg = get_error_string (result);
99 DEBUG_MIDI (error_msg);
102 result = midiInUnprepareHeader (m_handle, &m_sysex_header, sizeof(MIDIHDR));
103 if (result != MMSYSERR_NOERROR) {
104 error_msg = get_error_string (result);
105 DEBUG_MIDI (error_msg);
108 result = midiInClose (m_handle);
109 if (result != MMSYSERR_NOERROR) {
110 error_msg = get_error_string (result);
111 DEBUG_MIDI (error_msg);
116 DEBUG_MIDI (string_compose ("Closed MIDI device: %1\n", name ()));
118 DEBUG_MIDI (string_compose ("Unable to Close MIDI device: %1\n", name ()));
124 WinMMEMidiInputDevice::add_sysex_buffer (std::string& error_msg)
126 m_sysex_header.dwBufferLength = SYSEX_BUFFER_SIZE;
127 m_sysex_header.dwFlags = 0;
128 m_sysex_header.lpData = (LPSTR)m_sysex_buffer.get ();
130 MMRESULT result = midiInPrepareHeader (m_handle, &m_sysex_header, sizeof(MIDIHDR));
132 if (result != MMSYSERR_NOERROR) {
133 error_msg = get_error_string (result);
134 DEBUG_MIDI (error_msg);
137 result = midiInAddBuffer (m_handle, &m_sysex_header, sizeof(MIDIHDR));
138 if (result != MMSYSERR_NOERROR) {
139 error_msg = get_error_string (result);
140 DEBUG_MIDI (error_msg);
147 WinMMEMidiInputDevice::set_device_name (UINT index)
149 MIDIINCAPS capabilities;
150 MMRESULT result = midiInGetDevCaps (index, &capabilities, sizeof(capabilities));
151 if (result != MMSYSERR_NOERROR) {
152 DEBUG_MIDI (get_error_string (result));
153 m_name = "Unknown Midi Input Device";
156 m_name = capabilities.szPname;
162 WinMMEMidiInputDevice::get_error_string (MMRESULT error_code)
164 char error_msg[MAXERRORLENGTH];
165 MMRESULT result = midiInGetErrorText (error_code, error_msg, MAXERRORLENGTH);
166 if (result != MMSYSERR_NOERROR) {
169 return "WinMMEMidiInput: Unknown Error code";
173 WinMMEMidiInputDevice::winmm_input_callback(HMIDIIN handle,
179 WinMMEMidiInputDevice* midi_input = (WinMMEMidiInputDevice*)instance;
181 #ifdef USE_MMCSS_THREAD_PRIORITIES
183 static HANDLE input_thread = GetCurrentThread ();
184 static bool priority_boosted = false;
186 #if 0 // GetThreadId() is Vista or later only.
187 if (input_thread != GetCurrentThread ()) {
188 DWORD otid = GetThreadId (input_thread);
189 DWORD ntid = GetThreadId (GetCurrentThread ());
190 // There was a reference on the internet somewhere that it is possible
191 // for the callback to come from different threads(thread pool) this
192 // could be problematic but I haven't seen this behaviour yet
193 DEBUG_THREADS (string_compose (
194 "WinMME input Thread ID Changed: was %1, now %2\n", otid, ntid));
200 if (!priority_boosted) {
201 mmcss::set_thread_characteristics ("Pro Audio", &task_handle);
202 mmcss::set_thread_priority (task_handle, mmcss::AVRT_PRIORITY_HIGH);
203 priority_boosted = true;
210 // devices_changed_callback
213 // passing MIDI_IO_STATUS to midiInOpen means that MIM_MOREDATA
214 // will be sent when the callback isn't processing MIM_DATA messages
215 // fast enough to keep up with messages arriving at input device
216 // driver. I'm not sure what could be done differently if that occurs
217 // so just handle MIM_DATA as per normal
219 midi_input->handle_short_msg ((const uint8_t*)&midi_msg, (uint32_t)timestamp);
222 midi_input->handle_sysex_msg ((MIDIHDR*)&midi_msg, (uint32_t)timestamp);
225 DEBUG_MIDI ("WinMME: Driver sent an invalid MIDI message\n");
228 DEBUG_MIDI ("WinMME: Driver sent an invalid or incomplete SYSEX message\n");
234 WinMMEMidiInputDevice::handle_short_msg (const uint8_t* midi_data,
237 int length = get_midi_msg_length (midi_data[0]);
239 if (length == 0 || length == -1) {
240 DEBUG_MIDI ("ERROR: midi input driver sent an invalid midi message\n");
244 enqueue_midi_msg (midi_data, length, timestamp);
248 WinMMEMidiInputDevice::handle_sysex_msg (MIDIHDR* const midi_header,
252 LPMIDIHDR header = (LPMIDIHDR)midi_header;
253 size_t byte_count = header->dwBytesRecorded;
257 "ERROR: WinMME driver has returned sysex header to us with no bytes\n");
261 uint8_t* data = (uint8_t*)header->lpData;
263 if ((data[0] != 0xf0) || (data[byte_count - 1] != 0xf7)) {
264 DEBUG_MIDI (string_compose ("Discarding %1 byte sysex chunk\n", byte_count));
266 enqueue_midi_msg (data, byte_count, timestamp);
269 MMRESULT result = midiInAddBuffer (m_handle, &m_sysex_header, sizeof(MIDIHDR));
270 if (result != MMSYSERR_NOERROR) {
271 DEBUG_MIDI (get_error_string (result));
278 WinMMEMidiInputDevice::dequeue_midi_event (uint64_t timestamp_start,
279 uint64_t timestamp_end,
284 const uint32_t read_space = m_midi_buffer->read_space();
285 struct MidiEventHeader h(0,0);
287 if (read_space <= sizeof(MidiEventHeader)) {
291 RingBuffer<uint8_t>::rw_vector vector;
292 m_midi_buffer->get_read_vector (&vector);
293 if (vector.len[0] >= sizeof(MidiEventHeader)) {
294 memcpy ((uint8_t*)&h, vector.buf[0], sizeof(MidiEventHeader));
296 if (vector.len[0] > 0) {
297 memcpy ((uint8_t*)&h, vector.buf[0], vector.len[0]);
299 assert (vector.buf[1] || vector.len[0] == sizeof(MidiEventHeader));
300 memcpy (((uint8_t*)&h) + vector.len[0],
302 sizeof(MidiEventHeader) - vector.len[0]);
305 if (h.time >= timestamp_end) {
306 DEBUG_TIMING (string_compose ("WinMMEMidiInput EVENT %1(ms) early\n",
307 (h.time - timestamp_end) * 1e-3));
309 } else if (h.time < timestamp_start) {
310 DEBUG_TIMING (string_compose ("WinMMEMidiInput EVENT %1(ms) late\n",
311 (timestamp_start - h.time) * 1e-3));
314 m_midi_buffer->increment_read_idx (sizeof(MidiEventHeader));
317 if (h.size > data_size) {
318 DEBUG_MIDI ("WinMMEMidiInput::dequeue_event MIDI event too large!\n");
319 m_midi_buffer->increment_read_idx (h.size);
322 if (m_midi_buffer->read (&midi_data[0], h.size) != h.size) {
323 DEBUG_MIDI ("WinMMEMidiInput::dequeue_event Garbled MIDI EVENT DATA!!\n");
332 WinMMEMidiInputDevice::enqueue_midi_msg (const uint8_t* midi_data,
336 const uint32_t total_size = sizeof(MidiEventHeader) + data_size;
338 if (data_size == 0) {
339 DEBUG_MIDI ("ERROR: zero length midi data\n");
343 if (m_midi_buffer->write_space () < total_size) {
344 DEBUG_MIDI ("WinMMEMidiInput: ring buffer overflow\n");
348 // don't use winmme timestamps for now
349 uint64_t ts = PBD::get_microseconds ();
351 DEBUG_TIMING (string_compose (
352 "Enqueing MIDI data device: %1 with timestamp: %2 and size %3\n",
357 struct MidiEventHeader h (ts, data_size);
358 m_midi_buffer->write ((uint8_t*)&h, sizeof(MidiEventHeader));
359 m_midi_buffer->write (midi_data, data_size);
364 WinMMEMidiInputDevice::start ()
367 MMRESULT result = midiInStart (m_handle);
368 m_started = (result == MMSYSERR_NOERROR);
370 DEBUG_MIDI (get_error_string (result));
373 string_compose ("WinMMEMidiInput: device %1 started\n", name ()));
380 WinMMEMidiInputDevice::stop ()
383 MMRESULT result = midiInStop (m_handle);
384 m_started = (result != MMSYSERR_NOERROR);
386 DEBUG_MIDI (get_error_string (result));
389 string_compose ("WinMMEMidiInput: device %1 stopped\n", name ()));
395 } // namespace ARDOUR