2014-04-30 13:46:41 -04:00
|
|
|
/*
|
2014-10-07 16:09:29 -04:00
|
|
|
Copyright (C) 2013 Waves Audio Ltd.
|
2014-04-30 13:46:41 -04:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-05-06 12:00:35 -04:00
|
|
|
#include <iostream>
|
2015-05-13 19:11:27 -04:00
|
|
|
|
|
|
|
#include "pbd/error.h"
|
|
|
|
#include "pbd/debug.h"
|
|
|
|
#include "pbd/compose.h"
|
|
|
|
#include "pbd/stacktrace.h"
|
|
|
|
|
2014-04-30 13:46:41 -04:00
|
|
|
#include "waves_midi_device.h"
|
|
|
|
#include "waves_midi_event.h"
|
|
|
|
|
|
|
|
// use non-zero latency because we want output to be timestapmed
|
|
|
|
#define LATENCY 0
|
|
|
|
|
|
|
|
#define QUEUE_LENGTH 1024
|
|
|
|
|
|
|
|
using namespace ARDOUR;
|
2015-05-13 19:11:27 -04:00
|
|
|
using namespace PBD;
|
2014-04-30 13:46:41 -04:00
|
|
|
|
|
|
|
WavesMidiDevice::WavesMidiDevice (const std::string& device_name)
|
|
|
|
: _pm_input_id (pmNoDevice)
|
|
|
|
, _pm_output_id (pmNoDevice)
|
|
|
|
, _name (device_name)
|
|
|
|
, _input_queue (NULL)
|
|
|
|
, _output_queue (NULL)
|
|
|
|
, _input_pm_stream (NULL)
|
|
|
|
, _output_pm_stream (NULL)
|
|
|
|
, _incomplete_waves_midi_event (NULL)
|
|
|
|
{
|
2015-03-02 13:19:50 -05:00
|
|
|
_pm_input_id = _pm_output_id = pmNoDevice;
|
|
|
|
int count = Pm_CountDevices ();
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2015-03-02 13:19:50 -05:00
|
|
|
for (int i = 0; i < count; i++) {
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2015-03-02 13:19:50 -05:00
|
|
|
const PmDeviceInfo* pm_device_info = Pm_GetDeviceInfo (i);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2015-03-02 13:19:50 -05:00
|
|
|
if (pm_device_info == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (name () == pm_device_info->name) {
|
|
|
|
if (pm_device_info->input){
|
|
|
|
_pm_input_id = i;
|
|
|
|
}
|
|
|
|
if (pm_device_info->output){
|
|
|
|
_pm_output_id = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-30 13:46:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
WavesMidiDevice::~WavesMidiDevice ()
|
|
|
|
{
|
2015-05-13 19:11:27 -04:00
|
|
|
DEBUG_TRACE (DEBUG::WavesMIDI, string_compose ("WavesMidiDevice::~WavesMidiDevice (): %1\n", name()));
|
|
|
|
close ();
|
2014-04-30 13:46:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
WavesMidiDevice::open (PmTimeProcPtr time_proc, void* time_info)
|
|
|
|
{
|
2015-05-13 19:11:27 -04:00
|
|
|
DEBUG_TRACE (DEBUG::WavesMIDI, string_compose ("WavesMidiDevice::open (): %1", name ()));
|
|
|
|
|
|
|
|
if (is_input () ) {
|
2015-03-31 17:08:21 -04:00
|
|
|
// COMMENTED DBG LOGS */ std::cout << "WavesMidiDevice::open (): INPUT" << _pm_input_id << "-[" << name () << "]" << std::endl;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2015-03-31 17:08:21 -04:00
|
|
|
if (!_input_pm_stream) {
|
|
|
|
// create queue
|
|
|
|
if (!_input_queue) {
|
|
|
|
// COMMENTED DBG LOGS */ std::cout << " going to Pm_QueueCreate for INPUT: " << std::endl;
|
|
|
|
_input_queue = Pm_QueueCreate (QUEUE_LENGTH, sizeof (const WavesMidiEvent*));
|
|
|
|
// COMMENTED DBG LOGS */ std::cout << " DONE : " << std::endl;
|
|
|
|
if (NULL == _input_queue) {
|
2015-05-13 19:11:27 -04:00
|
|
|
std::cerr << "WavesMidiDevice::open (): _input_queue = Pm_QueueCreate () failed for " << _pm_input_id << "-[" << name () << "]!" << std::endl;
|
|
|
|
return -1;
|
2015-03-31 17:08:21 -04:00
|
|
|
}
|
2015-10-04 14:51:05 -04:00
|
|
|
}
|
2015-03-31 17:08:21 -04:00
|
|
|
// create stream
|
|
|
|
// COMMENTED DBG LOGS */ std::cout << " going to Pm_OpenInput : " << std::endl;
|
2015-10-04 14:51:05 -04:00
|
|
|
if (pmNoError != Pm_OpenInput (&_input_pm_stream,
|
2015-05-13 19:11:27 -04:00
|
|
|
_pm_input_id,
|
|
|
|
NULL,
|
|
|
|
1024,
|
|
|
|
time_proc,
|
|
|
|
time_info)) {
|
|
|
|
// COMMENTED DBG LOGS */ std::cout << " DONE : " << std::endl;
|
|
|
|
char* err_msg = new char[256];
|
|
|
|
Pm_GetHostErrorText(err_msg, 256);
|
|
|
|
std::cerr << "WavesMidiDevice::open (): Pm_OpenInput () failed for " << _pm_input_id << "-[" << name () << "]!" << std::endl;
|
|
|
|
std::cerr << " Port Midi Host Error: " << err_msg << std::endl;
|
|
|
|
close ();
|
|
|
|
return -1;
|
|
|
|
}
|
2015-03-31 17:08:21 -04:00
|
|
|
// COMMENTED DBG LOGS */ std::cout << " DONE : " << std::endl;
|
2015-10-04 14:51:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 17:08:21 -04:00
|
|
|
if (is_output () ) {
|
|
|
|
// COMMENTED DBG LOGS */ std::cout << "WavesMidiDevice::open (): OUTPUT" << _pm_output_id << "-[" << name () << "]" << std::endl;
|
|
|
|
|
|
|
|
if (!_output_pm_stream) {
|
|
|
|
// create queue
|
|
|
|
if (!_output_queue) {
|
|
|
|
// COMMENTED DBG LOGS */ std::cout << " going to Pm_QueueCreate for OUTPUT : " << std::endl;
|
|
|
|
_output_queue = Pm_QueueCreate (QUEUE_LENGTH, sizeof (const WavesMidiEvent*));
|
|
|
|
// COMMENTED DBG LOGS */ std::cout << " DONE : " << std::endl;
|
|
|
|
if (NULL == _output_queue) {
|
|
|
|
std::cerr << "WavesMidiDevice::open (): _output_queue = Pm_QueueCreate () failed for " << _pm_output_id << "-[" << name () << "]!" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// create stream
|
|
|
|
// COMMENTED DBG LOGS */ std::cout << " going to Pm_OpenOutput : " << std::endl;
|
2015-10-04 14:51:05 -04:00
|
|
|
if (pmNoError != Pm_OpenOutput (&_output_pm_stream,
|
|
|
|
_pm_output_id,
|
2015-03-31 17:08:21 -04:00
|
|
|
NULL,
|
|
|
|
1024,
|
|
|
|
time_proc,
|
|
|
|
time_info,
|
|
|
|
LATENCY)) {
|
|
|
|
// COMMENTED DBG LOGS */ std::cout << " DONE : " << std::endl;
|
|
|
|
char* err_msg = new char[256];
|
|
|
|
Pm_GetHostErrorText(err_msg, 256);
|
|
|
|
std::cerr << "WavesMidiDevice::open (): Pm_OpenOutput () failed for " << _pm_output_id << "-[" << name () << "]!" << std::endl;
|
|
|
|
std::cerr << " Port Midi Host Error: " << err_msg << std::endl;
|
|
|
|
close ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// COMMENTED DBG LOGS */ std::cout << " DONE : " << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2014-04-30 13:46:41 -04:00
|
|
|
}
|
|
|
|
|
2015-03-31 18:03:51 -04:00
|
|
|
void
|
2014-04-30 13:46:41 -04:00
|
|
|
WavesMidiDevice::close ()
|
|
|
|
{
|
2015-05-13 19:11:27 -04:00
|
|
|
DEBUG_TRACE (DEBUG::WavesMIDI, string_compose ("WavesMidiDevice::close (): %1\n", name ()));
|
2015-03-31 17:08:21 -04:00
|
|
|
WavesMidiEvent *waves_midi_event;
|
|
|
|
|
|
|
|
// save _input_pm_stream and _output_pm_stream to local buf
|
|
|
|
PmStream* input_pm_stream = _input_pm_stream;
|
|
|
|
PmStream* output_pm_stream = _output_pm_stream;
|
|
|
|
_input_pm_stream = _output_pm_stream = NULL;
|
|
|
|
|
2015-03-31 18:03:51 -04:00
|
|
|
// input
|
2015-03-31 17:08:21 -04:00
|
|
|
if (input_pm_stream) {
|
|
|
|
// close stream
|
|
|
|
PmError err = Pm_Close (input_pm_stream);
|
|
|
|
if (err != pmNoError) {
|
|
|
|
char* err_msg = new char[256];
|
|
|
|
Pm_GetHostErrorText(err_msg, 256);
|
|
|
|
std::cerr << "WavesMidiDevice::close (): Pm_Close (input_pm_stream) failed (" << err << ") for " << input_pm_stream << "-[" << name () << "]!" << std::endl;
|
|
|
|
std::cerr << " Port Midi Host Error: " << err_msg << std::endl;
|
|
|
|
}
|
|
|
|
_pm_input_id = pmNoDevice;
|
|
|
|
}
|
|
|
|
|
|
|
|
// close queue
|
|
|
|
if (_input_queue) {
|
|
|
|
while (1 == Pm_Dequeue (_input_queue, &waves_midi_event)) {
|
|
|
|
delete waves_midi_event; // XXX possible dup free in ~WavesMidiBuffer() (?)
|
|
|
|
}
|
|
|
|
Pm_QueueDestroy (_input_queue);
|
|
|
|
_input_queue = NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-05 10:17:49 -04:00
|
|
|
// output
|
2015-03-31 17:08:21 -04:00
|
|
|
if ( output_pm_stream ) {
|
|
|
|
// close stream
|
|
|
|
PmError err = Pm_Close (output_pm_stream);
|
|
|
|
if (err != pmNoError) {
|
|
|
|
char* err_msg = new char[256];
|
|
|
|
Pm_GetHostErrorText(err_msg, 256);
|
|
|
|
std::cerr << "WavesMidiDevice::close (): Pm_Close (output_pm_stream) failed (" << err << ") for " << output_pm_stream << "-[" << name () << "]!" << std::endl;
|
|
|
|
std::cerr << " Port Midi Host Error: " << err_msg << std::endl;
|
|
|
|
}
|
|
|
|
_pm_output_id = pmNoDevice;
|
|
|
|
}
|
|
|
|
|
|
|
|
// close queue
|
|
|
|
if (_output_queue) {
|
|
|
|
while (1 == Pm_Dequeue (_output_queue, &waves_midi_event)) {
|
|
|
|
delete waves_midi_event; // XXX possible dup free in ~WavesMidiBuffer() (?)
|
|
|
|
}
|
|
|
|
Pm_QueueDestroy (_output_queue);
|
|
|
|
_output_queue = NULL;
|
|
|
|
}
|
2014-04-30 13:46:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WavesMidiDevice::do_io ()
|
|
|
|
{
|
2015-05-13 19:11:27 -04:00
|
|
|
read_midi ();
|
|
|
|
write_midi ();
|
2014-04-30 13:46:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WavesMidiDevice::read_midi ()
|
|
|
|
{
|
2015-05-13 19:11:27 -04:00
|
|
|
if (NULL == _input_pm_stream) {
|
|
|
|
return;
|
2014-04-30 13:46:41 -04:00
|
|
|
}
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2015-05-13 19:11:27 -04:00
|
|
|
while (Pm_Poll (_input_pm_stream) > 0) {
|
|
|
|
|
|
|
|
PmEvent pm_event; // just one message at a time
|
|
|
|
int result = Pm_Read (_input_pm_stream, &pm_event, 1);
|
|
|
|
|
|
|
|
if (result < 0) {
|
|
|
|
DEBUG_TRACE (DEBUG::WavesMIDI, string_compose ("Pm_Read failed for (): [%1]\n", name()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_TRACE (DEBUG::WavesMIDI, string_compose ("WavesMidiDevice::_read_midi (): [%1] evt-tm: %2\n", name(), pm_event.timestamp));
|
|
|
|
|
|
|
|
if (_incomplete_waves_midi_event == NULL ) {
|
|
|
|
DEBUG_TRACE (DEBUG::WavesMIDI, string_compose ("WavesMidiDevice::_read_midi (): [%1] new incomplete_waves_midi_event\n", name()));
|
|
|
|
_incomplete_waves_midi_event = new WavesMidiEvent (pm_event.timestamp);
|
|
|
|
}
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2015-05-13 19:11:27 -04:00
|
|
|
WavesMidiEvent *nested_pm_event = _incomplete_waves_midi_event->append_data (pm_event);
|
|
|
|
|
|
|
|
if (nested_pm_event) {
|
|
|
|
Pm_Enqueue (_input_queue, &nested_pm_event);
|
|
|
|
DEBUG_TRACE (DEBUG::WavesMIDI, string_compose ("WavesMidiDevice::_read_midi (): [%1] : Pm_Enqueue (_input_queue, nested_pm_event)\n", name()));
|
|
|
|
}
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2015-05-13 19:11:27 -04:00
|
|
|
switch ( _incomplete_waves_midi_event->state ()) {
|
|
|
|
case WavesMidiEvent::BROKEN:
|
|
|
|
delete _incomplete_waves_midi_event;
|
|
|
|
_incomplete_waves_midi_event = NULL;
|
|
|
|
DEBUG_TRACE (DEBUG::WavesMIDI, string_compose ("WavesMidiDevice::_read_midi (): [%1] : case WavesMidiEvent::BROKEN:\n", name()));
|
|
|
|
break;
|
|
|
|
case WavesMidiEvent::COMPLETE:
|
|
|
|
DEBUG_TRACE (DEBUG::WavesMIDI, string_compose ("WavesMidiDevice::_read_midi (): [%1] : Pm_Enqueue (_input_queue, _incomplete_waves_midi_event); %3\n", name (), _incomplete_waves_midi_event));
|
|
|
|
|
|
|
|
if (pmNoError != Pm_Enqueue (_input_queue, &_incomplete_waves_midi_event) ) {
|
|
|
|
char* err_msg = new char[256];
|
|
|
|
Pm_GetHostErrorText(err_msg, 256);
|
|
|
|
std::cerr << "WavesMidiDevice::read_midi (): Pm_Enqueue () failed for [" << name () << "]!" << std::endl;
|
|
|
|
std::cerr << "Error: " << err_msg << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
_incomplete_waves_midi_event = NULL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-04-30 13:46:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WavesMidiDevice::write_midi ()
|
|
|
|
{
|
2015-05-13 19:11:27 -04:00
|
|
|
if (NULL == _output_pm_stream) {
|
|
|
|
return;
|
2014-04-30 13:46:41 -04:00
|
|
|
}
|
2015-05-13 19:11:27 -04:00
|
|
|
|
|
|
|
PmError err;
|
|
|
|
WavesMidiEvent *waves_midi_event;
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2015-05-13 19:11:27 -04:00
|
|
|
while (1 == Pm_Dequeue (_output_queue, &waves_midi_event)) {
|
|
|
|
if (waves_midi_event->sysex ()) {
|
|
|
|
// LATENCY compensation
|
|
|
|
err = Pm_WriteSysEx (_output_pm_stream, waves_midi_event->timestamp () - LATENCY, waves_midi_event->data ());
|
|
|
|
if (0 > err) {
|
|
|
|
std::cerr << "WavesMidiDevice::write_event_to_device (): [" << name () << "] Pm_WriteSysEx () failed (" << err << ")!" << std::endl;
|
|
|
|
};
|
|
|
|
DEBUG_TRACE (DEBUG::WavesMIDI, string_compose ("WavesMidiDevice::_write_midi (): SYSEX used, ev->tm: %1", waves_midi_event->timestamp () - LATENCY));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = Pm_WriteShort (_output_pm_stream, waves_midi_event->timestamp () - LATENCY, * (PmMessage*)waves_midi_event->data ());
|
|
|
|
if (0 > err) {
|
|
|
|
error << "WavesMidiDevice::write_event_to_device (): [" << name () << "] Pm_WriteShort () failed (" << err << ")!" << endmsg;
|
|
|
|
}
|
|
|
|
DEBUG_TRACE (DEBUG::WavesMIDI, string_compose ("WavesMidiDevice::_write_midi (): SHORTMSG used, ev->tm: %1\n", waves_midi_event->timestamp () - LATENCY));
|
|
|
|
}
|
2014-04-30 13:46:41 -04:00
|
|
|
}
|
|
|
|
|
2015-05-13 19:11:27 -04:00
|
|
|
return;
|
|
|
|
}
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2014-04-30 13:46:41 -04:00
|
|
|
int
|
|
|
|
WavesMidiDevice::enqueue_output_waves_midi_event (const WavesMidiEvent* waves_midi_event)
|
|
|
|
{
|
2015-05-13 19:11:27 -04:00
|
|
|
DEBUG_TRACE (DEBUG::WavesMIDI, string_compose ("WavesMidiDevice::enqueue_output_waves_midi_event () [%1]\n", name()));
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2015-05-13 19:11:27 -04:00
|
|
|
if (waves_midi_event == NULL) {
|
|
|
|
error << "WavesMidiDevice::put_event_to_callback (): 'waves_midi_event' is NULL!" << endmsg;
|
|
|
|
return -1;
|
|
|
|
}
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2015-05-13 19:11:27 -04:00
|
|
|
PmError err = Pm_Enqueue (_output_queue, &waves_midi_event);
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2015-05-13 19:11:27 -04:00
|
|
|
if (0 > err) {
|
|
|
|
error << "WavesMidiDevice::put_event_to_callback (): Pm_Enqueue () failed (" << err << ")!" << endmsg;
|
|
|
|
return -1;
|
|
|
|
};
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2015-05-13 19:11:27 -04:00
|
|
|
return 0;
|
2014-04-30 13:46:41 -04:00
|
|
|
}
|
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
WavesMidiEvent*
|
2014-04-30 13:46:41 -04:00
|
|
|
WavesMidiDevice::dequeue_input_waves_midi_event ()
|
|
|
|
{
|
2015-05-13 19:11:27 -04:00
|
|
|
WavesMidiEvent* waves_midi_event;
|
|
|
|
if (Pm_Dequeue (_input_queue, &waves_midi_event) == 1) {
|
|
|
|
return waves_midi_event;
|
|
|
|
}
|
|
|
|
return NULL;
|
2014-04-30 13:46:41 -04:00
|
|
|
}
|