2 Copyright (C) 2013 Waves Audio Ltd.
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.
20 #include "waves_midi_device_manager.h"
21 #include "waves_audiobackend.h"
23 #ifdef PLATFORM_WINDOWS
28 #elif defined(__APPLE__)
30 #include <CoreMIDI/MIDIServices.h>
32 #define midiInGetNumDevs MIDIGetNumberOfSources
33 #define midiOutGetNumDevs MIDIGetNumberOfDestinations
37 using namespace ARDOUR;
39 WavesMidiDeviceManager::WavesMidiDeviceManager (WavesAudioBackend& audiobackend)
42 , _input_device_count (0)
43 , _output_device_count (0)
44 , _audiobackend (audiobackend)
49 WavesMidiDeviceManager::~WavesMidiDeviceManager ()
55 WavesMidiDeviceManager::start ()
57 // COMMENTED DBG LOGS */ std::cout << "WavesMidiDeviceManager::start ():" << std::endl;
58 if ( _active == true ) {
62 if (Pm_Initialize () != pmNoError) {
68 _input_device_count = midiInGetNumDevs ();
69 _output_device_count = midiOutGetNumDevs ();
78 WavesMidiDeviceManager::stream (bool yn)
80 // COMMENTED DBG LOGS */ std::cout << "WavesMidiDeviceManager::stream (" << (yn?"true":"false") << "):" << std::endl;
82 std::cerr << "WavesMidiDeviceManager::stream (): the midi device manager is not started up !" << std::endl;
86 if (_streaming == 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;
98 if (Pt_Stop () != ptNoError) {
99 std::cerr << "WavesMidiDeviceManager::stream (): Pt_Stop () failed!" << std::endl;
110 WavesMidiDeviceManager::stop ()
112 // COMMENTED DBG LOGS */ std::cout << "WavesMidiDeviceManager::stop ():" << std::endl;
114 if ( _active == false ) {
123 if (Pm_Terminate () != pmNoError) {
124 std::cerr << "WavesMidiDeviceManager::stop (): Pt_Terminate () failed!" << std::endl;
132 WavesMidiDeviceManager::__portmidi_callback (PtTimestamp timestamp, void * userData)
134 // COMMENTED FREQUENT DBG LOGS */ std::cout << "WavesMidiDeviceManager::__portmidi_callback ():" << std::endl;
135 WavesMidiDeviceManager *dm = (WavesMidiDeviceManager *)userData;
141 dm->_portmidi_callback (timestamp);
145 WavesMidiDeviceManager::_portmidi_callback (PtTimestamp timestamp)
147 if ((!_active) || (!_streaming)) {
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;
159 void WavesMidiDeviceManager::do_read ()
161 for (std::vector<WavesMidiDevice *>::const_iterator it = _devices.begin (); it != _devices.end (); ++it) {
167 void WavesMidiDeviceManager::do_write ()
169 for (std::vector<WavesMidiDevice *>::const_iterator it = _devices.begin (); it != _devices.end (); ++it) {
170 (*it)->write_midi ();
176 WavesMidiDeviceManager::__get_time_ms (void *time_info)
178 return ((WavesAudioBackend*)time_info)->sample_time ();
182 WavesMidiDevice* WavesMidiDeviceManager::_get_device (const std::string& name)
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 ()) {
195 WavesMidiDeviceManager::_create_devices ()
197 // COMMENTED DBG LOGS */ std::cout << "WavesMidiDeviceManager::_create_devices () :" << std::endl;
198 int count = Pm_CountDevices ();
200 for (int i = 0; i < count; i++) {
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;
214 if (pm_device_info == NULL) {
215 std::cerr << "WavesMidiDeviceManager::_create_devices (): Pm_GetDeviceInfo (" << i << ") failed!" << std::endl;
219 WavesMidiDevice *device = _get_device (pm_device_info->name);
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;
234 WavesMidiDeviceManager::_delete_devices ()
236 // COMMENTED DBG LOGS */ std::cout << "WavesMidiDeviceManager::_delete_devices ():" << std::endl;
237 while (!_devices.empty ()) {
238 WavesMidiDevice * device = _devices.back ();
239 _devices.pop_back ();