cwMidiParser.h/cpp : Initial commit after being copied out of cwMidiDevice.h/cpp.

cwMidiDeviceTest.h/cpp : Initial commit.
Makefile.am : New files and name changes.
This commit is contained in:
kevin 2024-02-14 11:28:54 -05:00
parent 8fe6f3627c
commit 864142b9f0
5 changed files with 912 additions and 4 deletions

View File

@ -25,8 +25,8 @@ libcwSRC += src/libcw/cwThread.cpp src/libcw/cwMutex.cpp src/libcw/cwThreadMach
libcwHDR += src/libcw/cwMpScNbQueue.h src/libcw/cwSpScBuf.h src/libcw/cwSpScQueueTmpl.h
libcwSRC += src/libcw/cwSpScBuf.cpp src/libcw/cwSpScQueueTmpl.cpp
libcwHDR += src/libcw/cwAudioFile.h src/libcw/cwMidiFile.h src/libcw/cwMidiFileDev.h
libcwSRC += src/libcw/cwAudioFile.cpp src/libcw/cwMidiFile.cpp src/libcw/cwMidiFileDev.cpp
libcwHDR += src/libcw/cwAudioFile.h src/libcw/cwMidiFile.h
libcwSRC += src/libcw/cwAudioFile.cpp src/libcw/cwMidiFile.cpp
libcwHDR += src/libcw/cwAudioFileOps.h src/libcw/cwAudioTransforms.h src/libcw/cwDspTransforms.h src/libcw/cwAudioFileProc.h src/libcw/cwPvAudioFileProc.h
libcwSRC += src/libcw/cwAudioFileOps.cpp src/libcw/cwAudioTransforms.cpp src/libcw/cwDspTransforms.cpp src/libcw/cwAudioFileProc.cpp src/libcw/cwPvAudioFileProc.cpp
@ -57,8 +57,12 @@ libcwSRC += src/libcw/cwAudioDevice.cpp
if cwALSA
libcwHDR += src/libcw/cwMidiPort.h src/libcw/cwAudioDeviceAlsa.h src/libcw/cwAudioDeviceFile.h
libcwSRC += src/libcw/cwMidiPort.cpp src/libcw/cwMidiAlsa.cpp src/libcw/cwAudioDeviceAlsa.cpp src/libcw/cwAudioDeviceFile.cpp src/libcw/cwAudioDeviceTest.cpp
libcwHDR += src/libcw/cwMidiDevice.h src/libcw/cwMidiParser.h src/libcw/cwMidiAlsa.h src/libcw/cwMidiFileDev.h src/libcw/cwMidiDeviceTest.h
libcwSRC += src/libcw/cwMidiDevice.cpp src/libcw/cwMidiParser.cpp src/libcw/cwMidiAlsa.cpp src/libcw/cwMidiFileDev.cpp src/libcw/cwMidiDeviceTest.cpp
libcwHDR += src/libcw/cwAudioDeviceAlsa.h src/libcw/cwAudioDeviceFile.h
libcwSRC += src/libcw/cwAudioDeviceAlsa.cpp src/libcw/cwAudioDeviceFile.cpp src/libcw/cwAudioDeviceTest.cpp
if cwWEBSOCK
libcwHDR += src/libcw/cwIo.h src/libcw/cwIoTest.h src/libcw/cwIoMinTest.h src/libcw/cwIoSocketChat.h src/libcw/cwIoAudioPanel.h src/libcw/cwIoAudioMidi.h

389
cwMidiDeviceTest.cpp Normal file
View File

