midi::device now allows a MIDI file to act as a virtual MIDI input device.

Thread polling moved out of midi::alsa and into midi::device.
Added latency measure feature to midi::device.
Improved midi::alsa device lookup time with alsa_device_t.clientIdToDevIdxMap[].
Added cwMidiAlsa.h to provide an interface for midi::alsa independent of midi::device.
This commit is contained in:
kevin 2024-02-14 11:20:40 -05:00
parent 742fc05e81
commit 8fe6f3627c
6 changed files with 2053 additions and 1491 deletions

View File

@ -3,10 +3,17 @@
#include "cwCommonImpl.h" #include "cwCommonImpl.h"
#include "cwMem.h" #include "cwMem.h"
#include "cwTime.h" #include "cwTime.h"
#include "cwMidi.h" #include "cwText.h"
#include "cwObject.h"
#include "cwTextBuf.h" #include "cwTextBuf.h"
#include "cwMidiPort.h" #include "cwMidi.h"
#include "cwThread.h" #include "cwMidiDecls.h"
#include "cwMidiDevice.h"
#include "cwMidiParser.h"
#include <poll.h>
#include "cwMidiAlsa.h"
#include <alsa/asoundlib.h> #include <alsa/asoundlib.h>
@ -16,6 +23,10 @@ namespace cw
{ {
namespace device namespace device
{ {
namespace alsa
{
const unsigned kCtoD_MapN = 256;
typedef struct typedef struct
{ {
bool inputFl; // true if this an input port bool inputFl; // true if this an input port
@ -38,7 +49,7 @@ namespace cw
} dev_t; } dev_t;
typedef struct device_str typedef struct alsa_device_str
{ {
unsigned devCnt; // MIDI devices attached to this computer unsigned devCnt; // MIDI devices attached to this computer
dev_t* devArray; dev_t* devArray;
@ -47,7 +58,6 @@ namespace cw
snd_seq_t* h; // ALSA system sequencer handle snd_seq_t* h; // ALSA system sequencer handle
snd_seq_addr_t alsa_addr; // ALSA client/port address representing the application snd_seq_addr_t alsa_addr; // ALSA client/port address representing the application
int alsa_queue; // ALSA device queue int alsa_queue; // ALSA device queue
thread::handle_t thH; // MIDI input listening thread
int alsa_fdCnt; // MIDI input driver file descriptor array int alsa_fdCnt; // MIDI input driver file descriptor array
struct pollfd* alsa_fd; struct pollfd* alsa_fd;
dev_t* prvRcvDev; // the last device and port to rcv MIDI dev_t* prvRcvDev; // the last device and port to rcv MIDI
@ -56,16 +66,18 @@ namespace cw
unsigned eventCnt; // count of recognized events unsigned eventCnt; // count of recognized events
time::spec_t baseTimeStamp; time::spec_t baseTimeStamp;
unsigned clientIdToDevIdxMap[ kCtoD_MapN ];
bool latency_meas_enable_in_fl; bool latency_meas_enable_in_fl;
bool latency_meas_enable_out_fl; bool latency_meas_enable_out_fl;
latency_meas_result_t latency_meas_result; latency_meas_result_t latency_meas_result;
} device_t; } alsa_device_t;
#define _cmMpErrMsg( rc, alsaRc, str ) cwLogError(kOpFailRC,"%s : ALSA Error:%i %s",(str),(alsaRc),snd_strerror(alsaRc)) #define _cmMpErrMsg( rc, alsaRc, str ) cwLogError(kOpFailRC,"%s : ALSA Error:%i %s",(str),(alsaRc),snd_strerror(alsaRc))
#define _cmMpErrMsg1( rc, alsaRc, fmt, arg ) cwLogError(kOpFailRC, fmt"%s : ALSA Error:%i %s",(arg),(alsaRc),snd_strerror(alsaRc)) #define _cmMpErrMsg1( rc, alsaRc, fmt, arg ) cwLogError(kOpFailRC, fmt"%s : ALSA Error:%i %s",(arg),(alsaRc),snd_strerror(alsaRc))
device_t* _handleToPtr( handle_t h ){ return handleToPtr<handle_t,device_t>(h); } alsa_device_t* _handleToPtr( handle_t h ){ return handleToPtr<handle_t,alsa_device_t>(h); }
unsigned _cmMpGetPortCnt( snd_seq_t* h, snd_seq_port_info_t* pip, bool inputFl ) unsigned _cmMpGetPortCnt( snd_seq_t* h, snd_seq_port_info_t* pip, bool inputFl )
@ -81,12 +93,12 @@ namespace cw
return i; return i;
} }
dev_t* _cmMpClientIdToDev( device_t* p, int clientId ) dev_t* _cmMpClientIdToDev( alsa_device_t* p, unsigned char clientId )
{ {
unsigned i; unsigned devIdx = p->clientIdToDevIdxMap[ clientId ];
for(i=0; i<p->devCnt; ++i)
if( p->devArray[i].clientId == clientId ) if( devIdx != kInvalidIdx )
return p->devArray + i; return p->devArray + devIdx;
return NULL; return NULL;
} }
@ -109,13 +121,12 @@ namespace cw
*d1 = v & 0x7f; *d1 = v & 0x7f;
} }
rc_t _handle_alsa_device( device_t* p ) rc_t _handle_input_msg( alsa_device_t* p )
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;
snd_seq_event_t *ev; snd_seq_event_t *ev;
do do
{ {
int alsa_rc = snd_seq_event_input(p->h,&ev); int alsa_rc = snd_seq_event_input(p->h,&ev);
@ -134,8 +145,6 @@ namespace cw
break; break;
} }
// TODO: Improve performance of _cmMpClientIdToDev() and _cmMpInPortIdToPort().
// They currently use linear searchs!
// get the device this event arrived from // get the device this event arrived from
if( p->prvRcvDev==NULL || p->prvRcvDev->clientId != ev->source.client ) if( p->prvRcvDev==NULL || p->prvRcvDev->clientId != ev->source.client )
@ -256,6 +265,7 @@ namespace cw
if( status != 0 ) if( status != 0 )
{ {
uint8_t ch = ev->data.note.channel; uint8_t ch = ev->data.note.channel;
/*
time::spec_t ts; time::spec_t ts;
ts.tv_sec = p->baseTimeStamp.tv_sec + ev->time.time.tv_sec; ts.tv_sec = p->baseTimeStamp.tv_sec + ev->time.time.tv_sec;
ts.tv_nsec = p->baseTimeStamp.tv_nsec + ev->time.time.tv_nsec; ts.tv_nsec = p->baseTimeStamp.tv_nsec + ev->time.time.tv_nsec;
@ -267,6 +277,11 @@ namespace cw
//printf("MIDI: %ld %ld : 0x%x %i %i\n",ts.tv_sec,ts.tv_nsec,status,d0,d1); //printf("MIDI: %ld %ld : 0x%x %i %i\n",ts.tv_sec,ts.tv_nsec,status,d0,d1);
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;
parser::midiTriple(p->prvRcvPort->parserH, &ts, status | ch, d0, d1 ); parser::midiTriple(p->prvRcvPort->parserH, &ts, status | ch, d0, d1 );
p->prvTimeMicroSecs = microSecs1; p->prvTimeMicroSecs = microSecs1;
@ -280,28 +295,7 @@ namespace cw
return rc; return rc;
} }
rc_t _cmMpPoll(device_t* p) rc_t _cmMpAllocStruct( alsa_device_t* p, const char* appNameStr, cbFunc_t cbFunc, void* cbDataPtr, unsigned parserBufByteCnt )
{
rc_t rc = kOkRC;
int timeOutMs = 50;
if (poll(p->alsa_fd, p->alsa_fdCnt, timeOutMs) > 0)
{
rc = _handle_alsa_device(p);
}
return rc;
}
bool _threadCbFunc(void* arg)
{
device_t* p = static_cast<device_t*>(arg);
_cmMpPoll(p);
return true;
}
rc_t _cmMpAllocStruct( device_t* p, const char* appNameStr, cbFunc_t cbFunc, void* cbDataPtr, unsigned parserBufByteCnt )
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;
snd_seq_client_info_t* cip = NULL; snd_seq_client_info_t* cip = NULL;
@ -309,6 +303,9 @@ namespace cw
snd_seq_port_subscribe_t *subs = NULL; snd_seq_port_subscribe_t *subs = NULL;
unsigned i,j,k,arc; unsigned i,j,k,arc;
for(i=0; i<kCtoD_MapN; ++i)
p->clientIdToDevIdxMap[i] = kInvalidIdx;
// alloc the subscription recd on the stack // alloc the subscription recd on the stack
snd_seq_port_subscribe_alloca(&subs); snd_seq_port_subscribe_alloca(&subs);
@ -391,6 +388,8 @@ namespace cw
p->devArray[i].oPortArray = NULL; p->devArray[i].oPortArray = NULL;
p->devArray[i].clientId = client; 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_client(pip,client);
snd_seq_port_info_set_port(pip,-1); snd_seq_port_info_set_port(pip,-1);
@ -513,7 +512,7 @@ namespace cw
} }
rc_t _destroy( device_t* p ) rc_t _destroy( alsa_device_t* p )
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;
@ -521,12 +520,6 @@ namespace cw
{ {
int arc; int arc;
// stop the thread first
if((rc = thread::destroy(p->thH)) != kOkRC )
{
rc = _cmMpErrMsg(rc,0,"Thread destroy failed.");
goto errLabel;
}
// stop the queue // stop the queue
if( p->h != NULL ) if( p->h != NULL )
@ -584,14 +577,13 @@ namespace cw
errLabel: errLabel:
return rc; return rc;
} }
} // alsa
} // device } // device
} // midi } // midi
} // cw } // cw
cw::rc_t cw::midi::device::create( handle_t& h, cbFunc_t cbFunc, void* cbArg, unsigned parserBufByteCnt, const char* appNameStr ) cw::rc_t cw::midi::device::alsa::create( handle_t& h, cbFunc_t cbFunc, void* cbArg, unsigned parserBufByteCnt, const char* appNameStr )
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;
int arc = 0; int arc = 0;
@ -599,18 +591,11 @@ cw::rc_t cw::midi::device::create( handle_t& h, cbFunc_t cbFunc, void* cbArg, u
if((rc = destroy(h)) != kOkRC ) if((rc = destroy(h)) != kOkRC )
return rc; return rc;
device_t* p = mem::allocZ<device_t>(1); alsa_device_t* p = mem::allocZ<alsa_device_t>(1);
p->h = NULL; p->h = NULL;
p->alsa_queue = -1; p->alsa_queue = -1;
// create the listening thread
if((rc = thread::create( p->thH, _threadCbFunc, p)) != kOkRC )
{
rc = _cmMpErrMsg(rc,0,"Thread initialization failed.");
goto errLabel;
}
// initialize the ALSA sequencer // initialize the ALSA sequencer
if((arc = snd_seq_open(&p->h, "default", SND_SEQ_OPEN_DUPLEX, SND_SEQ_NONBLOCK )) < 0 ) if((arc = snd_seq_open(&p->h, "default", SND_SEQ_OPEN_DUPLEX, SND_SEQ_NONBLOCK )) < 0 )
{ {
@ -646,9 +631,6 @@ cw::rc_t cw::midi::device::create( handle_t& h, cbFunc_t cbFunc, void* cbArg, u
// all time stamps will be an offset from this time stamp // all time stamps will be an offset from this time stamp
clock_gettime(CLOCK_MONOTONIC,&p->baseTimeStamp); clock_gettime(CLOCK_MONOTONIC,&p->baseTimeStamp);
if((rc = thread::unpause(p->thH)) != kOkRC )
rc = _cmMpErrMsg(rc,0,"Thread start failed.");
h.set(p); h.set(p);
errLabel: errLabel:
@ -661,14 +643,14 @@ cw::rc_t cw::midi::device::create( handle_t& h, cbFunc_t cbFunc, void* cbArg, u
} }
cw::rc_t cw::midi::device::destroy( handle_t& h ) cw::rc_t cw::midi::device::alsa::destroy( handle_t& h )
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;
if( !h.isValid() ) if( !h.isValid() )
return rc; return rc;
device_t* p = _handleToPtr(h); alsa_device_t* p = _handleToPtr(h);
if((rc = _destroy(p)) != kOkRC ) if((rc = _destroy(p)) != kOkRC )
return rc; return rc;
@ -678,18 +660,32 @@ cw::rc_t cw::midi::device::destroy( handle_t& h )
return rc; return rc;
} }
bool cw::midi::device::isInitialized(handle_t h) bool cw::midi::device::alsa::isInitialized(handle_t h)
{ return h.isValid(); } { return h.isValid(); }
unsigned cw::midi::device::count(handle_t h) unsigned cw::midi::device::alsa::count(handle_t h)
{ {
device_t* p = _handleToPtr(h); alsa_device_t* p = _handleToPtr(h);
return p->devCnt; return p->devCnt;
} }
const char* cw::midi::device::name( handle_t h, unsigned devIdx ) struct pollfd* cw::midi::device::alsa::pollFdArray( handle_t h, unsigned& arrayCntRef )
{ {
device_t* p = _handleToPtr(h); 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 )
{
alsa_device_t* p = _handleToPtr(h);
if( p==NULL || devIdx>=p->devCnt) if( p==NULL || devIdx>=p->devCnt)
return NULL; return NULL;
@ -697,9 +693,20 @@ const char* cw::midi::device::name( handle_t h, unsigned devIdx )
return p->devArray[devIdx].nameStr; return p->devArray[devIdx].nameStr;
} }
unsigned cw::midi::device::portCount( handle_t h, unsigned devIdx, unsigned flags ) unsigned cw::midi::device::alsa::nameToIndex(handle_t h, const char* deviceName)
{ {
device_t* p = _handleToPtr(h); 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);
if( p==NULL || devIdx>=p->devCnt) if( p==NULL || devIdx>=p->devCnt)
return 0; return 0;
@ -710,9 +717,9 @@ unsigned cw::midi::device::portCount( handle_t h, unsigned devIdx, unsigned
return p->devArray[devIdx].oPortCnt; return p->devArray[devIdx].oPortCnt;
} }
const char* cw::midi::device::portName( handle_t h, unsigned devIdx, unsigned flags, unsigned portIdx ) const char* cw::midi::device::alsa::portName( handle_t h, unsigned devIdx, unsigned flags, unsigned portIdx )
{ {
device_t* p = _handleToPtr(h); alsa_device_t* p = _handleToPtr(h);
if( p==NULL || devIdx>=p->devCnt) if( p==NULL || devIdx>=p->devCnt)
return 0; return 0;
@ -731,13 +738,42 @@ const char* cw::midi::device::portName( handle_t h, unsigned devIdx, unsign
return p->devArray[devIdx].oPortArray[portIdx].nameStr; return p->devArray[devIdx].oPortArray[portIdx].nameStr;
} }
unsigned cw::midi::device::alsa::portNameToIndex( handle_t h, unsigned devIdx, unsigned flags, const char* portName )
{
alsa_device_t* p = _handleToPtr(h);
cw::rc_t cw::midi::device::send( handle_t h, unsigned devIdx, unsigned portIdx, uint8_t status, uint8_t d0, uint8_t d1 ) if( p==nullptr || devIdx>=p->devCnt )
return kInvalidIdx;
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 )
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;
snd_seq_event_t ev; snd_seq_event_t ev;
int arc; int arc;
device_t* p = _handleToPtr(h); alsa_device_t* p = _handleToPtr(h);
assert( p!=NULL && devIdx < p->devCnt && portIdx < p->devArray[devIdx].oPortCnt ); assert( p!=NULL && devIdx < p->devCnt && portIdx < p->devArray[devIdx].oPortCnt );
@ -826,110 +862,34 @@ cw::rc_t cw::midi::device::send( handle_t h, unsigned devIdx, unsigned portIdx,
return rc; return rc;
} }
cw::rc_t cw::midi::device::sendData( handle_t h, unsigned devIdx, unsigned portIdx, const uint8_t* dataPtr, unsigned byteCnt ) cw::rc_t cw::midi::device::alsa::sendData( handle_t h, unsigned devIdx, unsigned portIdx, const uint8_t* dataPtr, unsigned byteCnt )
{ {
return cwLogError(kInvalidOpRC,"cmMpDeviceSendData() has not yet been implemented for ALSA."); return cwLogError(kInvalidOpRC,"cmMpDeviceSendData() has not yet been implemented for ALSA.");
} }
cw::rc_t cw::midi::device::installCallback( handle_t h, unsigned devIdx, unsigned portIdx, cbFunc_t cbFunc, void* cbDataPtr ) void cw::midi::device::alsa::latency_measure_reset(handle_t h)
{ {
rc_t rc = kOkRC; alsa_device_t* p = _handleToPtr(h);
unsigned di;
unsigned dn = count(h);
device_t* p = _handleToPtr(h);
for(di=0; di<dn; ++di) p->latency_meas_result.note_on_input_ts = {};
if( di==devIdx || devIdx == kInvalidIdx ) p->latency_meas_result.note_on_output_ts = {};
{
unsigned pi;
unsigned pn = portCount(h,di,kInMpFl);
for(pi=0; pi<pn; ++pi)
if( pi==portIdx || portIdx == kInvalidIdx )
if( parser::installCallback( p->devArray[di].iPortArray[pi].parserH, cbFunc, cbDataPtr ) != kOkRC )
goto errLabel;
}
errLabel:
return rc;
}
cw::rc_t cw::midi::device::removeCallback( handle_t h, unsigned devIdx, unsigned portIdx, cbFunc_t cbFunc, void* cbDataPtr )
{
rc_t rc = kOkRC;
unsigned di;
unsigned dn = count(h);
unsigned remCnt = 0;
device_t* p = _handleToPtr(h);
for(di=0; di<dn; ++di)
if( di==devIdx || devIdx == kInvalidIdx )
{
unsigned pi;
unsigned pn = portCount(h,di,kInMpFl);
for(pi=0; pi<pn; ++pi)
if( pi==portIdx || portIdx == kInvalidIdx )
if( parser::hasCallback(p->devArray[di].iPortArray[pi].parserH, cbFunc, cbDataPtr ) )
{
if( parser::removeCallback( p->devArray[di].iPortArray[pi].parserH, cbFunc, cbDataPtr ) != kOkRC )
goto errLabel;
else
++remCnt;
}
}
if( remCnt == 0 && dn > 0 )
rc = _cmMpErrMsg(kInvalidArgRC,0,"The callback was not found on any of the specified devices or ports.");
errLabel:
return rc;
}
bool cw::midi::device::usesCallback( handle_t h, unsigned devIdx, unsigned portIdx, cbFunc_t cbFunc, void* cbDataPtr )
{
unsigned di;
unsigned dn = count(h);
device_t* p = _handleToPtr(h);
for(di=0; di<dn; ++di)
if( di==devIdx || devIdx == kInvalidIdx )
{
unsigned pi;
unsigned pn = portCount(h,di,kInMpFl);
for(pi=0; pi<pn; ++pi)
if( pi==portIdx || portIdx == kInvalidIdx )
if( parser::hasCallback( p->devArray[di].iPortArray[pi].parserH, cbFunc, cbDataPtr ) )
return true;
}
return false;
}
void cw::midi::device::latency_measure_setup(handle_t h)
{
device_t* p = _handleToPtr(h);
p->latency_meas_result.note_on_input_ts = {0};
p->latency_meas_result.note_on_output_ts = {0};
p->latency_meas_enable_in_fl = true; p->latency_meas_enable_in_fl = true;
p->latency_meas_enable_out_fl = true; p->latency_meas_enable_out_fl = true;
} }
cw::midi::device::latency_meas_result_t cw::midi::device::latency_measure_result(handle_t h) cw::midi::device::latency_meas_result_t cw::midi::device::alsa::latency_measure_result(handle_t h)
{ {
device_t* p = _handleToPtr(h); alsa_device_t* p = _handleToPtr(h);
return p->latency_meas_result; return p->latency_meas_result;
} }
void cw::midi::device::report( handle_t h, textBuf::handle_t tbH ) void cw::midi::device::alsa::report( handle_t h, textBuf::handle_t tbH )
{ {
device_t* p = _handleToPtr(h); alsa_device_t* p = _handleToPtr(h);
unsigned i,j; unsigned i,j;
textBuf::print( tbH,"Buffer size bytes in:%i out:%i\n",snd_seq_get_input_buffer_size(p->h),snd_seq_get_output_buffer_size(p->h)); 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));
for(i=0; i<p->devCnt; ++i) for(i=0; i<p->devCnt; ++i)
{ {

44
cwMidiAlsa.h Normal file
View File

@ -0,0 +1,44 @@
namespace cw
{
namespace midi
{
namespace device
{
namespace alsa
{
typedef handle< struct alsa_device_str> handle_t;
rc_t create( handle_t& h, cbFunc_t cbFunc, void* cbDataPtr, unsigned parserBufByteCnt, const char* appNameStr );
rc_t destroy( handle_t& h);
bool isInitialized( handle_t h );
struct pollfd* pollFdArray( handle_t h, unsigned& arrayCntRef );
rc_t handleInputMsg( handle_t h );
unsigned count( handle_t h );
const char* name( handle_t h, unsigned devIdx );
unsigned nameToIndex(handle_t h, const char* deviceName);
unsigned portCount( handle_t h, unsigned devIdx, unsigned flags );
const char* portName( handle_t h, unsigned devIdx, unsigned flags, unsigned portIdx );
unsigned portNameToIndex( handle_t h, unsigned devIdx, unsigned flags, const char* portName );
rc_t portEnable( handle_t h, unsigned devIdx, unsigned flags, unsigned portIdx, bool enableFl );
rc_t send( handle_t h, unsigned devIdx, unsigned portIdx, uint8_t st, uint8_t d0, uint8_t d1 );
rc_t sendData( handle_t h, unsigned devIdx, unsigned portIdx, const uint8_t* dataPtr, unsigned byteCnt );
// Latency measurment:
// Record the time of the next incoming note-on msg
// and the time of the next outgoing note-on msg
// Reset the latency measurement process.
void latency_measure_reset(handle_t h);
latency_meas_result_t latency_measure_result(handle_t h);
void report( handle_t h, textBuf::handle_t tbH);
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,15 +1,12 @@
#ifndef cwMidiPort_H #ifndef cwMidiPort_H
#define cwMidiPort_H #define cwMidiPort_H
#include "cwMidiDecls.h"
namespace cw namespace cw
{ {
namespace midi namespace midi
{ {
//( { file_desc:"Device independent MIDI port related code." kw:[midi]}
// Flags used to identify input and output ports on MIDI devices // Flags used to identify input and output ports on MIDI devices
enum enum
{ {
@ -17,60 +14,29 @@ namespace cw
kOutMpFl = 0x02 kOutMpFl = 0x02
}; };
typedef void (*cbFunc_t)( const packet_t* pktArray, unsigned pktCnt );
//)
//( { label:parser file_desc:"MIDI event parser converts raw MIDI events into packet_t messages." kw:[midi]}
//===============================================================================================
// MIDI Parser
//
namespace parser
{
typedef handle<struct parser_str> handle_t;
// 'cbFunc' and 'cbDataPtr' are optional. If 'cbFunc' is not supplied in the call to
// create() it may be supplied later by installCallback().
// 'bufByteCnt' defines is the largest complete system-exclusive message the parser will
// by able to transmit. System-exclusive messages larger than this will be broken into
// multiple sequential callbacks.
rc_t create( handle_t& hRef, unsigned devIdx, unsigned portIdx, cbFunc_t cbFunc, void* cbArg, unsigned bufByteCnt );
rc_t destroy( handle_t& hRef );
unsigned errorCount( handle_t h );
void parseMidiData( handle_t h, const time::spec_t* timestamp, const uint8_t* buf, unsigned bufByteCnt );
// The following two functions are intended to be used togetther.
// Use midiTriple() to insert pre-parsed msg's to the output buffer,
// and then use transmit() to send the buffer via the parsers callback function.
// Set the data bytes to 0xff if they are not used by the message.
rc_t midiTriple( handle_t h, const time::spec_t* timestamp, uint8_t status, uint8_t d0, uint8_t d1 );
rc_t transmit( handle_t h );
// Install/Remove additional callbacks.
rc_t installCallback( handle_t h, cbFunc_t cbFunc, void* cbDataPtr );
rc_t removeCallback( handle_t h, cbFunc_t cbFunc, void* cbDataPtr );
// Returns true if the parser uses the given callback.
bool hasCallback( handle_t h, cbFunc_t cbFunc, void* cbDataPtr );
}
//)
//( { label:cmMidiPort file_desc:"Device independent MIDI port." kw:[midi]}
//===============================================================================================
// MIDI Device Interface
//
namespace device namespace device
{ {
typedef handle< struct device_str> handle_t; typedef handle< struct device_str> handle_t;
// 'cbFunc' and 'cbDataPtr' are optional (they may be set to NULL). In this case
// 'cbFunc' and 'cbDataPtr' may be set in a later call to cmMpInstallCallback().
rc_t create( handle_t& h, cbFunc_t cbFunc, void* cbDataPtr, unsigned parserBufByteCnt, const char* appNameStr ); rc_t create( handle_t& h,
cbFunc_t cbFunc,
void* cbArg,
const char* filePortLabelA[], // filePortLabelA[ maxFileCnt ]
unsigned maxFileCnt, // count of file dev ports
const char* appNameStr,
const char* fileDevName = "file_dev",
unsigned fileDevReadAheadMicros = 3000,
unsigned parserBufByteCnt = 1024 );
rc_t create( handle_t& h,
cbFunc_t cbFunc,
void* cbArg,
const object_t* args );
rc_t destroy( handle_t& h); rc_t destroy( handle_t& h);
bool isInitialized( handle_t h ); bool isInitialized( handle_t h );
@ -80,15 +46,19 @@ namespace cw
unsigned portCount( handle_t h, unsigned devIdx, unsigned flags ); unsigned portCount( handle_t h, unsigned devIdx, unsigned flags );
const char* portName( handle_t h, unsigned devIdx, unsigned flags, unsigned portIdx ); const char* portName( handle_t h, unsigned devIdx, unsigned flags, unsigned portIdx );
unsigned portNameToIndex( handle_t h, unsigned devIdx, unsigned flags, const char* portName ); unsigned portNameToIndex( handle_t h, unsigned devIdx, unsigned flags, const char* portName );
rc_t portEnable( handle_t h, unsigned devIdx, unsigned flags, unsigned portIdx, bool enableFl );
rc_t send( handle_t h, unsigned devIdx, unsigned portIdx, uint8_t st, uint8_t d0, uint8_t d1 ); rc_t send( handle_t h, unsigned devIdx, unsigned portIdx, uint8_t st, uint8_t d0, uint8_t d1 );
rc_t sendData( handle_t h, unsigned devIdx, unsigned portIdx, const uint8_t* dataPtr, unsigned byteCnt ); rc_t sendData( handle_t h, unsigned devIdx, unsigned portIdx, const uint8_t* dataPtr, unsigned byteCnt );
// Set devIdx to -1 to assign the callback to all devices. rc_t openMidiFile( handle_t h, unsigned devIdx, unsigned portIdx, const char* fname );
// Set portIdx to -1 to assign the callback to all ports on the specified devices. rc_t seekToMsg( handle_t h, unsigned devIdx, unsigned portIdx, unsigned msgIdx );
// rc_t setEndMsg( handle_t h, unsigned devIdx, unsigned portidx, unsigned msgIdx );
rc_t installCallback( handle_t h, unsigned devIdx, unsigned portIdx, cbFunc_t cbFunc, void* cbDataPtr );
rc_t removeCallback( handle_t h, unsigned devIdx, unsigned portIdx, cbFunc_t cbFunc, void* cbDataPtr ); rc_t start( handle_t h );
bool usesCallback( handle_t h, unsigned devIdx, unsigned portIdx, cbFunc_t cbFunc, void* cbDataPtr ); rc_t stop( handle_t h );
rc_t pause( handle_t h, bool pause_fl );
typedef struct typedef struct
{ {
@ -96,13 +66,21 @@ namespace cw
time::spec_t note_on_output_ts; time::spec_t note_on_output_ts;
} latency_meas_result_t; } latency_meas_result_t;
// Reset the latency measurement process. typedef struct
void latency_measure_setup(handle_t h); {
latency_meas_result_t latency_measure_result(handle_t h); latency_meas_result_t alsa_dev;
latency_meas_result_t file_dev;
} latency_meas_combined_result_t;
// Reset the latency measurement process. Record the time of the first
// incoming note-on msg and the first outgoing note-on msg.
void latency_measure_reset(handle_t h);
latency_meas_combined_result_t latency_measure_result(handle_t h);
rc_t report( handle_t h );
void report( handle_t h, textBuf::handle_t tbH); void report( handle_t h, textBuf::handle_t tbH);
rc_t test(); rc_t testReport();
} }
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,38 +1,100 @@
namespace cw namespace cw
{ {
namespace midi namespace midi
{
namespace device
{ {
namespace file_dev namespace file_dev
{ {
typedef handle<struct file_dev_str> handle_t; typedef handle<struct file_dev_str> handle_t;
rc_t create( handle_t& hRef, const char* labelA[], unsigned max_file_cnt ); //
// Count of labels determines the count of ports.
// Note that port indexes and file indexes are synonomous.
rc_t create( handle_t& hRef,
cbFunc_t cbFunc,
void* cbArg,
unsigned baseDevIdx, // device index assigned to packet_t.devIndex in callbacks
const char* labelA[], // labelA[ max_file_cnt ] assigns a textual label to each port.
unsigned max_file_cnt,
const char* dev_name = "file_dev", // name of the file device
unsigned read_ahead_micros = 3000); // exec() will xmit events up to 'read_ahead_micros' after the current time
rc_t destroy( handle_t& hRef ); rc_t destroy( handle_t& hRef );
// Return true if at least one enabled file exists, otherwise return false.
bool is_active( handle_t h );
// Returns create(...,max_file_cnt...)
unsigned file_count( handle_t h ); unsigned file_count( handle_t h );
// Assign a MIDI file to an input port.
rc_t open_midi_file( handle_t h, unsigned file_idx, const char* fname ); rc_t open_midi_file( handle_t h, unsigned file_idx, const char* fname );
rc_t load_messages( handle_t h, unsigned file_idx, const msg_t* msgA, unsigned msgN );
rc_t seek_to_event( handle_t h, unsigned file_idx, unsigned msg_idx ); // Enable and disble the output of the specified file/port.
rc_t enable_file( handle_t h, unsigned file_idx, bool enableFl );
rc_t start( handle_t h );
rc_t stop( handle_t h );
rc_t enable_file( handle_t h, unsigned file_idx ); rc_t enable_file( handle_t h, unsigned file_idx );
rc_t disable_file( handle_t h,unsigned file_idx ); rc_t disable_file( handle_t h,unsigned file_idx );
int file_descriptor( handle_t h );
typedef struct msg_str // Device count: Always 1.
unsigned count( handle_t h );
// Device name as set in create()
const char* name( handle_t h, unsigned devIdx );
// Returns 0 if deviceName == name() else kInvalidIdx
unsigned nameToIndex(handle_t h, const char* deviceName);
// The count of ports is determined by count of labels in create(...,labelA[],...).
unsigned portCount( handle_t h, unsigned devIdx, unsigned flags );
// Port name are provided by create(...,labelA[],...)
// Port indexes and file indexes are the same.
const char* portName( handle_t h, unsigned devIdx, unsigned flags, unsigned portIdx );
unsigned portNameToIndex( handle_t h, unsigned devIdx, unsigned flags, const char* portName );
rc_t portEnable( handle_t h, unsigned devIdx, unsigned flags, unsigned portIdx, bool enableFl );
typedef struct exec_result_str
{ {
const file::trackMsg_t* msg; unsigned next_msg_wait_micros; // microseconds before the next file msg must be transmitted
unsigned file_idx; unsigned xmit_cnt; // count of msg's sent to callback during this exec
} msg_t; bool eof_fl; // true if there are no more file msg's to transmit
rc_t read( handle_t h, msg_t* buf, unsigned buf_msg_cnt, unsigned& actual_msg_cnt_ref ); } exec_result_t;
// Set the next msg to be returned.
rc_t seek_to_msg_index( handle_t h, unsigned file_idx, unsigned msg_idx );
rc_t set_end_msg_index( handle_t h, unsigned file_idx, unsigned msg_idx );
// Seek to the start of the file or to the last msg_idx set by seek_to_event().
rc_t rewind( handle_t h );
// Delay the first MIDI msg by 'start_delay_micros'.
rc_t set_start_delay( handle_t h, unsigned start_delay_micros );
// Callback create(...,cbFunc,...) with msg's whose time has expired and return the
// time delay prior to the next message.
exec_result_t exec( handle_t h, unsigned long long elapsed_micros );
rc_t send( handle_t h, unsigned devIdx, unsigned portIdx, uint8_t st, uint8_t d0, uint8_t d1 );
rc_t sendData( handle_t h, unsigned devIdx, unsigned portIdx, const uint8_t* dataPtr, unsigned byteCnt );
// Reset the latency measurement process.
void latency_measure_reset(handle_t h);
latency_meas_result_t latency_measure_result(handle_t h);
void report( handle_t h, textBuf::handle_t tbH );
rc_t test( const object_t* cfg ); rc_t test( const object_t* cfg );
}
} }
} }
} }