Add a missing include from portaudio WINMME source file
[ardour.git] / libs / backends / portaudio / winmmemidi_io.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 <windows.h>
20 #include <mmsystem.h>
21
22 #include <sstream>
23 #include <set>
24
25 #include "pbd/error.h"
26 #include "pbd/compose.h"
27 #include "pbd/windows_timer_utils.h"
28
29 #include "winmmemidi_io.h"
30 #include "debug.h"
31
32 #include "i18n.h"
33
34 using namespace ARDOUR;
35
36 WinMMEMidiIO::WinMMEMidiIO()
37         : m_active (false)
38         , m_enabled (true)
39         , m_run (false)
40         , m_changed_callback (0)
41         , m_changed_arg (0)
42 {
43         pthread_mutex_init (&m_device_lock, 0);
44 }
45
46 WinMMEMidiIO::~WinMMEMidiIO()
47 {
48         pthread_mutex_lock (&m_device_lock);
49         cleanup();
50         pthread_mutex_unlock (&m_device_lock);
51         pthread_mutex_destroy (&m_device_lock);
52 }
53
54 void
55 WinMMEMidiIO::cleanup()
56 {
57         DEBUG_MIDI ("MIDI cleanup\n");
58         m_active = false;
59
60         destroy_input_devices ();
61         destroy_output_devices ();
62 }
63
64 bool
65 WinMMEMidiIO::dequeue_input_event (uint32_t port,
66                                    uint64_t timestamp_start,
67                                    uint64_t timestamp_end,
68                                    uint64_t &timestamp,
69                                    uint8_t *d,
70                                    size_t &s)
71 {
72         if (!m_active) {
73                 return false;
74         }
75         assert(port < m_inputs.size());
76
77         // m_inputs access should be protected by trylock
78         return m_inputs[port]->dequeue_midi_event (
79             timestamp_start, timestamp_end, timestamp, d, s);
80 }
81
82 bool
83 WinMMEMidiIO::enqueue_output_event (uint32_t port,
84                                     uint64_t timestamp,
85                                     const uint8_t *d,
86                                     const size_t s)
87 {
88         if (!m_active) {
89                 return false;
90         }
91         assert(port < m_outputs.size());
92
93         // m_outputs access should be protected by trylock
94         return m_outputs[port]->enqueue_midi_event (timestamp, d, s);
95 }
96
97
98 std::string
99 WinMMEMidiIO::port_id (uint32_t port, bool input)
100 {
101         std::stringstream ss;
102
103         if (input) {
104                 ss << "system:midi_capture_";
105                 ss << port;
106         } else {
107                 ss << "system:midi_playback_";
108                 ss << port;
109         }
110         return ss.str();
111 }
112
113 std::string WinMMEMidiIO::port_name(uint32_t port, bool input)
114 {
115         if (input) {
116                 if (port < m_inputs.size ()) {
117                         return m_inputs[port]->name ();
118                 }
119         } else {
120                 if (port < m_outputs.size ()) {
121                         return m_outputs[port]->name ();
122                 }
123         }
124         return "";
125 }
126
127 void
128 WinMMEMidiIO::start ()
129 {
130         if (m_run) {
131                 DEBUG_MIDI ("MIDI driver already started\n");
132                 return;
133         }
134
135         m_run = true;
136         DEBUG_MIDI ("Starting MIDI driver\n");
137
138         PBD::MMTIMERS::set_min_resolution();
139         discover();
140         start_devices ();
141 }
142
143
144 void
145 WinMMEMidiIO::stop ()
146 {
147         if (!m_run) {
148                 DEBUG_MIDI ("MIDI driver already stopped\n");
149                 return;
150         }
151         DEBUG_MIDI ("Stopping MIDI driver\n");
152         m_run = false;
153         stop_devices ();
154         pthread_mutex_lock (&m_device_lock);
155         cleanup ();
156         pthread_mutex_unlock (&m_device_lock);
157
158         PBD::MMTIMERS::reset_resolution();
159 }
160
161 void
162 WinMMEMidiIO::start_devices ()
163 {
164         for (std::vector<WinMMEMidiInputDevice*>::iterator i = m_inputs.begin ();
165              i < m_inputs.end();
166              ++i) {
167                 if (!(*i)->start ()) {
168                         PBD::error << string_compose (_("Unable to start MIDI input device %1\n"),
169                                                       (*i)->name ()) << endmsg;
170                 }
171         }
172         for (std::vector<WinMMEMidiOutputDevice*>::iterator i = m_outputs.begin ();
173              i < m_outputs.end();
174              ++i) {
175                 if (!(*i)->start ()) {
176                         PBD::error << string_compose (_ ("Unable to start MIDI output device %1\n"),
177                                                       (*i)->name ()) << endmsg;
178                 }
179         }
180 }
181
182 void
183 WinMMEMidiIO::stop_devices ()
184 {
185         for (std::vector<WinMMEMidiInputDevice*>::iterator i = m_inputs.begin ();
186              i < m_inputs.end();
187              ++i) {
188                 if (!(*i)->stop ()) {
189                         PBD::error << string_compose (_ ("Unable to stop MIDI input device %1\n"),
190                                                       (*i)->name ()) << endmsg;
191                 }
192         }
193         for (std::vector<WinMMEMidiOutputDevice*>::iterator i = m_outputs.begin ();
194              i < m_outputs.end();
195              ++i) {
196                 if (!(*i)->stop ()) {
197                         PBD::error << string_compose (_ ("Unable to stop MIDI output device %1\n"),
198                                                       (*i)->name ()) << endmsg;
199                 }
200         }
201 }
202
203 void
204 WinMMEMidiIO::clear_device_info ()
205 {
206         for (std::vector<MidiDeviceInfo*>::iterator i = m_device_info.begin();
207              i != m_device_info.end();
208              ++i) {
209           delete *i;
210         }
211         m_device_info.clear();
212 }
213
214 bool
215 WinMMEMidiIO::get_input_name_from_index (int index, std::string& name)
216 {
217         MIDIINCAPS capabilities;
218         MMRESULT result = midiInGetDevCaps(index, &capabilities, sizeof(capabilities));
219         if (result == MMSYSERR_NOERROR) {
220                 name = capabilities.szPname;
221                 return true;
222         }
223         return false;
224 }
225
226 bool
227 WinMMEMidiIO::get_output_name_from_index (int index, std::string& name)
228 {
229         MIDIOUTCAPS capabilities;
230         MMRESULT result = midiOutGetDevCaps(index, &capabilities, sizeof(capabilities));
231         if (result == MMSYSERR_NOERROR) {
232                 name = capabilities.szPname;
233                 return true;
234         }
235         return false;
236 }
237
238 void
239 WinMMEMidiIO::update_device_info ()
240 {
241         std::set<std::string> device_names;
242
243         int in_count = midiInGetNumDevs ();
244
245         for (int i = 0; i < in_count; ++i) {
246                 std::string input_name;
247                 if (get_input_name_from_index(i, input_name)) {
248                         device_names.insert(input_name);
249                 }
250         }
251
252         int out_count = midiOutGetNumDevs ();
253
254         for (int i = 0; i < out_count; ++i) {
255                 std::string output_name;
256                 if (get_output_name_from_index(i, output_name)) {
257                         device_names.insert(output_name);
258                 }
259         }
260
261         clear_device_info ();
262
263         for (std::set<std::string>::const_iterator i = device_names.begin();
264              i != device_names.end();
265              ++i) {
266           m_device_info.push_back(new MidiDeviceInfo(*i));
267         }
268 }
269
270 MidiDeviceInfo*
271 WinMMEMidiIO::get_device_info (const std::string& name)
272 {
273         for (std::vector<MidiDeviceInfo*>::const_iterator i = m_device_info.begin();
274              i != m_device_info.end();
275              ++i) {
276                 if ((*i)->device_name == name) {
277                         return *i;
278                 }
279         }
280         return 0;
281 }
282
283 void
284 WinMMEMidiIO::create_input_devices ()
285 {
286         int srcCount = midiInGetNumDevs ();
287
288         DEBUG_MIDI (string_compose ("MidiIn count: %1\n", srcCount));
289
290         for (int i = 0; i < srcCount; ++i) {
291                 std::string input_name;
292                 if (!get_input_name_from_index (i, input_name)) {
293                         DEBUG_MIDI ("Unable to get MIDI input name from index\n");
294                         continue;
295                 }
296
297                 MidiDeviceInfo* info = get_device_info (input_name);
298
299                 if (!info) {
300                         DEBUG_MIDI ("Unable to MIDI device info from name\n");
301                         continue;
302                 }
303
304                 if (!info->enable) {
305                         DEBUG_MIDI(string_compose(
306                             "MIDI input device %1 not enabled, not opening device\n", input_name));
307                         continue;
308                 }
309
310                 try {
311                         WinMMEMidiInputDevice* midi_input = new WinMMEMidiInputDevice (i);
312                         if (midi_input) {
313                                 m_inputs.push_back (midi_input);
314                         }
315                 }
316                 catch (...) {
317                         DEBUG_MIDI ("Unable to create MIDI input\n");
318                         continue;
319                 }
320         }
321 }
322 void
323 WinMMEMidiIO::create_output_devices ()
324 {
325         int dstCount = midiOutGetNumDevs ();
326
327         DEBUG_MIDI (string_compose ("MidiOut count: %1\n", dstCount));
328
329         for (int i = 0; i < dstCount; ++i) {
330                 std::string output_name;
331                 if (!get_output_name_from_index (i, output_name)) {
332                         DEBUG_MIDI ("Unable to get MIDI output name from index\n");
333                         continue;
334                 }
335
336                 MidiDeviceInfo* info = get_device_info (output_name);
337
338                 if (!info) {
339                         DEBUG_MIDI ("Unable to MIDI device info from name\n");
340                         continue;
341                 }
342
343                 if (!info->enable) {
344                         DEBUG_MIDI(string_compose(
345                             "MIDI output device %1 not enabled, not opening device\n", output_name));
346                         continue;
347                 }
348
349                 try {
350                         WinMMEMidiOutputDevice* midi_output = new WinMMEMidiOutputDevice(i);
351                         if (midi_output) {
352                                 m_outputs.push_back(midi_output);
353                         }
354                 } catch(...) {
355                         DEBUG_MIDI ("Unable to create MIDI output\n");
356                         continue;
357                 }
358         }
359 }
360
361 void
362 WinMMEMidiIO::destroy_input_devices ()
363 {
364         while (!m_inputs.empty ()) {
365                 WinMMEMidiInputDevice* midi_input = m_inputs.back ();
366                 // assert(midi_input->stopped ());
367                 m_inputs.pop_back ();
368                 delete midi_input;
369         }
370 }
371
372 void
373 WinMMEMidiIO::destroy_output_devices ()
374 {
375         while (!m_outputs.empty ()) {
376                 WinMMEMidiOutputDevice* midi_output = m_outputs.back ();
377                 // assert(midi_output->stopped ());
378                 m_outputs.pop_back ();
379                 delete midi_output;
380         }
381 }
382
383 void
384 WinMMEMidiIO::discover()
385 {
386         if (!m_run) {
387                 return;
388         }
389
390         if (pthread_mutex_trylock (&m_device_lock)) {
391                 return;
392         }
393
394         cleanup ();
395
396         create_input_devices ();
397         create_output_devices ();
398
399         if (!(m_inputs.size () || m_outputs.size ())) {
400                 DEBUG_MIDI ("No midi inputs or outputs\n");
401                 pthread_mutex_unlock (&m_device_lock);
402                 return;
403         }
404
405         DEBUG_MIDI (string_compose ("Discovered %1 inputs and %2 outputs\n",
406                                     m_inputs.size (),
407                                     m_outputs.size ()));
408
409         if (m_changed_callback) {
410                 m_changed_callback(m_changed_arg);
411         }
412
413         m_active = true;
414         pthread_mutex_unlock (&m_device_lock);
415 }