globally remove all trailing whitespace from ardour code base.
[ardour.git] / libs / backends / wavesaudio / waves_midi_device_manager.cc
1 /*
2     Copyright (C) 2013 Waves Audio Ltd.
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
20 #include "waves_midi_device_manager.h"
21 #include "waves_audiobackend.h"
22
23 #ifdef PLATFORM_WINDOWS
24
25 #include "windows.h"
26 #include "mmsystem.h"
27
28 #elif defined(__APPLE__)
29
30 #include <CoreMIDI/MIDIServices.h>
31
32 #define midiInGetNumDevs MIDIGetNumberOfSources
33 #define midiOutGetNumDevs MIDIGetNumberOfDestinations
34
35 #endif
36
37 using namespace ARDOUR;
38
39 WavesMidiDeviceManager::WavesMidiDeviceManager (WavesAudioBackend& audiobackend)
40     : _active (false)
41     , _streaming (false)
42     , _input_device_count (0)
43     , _output_device_count (0)
44     , _audiobackend (audiobackend)
45 {
46 }
47
48
49 WavesMidiDeviceManager::~WavesMidiDeviceManager ()
50 {
51 }
52
53
54 int
55 WavesMidiDeviceManager::start ()
56 {
57     // COMMENTED DBG LOGS */ std::cout << "WavesMidiDeviceManager::start ():" << std::endl;
58     if ( _active == true ) {
59         return -1;
60     }
61
62     if (Pm_Initialize () != pmNoError) {
63         return -1;
64     }
65
66     _create_devices ();
67
68     _input_device_count = midiInGetNumDevs ();
69     _output_device_count = midiOutGetNumDevs ();
70
71     _active = true;
72
73     return 0;
74 }
75
76
77 int
78 WavesMidiDeviceManager::stream (bool yn)
79 {
80     // COMMENTED DBG LOGS */ std::cout << "WavesMidiDeviceManager::stream (" << (yn?"true":"false") << "):" << std::endl;
81     if (!_active) {
82         std::cerr << "WavesMidiDeviceManager::stream (): the midi device manager is not started up !" << std::endl;
83         return -1;
84     }
85
86     if (_streaming == yn) {
87         return 0;
88     }
89
90     if (yn)    {
91         // __portmidi_callback will be called once per 50 msec
92         if ( Pt_Start (50, __portmidi_callback, this) != ptNoError) {
93             std::cerr << "WavesMidiDeviceManager::stream (): Pt_Start () failed!" << std::endl;
94             return -1;
95         }
96     }
97     else {
98         if (Pt_Stop () != ptNoError) {
99             std::cerr << "WavesMidiDeviceManager::stream (): Pt_Stop () failed!" << std::endl;
100             return -1;
101         }
102     }
103
104     _streaming = yn;
105     return 0;
106 }
107
108
109 int
110 WavesMidiDeviceManager::stop ()
111 {
112     // COMMENTED DBG LOGS */ std::cout << "WavesMidiDeviceManager::stop ():" << std::endl;
113
114     if ( _active == false ) {
115         return 0;
116         }
117
118     stream (false);
119
120     _delete_devices ();
121     _active = false;
122
123     if (Pm_Terminate () != pmNoError) {
124         std::cerr << "WavesMidiDeviceManager::stop (): Pt_Terminate () failed!" << std::endl;
125         return -1;
126     }
127
128     return 0;
129 }
130
131 void
132 WavesMidiDeviceManager::__portmidi_callback (PtTimestamp timestamp, void * userData)
133 {
134     // COMMENTED FREQUENT DBG LOGS */ std::cout << "WavesMidiDeviceManager::__portmidi_callback ():" << std::endl;
135     WavesMidiDeviceManager *dm = (WavesMidiDeviceManager *)userData;
136
137     if (dm == NULL) {
138         return;
139     }
140
141     dm->_portmidi_callback (timestamp);
142 }
143
144 void
145 WavesMidiDeviceManager::_portmidi_callback (PtTimestamp timestamp)
146 {
147     if ((!_active) || (!_streaming)) {
148         return;
149     }
150
151     if ((_input_device_count != midiInGetNumDevs ()) || (_output_device_count != midiOutGetNumDevs ())) {
152         _audiobackend._changed_midi_devices ();
153         // COMMENTED DBG LOGS */ std::cout << "WavesMidiDeviceManager::_portmidi_callback ():" << std::endl;
154         // COMMENTED DBG LOGS */ std::cout << "                        _input_device_count ?= midiInGetNumDevs () :" << _input_device_count << " ?= " << midiInGetNumDevs () << std::endl;
155         // COMMENTED DBG LOGS */ std::cout << "                        _output_device_count ?= midiOutGetNumDevs () :" << _output_device_count << " ?= " << midiOutGetNumDevs () << std::endl;
156    }
157 }
158
159 void WavesMidiDeviceManager::do_read ()
160 {
161     for (std::vector<WavesMidiDevice *>::const_iterator it = _devices.begin ();  it != _devices.end (); ++it) {
162         (*it)->read_midi ();
163     }
164 }
165
166
167 void WavesMidiDeviceManager::do_write ()
168 {
169     for (std::vector<WavesMidiDevice *>::const_iterator it = _devices.begin ();  it != _devices.end (); ++it) {
170         (*it)->write_midi ();
171     }
172 }
173
174
175 PmTimestamp
176 WavesMidiDeviceManager::__get_time_ms (void *time_info)
177 {
178     return ((WavesAudioBackend*)time_info)->sample_time ();
179 }
180
181
182 WavesMidiDevice* WavesMidiDeviceManager::_get_device (const std::string& name)
183 {
184     // COMMENTED DBG LOGS */ std::cout << "WavesMidiDeviceManager::_get_device ():" << std::endl;
185     for (size_t i = 0; i < _devices.size (); i++) {
186         if (name == _devices[i]->name ()) {
187             return _devices[i];
188         }
189     }
190     return NULL;
191 }
192
193
194 int
195 WavesMidiDeviceManager::_create_devices ()
196 {
197     // COMMENTED DBG LOGS */ std::cout << "WavesMidiDeviceManager::_create_devices () :" << std::endl;
198     int count = Pm_CountDevices ();
199
200     for (int i = 0; i < count; i++) {
201
202         const PmDeviceInfo* pm_device_info = Pm_GetDeviceInfo (i);
203         // COMMENTED DBG LOGS */ std::cout << "                                    interf : " << pm_device_info->interf << std::endl;
204         // COMMENTED DBG LOGS */ std::cout << "                                      name : " << pm_device_info->name << std::endl;
205         // COMMENTED DBG LOGS */ std::cout << "                                     input : " << pm_device_info->input << std::endl;
206         // COMMENTED DBG LOGS */ std::cout << "                                    output : " << pm_device_info->output << std::endl;
207         // COMMENTED DBG LOGS */ std::cout << "                                    opened : " << pm_device_info->opened << std::endl;
208 #if defined (PLATFORM_WINDOWS)
209        if (strncmp (pm_device_info->name, "Microsoft", strlen ("Microsoft")) == 0) {
210            // COMMENTED DBG LOGS */ std::cout << "      skipping anything from Microsoft :" << pm_device_info->name << std::endl;
211            continue;
212        }
213 #endif
214         if (pm_device_info == NULL) {
215             std::cerr << "WavesMidiDeviceManager::_create_devices (): Pm_GetDeviceInfo (" << i << ") failed!" << std::endl;
216             continue;
217         }
218
219         WavesMidiDevice *device = _get_device (pm_device_info->name);
220         if (!device) {
221             device = new WavesMidiDevice (pm_device_info->name);
222             _devices.push_back (device);
223                         if (device->open (__get_time_ms, (void*)&_audiobackend)) {
224                                 std::cerr << "WavesMidiDeviceManager::_create_devices (): [" << device->name () << "]->open () failed!" << std::endl;
225                         }
226                 }
227     }
228
229     return 0;
230 }
231
232
233 int
234 WavesMidiDeviceManager::_delete_devices ()
235 {
236     // COMMENTED DBG LOGS */ std::cout << "WavesMidiDeviceManager::_delete_devices ():" << std::endl;
237     while (!_devices.empty ()) {
238         WavesMidiDevice * device = _devices.back ();
239         _devices.pop_back ();
240                 device->close ();
241         delete device;
242     }
243     return 0;
244 }
245