@ -0,0 +1,389 @@
#include "cwCommon.h"
#include "cwLog.h"
#include "cwCommonImpl.h"
#include "cwMem.h"
#include "cwTime.h"
#include "cwObject.h"
#include "cwText.h"
#include "cwTextBuf.h"
#include "cwThread.h"
#include "cwMidi.h"
#include "cwMidiDecls.h"
#include "cwMidiFile.h"
#include "cwMidiDevice.h"
#include "cwMidiDeviceTest.h"
namespace cw
{
namespace midi
{
namespace device
{
typedef struct test_msg_str
{
msg_t msg;
time::spec_t t;
} test_msg_t;
typedef struct test_str
{
test_msg_t* msgA;
unsigned msgN;
unsigned msg_idx;
unsigned file_dev_idx;
unsigned port_idx;
} test_t;
rc_t _test_create( test_t*& pRef )
{
rc_t rc = kOkRC;
test_t* p = nullptr;
p = mem::allocZ<test_t>();
p->msgN = 0;
p->msgA = nullptr;
p->file_dev_idx = kInvalidIdx;
p->port_idx = kInvalidIdx;
pRef = p;
return rc;
}
rc_t _test_open( test_t* p, unsigned fileDevIdx, unsigned portIdx, const char* fname )
{
rc_t rc = kOkRC;
if( p->file_dev_idx == kInvalidIdx )
{
file::handle_t mfH;
if((rc = open(mfH,fname)) != kOkRC )
goto errLabel;
p->msgN = msgCount(mfH);
p->msgA = mem::allocZ<test_msg_t>(p->msgN);
p->file_dev_idx = fileDevIdx;
p->port_idx = portIdx;
close(mfH);
}
errLabel:
return rc;
}
rc_t _test_destroy( test_t* p )
{
if( p != nullptr )
{
mem::release(p->msgA);
mem::release(p);
}
return kOkRC;
}
void _test_callback( const packet_t* pktArray, unsigned pktCnt )
{
unsigned i,j;
time::spec_t cur_time = time::current_time();
for(i=0; i<pktCnt; ++i)
{
const packet_t* p = pktArray + i;
test_t* t = (test_t*)p->cbArg;
for(j=0; j<p->msgCnt; ++j)
if( p->msgArray != NULL )
{
if( t->msg_idx < t->msgN && p->devIdx == t->file_dev_idx && p->portIdx == t->port_idx )
{
t->msgA[t->msg_idx].msg = p->msgArray[j];
t->msgA[t->msg_idx].t = cur_time;
t->msg_idx += 1;
}
if( isNoteOn(p->msgArray[j].status,p->msgArray[j].d1) )
printf("%ld %ld %i 0x%x %i %i\n", p->msgArray[j].timeStamp.tv_sec, p->msgArray[j].timeStamp.tv_nsec, p->msgArray[j].ch, p->msgArray[j].status,p->msgArray[j].d0, p->msgArray[j].d1);
}
else
{
printf("0x%x ",p->sysExMsg[j]);
}
}
}
bool _test_is_not_equal( const file::trackMsg_t* tmsg, const test_msg_t& m )
{ return tmsg->status != m.msg.status || tmsg->u.chMsgPtr->d0 != m.msg.d0 || tmsg->u.chMsgPtr->d1 != m.msg.d1; }
bool _test_is_equal( const file::trackMsg_t* tmsg, const test_msg_t& m )
{ return !_test_is_not_equal(tmsg,m); }
void _test_print( const file::trackMsg_t* tmsg, const test_msg_t& m )
{
const char* eql_mark = _test_is_equal(tmsg,m) ? "" : "*";
printf("%2i 0x%2x %3i %3i : %2i 0x%2x %3i %3i : %s\n",tmsg->u.chMsgPtr->ch, tmsg->status, tmsg->u.chMsgPtr->d0, tmsg->u.chMsgPtr->d1, m.msg.ch, m.msg.status, m.msg.d0, m.msg.d1, eql_mark);
}
void _test_print( unsigned long long t0, const file::trackMsg_t* tmsg, unsigned long long t1, const test_msg_t& m, unsigned dt )
{
const char* eql_mark = _test_is_equal(tmsg,m) ? "" : "*";
printf("%6llu %2i 0x%2x %3i %3i : %6llu %2i 0x%2x %3i %3i : %6i : %s\n",t0, tmsg->u.chMsgPtr->ch, tmsg->status, tmsg->u.chMsgPtr->d0, tmsg->u.chMsgPtr->d1, t1, m.msg.ch, m.msg.status, m.msg.d0, m.msg.d1, dt, eql_mark);
}
rc_t _test_analyze( test_t* p, const char* fname )
{
rc_t rc = kOkRC;
file::handle_t mfH;
const file::trackMsg_t** tmsgA;
unsigned tmsgN;
unsigned max_diff_micros = 0;
unsigned sum_micros = 0;
unsigned sum_cnt = 0;
unsigned i0 = kInvalidIdx;
unsigned j0 = kInvalidIdx;
// open the MIDI file under test
if((rc = open(mfH,fname)) != kOkRC )
goto errLabel;
tmsgA = msgArray(mfH);
tmsgN = msgCount(mfH);
printf("file:%i test:%i\n",tmsgN,p->msg_idx);
// for file trk msg and recorded msg
for(unsigned i=0,j=0; i<tmsgN && j<p->msg_idx; ++i)
{
// skip non-channel messages
if( isChStatus(tmsgA[i]->status))
{
unsigned long long d0 = 0;
unsigned long long d1 = 0;
unsigned dt = 0;
// if there is a previous file msg
if( i0 != kInvalidIdx )
{
// get the elapsed time between the cur and prev file msg
d0 = tmsgA[i]->amicro - tmsgA[i0]->amicro;
// if there is a previous recorded msg
if( j0 != kInvalidIdx )
{
// get the time elapsed between the cur and prev recorded msg
d1 = time::elapsedMicros(p->msgA[j0].t,p->msgA[j].t);
dt = (unsigned)(d0>d1 ? d0-d1 : d1-d0);
sum_micros += dt;
sum_cnt += 1;
if( dt > max_diff_micros )
max_diff_micros = dt;
}
}
_test_print(d0, tmsgA[i], d1, p->msgA[j], dt );
i0 = i;
j0 = j;
j += 1;
}
}
printf("max diff:%i avg diff:%i micros\n",max_diff_micros,sum_cnt==0 ? 0 : sum_micros/sum_cnt);
errLabel:
close(mfH);
return rc;
}
}
}
}
cw::rc_t cw::midi::device::test( const object_t* cfg )
{
rc_t rc = kOkRC;
const char* testMidiFname = nullptr;
const char* fileDevName = nullptr;
const char* testFileLabel = nullptr;
bool testFileEnableFl = false;
const object_t* file_ports = nullptr;
test_t* t = nullptr;
bool quit_fl = false;
char ch;
handle_t h;
if((rc = _test_create( t )) != kOkRC )
{
rc = cwLogError(rc,"Test create failed.");
goto errLabel;
}
if((rc = create(h,_test_callback,t,cfg)) != kOkRC )
{
rc = cwLogError(rc,"MIDI dev create failed.");
goto errLabel;
}
report(h);
if((rc = cfg->getv("fileDevName", fileDevName,
"testFileLabel", testFileLabel,
"testFileEnableFl", testFileEnableFl,
"file_ports", file_ports)) != kOkRC )
{
rc = cwLogError(rc,"Parse 'file_ports' failed.");
goto errLabel;
}
// for each file dev port
for(unsigned i=0; i<file_ports->child_count(); ++i)
{
const char* fname = nullptr;
const char* label = nullptr;
bool enable_fl = false;
unsigned fileDevIdx = kInvalidIdx;
unsigned portIdx = kInvalidIdx;
// parse the file/label pair
if((rc = file_ports->child_ele(i)->getv("file",fname,
"enable_fl", enable_fl,
"label",label)) != kOkRC )
{
rc = cwLogError(rc,"Parse failed on 'file_port' index %i.",i);
goto errLabel;
}
// get the file device name
if((fileDevIdx = nameToIndex(h,fileDevName)) == kInvalidIdx )
{
rc = cwLogError(kInvalidArgRC,"Unable to locate the MIDI file device '%s'.",cwStringNullGuard(fileDevName));
goto errLabel;
}
// get the file/label port index
if((portIdx = portNameToIndex(h,fileDevIdx,kInMpFl,label)) == kInvalidIdx )
{
rc = cwLogError(kInvalidArgRC,"Unable to locate the port '%s' on device '%s'.",cwStringNullGuard(label),cwStringNullGuard(fileDevName));
goto errLabel;
}
// open the MIDI file on this port
if((rc = openMidiFile(h,fileDevIdx,portIdx,fname)) != kOkRC )
{
rc = cwLogError(rc,"MIDI file open failed on '%s'.",fname);
goto errLabel;
}
if((rc = portEnable(h,fileDevIdx,kInMpFl,portIdx,enable_fl)) != kOkRC )
{
rc = cwLogError(rc,"MIDI file enable failed on '%s'.",fname);
goto errLabel;
}
// if this is the test port
if( testFileEnableFl && testFileLabel != nullptr && textIsEqual(label,testFileLabel) )
{
testMidiFname = fname;
cwLogInfo("Test label:%s device:%i fname:%s",testFileLabel,fileDevIdx,fname);
if((rc = _test_open(t,fileDevIdx,portIdx,fname)) != kOkRC )
{
rc = cwLogError(rc,"Test create failed.");
goto errLabel;
}
}
}
cwLogInfo("menu: (q)uit (b)egin (s)top (p)ause (u)npause (n)ote-on\n");
while( !quit_fl)
{
ch = getchar();
switch(ch)
{
case 'q':
quit_fl = true;
break;
case 'b':
printf("starting ...\n");
start(h);
break;
case 's':
printf("stopping ...\n");
stop(h);
break;
case 'p':
printf("pausing ...\n");
pause(h,true);
break;
case 'u':
printf("unpausing ...\n");
pause(h,false);
break;
case 'n':
printf("sending ...\n");
send(h,2,0,0x90,60,60);
break;
}
}
errLabel:
if( testMidiFname != nullptr )
_test_analyze(t,testMidiFname);
destroy(h);
_test_destroy(t);
return rc;
}
cw::rc_t cw::midi::device::testReport()
{
rc_t rc = kOkRC;
textBuf::handle_t tbH;
handle_t h;
if((rc = create(h,nullptr,nullptr,nullptr,0,"test_report")) != kOkRC )
return rc;
// create a text buffer to hold the MIDI system report text
if((rc = textBuf::create(tbH)) != kOkRC )
goto errLabel;
// generate and print the MIDI system report
report(h,tbH);
cwLogInfo("%s",textBuf::text(tbH));
errLabel:
textBuf::destroy(tbH);
destroy(h);
return rc;
}

