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::stream ():" << 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 ():" << std::endl;
82 std::cerr << "WavesMidiDeviceManager::stream (): the midi device manager is not started up !" << std::endl;
86 if (_streaming == yn) {
91 if ( Pt_Start (1, __portmidi_callback, this) != ptNoError) {
92 std::cerr << "WavesMidiDeviceManager::stream (): Pt_Start () failed!" << std::endl;
97 if (Pt_Stop () != ptNoError) {
98 std::cerr << "WavesMidiDeviceManager::stream (): Pt_Stop () failed!" << std::endl;
109 WavesMidiDeviceManager::stop ()
111 // COMMENTED DBG LOGS */ std::cout << "WavesMidiDeviceManager::stop ():" << std::endl;
113 if ( _active == false ) {
122 if (Pm_Terminate () != pmNoError) {
123 std::cerr << "WavesMidiDeviceManager::stop (): Pt_Terminate () failed!" << std::endl;
131 WavesMidiDeviceManager::__portmidi_callback (PtTimestamp timestamp, void * userData)
133 // COMMENTED DBG LOGS */ std::cout << "WavesMidiDeviceManager::__portmidi_callback ():" << std::endl;
134 WavesMidiDeviceManager *dm = (WavesMidiDeviceManager *)userData;
140 dm->_portmidi_callback (timestamp);
144 WavesMidiDeviceManager::_portmidi_callback (PtTimestamp timestamp)
146 if ((!_active) || (!_streaming)) {
150 if ((_input_device_count != midiInGetNumDevs ()) || (_output_device_count != midiOutGetNumDevs ())) {
151 _audiobackend._changed_midi_devices ();
156 void WavesMidiDeviceManager::do_read ()
158 for (std::vector<WavesMidiDevice *>::const_iterator it = _devices.begin (); it != _devices.end (); ++it) {
164 void WavesMidiDeviceManager::do_write ()
166 for (std::vector<WavesMidiDevice *>::const_iterator it = _devices.begin (); it != _devices.end (); ++it) {
167 (*it)->write_midi ();
173 WavesMidiDeviceManager::__get_time_ms (void *time_info)
175 return ((WavesAudioBackend*)time_info)->sample_time ();
179 WavesMidiDevice* WavesMidiDeviceManager::_get_device (const std::string& name)
181 for (size_t i = 0; i < _devices.size (); i++) {
182 if (name == _devices[i]->name ()) {
191 WavesMidiDeviceManager::_create_devices ()
193 int count = Pm_CountDevices ();
195 for (int i = 0; i < count; i++) {
197 const PmDeviceInfo* pm_device_info = Pm_GetDeviceInfo (i);
198 // COMMENTED DBG LOGS */ std::cout << " interf : " << pm_device_info->interf << std::endl;
199 // COMMENTED DBG LOGS */ std::cout << " name : " << pm_device_info->name << std::endl;
200 // COMMENTED DBG LOGS */ std::cout << " input : " << pm_device_info->input << std::endl;
201 // COMMENTED DBG LOGS */ std::cout << " output : " << pm_device_info->output << std::endl;
202 // COMMENTED DBG LOGS */ std::cout << " opened : " << pm_device_info->opened << std::endl;
203 #if defined (PLATFORM_WINDOWS)
204 if (strncmp (pm_device_info->name, "Microsoft", strlen ("Microsoft")) == 0) {
205 // COMMENTED DBG LOGS */ std::cout << " skipping anything from Microsoft :" << pm_device_info->name << std::endl;
209 if (pm_device_info == NULL) {
210 std::cerr << "WavesMidiDeviceManager::_create_devices (): Pm_GetDeviceInfo (" << i << ") failed!" << std::endl;
214 WavesMidiDevice *device = _get_device (pm_device_info->name);
216 device = new WavesMidiDevice (pm_device_info->name);
217 _devices.push_back (device);
218 if (device->open (__get_time_ms, (void*)&_audiobackend)) {
219 std::cerr << "WavesMidiDeviceManager::_create_devices (): [" << device->name () << "]->open () failed!" << std::endl;
229 WavesMidiDeviceManager::_delete_devices ()
231 // COMMENTED DBG LOGS */ std::cout << "WavesMidiDeviceManager::_delete_devices ():" << std::endl;
232 while (!_devices.empty ()) {
233 WavesMidiDevice * device = _devices.back ();
234 _devices.pop_back ();