2024-12-01 19:35:24 +00:00
|
|
|
//| Copyright: (C) 2020-2024 Kevin Larke <contact AT larke DOT org>
|
|
|
|
//| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
|
2019-12-24 15:05:24 +00:00
|
|
|
#include "cwCommon.h"
|
|
|
|
#include "cwLog.h"
|
|
|
|
#include "cwCommonImpl.h"
|
2024-05-29 16:36:57 +00:00
|
|
|
#include "cwTest.h"
|
2019-12-24 15:05:24 +00:00
|
|
|
#include "cwMem.h"
|
|
|
|
#include "cwTime.h"
|
2024-02-14 16:20:40 +00:00
|
|
|
#include "cwText.h"
|
|
|
|
#include "cwObject.h"
|
2019-12-24 15:05:24 +00:00
|
|
|
#include "cwTextBuf.h"
|
2024-02-14 16:20:40 +00:00
|
|
|
#include "cwMidi.h"
|
|
|
|
#include "cwMidiDecls.h"
|
|
|
|
#include "cwMidiDevice.h"
|
|
|
|
#include "cwMidiParser.h"
|
|
|
|
|
|
|
|
#include <poll.h>
|
|
|
|
|
|
|
|
#include "cwMidiAlsa.h"
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
#include <alsa/asoundlib.h>
|
|
|
|
|
|
|
|
namespace cw
|
|
|
|
{
|
|
|
|
namespace midi
|
|
|
|
{
|
|
|
|
namespace device
|
|
|
|
{
|
2024-02-14 16:20:40 +00:00
|
|
|
namespace alsa
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2024-02-14 16:20:40 +00:00
|
|
|
const unsigned kCtoD_MapN = 256;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
bool inputFl; // true if this an input port
|
|
|
|
char* nameStr; // string label of this device
|
|
|
|
unsigned alsa_type; // ALSA type flags from snd_seq_port_info_get_type()
|
|
|
|
unsigned alsa_cap; // ALSA capability flags from snd_seq_port_info_get_capability()
|
|
|
|
snd_seq_addr_t alsa_addr; // ALSA client/port address for this port
|
|
|
|
parser::handle_t parserH; // interface to the client callback function for this port
|
|
|
|
} port_t;
|
|
|
|
|
|
|
|
// MIDI devices
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char* nameStr; // string label for this device
|
|
|
|
unsigned iPortCnt; // input ports on this device
|
|
|
|
port_t* iPortArray; //
|
|
|
|
unsigned oPortCnt; // output ports on this device
|
|
|
|
port_t* oPortArray; //
|
|
|
|
unsigned char clientId; // ALSA client id (all ports on this device use use this client id in their address)
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
} dev_t;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
typedef struct alsa_device_str
|
|
|
|
{
|
|
|
|
unsigned devCnt; // MIDI devices attached to this computer
|
|
|
|
dev_t* devArray;
|
2024-07-08 20:56:33 +00:00
|
|
|
cbFunc_t cbFunc; // MIDI input application callback
|
|
|
|
bool filterRtSenseFl;
|
2024-02-14 16:20:40 +00:00
|
|
|
void* cbDataPtr;
|
|
|
|
snd_seq_t* h; // ALSA system sequencer handle
|
|
|
|
snd_seq_addr_t alsa_addr; // ALSA client/port address representing the application
|
|
|
|
int alsa_queue; // ALSA device queue
|
|
|
|
int alsa_fdCnt; // MIDI input driver file descriptor array
|
|
|
|
struct pollfd* alsa_fd;
|
|
|
|
dev_t* prvRcvDev; // the last device and port to rcv MIDI
|
|
|
|
port_t* prvRcvPort;
|
|
|
|
unsigned prvTimeMicroSecs; // time of last recognized event in microseconds
|
|
|
|
unsigned eventCnt; // count of recognized events
|
|
|
|
time::spec_t baseTimeStamp;
|
|
|
|
|
|
|
|
unsigned clientIdToDevIdxMap[ kCtoD_MapN ];
|
|
|
|
|
|
|
|
bool latency_meas_enable_in_fl;
|
|
|
|
bool latency_meas_enable_out_fl;
|
|
|
|
latency_meas_result_t latency_meas_result;
|
2024-07-08 20:56:33 +00:00
|
|
|
|
2024-02-10 16:34:06 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
} alsa_device_t;
|
2020-01-27 22:47:14 +00:00
|
|
|
|
2024-02-08 15:58:52 +00:00
|
|
|
#define _cmMpErrMsg( rc, alsaRc, str ) cwLogError(kOpFailRC,"%s : ALSA Error:%i %s",(str),(alsaRc),snd_strerror(alsaRc))
|
2024-03-25 15:45:42 +00:00
|
|
|
#define _cmMpErrMsg1( rc, alsaRc, fmt, arg ) cwLogError(kOpFailRC, fmt" : ALSA Error:%i %s",(arg),(alsaRc),snd_strerror(alsaRc))
|
|
|
|
#define _cmMpWarnMsg2( alsaRc, fmt, arg0, arg1 ) cwLogWarning(fmt" : ALSA Error:%i %s",(arg0),(arg1),(alsaRc),snd_strerror(alsaRc))
|
2024-02-08 15:58:52 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
alsa_device_t* _handleToPtr( handle_t h ){ return handleToPtr<handle_t,alsa_device_t>(h); }
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-08 15:58:52 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
unsigned _cmMpGetPortCnt( snd_seq_t* h, snd_seq_port_info_t* pip, bool inputFl )
|
|
|
|
{
|
|
|
|
unsigned i = 0;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
snd_seq_port_info_set_port(pip,-1);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
while( snd_seq_query_next_port(h,pip) == 0)
|
|
|
|
if( cwIsFlag(snd_seq_port_info_get_capability(pip),inputFl?SND_SEQ_PORT_CAP_READ:SND_SEQ_PORT_CAP_WRITE) )
|
|
|
|
++i;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
return i;
|
|
|
|
}
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
dev_t* _cmMpClientIdToDev( alsa_device_t* p, unsigned char clientId )
|
|
|
|
{
|
|
|
|
unsigned devIdx = p->clientIdToDevIdxMap[ clientId ];
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
if( devIdx != kInvalidIdx )
|
|
|
|
return p->devArray + devIdx;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
port_t* _cmMpInPortIdToPort( dev_t* dev, int portId )
|
|
|
|
{
|
|
|
|
unsigned i;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
for(i=0; i<dev->iPortCnt; ++i)
|
|
|
|
if( dev->iPortArray[i].alsa_addr.port == portId )
|
|
|
|
return dev->iPortArray + i;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
void _cmMpSplit14Bits( unsigned v, uint8_t* d0, uint8_t* d1 )
|
|
|
|
{
|
|
|
|
*d0 = (v & 0x3f80) >> 7;
|
|
|
|
*d1 = v & 0x7f;
|
|
|
|
}
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
rc_t _handle_input_msg( alsa_device_t* p )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2024-02-14 16:20:40 +00:00
|
|
|
rc_t rc = kOkRC;
|
|
|
|
|
|
|
|
snd_seq_event_t *ev;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2024-02-10 16:34:06 +00:00
|
|
|
int alsa_rc = snd_seq_event_input(p->h,&ev);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
// if no input
|
2024-02-10 16:34:06 +00:00
|
|
|
if( alsa_rc == -EAGAIN )
|
2022-11-11 18:15:35 +00:00
|
|
|
{
|
|
|
|
// TODO: report or at least count error
|
2019-12-24 15:05:24 +00:00
|
|
|
break;
|
2022-11-11 18:15:35 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
// if input buffer overrun
|
2024-02-10 16:34:06 +00:00
|
|
|
if( alsa_rc == -ENOSPC )
|
2022-11-11 18:15:35 +00:00
|
|
|
{
|
|
|
|
// TODO: report or at least count error
|
2019-12-24 15:05:24 +00:00
|
|
|
break;
|
2022-11-11 18:15:35 +00:00
|
|
|
}
|
2024-02-10 16:34:06 +00:00
|
|
|
|
2022-11-11 18:15:35 +00:00
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
// get the device this event arrived from
|
|
|
|
if( p->prvRcvDev==NULL || p->prvRcvDev->clientId != ev->source.client )
|
2020-01-27 22:47:14 +00:00
|
|
|
p->prvRcvDev = _cmMpClientIdToDev(p,ev->source.client);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
// get the port this event arrived from
|
|
|
|
if( p->prvRcvDev != NULL && (p->prvRcvPort==NULL || p->prvRcvPort->alsa_addr.port != ev->source.port) )
|
|
|
|
p->prvRcvPort = _cmMpInPortIdToPort(p->prvRcvDev,ev->source.port);
|
|
|
|
|
|
|
|
if( p->prvRcvDev == NULL || p->prvRcvPort == NULL )
|
|
|
|
continue;
|
2022-11-11 18:15:35 +00:00
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
//printf("%i %x\n",ev->type,ev->type);
|
|
|
|
//printf("dev:%i port:%i ch:%i %i\n",ev->source.client,ev->source.port,ev->data.note.channel,ev->data.note.note);
|
|
|
|
|
|
|
|
unsigned microSecs1 = (ev->time.time.tv_sec * 1000000) + (ev->time.time.tv_nsec/1000);
|
|
|
|
//unsigned deltaMicroSecs = p->prvTimeMicroSecs==0 ? 0 : microSecs1 - p->prvTimeMicroSecs;
|
2020-03-23 14:41:28 +00:00
|
|
|
uint8_t d0 = 0xff;
|
|
|
|
uint8_t d1 = 0xff;
|
|
|
|
uint8_t status = 0;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
switch(ev->type)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// MIDI Channel Messages
|
|
|
|
//
|
|
|
|
|
|
|
|
case SND_SEQ_EVENT_NOTEON:
|
|
|
|
status = kNoteOnMdId;
|
|
|
|
d0 = ev->data.note.note;
|
|
|
|
d1 = ev->data.note.velocity;
|
2024-02-10 16:34:06 +00:00
|
|
|
|
|
|
|
if( p->latency_meas_enable_in_fl )
|
|
|
|
{
|
|
|
|
p->latency_meas_enable_in_fl = false;
|
|
|
|
time::get(p->latency_meas_result.note_on_input_ts);
|
|
|
|
//p->latency_meas_result.note_on_input_ts.tv_sec = ev->time.time.tv_sec;
|
|
|
|
//p->latency_meas_result.note_on_input_ts.tv_nsec = ev->time.time.tv_nsec;
|
|
|
|
}
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
//printf("%s (%i : %i) (%i)\n", snd_seq_ev_is_abstime(ev)?"abs":"rel",ev->time.time.tv_sec,ev->time.time.tv_nsec, deltaMicroSecs/1000);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_SEQ_EVENT_NOTEOFF:
|
|
|
|
status = kNoteOffMdId;
|
|
|
|
d0 = ev->data.note.note;
|
|
|
|
d1 = ev->data.note.velocity;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_SEQ_EVENT_KEYPRESS:
|
|
|
|
status = kPolyPresMdId;
|
|
|
|
d0 = ev->data.note.note;
|
|
|
|
d1 = ev->data.note.velocity;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_SEQ_EVENT_PGMCHANGE:
|
|
|
|
status = kPgmMdId;
|
|
|
|
d0 = ev->data.control.param;
|
|
|
|
d1 = 0xff;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_SEQ_EVENT_CHANPRESS:
|
|
|
|
status = kChPresMdId;
|
|
|
|
d0 = ev->data.control.param;
|
|
|
|
d1 = 0xff;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_SEQ_EVENT_CONTROLLER:
|
|
|
|
status = kCtlMdId;
|
|
|
|
d0 = ev->data.control.param;
|
|
|
|
d1 = ev->data.control.value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_SEQ_EVENT_PITCHBEND:
|
|
|
|
_cmMpSplit14Bits(ev->data.control.value + 8192, &d0, &d1 );
|
|
|
|
status = kPbendMdId;
|
|
|
|
break;
|
|
|
|
|
|
|
|
//
|
|
|
|
// MIDI System Common Messages
|
|
|
|
//
|
|
|
|
case SND_SEQ_EVENT_QFRAME:
|
|
|
|
status = kSysComMtcMdId;
|
|
|
|
d0 = ev->data.control.value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_SEQ_EVENT_SONGPOS:
|
|
|
|
_cmMpSplit14Bits(ev->data.control.value, &d0, &d1 );
|
|
|
|
status = kSysComSppMdId;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_SEQ_EVENT_SONGSEL:
|
|
|
|
status = kSysComSelMdId;
|
|
|
|
d0 = ev->data.control.value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_SEQ_EVENT_TUNE_REQUEST:
|
|
|
|
status = kSysComTuneMdId;
|
|
|
|
break;
|
|
|
|
|
|
|
|
//
|
|
|
|
// MIDI System Real-time Messages
|
|
|
|
//
|
|
|
|
case SND_SEQ_EVENT_CLOCK: status = kSysRtClockMdId; break;
|
|
|
|
case SND_SEQ_EVENT_START: status = kSysRtStartMdId; break;
|
|
|
|
case SND_SEQ_EVENT_CONTINUE: status = kSysRtContMdId; break;
|
|
|
|
case SND_SEQ_EVENT_STOP: status = kSysRtStopMdId; break;
|
2024-07-08 20:56:33 +00:00
|
|
|
case SND_SEQ_EVENT_SENSING: status = p->filterRtSenseFl ? 0 : kSysRtSenseMdId; break;
|
2019-12-24 15:05:24 +00:00
|
|
|
case SND_SEQ_EVENT_RESET: status = kSysRtResetMdId; break;
|
|
|
|
|
2021-11-01 14:16:36 +00:00
|
|
|
case SND_SEQ_EVENT_SYSEX:
|
|
|
|
//printf("Sysex: %i\n",ev->data.ext.len);
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( status != 0 )
|
|
|
|
{
|
2020-03-23 14:41:28 +00:00
|
|
|
uint8_t ch = ev->data.note.channel;
|
2024-02-14 16:20:40 +00:00
|
|
|
/*
|
2019-12-24 15:05:24 +00:00
|
|
|
time::spec_t ts;
|
|
|
|
ts.tv_sec = p->baseTimeStamp.tv_sec + ev->time.time.tv_sec;
|
|
|
|
ts.tv_nsec = p->baseTimeStamp.tv_nsec + ev->time.time.tv_nsec;
|
2021-11-01 14:16:36 +00:00
|
|
|
while( ts.tv_nsec > 1000000000 )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
|
|
|
ts.tv_nsec -= 1000000000;
|
|
|
|
ts.tv_sec += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//printf("MIDI: %ld %ld : 0x%x %i %i\n",ts.tv_sec,ts.tv_nsec,status,d0,d1);
|
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
parser::midiTriple(p->prvRcvPort->parserH, &ts, status | ch, d0, d1 );
|
|
|
|
*/
|
|
|
|
time::spec_t ts;
|
|
|
|
ts.tv_sec = ev->time.time.tv_sec;
|
|
|
|
ts.tv_nsec = ev->time.time.tv_nsec;
|
2024-04-06 20:02:52 +00:00
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
parser::midiTriple(p->prvRcvPort->parserH, &ts, status | ch, d0, d1 );
|
|
|
|
|
|
|
|
p->prvTimeMicroSecs = microSecs1;
|
|
|
|
p->eventCnt += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}while( snd_seq_event_input_pending(p->h,0));
|
|
|
|
|
|
|
|
parser::transmit(p->prvRcvPort->parserH);
|
2024-02-10 16:34:06 +00:00
|
|
|
|
|
|
|
return rc;
|
2024-02-14 16:20:40 +00:00
|
|
|
}
|
2024-02-10 16:34:06 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
rc_t _cmMpAllocStruct( alsa_device_t* p, const char* appNameStr, cbFunc_t cbFunc, void* cbDataPtr, unsigned parserBufByteCnt )
|
2024-02-10 16:34:06 +00:00
|
|
|
{
|
2024-02-14 16:20:40 +00:00
|
|
|
rc_t rc = kOkRC;
|
|
|
|
snd_seq_client_info_t* cip = NULL;
|
|
|
|
snd_seq_port_info_t* pip = NULL;
|
|
|
|
snd_seq_port_subscribe_t *subs = NULL;
|
2024-03-25 15:45:42 +00:00
|
|
|
unsigned i,j,k;
|
|
|
|
int arc;
|
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
for(i=0; i<kCtoD_MapN; ++i)
|
|
|
|
p->clientIdToDevIdxMap[i] = kInvalidIdx;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
// alloc the subscription recd on the stack
|
|
|
|
snd_seq_port_subscribe_alloca(&subs);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
// alloc the client recd
|
|
|
|
if((arc = snd_seq_client_info_malloc(&cip)) < 0 )
|
|
|
|
{
|
|
|
|
rc = _cmMpErrMsg(kOpFailRC,arc,"ALSA seq client info allocation failed.");
|
|
|
|
goto errLabel;
|
|
|
|
}
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
// alloc the port recd
|
|
|
|
if((arc = snd_seq_port_info_malloc(&pip)) < 0 )
|
|
|
|
{
|
|
|
|
rc = _cmMpErrMsg(kOpFailRC,arc,"ALSA seq port info allocation failed.");
|
|
|
|
goto errLabel;
|
|
|
|
}
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
if((p->alsa_queue = snd_seq_alloc_queue(p->h)) < 0 )
|
|
|
|
{
|
|
|
|
rc = _cmMpErrMsg(kOpFailRC,p->alsa_queue,"ALSA queue allocation failed.");
|
|
|
|
goto errLabel;
|
|
|
|
}
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
// Set arbitrary tempo (mm=100) and resolution (240) (FROM RtMidi.cpp)
|
|
|
|
/*
|
|
|
|
snd_seq_queue_tempo_t *qtempo;
|
|
|
|
snd_seq_queue_tempo_alloca(&qtempo);
|
|
|
|
snd_seq_queue_tempo_set_tempo(qtempo, 600000);
|
|
|
|
snd_seq_queue_tempo_set_ppq(qtempo, 240);
|
|
|
|
snd_seq_set_queue_tempo(p->h, p->alsa_queue, qtempo);
|
|
|
|
snd_seq_drain_output(p->h);
|
|
|
|
*/
|
|
|
|
|
|
|
|
// setup the client port
|
|
|
|
snd_seq_set_client_name(p->h,appNameStr);
|
|
|
|
snd_seq_port_info_set_client(pip, p->alsa_addr.client = snd_seq_client_id(p->h) );
|
|
|
|
snd_seq_port_info_set_name(pip,cwStringNullGuard(appNameStr));
|
|
|
|
snd_seq_port_info_set_capability(pip,SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_DUPLEX | SND_SEQ_PORT_CAP_SUBS_READ | SND_SEQ_PORT_CAP_SUBS_WRITE );
|
|
|
|
snd_seq_port_info_set_type(pip, SND_SEQ_PORT_TYPE_SOFTWARE | SND_SEQ_PORT_TYPE_APPLICATION | SND_SEQ_PORT_TYPE_MIDI_GENERIC );
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
snd_seq_port_info_set_midi_channels(pip, 16);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
// cfg for real-time time stamping
|
|
|
|
snd_seq_port_info_set_timestamping(pip, 1);
|
|
|
|
snd_seq_port_info_set_timestamp_real(pip, 1);
|
|
|
|
snd_seq_port_info_set_timestamp_queue(pip, p->alsa_queue);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
// create the client port
|
2024-03-25 15:45:42 +00:00
|
|
|
//if((p->alsa_addr.port = snd_seq_create_port(p->h,pip)) < 0 )
|
|
|
|
if((arc = snd_seq_create_port(p->h,pip)) < 0 )
|
2024-02-14 16:20:40 +00:00
|
|
|
{
|
2024-03-25 15:45:42 +00:00
|
|
|
rc = _cmMpErrMsg(kOpFailRC,arc,"ALSA client port creation failed.");
|
2024-02-14 16:20:40 +00:00
|
|
|
goto errLabel;
|
|
|
|
}
|
2024-03-25 15:45:42 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
p->alsa_addr.port = arc;
|
|
|
|
}
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
p->devCnt = 0;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
// determine the count of devices
|
|
|
|
snd_seq_client_info_set_client(cip, -1);
|
|
|
|
while( snd_seq_query_next_client(p->h,cip) == 0)
|
|
|
|
p->devCnt += 1;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
// allocate the device array
|
|
|
|
p->devArray = mem::allocZ<dev_t>(p->devCnt);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
// fill in each device record
|
|
|
|
snd_seq_client_info_set_client(cip, -1);
|
|
|
|
for(i=0; snd_seq_query_next_client(p->h,cip)==0; ++i)
|
|
|
|
{
|
|
|
|
assert(i<p->devCnt);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
int client = snd_seq_client_info_get_client(cip);
|
|
|
|
const char* name = snd_seq_client_info_get_name(cip);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
// initalize the device record
|
|
|
|
p->devArray[i].nameStr = mem::duplStr(cwStringNullGuard(name));
|
|
|
|
p->devArray[i].iPortCnt = 0;
|
|
|
|
p->devArray[i].oPortCnt = 0;
|
|
|
|
p->devArray[i].iPortArray = NULL;
|
|
|
|
p->devArray[i].oPortArray = NULL;
|
|
|
|
p->devArray[i].clientId = client;
|
|
|
|
|
|
|
|
assert( (unsigned)client < kCtoD_MapN );
|
|
|
|
p->clientIdToDevIdxMap[ client ] = i;
|
|
|
|
|
|
|
|
snd_seq_port_info_set_client(pip,client);
|
|
|
|
snd_seq_port_info_set_port(pip,-1);
|
|
|
|
|
|
|
|
// determine the count of in/out ports on this device
|
|
|
|
while( snd_seq_query_next_port(p->h,pip) == 0 )
|
|
|
|
{
|
|
|
|
unsigned caps = snd_seq_port_info_get_capability(pip);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
if( cwIsFlag(caps,SND_SEQ_PORT_CAP_READ) )
|
|
|
|
p->devArray[i].iPortCnt += 1;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
if( cwIsFlag(caps,SND_SEQ_PORT_CAP_WRITE) )
|
|
|
|
p->devArray[i].oPortCnt += 1;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
}
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
// allocate the device port arrays
|
|
|
|
if( p->devArray[i].iPortCnt > 0 )
|
|
|
|
p->devArray[i].iPortArray = mem::allocZ<port_t>(p->devArray[i].iPortCnt);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
if( p->devArray[i].oPortCnt > 0 )
|
|
|
|
p->devArray[i].oPortArray = mem::allocZ<port_t>(p->devArray[i].oPortCnt);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
snd_seq_port_info_set_client(pip,client); // set the ports client id
|
|
|
|
snd_seq_port_info_set_port(pip,-1);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
// fill in the port information
|
|
|
|
for(j=0,k=0; snd_seq_query_next_port(p->h,pip) == 0; )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2024-02-14 16:20:40 +00:00
|
|
|
const char* port = snd_seq_port_info_get_name(pip);
|
|
|
|
unsigned type = snd_seq_port_info_get_type(pip);
|
|
|
|
unsigned caps = snd_seq_port_info_get_capability(pip);
|
|
|
|
snd_seq_addr_t addr = *snd_seq_port_info_get_addr(pip);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-03-25 15:45:42 +00:00
|
|
|
//printf("0x%x 0x%x %s %s\n",type,caps,name,port);
|
|
|
|
|
|
|
|
if( cwIsFlag(caps,SND_SEQ_PORT_CAP_READ) && cwIsFlag(caps,SND_SEQ_PORT_CAP_SUBS_READ) )
|
2024-02-14 16:20:40 +00:00
|
|
|
{
|
|
|
|
assert(j<p->devArray[i].iPortCnt);
|
|
|
|
p->devArray[i].iPortArray[j].inputFl = true;
|
2024-04-06 20:02:52 +00:00
|
|
|
p->devArray[i].iPortArray[j].nameStr = mem::duplStr(port==nullptr ? "<None>" : port);
|
2024-02-14 16:20:40 +00:00
|
|
|
p->devArray[i].iPortArray[j].alsa_type = type;
|
|
|
|
p->devArray[i].iPortArray[j].alsa_cap = caps;
|
|
|
|
p->devArray[i].iPortArray[j].alsa_addr = addr;
|
|
|
|
parser::create(p->devArray[i].iPortArray[j].parserH, i, j, cbFunc, cbDataPtr, parserBufByteCnt);
|
|
|
|
|
|
|
|
// port->app
|
|
|
|
snd_seq_port_subscribe_set_sender(subs, &addr);
|
|
|
|
snd_seq_port_subscribe_set_dest(subs, &p->alsa_addr);
|
|
|
|
snd_seq_port_subscribe_set_queue(subs, 1);
|
|
|
|
snd_seq_port_subscribe_set_time_update(subs, 1);
|
|
|
|
snd_seq_port_subscribe_set_time_real(subs, 1);
|
|
|
|
if((arc = snd_seq_subscribe_port(p->h, subs)) < 0)
|
2024-03-25 15:45:42 +00:00
|
|
|
_cmMpWarnMsg2(arc,"Input port to app. subscription failed on device:%s port '%s'.",cwStringNullGuard(name),cwStringNullGuard(port));
|
2024-02-14 16:20:40 +00:00
|
|
|
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
|
2024-03-25 15:45:42 +00:00
|
|
|
if( cwIsFlag(caps,SND_SEQ_PORT_CAP_WRITE) && cwIsFlag(caps,SND_SEQ_PORT_CAP_SUBS_WRITE) && cwIsNotFlag(type,SND_SEQ_PORT_TYPE_APPLICATION) )
|
2024-02-14 16:20:40 +00:00
|
|
|
{
|
|
|
|
assert(k<p->devArray[i].oPortCnt);
|
|
|
|
p->devArray[i].oPortArray[k].inputFl = false;
|
2024-04-06 20:02:52 +00:00
|
|
|
p->devArray[i].oPortArray[k].nameStr = mem::duplStr(port==nullptr ? "<None>" : port);
|
2024-02-14 16:20:40 +00:00
|
|
|
p->devArray[i].oPortArray[k].alsa_type = type;
|
|
|
|
p->devArray[i].oPortArray[k].alsa_cap = caps;
|
|
|
|
p->devArray[i].oPortArray[k].alsa_addr = addr;
|
|
|
|
|
|
|
|
// app->port connection
|
|
|
|
snd_seq_port_subscribe_set_sender(subs, &p->alsa_addr);
|
|
|
|
snd_seq_port_subscribe_set_dest( subs, &addr);
|
|
|
|
if((arc = snd_seq_subscribe_port(p->h, subs)) < 0 )
|
2024-03-25 15:45:42 +00:00
|
|
|
_cmMpWarnMsg2(arc,"App to output port subscription failed on device:%s port '%s'.",cwStringNullGuard(name), cwStringNullGuard(port));
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
++k;
|
|
|
|
}
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
2024-04-06 20:02:52 +00:00
|
|
|
|
|
|
|
// The capabilities of some ports may not have been as expected
|
|
|
|
// decrease the in/out port count to account for these ports
|
|
|
|
p->devArray[i].iPortCnt = j;
|
|
|
|
p->devArray[i].oPortCnt = k;
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
errLabel:
|
|
|
|
if( pip != NULL)
|
|
|
|
snd_seq_port_info_free(pip);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
if( cip != NULL )
|
|
|
|
snd_seq_client_info_free(cip);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
return rc;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
}
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
void _cmMpReportPort( textBuf::handle_t tbH, const port_t* port )
|
|
|
|
{
|
|
|
|
textBuf::print( tbH," client:%i port:%i '%s' caps:(",port->alsa_addr.client,port->alsa_addr.port,port->nameStr);
|
|
|
|
if( port->alsa_cap & SND_SEQ_PORT_CAP_READ ) textBuf::print( tbH,"Read " );
|
|
|
|
if( port->alsa_cap & SND_SEQ_PORT_CAP_WRITE ) textBuf::print( tbH,"Writ " );
|
|
|
|
if( port->alsa_cap & SND_SEQ_PORT_CAP_SYNC_READ ) textBuf::print( tbH,"Syrd " );
|
|
|
|
if( port->alsa_cap & SND_SEQ_PORT_CAP_SYNC_WRITE ) textBuf::print( tbH,"Sywr " );
|
|
|
|
if( port->alsa_cap & SND_SEQ_PORT_CAP_DUPLEX ) textBuf::print( tbH,"Dupl " );
|
|
|
|
if( port->alsa_cap & SND_SEQ_PORT_CAP_SUBS_READ ) textBuf::print( tbH,"Subr " );
|
|
|
|
if( port->alsa_cap & SND_SEQ_PORT_CAP_SUBS_WRITE ) textBuf::print( tbH,"Subw " );
|
|
|
|
if( port->alsa_cap & SND_SEQ_PORT_CAP_NO_EXPORT ) textBuf::print( tbH,"Nexp " );
|
|
|
|
|
|
|
|
textBuf::print( tbH,") type:(");
|
|
|
|
if( port->alsa_type & SND_SEQ_PORT_TYPE_SPECIFIC ) textBuf::print( tbH,"Spec ");
|
|
|
|
if( port->alsa_type & SND_SEQ_PORT_TYPE_MIDI_GENERIC) textBuf::print( tbH,"Gnrc ");
|
|
|
|
if( port->alsa_type & SND_SEQ_PORT_TYPE_MIDI_GM ) textBuf::print( tbH,"GM ");
|
|
|
|
if( port->alsa_type & SND_SEQ_PORT_TYPE_MIDI_GS ) textBuf::print( tbH,"GS ");
|
|
|
|
if( port->alsa_type & SND_SEQ_PORT_TYPE_MIDI_XG ) textBuf::print( tbH,"XG ");
|
|
|
|
if( port->alsa_type & SND_SEQ_PORT_TYPE_MIDI_MT32 ) textBuf::print( tbH,"MT32 ");
|
|
|
|
if( port->alsa_type & SND_SEQ_PORT_TYPE_MIDI_GM2 ) textBuf::print( tbH,"GM2 ");
|
|
|
|
if( port->alsa_type & SND_SEQ_PORT_TYPE_SYNTH ) textBuf::print( tbH,"Syn ");
|
|
|
|
if( port->alsa_type & SND_SEQ_PORT_TYPE_DIRECT_SAMPLE) textBuf::print( tbH,"Dsmp ");
|
|
|
|
if( port->alsa_type & SND_SEQ_PORT_TYPE_SAMPLE ) textBuf::print( tbH,"Samp ");
|
|
|
|
if( port->alsa_type & SND_SEQ_PORT_TYPE_HARDWARE ) textBuf::print( tbH,"Hwar ");
|
|
|
|
if( port->alsa_type & SND_SEQ_PORT_TYPE_SOFTWARE ) textBuf::print( tbH,"Soft ");
|
|
|
|
if( port->alsa_type & SND_SEQ_PORT_TYPE_SYNTHESIZER ) textBuf::print( tbH,"Sizr ");
|
|
|
|
if( port->alsa_type & SND_SEQ_PORT_TYPE_PORT ) textBuf::print( tbH,"Port ");
|
|
|
|
if( port->alsa_type & SND_SEQ_PORT_TYPE_APPLICATION ) textBuf::print( tbH,"Appl ");
|
|
|
|
|
|
|
|
textBuf::print( tbH,")\n");
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
}
|
2020-01-27 22:47:14 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
rc_t _destroy( alsa_device_t* p )
|
2020-01-27 22:47:14 +00:00
|
|
|
{
|
2024-02-14 16:20:40 +00:00
|
|
|
rc_t rc = kOkRC;
|
|
|
|
|
|
|
|
if( p != NULL )
|
2020-01-27 22:47:14 +00:00
|
|
|
{
|
2024-02-14 16:20:40 +00:00
|
|
|
int arc;
|
2020-01-27 22:47:14 +00:00
|
|
|
|
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
// stop the queue
|
|
|
|
if( p->h != NULL )
|
|
|
|
if((arc = snd_seq_stop_queue(p->h,p->alsa_queue, NULL)) < 0 )
|
|
|
|
{
|
|
|
|
rc = _cmMpErrMsg(kOpFailRC,arc,"ALSA queue stop failed.");
|
|
|
|
goto errLabel;
|
|
|
|
}
|
2020-01-27 22:47:14 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
// release the alsa queue
|
|
|
|
if( p->alsa_queue != -1 )
|
2020-01-27 22:47:14 +00:00
|
|
|
{
|
2024-02-14 16:20:40 +00:00
|
|
|
if((arc = snd_seq_free_queue(p->h,p->alsa_queue)) < 0 )
|
|
|
|
rc = _cmMpErrMsg(kOpFailRC,arc,"ALSA queue release failed.");
|
|
|
|
else
|
|
|
|
p->alsa_queue = -1;
|
2020-01-27 22:47:14 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
// release the alsa system handle
|
|
|
|
if( p->h != NULL )
|
2020-01-27 22:47:14 +00:00
|
|
|
{
|
2024-02-14 16:20:40 +00:00
|
|
|
if( (arc = snd_seq_close(p->h)) < 0 )
|
|
|
|
rc = _cmMpErrMsg(kOpFailRC,arc,"ALSA sequencer close failed.");
|
|
|
|
else
|
|
|
|
p->h = NULL;
|
2020-01-27 22:47:14 +00:00
|
|
|
}
|
2024-02-14 16:20:40 +00:00
|
|
|
|
|
|
|
unsigned i,j;
|
|
|
|
for(i=0; i<p->devCnt; ++i)
|
|
|
|
{
|
|
|
|
for(j=0; j<p->devArray[i].iPortCnt; ++j)
|
|
|
|
{
|
|
|
|
parser::destroy(p->devArray[i].iPortArray[j].parserH);
|
|
|
|
mem::release( p->devArray[i].iPortArray[j].nameStr );
|
|
|
|
}
|
|
|
|
|
|
|
|
for(j=0; j<p->devArray[i].oPortCnt; ++j)
|
|
|
|
{
|
|
|
|
mem::release( p->devArray[i].oPortArray[j].nameStr );
|
|
|
|
}
|
2020-01-27 22:47:14 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
mem::release(p->devArray[i].iPortArray);
|
|
|
|
mem::release(p->devArray[i].oPortArray);
|
|
|
|
mem::release(p->devArray[i].nameStr);
|
2020-01-27 22:47:14 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
}
|
2020-01-27 22:47:14 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
mem::release(p->devArray);
|
2020-01-27 22:47:14 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
mem::free(p->alsa_fd);
|
2020-01-27 22:47:14 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
mem::release(p);
|
2020-01-27 22:47:14 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
}
|
|
|
|
errLabel:
|
|
|
|
return rc;
|
2020-01-27 22:47:14 +00:00
|
|
|
}
|
2024-02-14 16:20:40 +00:00
|
|
|
} // alsa
|
2019-12-24 15:05:24 +00:00
|
|
|
} // device
|
|
|
|
} // midi
|
|
|
|
} // cw
|
|
|
|
|
2020-01-27 22:47:14 +00:00
|
|
|
|
2024-07-08 20:56:33 +00:00
|
|
|
cw::rc_t cw::midi::device::alsa::create( handle_t& h,
|
|
|
|
cbFunc_t cbFunc,
|
|
|
|
void* cbArg,
|
|
|
|
unsigned parserBufByteCnt,
|
|
|
|
const char* appNameStr,
|
|
|
|
bool filterRtSenseFl )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2020-01-27 22:47:14 +00:00
|
|
|
rc_t rc = kOkRC;
|
|
|
|
int arc = 0;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2020-01-27 22:47:14 +00:00
|
|
|
if((rc = destroy(h)) != kOkRC )
|
2019-12-24 15:05:24 +00:00
|
|
|
return rc;
|
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
alsa_device_t* p = mem::allocZ<alsa_device_t>(1);
|
2019-12-24 15:05:24 +00:00
|
|
|
p->h = NULL;
|
|
|
|
p->alsa_queue = -1;
|
|
|
|
|
|
|
|
|
|
|
|
// initialize the ALSA sequencer
|
|
|
|
if((arc = snd_seq_open(&p->h, "default", SND_SEQ_OPEN_DUPLEX, SND_SEQ_NONBLOCK )) < 0 )
|
|
|
|
{
|
|
|
|
rc = _cmMpErrMsg(kOpFailRC,arc,"ALSA Sequencer open failed.");
|
|
|
|
goto errLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
// prevent valgrind from report memory leaks in libasound (https://stackoverflow.com/questions/13478861/alsa-mem-leak)
|
|
|
|
snd_config_update_free_global();
|
|
|
|
|
|
|
|
// setup the device and port structures
|
|
|
|
if((rc = _cmMpAllocStruct(p,appNameStr,cbFunc,cbArg,parserBufByteCnt)) != kOkRC )
|
|
|
|
goto errLabel;
|
|
|
|
|
|
|
|
// allocate the file descriptors used for polling
|
|
|
|
p->alsa_fdCnt = snd_seq_poll_descriptors_count(p->h, POLLIN);
|
2024-02-14 16:20:40 +00:00
|
|
|
p->alsa_fd = mem::allocZ<struct pollfd>(p->alsa_fdCnt);
|
2019-12-24 15:05:24 +00:00
|
|
|
snd_seq_poll_descriptors(p->h, p->alsa_fd, p->alsa_fdCnt, POLLIN);
|
|
|
|
|
2024-07-08 20:56:33 +00:00
|
|
|
p->cbFunc = cbFunc;
|
|
|
|
p->cbDataPtr = cbArg;
|
|
|
|
p->filterRtSenseFl = filterRtSenseFl;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
// start the sequencer queue
|
|
|
|
if((arc = snd_seq_start_queue(p->h, p->alsa_queue, NULL)) < 0 )
|
|
|
|
{
|
|
|
|
rc = _cmMpErrMsg(kOpFailRC,arc,"ALSA queue start failed.");
|
|
|
|
goto errLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
// send any pending commands to the driver
|
|
|
|
snd_seq_drain_output(p->h);
|
|
|
|
|
|
|
|
// all time stamps will be an offset from this time stamp
|
|
|
|
clock_gettime(CLOCK_MONOTONIC,&p->baseTimeStamp);
|
|
|
|
|
2020-01-27 22:47:14 +00:00
|
|
|
h.set(p);
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
errLabel:
|
|
|
|
|
|
|
|
if( rc != kOkRC )
|
2020-01-27 22:47:14 +00:00
|
|
|
_destroy(p);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
cw::rc_t cw::midi::device::alsa::destroy( handle_t& h )
|
2020-01-27 22:47:14 +00:00
|
|
|
{
|
|
|
|
rc_t rc = kOkRC;
|
|
|
|
|
|
|
|
if( !h.isValid() )
|
|
|
|
return rc;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
alsa_device_t* p = _handleToPtr(h);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2020-01-27 22:47:14 +00:00
|
|
|
if((rc = _destroy(p)) != kOkRC )
|
|
|
|
return rc;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2020-01-27 22:47:14 +00:00
|
|
|
h.clear();
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
bool cw::midi::device::alsa::isInitialized(handle_t h)
|
2020-01-27 22:47:14 +00:00
|
|
|
{ return h.isValid(); }
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
unsigned cw::midi::device::alsa::count(handle_t h)
|
2020-01-27 22:47:14 +00:00
|
|
|
{
|
2024-02-14 16:20:40 +00:00
|
|
|
alsa_device_t* p = _handleToPtr(h);
|
2020-01-27 22:47:14 +00:00
|
|
|
return p->devCnt;
|
|
|
|
}
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
struct pollfd* cw::midi::device::alsa::pollFdArray( handle_t h, unsigned& arrayCntRef )
|
|
|
|
{
|
|
|
|
alsa_device_t* p = _handleToPtr(h);
|
|
|
|
arrayCntRef = p->alsa_fdCnt;
|
|
|
|
return p->alsa_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
cw::rc_t cw::midi::device::alsa::handleInputMsg( handle_t h )
|
|
|
|
{
|
|
|
|
alsa_device_t* p = _handleToPtr(h);
|
|
|
|
return _handle_input_msg(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char* cw::midi::device::alsa::name( handle_t h, unsigned devIdx )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2024-02-14 16:20:40 +00:00
|
|
|
alsa_device_t* p = _handleToPtr(h);
|
2020-01-27 22:47:14 +00:00
|
|
|
|
|
|
|
if( p==NULL || devIdx>=p->devCnt)
|
2019-12-24 15:05:24 +00:00
|
|
|
return NULL;
|
|
|
|
|
2020-01-27 22:47:14 +00:00
|
|
|
return p->devArray[devIdx].nameStr;
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
unsigned cw::midi::device::alsa::nameToIndex(handle_t h, const char* deviceName)
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2024-02-14 16:20:40 +00:00
|
|
|
alsa_device_t* p = _handleToPtr(h);
|
|
|
|
for(unsigned i=0; i<p->devCnt; ++i)
|
|
|
|
if( textIsEqual(p->devArray[i].nameStr,deviceName) )
|
|
|
|
return i;
|
|
|
|
|
|
|
|
return kInvalidIdx;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned cw::midi::device::alsa::portCount( handle_t h, unsigned devIdx, unsigned flags )
|
|
|
|
{
|
|
|
|
alsa_device_t* p = _handleToPtr(h);
|
2020-01-27 22:47:14 +00:00
|
|
|
|
|
|
|
if( p==NULL || devIdx>=p->devCnt)
|
2019-12-24 15:05:24 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if( cwIsFlag(flags,kInMpFl) )
|
2020-01-27 22:47:14 +00:00
|
|
|
return p->devArray[devIdx].iPortCnt;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2020-01-27 22:47:14 +00:00
|
|
|
return p->devArray[devIdx].oPortCnt;
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
const char* cw::midi::device::alsa::portName( handle_t h, unsigned devIdx, unsigned flags, unsigned portIdx )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2024-02-14 16:20:40 +00:00
|
|
|
alsa_device_t* p = _handleToPtr(h);
|
2020-01-27 22:47:14 +00:00
|
|
|
|
|
|
|
if( p==NULL || devIdx>=p->devCnt)
|
2019-12-24 15:05:24 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if( cwIsFlag(flags,kInMpFl) )
|
|
|
|
{
|
2020-01-27 22:47:14 +00:00
|
|
|
if( portIdx >= p->devArray[devIdx].iPortCnt )
|
2024-04-06 20:02:52 +00:00
|
|
|
return nullptr;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2020-01-27 22:47:14 +00:00
|
|
|
return p->devArray[devIdx].iPortArray[portIdx].nameStr;
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 22:47:14 +00:00
|
|
|
if( portIdx >= p->devArray[devIdx].oPortCnt )
|
2019-12-24 15:05:24 +00:00
|
|
|
return 0;
|
|
|
|
|
2020-01-27 22:47:14 +00:00
|
|
|
return p->devArray[devIdx].oPortArray[portIdx].nameStr;
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
unsigned cw::midi::device::alsa::portNameToIndex( handle_t h, unsigned devIdx, unsigned flags, const char* portName )
|
|
|
|
{
|
|
|
|
alsa_device_t* p = _handleToPtr(h);
|
|
|
|
|
|
|
|
if( p==nullptr || devIdx>=p->devCnt )
|
|
|
|
return kInvalidIdx;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
if( cwIsFlag(flags,kInMpFl) )
|
|
|
|
{
|
|
|
|
for(unsigned i=0; i<p->devArray[devIdx].iPortCnt; ++i)
|
|
|
|
if( textIsEqual(p->devArray[devIdx].iPortArray[i].nameStr,portName) )
|
|
|
|
return i;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(unsigned i=0; i<p->devArray[devIdx].oPortCnt; ++i)
|
|
|
|
if( textIsEqual(p->devArray[devIdx].oPortArray[i].nameStr,portName) )
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return kInvalidIdx;
|
|
|
|
}
|
|
|
|
|
|
|
|
cw::rc_t cw::midi::device::alsa::portEnable( handle_t h, unsigned devIdx, unsigned flags, unsigned portIdx, bool enableFl )
|
|
|
|
{
|
|
|
|
return cwLogError(kNotImplementedRC,"Port enable/disable has not been implemneted on ALSA MIDI ports.");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cw::rc_t cw::midi::device::alsa::send( handle_t h, unsigned devIdx, unsigned portIdx, uint8_t status, uint8_t d0, uint8_t d1 )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2020-01-27 22:47:14 +00:00
|
|
|
rc_t rc = kOkRC;
|
2019-12-24 15:05:24 +00:00
|
|
|
snd_seq_event_t ev;
|
2020-01-27 22:47:14 +00:00
|
|
|
int arc;
|
2024-04-06 20:02:52 +00:00
|
|
|
alsa_device_t* p = _handleToPtr(h);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
assert( p!=NULL && devIdx < p->devCnt && portIdx < p->devArray[devIdx].oPortCnt );
|
|
|
|
|
|
|
|
port_t* port = p->devArray[devIdx].oPortArray + portIdx;
|
|
|
|
|
|
|
|
snd_seq_ev_clear(&ev);
|
|
|
|
snd_seq_ev_set_source(&ev, p->alsa_addr.port);
|
|
|
|
//snd_seq_ev_set_subs(&ev);
|
|
|
|
|
|
|
|
snd_seq_ev_set_dest(&ev, port->alsa_addr.client, port->alsa_addr.port);
|
|
|
|
snd_seq_ev_set_direct(&ev);
|
|
|
|
snd_seq_ev_set_fixed(&ev);
|
|
|
|
|
2024-04-06 20:02:52 +00:00
|
|
|
uint8_t status_wo_ch = status;
|
|
|
|
if( midi::isChStatus(status) )
|
|
|
|
status_wo_ch = status & 0xf0;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2024-04-06 20:02:52 +00:00
|
|
|
switch( status_wo_ch )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
|
|
|
case kNoteOffMdId:
|
|
|
|
ev.type = SND_SEQ_EVENT_NOTEOFF;
|
|
|
|
ev.data.note.note = d0;
|
|
|
|
ev.data.note.velocity = d1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kNoteOnMdId:
|
|
|
|
ev.type = SND_SEQ_EVENT_NOTEON;
|
|
|
|
ev.data.note.note = d0;
|
|
|
|
ev.data.note.velocity = d1;
|
2024-02-10 16:34:06 +00:00
|
|
|
|
|
|
|
if( p->latency_meas_enable_out_fl )
|
|
|
|
{
|
|
|
|
p->latency_meas_enable_out_fl = false;
|
|
|
|
time::get(p->latency_meas_result.note_on_output_ts);
|
|
|
|
}
|
2019-12-24 15:05:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case kPolyPresMdId:
|
|
|
|
ev.type = SND_SEQ_EVENT_KEYPRESS ;
|
|
|
|
ev.data.note.note = d0;
|
|
|
|
ev.data.note.velocity = d1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kCtlMdId:
|
|
|
|
ev.type = SND_SEQ_EVENT_CONTROLLER;
|
|
|
|
ev.data.control.param = d0;
|
|
|
|
ev.data.control.value = d1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kPgmMdId:
|
|
|
|
ev.type = SND_SEQ_EVENT_PGMCHANGE;
|
|
|
|
ev.data.control.param = d0;
|
|
|
|
ev.data.control.value = d1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kChPresMdId:
|
|
|
|
ev.type = SND_SEQ_EVENT_CHANPRESS;
|
|
|
|
ev.data.control.param = d0;
|
|
|
|
ev.data.control.value = d1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kPbendMdId:
|
|
|
|
{
|
|
|
|
int val = d0;
|
|
|
|
val <<= 7;
|
|
|
|
val += d1;
|
|
|
|
val -= 8192;
|
|
|
|
|
|
|
|
ev.type = SND_SEQ_EVENT_PITCHBEND;
|
|
|
|
ev.data.control.param = 0;
|
|
|
|
ev.data.control.value = val;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2024-04-06 20:02:52 +00:00
|
|
|
case kSysRtClockMdId:
|
|
|
|
ev.type = SND_SEQ_EVENT_CLOCK;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kSysRtStartMdId:
|
|
|
|
ev.type =SND_SEQ_EVENT_START;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kSysRtContMdId:
|
|
|
|
ev.type = SND_SEQ_EVENT_CONTINUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kSysRtStopMdId:
|
|
|
|
ev.type = SND_SEQ_EVENT_STOP;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kSysRtSenseMdId:
|
|
|
|
ev.type = SND_SEQ_EVENT_SENSING;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kSysRtResetMdId:
|
|
|
|
ev.type = SND_SEQ_EVENT_RESET;
|
|
|
|
break;
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
default:
|
2024-04-06 20:02:52 +00:00
|
|
|
rc = _cmMpErrMsg1(kInvalidArgRC,0,"Cannot send an invalid MIDI status byte:0x%x.",status);
|
2019-12-24 15:05:24 +00:00
|
|
|
goto errLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
ev.data.note.channel = status & 0x0f;
|
|
|
|
|
|
|
|
if((arc = snd_seq_event_output(p->h, &ev)) < 0 )
|
|
|
|
rc = _cmMpErrMsg(kOpFailRC,arc,"MIDI event output failed.");
|
|
|
|
|
|
|
|
if((arc = snd_seq_drain_output(p->h)) < 0 )
|
|
|
|
rc = _cmMpErrMsg(kOpFailRC,arc,"MIDI event output drain failed.");
|
|
|
|
|
|
|
|
errLabel:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
cw::rc_t cw::midi::device::alsa::sendData( handle_t h, unsigned devIdx, unsigned portIdx, const uint8_t* dataPtr, unsigned byteCnt )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
|
|
|
return cwLogError(kInvalidOpRC,"cmMpDeviceSendData() has not yet been implemented for ALSA.");
|
|
|
|
}
|
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
void cw::midi::device::alsa::latency_measure_reset(handle_t h)
|
2024-02-10 16:34:06 +00:00
|
|
|
{
|
2024-02-14 16:20:40 +00:00
|
|
|
alsa_device_t* p = _handleToPtr(h);
|
2024-02-10 16:34:06 +00:00
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
p->latency_meas_result.note_on_input_ts = {};
|
|
|
|
p->latency_meas_result.note_on_output_ts = {};
|
2024-02-10 16:34:06 +00:00
|
|
|
p->latency_meas_enable_in_fl = true;
|
|
|
|
p->latency_meas_enable_out_fl = true;
|
|
|
|
}
|
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
cw::midi::device::latency_meas_result_t cw::midi::device::alsa::latency_measure_result(handle_t h)
|
2024-02-10 16:34:06 +00:00
|
|
|
{
|
2024-02-14 16:20:40 +00:00
|
|
|
alsa_device_t* p = _handleToPtr(h);
|
2024-02-10 16:34:06 +00:00
|
|
|
return p->latency_meas_result;
|
|
|
|
}
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
void cw::midi::device::alsa::report( handle_t h, textBuf::handle_t tbH )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2024-02-14 16:20:40 +00:00
|
|
|
alsa_device_t* p = _handleToPtr(h);
|
2019-12-24 15:05:24 +00:00
|
|
|
unsigned i,j;
|
|
|
|
|
2024-02-14 16:20:40 +00:00
|
|
|
textBuf::print( tbH,"ALSA Buffer size bytes in:%i out:%i\n",snd_seq_get_input_buffer_size(p->h),snd_seq_get_output_buffer_size(p->h));
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
for(i=0; i<p->devCnt; ++i)
|
|
|
|
{
|
|
|
|
const dev_t* d = p->devArray + i;
|
|
|
|
|
2022-05-06 20:04:42 +00:00
|
|
|
textBuf::print( tbH,"%i : Device: '%s' \n",i,cwStringNullGuard(d->nameStr));
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
if(d->iPortCnt > 0 )
|
|
|
|
textBuf::print( tbH," Input:\n");
|
|
|
|
|
|
|
|
for(j=0; j<d->iPortCnt; ++j)
|
|
|
|
_cmMpReportPort(tbH,d->iPortArray+j);
|
|
|
|
|
|
|
|
if(d->oPortCnt > 0 )
|
|
|
|
textBuf::print( tbH," Output:\n");
|
|
|
|
|
|
|
|
for(j=0; j<d->oPortCnt; ++j)
|
|
|
|
_cmMpReportPort(tbH,d->oPortArray+j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|