10
cwMidiDeviceTest.h Normal file
View File

@ -0,0 +1,10 @@
namespace cw
{
namespace midi
{
namespace device
{
rc_t test( const object_t* cfg );
}
}
}

469
cwMidiParser.cpp Normal file
View File

@ -0,0 +1,469 @@
#include "cwCommon.h"
#include "cwLog.h"
#include "cwCommonImpl.h"
#include "cwMem.h"
#include "cwTime.h"
#include "cwMidi.h"
#include "cwMidiDecls.h"
#include "cwMidiParser.h"
namespace cw
{
namespace midi
{
namespace parser
{
enum
{
kBufByteCnt = 1024,
kExpectStatusStId=0, // 0
kExpectDataStId, // 1
kExpectStatusOrDataStId, // 2
kExpectEOXStId // 3
};
typedef struct cmMpParserCb_str
{
cbFunc_t cbFunc;
void* cbDataPtr;
struct cmMpParserCb_str* linkPtr;
} cbRecd_t;
typedef struct parser_str
{
cbRecd_t* cbChain;
packet_t pkt;
unsigned state; // parser state id
unsigned errCnt; // accumlated error count
uint8_t status; // running status
uint8_t data0; // data byte 0
unsigned dataCnt; // data byte cnt for current status
unsigned dataIdx; // index (0 or 1) of next data byte
uint8_t* buf; // output buffer
unsigned bufByteCnt; // output buffer byte cnt
unsigned bufIdx; // next output buffer index
unsigned msgCnt; // count of channel messages in the buffer
} parser_t;
parser_t* _handleToPtr( handle_t h )
{
return handleToPtr<handle_t,parser_t>(h);
}
void _cmMpParserCb( parser_t* p, packet_t* pkt, unsigned pktCnt )
{
cbRecd_t* c = p->cbChain;
for(; c!=NULL; c=c->linkPtr)
{
pkt->cbArg = c->cbDataPtr;
c->cbFunc( pkt, pktCnt );
}
}
void _cmMpTransmitChMsgs( parser_t* p )
{
if( p->msgCnt > 0 )
{
p->pkt.msgArray = (msg_t*)p->buf;
p->pkt.msgCnt = p->msgCnt;
p->pkt.sysExMsg = NULL;
//p->cbFunc( &p->pkt, 1 );
_cmMpParserCb(p,&p->pkt,1);
p->bufIdx = 0;
p->msgCnt = 0;
}
}
void _cmMpTransmitSysEx( parser_t* p )
{
p->pkt.msgArray = NULL;
p->pkt.sysExMsg = p->buf;
p->pkt.msgCnt = p->bufIdx;
_cmMpParserCb(p,&p->pkt,1);
p->bufIdx = 0;
}
void _cmMpParserStoreChMsg( parser_t* p, const time::spec_t* timeStamp, uint8_t d )
{
// if there is not enough room left in the buffer then transmit
// the current messages
if( p->bufByteCnt - p->bufIdx < sizeof(msg_t) )
_cmMpTransmitChMsgs(p);
assert( p->bufByteCnt - p->bufIdx >= sizeof(msg_t) );
// get a pointer to the next msg in the buffer
msg_t* msgPtr = (msg_t*)(p->buf + p->bufIdx);
// fill the buffer msg
msgPtr->timeStamp = *timeStamp;
msgPtr->status = p->status & 0xf0;
msgPtr->ch = p->status & 0x0f;
msgPtr->uid = kInvalidId;
switch( p->dataCnt )
{
case 0:
break;
case 1:
msgPtr->d0 = d;
msgPtr->d1 = 0;
break;
case 2:
msgPtr->d0 = p->data0;
msgPtr->d1 = d;
break;
default:
assert(0);
}
// update the msg count and next buffer
++p->msgCnt;
p->bufIdx += sizeof(msg_t);
}
void _report( parser_t* p )
{
cwLogInfo("state:%i st:0x%x d0:%i dcnt:%i didx:%i buf[%i]->%i msg:%i err:%i\n",p->state,p->status,p->data0,p->dataCnt,p->dataIdx,p->bufByteCnt,p->bufIdx,p->msgCnt,p->errCnt);
}
void _destroy( parser_t* p )
{
mem::release(p->buf);
cbRecd_t* c = p->cbChain;
while(c != NULL)
{
cbRecd_t* nc = c->linkPtr;
mem::release(c);
c = nc;
}
mem::release(p);
}
} // parser
} // midi
} // cw
cw::rc_t cw::midi::parser::create( handle_t& hRef, unsigned devIdx, unsigned portIdx, cbFunc_t cbFunc, void* cbDataPtr, unsigned bufByteCnt )
{
rc_t rc = kOkRC;
parser_t* p = mem::allocZ<parser_t>( 1 );
p->pkt.devIdx = devIdx;
p->pkt.portIdx = portIdx;
p->cbChain = NULL;
p->buf = mem::allocZ<uint8_t>( bufByteCnt );
p->bufByteCnt = bufByteCnt;
p->bufIdx = 0;
p->msgCnt = 0;
p->state = kExpectStatusStId;
p->dataIdx = kInvalidIdx;
p->dataCnt = kInvalidCnt;
p->status = kInvalidStatusMdId;
hRef.set(p);
if( cbFunc != NULL )
rc = installCallback(hRef, cbFunc, cbDataPtr );
if( rc != kOkRC )
{
_destroy(p);
hRef.clear();
}
return rc;
}
cw::rc_t cw::midi::parser::destroy( handle_t& hRef )
{
rc_t rc = kOkRC;
if( !hRef.isValid() )
return rc;
parser_t* p = _handleToPtr(hRef);
_destroy(p);
hRef.clear();
return rc;
}
unsigned cw::midi::parser::errorCount( handle_t h )
{
parser_t* p = _handleToPtr(h);
if( p == NULL )
return 0;
return p->errCnt;
}
void cw::midi::parser::parseMidiData( handle_t h, const time::spec_t* timeStamp, const uint8_t* iBuf, unsigned iByteCnt )
{
parser_t* p = _handleToPtr(h);
if( p == NULL )
return;
const uint8_t* ip = iBuf;
const uint8_t* ep = iBuf + iByteCnt;
for(; ip < ep; ++ip )
{
// if this byte is a status byte
if( isStatus(*ip) )
{
if( p->state != kExpectStatusStId && p->state != kExpectStatusOrDataStId )
++p->errCnt;
p->status = *ip;
p->dataCnt = statusToByteCount(*ip);
switch( p->status )
{
case kSysExMdId: // if this is the start of a sys-ex msg ...
// ... clear the buffer to prepare from sys-ex data
_cmMpTransmitChMsgs(p);
p->state = kExpectEOXStId;
p->dataCnt = kInvalidCnt;
p->dataIdx = kInvalidIdx;
p->buf[ p->bufIdx++ ] = kSysExMdId;
break;
case kSysComEoxMdId: // if this is the end of a sys-ex msg
assert( p->bufIdx < p->bufByteCnt );
p->buf[p->bufIdx++] = *ip;
_cmMpTransmitSysEx(p);
p->state = kExpectStatusStId;
break;
default: // ... otherwise it is a 1,2, or 3 byte msg status
if( p->dataCnt > 0 )
{
p->state = kExpectDataStId;
p->dataIdx = 0;
}
else
{
// this is a status only msg - store it
_cmMpParserStoreChMsg(p,timeStamp,*ip);
p->state = kExpectStatusStId;
p->dataIdx = kInvalidIdx;
p->dataCnt = kInvalidCnt;
}
}
continue;
}
// at this point the current byte (*ip) is a data byte
switch(p->state)
{
case kExpectStatusOrDataStId:
assert( p->dataIdx == 0 );
case kExpectDataStId:
switch( p->dataIdx )
{
case 0: // expecting data byte 0 ...
switch( p->dataCnt )
{
case 1: // ... of a 1 byte msg - the msg is complete
_cmMpParserStoreChMsg(p,timeStamp,*ip);
p->state = kExpectStatusOrDataStId;
break;
case 2: // ... of a 2 byte msg - prepare to recv the second data byte
p->state = kExpectDataStId;
p->dataIdx = 1;
p->data0 = *ip;
break;
default:
assert(0);
}
break;
case 1: // expecting data byte 1 of a two byte msg
assert( p->dataCnt == 2 );
assert( p->state == kExpectDataStId );
_cmMpParserStoreChMsg(p,timeStamp,*ip);
p->state = kExpectStatusOrDataStId;
p->dataIdx = 0;
break;
default:
assert(0);
}
break;
case kExpectEOXStId:
assert( p->bufIdx < p->bufByteCnt );
p->buf[p->bufIdx++] = *ip;
// if the buffer is full - then transmit it
if( p->bufIdx == p->bufByteCnt )
_cmMpTransmitSysEx(p);
break;
}
} // ip loop
_cmMpTransmitChMsgs(p);
}
cw::rc_t cw::midi::parser::midiTriple( handle_t h, const time::spec_t* timeStamp, uint8_t status, uint8_t d0, uint8_t d1 )
{
rc_t rc = kOkRC;
parser_t* p = _handleToPtr(h);
uint8_t mb = 0xff; // a midi triple may never have a status of 0xff
if( d0 == 0xff )
p->dataCnt = 0;
else
if( d1 == 0xff )
p->dataCnt = 1;
else
p->dataCnt = 2;
p->status = status;
switch( p->dataCnt )
{
case 0:
mb = status;
break;
case 1:
mb = d0;
break;
case 2:
p->data0 = d0;
mb = d1;
break;
default:
rc = cwLogError(kInvalidArgRC,"An invalid MIDI status byte (0x%x) was encountered by the MIDI data parser.");
goto errLabel;
break;
}
if( mb != 0xff )
_cmMpParserStoreChMsg(p,timeStamp,mb);
p->dataCnt = kInvalidCnt;
errLabel:
return rc;
}
cw::rc_t cw::midi::parser::transmit( handle_t h )
{
parser_t* p = _handleToPtr(h);
_cmMpTransmitChMsgs(p);
return kOkRC;
}
cw::rc_t cw::midi::parser::installCallback( handle_t h, cbFunc_t cbFunc, void* cbDataPtr )
{
parser_t* p = _handleToPtr(h);
cbRecd_t* newCbPtr = mem::allocZ<cbRecd_t>( 1 );
cbRecd_t* c = p->cbChain;
newCbPtr->cbFunc = cbFunc;
newCbPtr->cbDataPtr = cbDataPtr;
newCbPtr->linkPtr = NULL;
if( p->cbChain == NULL )
p->cbChain = newCbPtr;
else
{
while( c->linkPtr != NULL )
c = c->linkPtr;
c->linkPtr = newCbPtr;
}
return kOkRC;
}
cw::rc_t cw::midi::parser::removeCallback( handle_t h, cbFunc_t cbFunc, void* cbDataPtr )
{
parser_t* p = _handleToPtr(h);
cbRecd_t* c1 = p->cbChain; // target link
cbRecd_t* c0 = NULL; // link pointing to target
// search for the cbFunc to remove
for(; c1!=NULL; c1=c1->linkPtr)
{
if( c1->cbFunc == cbFunc && c1->cbDataPtr == cbDataPtr)
break;
c0 = c1;
}
// if the cbFunc was not found
if( c1 == NULL )
return cwLogError(kInvalidArgRC,"Unable to locate the callback function %p for removal.",cbFunc);
// the cbFunc to remove was found
// if it was the first cb in the chain
if( c0 == NULL )
p->cbChain = c1->linkPtr;
else
c0->linkPtr = c1->linkPtr;
mem::release(c1);
return kOkRC;
}
bool cw::midi::parser::hasCallback( handle_t h, cbFunc_t cbFunc, void* cbArg )
{
parser_t* p = _handleToPtr(h);
cbRecd_t* c = p->cbChain; // target link
// search for the cbFunc to remove
for(; c!=NULL; c=c->linkPtr)
if( c->cbFunc == cbFunc && c->cbDataPtr == cbArg )
return true;
return false;
}

36
cwMidiParser.h Normal file
View File

@ -0,0 +1,36 @@
namespace cw
{
namespace midi
{
namespace parser
{
typedef handle<struct parser_str> handle_t;
// 'cbFunc' and 'cbArg' are optional. If 'cbFunc' is not supplied in the call to
// create() it may be supplied later by installCallback().
// 'bufByteCnt' define 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 );
}
}
}