594cf30b4b4476bdee87b661cecc7595ec377a14
[ardour.git] / libs / backends / portaudio / winmmemidi_input_device.cc
1 /*
2  * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include "winmmemidi_input_device.h"
20
21 #include <stdexcept>
22 #include <cmath>
23
24 #include "pbd/compose.h"
25
26 #include "win_utils.h"
27 #include "midi_util.h"
28
29 #include "mmcss.h"
30
31 #include "debug.h"
32
33 static const uint32_t MIDI_BUFFER_SIZE = 32768;
34 static const uint32_t SYSEX_BUFFER_SIZE = 32768;
35
36 namespace ARDOUR {
37
38 WinMMEMidiInputDevice::WinMMEMidiInputDevice (int index)
39         : m_handle(0)
40         , m_midi_buffer(new RingBuffer<uint8_t>(MIDI_BUFFER_SIZE))
41         , m_sysex_buffer(new uint8_t[SYSEX_BUFFER_SIZE])
42 {
43         DEBUG_MIDI (string_compose ("Creating midi input device index: %1\n", index));
44
45         std::string error_msg;
46
47         if (!open (index, error_msg)) {
48                 DEBUG_MIDI (error_msg);
49                 throw std::runtime_error (error_msg);
50         }
51
52         // perhaps this should be called in open
53         if (!add_sysex_buffer (error_msg)) {
54                 DEBUG_MIDI (error_msg);
55                 std::string close_error;
56                 if (!close (close_error)) {
57                         DEBUG_MIDI (close_error);
58                 }
59                 throw std::runtime_error (error_msg);
60         }
61
62         set_device_name (index);
63 }
64
65 WinMMEMidiInputDevice::~WinMMEMidiInputDevice ()
66 {
67         std::string error_msg;
68         if (!close (error_msg)) {
69                 DEBUG_MIDI (error_msg);
70         }
71 }
72
73 bool
74 WinMMEMidiInputDevice::open (UINT index, std::string& error_msg)
75 {
76         MMRESULT result = midiInOpen (&m_handle,
77                                       index,
78                                       (DWORD_PTR) winmm_input_callback,
79                                       (DWORD_PTR) this,
80                                       CALLBACK_FUNCTION | MIDI_IO_STATUS);
81         if (result != MMSYSERR_NOERROR) {
82                 error_msg = get_error_string (result);
83                 return false;
84         }
85         DEBUG_MIDI (string_compose ("Opened MIDI device index %1\n", index));
86         return true;
87 }
88
89 bool
90 WinMMEMidiInputDevice::close (std::string& error_msg)
91 {
92         // return error message for first error encountered?
93         bool success = true;
94
95         MMRESULT result = midiInReset (m_handle);
96         if (result != MMSYSERR_NOERROR) {
97                 error_msg = get_error_string (result);
98                 DEBUG_MIDI (error_msg);
99                 success = false;
100         }
101         result = midiInUnprepareHeader (m_handle, &m_sysex_header, sizeof(MIDIHDR));
102         if (result != MMSYSERR_NOERROR) {
103                 error_msg = get_error_string (result);
104                 DEBUG_MIDI (error_msg);
105                 success = false;
106         }
107         result = midiInClose (m_handle);
108         if (result != MMSYSERR_NOERROR) {
109                 error_msg = get_error_string (result);
110                 DEBUG_MIDI (error_msg);
111                 success = false;
112         }
113         m_handle = 0;
114         if (success) {
115                 DEBUG_MIDI (string_compose ("Closed MIDI device: %1\n", name ()));
116         } else {
117                 DEBUG_MIDI (string_compose ("Unable to Close MIDI device: %1\n", name ()));
118         }
119         return success;
120 }
121
122 bool
123 WinMMEMidiInputDevice::add_sysex_buffer (std::string& error_msg)
124 {
125         m_sysex_header.dwBufferLength = SYSEX_BUFFER_SIZE;
126         m_sysex_header.dwFlags = 0;
127         m_sysex_header.lpData = (LPSTR)m_sysex_buffer.get ();
128
129         MMRESULT result = midiInPrepareHeader (m_handle, &m_sysex_header, sizeof(MIDIHDR));
130
131         if (result != MMSYSERR_NOERROR) {
132                 error_msg = get_error_string (result);
133                 DEBUG_MIDI (error_msg);
134                 return false;
135         }
136         result = midiInAddBuffer (m_handle, &m_sysex_header, sizeof(MIDIHDR));
137         if (result != MMSYSERR_NOERROR) {
138                 error_msg = get_error_string (result);
139                 DEBUG_MIDI (error_msg);
140                 return false;
141         }
142         return true;
143 }
144
145 bool
146 WinMMEMidiInputDevice::set_device_name (UINT index)
147 {
148         MIDIINCAPS capabilities;
149         MMRESULT result = midiInGetDevCaps (index, &capabilities, sizeof(capabilities));
150         if (result != MMSYSERR_NOERROR) {
151                 DEBUG_MIDI (get_error_string (result));
152                 m_name = "Unknown Midi Input Device";
153                 return false;
154         } else {
155                 m_name = capabilities.szPname;
156         }
157         return true;
158 }
159
160 std::string
161 WinMMEMidiInputDevice::get_error_string (MMRESULT error_code)
162 {
163         char error_msg[MAXERRORLENGTH];
164         MMRESULT result = midiInGetErrorText (error_code, error_msg, MAXERRORLENGTH);
165         if (result != MMSYSERR_NOERROR) {
166                 return error_msg;
167         }
168         return "WinMMEMidiInput: Unknown Error code";
169 }
170
171 void CALLBACK
172 WinMMEMidiInputDevice::winmm_input_callback(HMIDIIN handle,
173                                             UINT msg,
174                                             DWORD_PTR instance,
175                                             DWORD_PTR midi_msg,
176                                             DWORD timestamp)
177 {
178         WinMMEMidiInputDevice* midi_input = (WinMMEMidiInputDevice*)instance;
179
180 #ifdef USE_MMCSS_THREAD_PRIORITIES
181
182         static HANDLE input_thread = GetCurrentThread ();
183         static bool priority_boosted = false;
184
185 #if 0 // GetThreadId() is Vista or later only.
186         if (input_thread != GetCurrentThread ()) {
187                 DWORD otid = GetThreadId (input_thread);
188                 DWORD ntid = GetThreadId (GetCurrentThread ());
189                 // There was a reference on the internet somewhere that it is possible
190                 // for the callback to come from different threads(thread pool) this
191                 // could be problematic but I haven't seen this behaviour yet
192                 DEBUG_THREADS (string_compose (
193                     "WinMME input Thread ID Changed: was %1, now %2\n", otid, ntid));
194         }
195 #endif
196
197         HANDLE task_handle;
198
199         if (!priority_boosted) {
200                 mmcss::set_thread_characteristics ("Pro Audio", &task_handle);
201                 mmcss::set_thread_priority (task_handle, mmcss::AVRT_PRIORITY_HIGH);
202                 priority_boosted = true;
203         }
204 #endif
205
206         switch (msg) {
207         case MIM_OPEN:
208         case MIM_CLOSE:
209                 // devices_changed_callback
210                 break;
211         case MIM_MOREDATA:
212                 // passing MIDI_IO_STATUS to midiInOpen means that MIM_MOREDATA
213                 // will be sent when the callback isn't processing MIM_DATA messages
214                 // fast enough to keep up with messages arriving at input device
215                 // driver. I'm not sure what could be done differently if that occurs
216                 // so just handle MIM_DATA as per normal
217         case MIM_DATA:
218                 midi_input->handle_short_msg ((const uint8_t*)&midi_msg, (uint32_t)timestamp);
219                 break;
220         case MIM_LONGDATA:
221                 midi_input->handle_sysex_msg ((MIDIHDR*)&midi_msg, (uint32_t)timestamp);
222                 break;
223         case MIM_ERROR:
224                 DEBUG_MIDI ("WinMME: Driver sent an invalid MIDI message\n");
225                 break;
226         case MIM_LONGERROR:
227                 DEBUG_MIDI ("WinMME: Driver sent an invalid or incomplete SYSEX message\n");
228                 break;
229         }
230 }
231
232 void
233 WinMMEMidiInputDevice::handle_short_msg (const uint8_t* midi_data,
234                                          uint32_t timestamp)
235 {
236         int length = get_midi_msg_length (midi_data[0]);
237
238         if (length == 0 || length == -1) {
239                 DEBUG_MIDI ("ERROR: midi input driver sent an invalid midi message\n");
240                 return;
241         }
242
243         enqueue_midi_msg (midi_data, length, timestamp);
244 }
245
246 void
247 WinMMEMidiInputDevice::handle_sysex_msg (MIDIHDR* const midi_header,
248                                          uint32_t timestamp)
249 {
250 #ifdef ENABLE_SYSEX
251         LPMIDIHDR header = (LPMIDIHDR)midi_header;
252         size_t byte_count = header->dwBytesRecorded;
253
254         if (!byte_count) {
255                 DEBUG_MIDI (
256                     "ERROR: WinMME driver has returned sysex header to us with no bytes\n");
257                 return;
258         }
259
260         uint8_t* data = (uint8_t*)header->lpData;
261
262         if ((data[0] != 0xf0) || (data[byte_count - 1] != 0xf7)) {
263                 DEBUG_MIDI (string_compose ("Discarding %1 byte sysex chunk\n", byte_count));
264         } else {
265                 enqueue_midi_msg (data, byte_count, timestamp);
266         }
267
268         MMRESULT result = midiInAddBuffer (m_handle, &m_sysex_header, sizeof(MIDIHDR));
269         if (result != MMSYSERR_NOERROR) {
270                 DEBUG_MIDI (get_error_string (result));
271         }
272 #endif
273 }
274
275 // fix param order
276 bool
277 WinMMEMidiInputDevice::dequeue_midi_event (uint64_t timestamp_start,
278                                            uint64_t timestamp_end,
279                                            uint64_t& timestamp,
280                                            uint8_t* midi_data,
281                                            size_t& data_size)
282 {
283         const uint32_t read_space = m_midi_buffer->read_space();
284         struct MidiEventHeader h(0,0);
285
286         if (read_space <= sizeof(MidiEventHeader)) {
287                 return false;
288         }
289
290         RingBuffer<uint8_t>::rw_vector vector;
291         m_midi_buffer->get_read_vector (&vector);
292         if (vector.len[0] >= sizeof(MidiEventHeader)) {
293                 memcpy ((uint8_t*)&h, vector.buf[0], sizeof(MidiEventHeader));
294         } else {
295                 if (vector.len[0] > 0) {
296                         memcpy ((uint8_t*)&h, vector.buf[0], vector.len[0]);
297                 }
298                 assert (vector.buf[1] || vector.len[0] == sizeof(MidiEventHeader));
299                 memcpy (((uint8_t*)&h) + vector.len[0],
300                         vector.buf[1],
301                         sizeof(MidiEventHeader) - vector.len[0]);
302         }
303
304         if (h.time >= timestamp_end) {
305                 DEBUG_TIMING (string_compose ("WinMMEMidiInput EVENT %1(ms) early\n",
306                                               (h.time - timestamp_end) * 1e-3));
307                 return false;
308         } else if (h.time < timestamp_start) {
309                 DEBUG_TIMING (string_compose ("WinMMEMidiInput EVENT %1(ms) late\n",
310                                               (timestamp_start - h.time) * 1e-3));
311         }
312
313         m_midi_buffer->increment_read_idx (sizeof(MidiEventHeader));
314
315         assert (h.size > 0);
316         if (h.size > data_size) {
317                 DEBUG_MIDI ("WinMMEMidiInput::dequeue_event MIDI event too large!\n");
318                 m_midi_buffer->increment_read_idx (h.size);
319                 return false;
320         }
321         if (m_midi_buffer->read (&midi_data[0], h.size) != h.size) {
322                 DEBUG_MIDI ("WinMMEMidiInput::dequeue_event Garbled MIDI EVENT DATA!!\n");
323                 return false;
324         }
325         timestamp = h.time;
326         data_size = h.size;
327         return true;
328 }
329
330 bool
331 WinMMEMidiInputDevice::enqueue_midi_msg (const uint8_t* midi_data,
332                                          size_t data_size,
333                                          uint32_t timestamp)
334 {
335         const uint32_t total_size = sizeof(MidiEventHeader) + data_size;
336
337         if (data_size == 0) {
338                 DEBUG_MIDI ("ERROR: zero length midi data\n");
339                 return false;
340         }
341
342         if (m_midi_buffer->write_space () < total_size) {
343                 DEBUG_MIDI ("WinMMEMidiInput: ring buffer overflow\n");
344                 return false;
345         }
346
347         // don't use winmme timestamps for now
348         uint64_t ts = utils::get_microseconds ();
349
350         DEBUG_TIMING (string_compose (
351             "Enqueing MIDI data device: %1 with timestamp: %2 and size %3\n",
352             name (),
353             ts,
354             data_size));
355
356         struct MidiEventHeader h (ts, data_size);
357         m_midi_buffer->write ((uint8_t*)&h, sizeof(MidiEventHeader));
358         m_midi_buffer->write (midi_data, data_size);
359         return true;
360 }
361
362 bool
363 WinMMEMidiInputDevice::start ()
364 {
365         if (!m_started) {
366                 MMRESULT result = midiInStart (m_handle);
367                 m_started = (result == MMSYSERR_NOERROR);
368                 if (!m_started) {
369                         DEBUG_MIDI (get_error_string (result));
370                 } else {
371                         DEBUG_MIDI (
372                             string_compose ("WinMMEMidiInput: device %1 started\n", name ()));
373                 }
374         }
375         return m_started;
376 }
377
378 bool
379 WinMMEMidiInputDevice::stop ()
380 {
381         if (m_started) {
382                 MMRESULT result = midiInStop (m_handle);
383                 m_started = (result != MMSYSERR_NOERROR);
384                 if (m_started) {
385                         DEBUG_MIDI (get_error_string (result));
386                 } else {
387                         DEBUG_MIDI (
388                             string_compose ("WinMMEMidiInput: device %1 stopped\n", name ()));
389                 }
390         }
391         return !m_started;
392 }
393
394 } // namespace ARDOUR