Standardized filesys and mem namespaces.

Removed use of global variable in cwAudioBuf.
This commit is contained in:
kpl 2019-12-27 21:51:28 -05:00
parent 0ead6fa974
commit 5a62d884dd
26 changed files with 580 additions and 765 deletions

View File

@ -75,12 +75,12 @@ namespace cw
{ {
unsigned chCnt; unsigned chCnt;
cmApCh* chArray; cmApCh* chArray;
unsigned n; // length of b[] (multiple of dspFrameCnt) bufCnt*framesPerCycle unsigned n; // length of b[] (multiple of dspFrameCnt) bufCnt*framesPerCycle
double srate; // device sample rate; double srate; // device sample rate;
unsigned faultCnt; unsigned faultCnt;
unsigned framesPerCycle; unsigned framesPerCycle;
unsigned dspFrameCnt; unsigned dspFrameCnt;
time::spec_t timeStamp; // base (starting) time stamp for this device time::spec_t timeStamp; // base (starting) time stamp for this device
std::atomic<unsigned> ioFrameCnt; // count of frames input or output for this device std::atomic<unsigned> ioFrameCnt; // count of frames input or output for this device
} cmApIO; } cmApIO;
@ -88,30 +88,30 @@ namespace cw
typedef struct typedef struct
{ {
// ioArray[] always contains 2 elements - one for input the other for output. // ioArray[] always contains 2 elements - one for input the other for output.
cmApIO ioArray[kIoApCnt]; cmApIO ioArray[kIoApCnt];
} cmApDev; } cmApDev;
typedef struct typedef struct audioBuf_str
{ {
cmApDev* devArray; cmApDev* devArray;
unsigned devCnt; unsigned devCnt;
unsigned meterMs; unsigned meterMs;
sample_t* zeroBuf; // buffer of zeros sample_t* zeroBuf; // buffer of zeros
unsigned zeroBufCnt; // max of all dspFrameCnt for all devices. unsigned zeroBufCnt; // max of all dspFrameCnt for all devices.
} cmApBuf; } audioBuf_t;
cmApBuf _theBuf; inline audioBuf_t* _handleToPtr( handle_t h ) { return handleToPtr<handle_t,audioBuf_t>(h); }
sample_t _cmApMeterValue( const cmApCh* cp ) sample_t _cmApMeterValue( const cmApCh* cp )
{ {
double sum = 0; double sum = 0;
unsigned i; unsigned i;
for(i=0; i<cp->mn; ++i) for(i=0; i<cp->mn; ++i)
sum += cp->m[i]; sum += cp->m[i];
return cp->mn==0 ? 0 : (sample_t)sqrt(sum/cp->mn); return cp->mn == 0 ? 0 : (sample_t)sqrt(sum/cp->mn);
} }
void _cmApSine( cmApCh* cp, sample_t* b0, unsigned n0, sample_t* b1, unsigned n1, unsigned stride, float srate ) void _cmApSine( cmApCh* cp, sample_t* b0, unsigned n0, sample_t* b1, unsigned n1, unsigned stride, float srate )
@ -130,24 +130,24 @@ namespace cw
const sample_t* ep = b + bn; const sample_t* ep = b + bn;
sample_t sum = 0; sample_t sum = 0;
for(; b<ep; b+=stride) for(; b<ep; b += stride)
sum += *b * *b; sum += *b * *b;
return sum / bn; return sum / bn;
} }
void _cmApChFinalize( cmApCh* chPtr ) void _cmApChFinalize( cmApCh* chPtr )
{ {
memRelease( chPtr->b ); mem::release( chPtr->b );
memRelease( chPtr->m ); mem::release( chPtr->m );
} }
// n=buf sample cnt mn=meter buf smp cnt // n = buf sample cnt mn=meter buf smp cnt
void _cmApChInitialize( cmApCh* chPtr, unsigned n, unsigned mn ) void _cmApChInitialize( cmApCh* chPtr, unsigned n, unsigned mn )
{ {
_cmApChFinalize(chPtr); _cmApChFinalize(chPtr);
chPtr->b = n==0 ? NULL : memAllocZ<sample_t>( n ); chPtr->b = n==0 ? NULL : mem::allocZ<sample_t>( n );
chPtr->ii = 0; chPtr->ii = 0;
chPtr->oi = 0; chPtr->oi = 0;
chPtr->fn = 0; chPtr->fn = 0;
@ -155,7 +155,7 @@ namespace cw
chPtr->hz = 1000; chPtr->hz = 1000;
chPtr->gain = 1.0; chPtr->gain = 1.0;
chPtr->mn = mn; chPtr->mn = mn;
chPtr->m = memAllocZ<sample_t>(mn); chPtr->m = mem::allocZ<sample_t>(mn);
chPtr->mi = 0; chPtr->mi = 0;
} }
@ -165,7 +165,7 @@ namespace cw
for(i=0; i<ioPtr->chCnt; ++i) for(i=0; i<ioPtr->chCnt; ++i)
_cmApChFinalize( ioPtr->chArray + i ); _cmApChFinalize( ioPtr->chArray + i );
memRelease(ioPtr->chArray); mem::release(ioPtr->chArray);
ioPtr->chCnt = 0; ioPtr->chCnt = 0;
ioPtr->n = 0; ioPtr->n = 0;
} }
@ -178,7 +178,7 @@ namespace cw
n += (n % dspFrameCnt); // force buffer size to be a multiple of dspFrameCnt n += (n % dspFrameCnt); // force buffer size to be a multiple of dspFrameCnt
ioPtr->chArray = chCnt==0 ? NULL : memAllocZ<cmApCh>(chCnt ); ioPtr->chArray = chCnt==0 ? NULL : mem::allocZ<cmApCh>(chCnt );
ioPtr->chCnt = chCnt; ioPtr->chCnt = chCnt;
ioPtr->n = n; ioPtr->n = n;
ioPtr->faultCnt = 0; ioPtr->faultCnt = 0;
@ -189,7 +189,7 @@ namespace cw
ioPtr->timeStamp.tv_nsec = 0; ioPtr->timeStamp.tv_nsec = 0;
ioPtr->ioFrameCnt = 0; ioPtr->ioFrameCnt = 0;
for(i=0; i<chCnt; ++i ) for(i = 0; i<chCnt; ++i )
_cmApChInitialize( ioPtr->chArray + i, n, meterBufN ); _cmApChInitialize( ioPtr->chArray + i, n, meterBufN );
} }
@ -197,7 +197,7 @@ namespace cw
void _cmApDevFinalize( cmApDev* dp ) void _cmApDevFinalize( cmApDev* dp )
{ {
unsigned i; unsigned i;
for(i=0; i<kIoApCnt; ++i) for(i = 0; i<kIoApCnt; ++i)
_cmApIoFinalize( dp->ioArray+i); _cmApIoFinalize( dp->ioArray+i);
} }
@ -207,7 +207,7 @@ namespace cw
_cmApDevFinalize(dp); _cmApDevFinalize(dp);
for(i=0; i<kIoApCnt; ++i) for(i = 0; i<kIoApCnt; ++i)
{ {
unsigned chCnt = i==kInApIdx ? iChCnt : oChCnt; unsigned chCnt = i==kInApIdx ? iChCnt : oChCnt;
unsigned bufN = i==kInApIdx ? iBufN : oBufN; unsigned bufN = i==kInApIdx ? iBufN : oBufN;
@ -220,15 +220,15 @@ namespace cw
void _theBufCalcTimeStamp( double srate, const time::spec_t* baseTimeStamp, unsigned frmCnt, time::spec_t* retTimeStamp ) void _theBufCalcTimeStamp( double srate, const time::spec_t* baseTimeStamp, unsigned frmCnt, time::spec_t* retTimeStamp )
{ {
if( retTimeStamp==NULL ) if( retTimeStamp == NULL )
return; return;
double secs = frmCnt / srate; double secs = frmCnt / srate;
unsigned int_secs = floor(secs); unsigned int_secs = floor(secs);
double frac_secs = secs - int_secs; double frac_secs = secs - int_secs;
retTimeStamp->tv_nsec = floor(baseTimeStamp->tv_nsec + frac_secs * 1000000000); retTimeStamp->tv_nsec = floor(baseTimeStamp->tv_nsec + frac_secs * 1000000000);
retTimeStamp->tv_sec = baseTimeStamp->tv_sec + int_secs; retTimeStamp->tv_sec = baseTimeStamp->tv_sec + int_secs;
if( retTimeStamp->tv_nsec > 1000000000 ) if( retTimeStamp->tv_nsec > 1000000000 )
{ {
@ -242,34 +242,48 @@ void _theBufCalcTimeStamp( double srate, const time::spec_t* baseTimeStamp, unsi
} }
} }
cw::rc_t cw::audio::buf::initialize( unsigned devCnt, unsigned meterMs ) cw::rc_t cw::audio::buf::create( handle_t& hRef, unsigned devCnt, unsigned meterMs )
{ {
rc_t rc; rc_t rc;
if((rc = finalize()) != kOkRC ) if((rc = destroy(hRef)) != kOkRC )
return rc;
audioBuf_t* p = mem::allocZ<audioBuf_t>();
p->devArray = mem::allocZ<cmApDev>(devCnt );
p->devCnt = devCnt;
hRef.set(p);
setMeterMs(hRef,meterMs);
return kOkRC;
}
cw::rc_t cw::audio::buf::destroy( handle_t& hRef )
{
rc_t rc = kOkRC;
if( !hRef.isValid() )
return rc; return rc;
_theBuf.devArray = memAllocZ<cmApDev>(devCnt ); audioBuf_t* p = _handleToPtr(hRef);
_theBuf.devCnt = devCnt;
setMeterMs(meterMs);
return kOkRC;
}
cw::rc_t cw::audio::buf::finalize()
{
unsigned i;
for(i=0; i<_theBuf.devCnt; ++i)
_cmApDevFinalize(_theBuf.devArray + i);
memRelease( _theBuf.devArray );
memRelease( _theBuf.zeroBuf );
_theBuf.devCnt = 0; unsigned i;
for(i=0; i<p->devCnt; ++i)
_cmApDevFinalize(p->devArray + i);
mem::release( p->devArray );
mem::release( p->zeroBuf );
p->devCnt = 0;
return kOkRC; return kOkRC;
} }
cw::rc_t cw::audio::buf::setup( cw::rc_t cw::audio::buf::setup(
handle_t h,
unsigned devIdx, unsigned devIdx,
double srate, double srate,
unsigned dspFrameCnt, unsigned dspFrameCnt,
@ -279,25 +293,27 @@ cw::rc_t cw::audio::buf::setup(
unsigned outChCnt, unsigned outChCnt,
unsigned outFramesPerCycle) unsigned outFramesPerCycle)
{ {
cmApDev* devPtr = _theBuf.devArray + devIdx; audioBuf_t* p = _handleToPtr(h);
cmApDev* devPtr = p->devArray + devIdx;
unsigned iBufN = bufCnt * inFramesPerCycle; unsigned iBufN = bufCnt * inFramesPerCycle;
unsigned oBufN = bufCnt * outFramesPerCycle; unsigned oBufN = bufCnt * outFramesPerCycle;
unsigned meterBufN = std::max(1.0,floor(srate * _theBuf.meterMs / (1000.0 * outFramesPerCycle))); unsigned meterBufN = std::max(1.0,floor(srate * p->meterMs / (1000.0 * outFramesPerCycle)));
_cmApDevInitialize( devPtr, srate, inFramesPerCycle, inChCnt, iBufN, outFramesPerCycle, outChCnt, oBufN, meterBufN, dspFrameCnt ); _cmApDevInitialize( devPtr, srate, inFramesPerCycle, inChCnt, iBufN, outFramesPerCycle, outChCnt, oBufN, meterBufN, dspFrameCnt );
if( inFramesPerCycle > _theBuf.zeroBufCnt || outFramesPerCycle > _theBuf.zeroBufCnt ) if( inFramesPerCycle > p->zeroBufCnt || outFramesPerCycle > p->zeroBufCnt )
{ {
_theBuf.zeroBufCnt = std::max(inFramesPerCycle,outFramesPerCycle); p->zeroBufCnt = std::max(inFramesPerCycle,outFramesPerCycle);
_theBuf.zeroBuf = memResizeZ<sample_t>(_theBuf.zeroBuf,_theBuf.zeroBufCnt); p->zeroBuf = mem::resizeZ<sample_t>(p->zeroBuf,p->zeroBufCnt);
} }
return kOkRC; return kOkRC;
} }
cw::rc_t cw::audio::buf::primeOutput( unsigned devIdx, unsigned audioCycleCnt ) cw::rc_t cw::audio::buf::primeOutput( handle_t h, unsigned devIdx, unsigned audioCycleCnt )
{ {
cmApIO* iop = _theBuf.devArray[devIdx].ioArray + kOutApIdx; audioBuf_t* p = _handleToPtr(h);
cmApIO* iop = p->devArray[devIdx].ioArray + kOutApIdx;
unsigned i; unsigned i;
for(i=0; i<iop->chCnt; ++i) for(i=0; i<iop->chCnt; ++i)
@ -313,17 +329,18 @@ cw::rc_t cw::audio::buf::primeOutput( unsigned devIdx, unsigned audioCycleCnt )
return kOkRC; return kOkRC;
} }
void cw::audio::buf::onPortEnable( unsigned devIdx, bool enableFl ) void cw::audio::buf::onPortEnable( handle_t h, unsigned devIdx, bool enableFl )
{ {
audioBuf_t* p = _handleToPtr(h);
if( devIdx == kInvalidIdx || enableFl==false) if( devIdx == kInvalidIdx || enableFl==false)
return; return;
cmApIO* iop = _theBuf.devArray[devIdx].ioArray + kOutApIdx; cmApIO* iop = p->devArray[devIdx].ioArray + kOutApIdx;
iop->timeStamp.tv_sec = 0; iop->timeStamp.tv_sec = 0;
iop->timeStamp.tv_nsec = 0; iop->timeStamp.tv_nsec = 0;
iop->ioFrameCnt = 0; iop->ioFrameCnt = 0;
iop = _theBuf.devArray[devIdx].ioArray + kInApIdx; iop = p->devArray[devIdx].ioArray + kInApIdx;
iop->timeStamp.tv_sec = 0; iop->timeStamp.tv_sec = 0;
iop->timeStamp.tv_nsec = 0; iop->timeStamp.tv_nsec = 0;
iop->ioFrameCnt = 0; iop->ioFrameCnt = 0;
@ -332,12 +349,14 @@ void cw::audio::buf::onPortEnable( unsigned devIdx, bool enableFl )
} }
cw::rc_t cw::audio::buf::update( cw::rc_t cw::audio::buf::update(
handle_t h,
device::audioPacket_t* inPktArray, device::audioPacket_t* inPktArray,
unsigned inPktCnt, unsigned inPktCnt,
device::audioPacket_t* outPktArray, device::audioPacket_t* outPktArray,
unsigned outPktCnt ) unsigned outPktCnt )
{ {
unsigned i,j; unsigned i,j;
audioBuf_t* p = _handleToPtr(h);
// copy samples from the packet to the buffer // copy samples from the packet to the buffer
if( inPktArray != NULL ) if( inPktArray != NULL )
@ -345,7 +364,7 @@ cw::rc_t cw::audio::buf::update(
for(i=0; i<inPktCnt; ++i) for(i=0; i<inPktCnt; ++i)
{ {
device::audioPacket_t* pp = inPktArray + i; device::audioPacket_t* pp = inPktArray + i;
cmApIO* ip = _theBuf.devArray[pp->devIdx].ioArray + kInApIdx; // dest io recd cmApIO* ip = p->devArray[pp->devIdx].ioArray + kInApIdx; // dest io recd
// if the base time stamp has not yet been set - then set it // if the base time stamp has not yet been set - then set it
if( ip->timeStamp.tv_sec==0 && ip->timeStamp.tv_nsec==0 ) if( ip->timeStamp.tv_sec==0 && ip->timeStamp.tv_nsec==0 )
@ -375,7 +394,7 @@ cw::rc_t cw::audio::buf::update(
n0 = pp->audioFramesCnt; n0 = pp->audioFramesCnt;
bool enaFl = cwIsFlag(cp->fl,kChFl) && cwIsFlag(cp->fl,kMuteFl)==false; bool enaFl = cwIsFlag(cp->fl,kChFl) && cwIsFlag(cp->fl,kMuteFl)==false;
const sample_t* sp = enaFl ? ((sample_t*)pp->audioBytesPtr) + j : _theBuf.zeroBuf; const sample_t* sp = enaFl ? ((sample_t*)pp->audioBytesPtr) + j : p->zeroBuf;
unsigned ssn = enaFl ? pp->chCnt : 1; // stride (packet samples are interleaved) unsigned ssn = enaFl ? pp->chCnt : 1; // stride (packet samples are interleaved)
sample_t* dp = cp->b + cp->ii; sample_t* dp = cp->b + cp->ii;
const sample_t* ep = dp + n0; const sample_t* ep = dp + n0;
@ -426,7 +445,7 @@ cw::rc_t cw::audio::buf::update(
for(i=0; i<outPktCnt; ++i) for(i=0; i<outPktCnt; ++i)
{ {
device::audioPacket_t* pp = outPktArray + i; device::audioPacket_t* pp = outPktArray + i;
cmApIO* op = _theBuf.devArray[pp->devIdx].ioArray + kOutApIdx; // dest io recd cmApIO* op = p->devArray[pp->devIdx].ioArray + kOutApIdx; // dest io recd
// if the base timestamp has not yet been set then set it. // if the base timestamp has not yet been set then set it.
if( op->timeStamp.tv_sec==0 && op->timeStamp.tv_nsec==0 ) if( op->timeStamp.tv_sec==0 && op->timeStamp.tv_nsec==0 )
@ -474,7 +493,7 @@ cw::rc_t cw::audio::buf::update(
} }
else // otherwise copy samples from the output buffer to the packet else // otherwise copy samples from the output buffer to the packet
{ {
const sample_t* sp = enaFl ? cp->b + cp->oi : _theBuf.zeroBuf; const sample_t* sp = enaFl ? cp->b + cp->oi : p->zeroBuf;
const sample_t* ep = sp + n0; const sample_t* ep = sp + n0;
// copy the first segment // copy the first segment
@ -485,7 +504,7 @@ cw::rc_t cw::audio::buf::update(
if( n1 > 0 ) if( n1 > 0 )
{ {
// copy the second segment // copy the second segment
sp = enaFl ? cp->b : _theBuf.zeroBuf; sp = enaFl ? cp->b : p->zeroBuf;
ep = sp + n1; ep = sp + n1;
for(; sp<ep; dp += pp->chCnt ) for(; sp<ep; dp += pp->chCnt )
*dp = cp->gain * *sp++; *dp = cp->gain * *sp++;
@ -511,119 +530,136 @@ cw::rc_t cw::audio::buf::update(
return kOkRC; return kOkRC;
} }
unsigned cw::audio::buf::meterMs() unsigned cw::audio::buf::meterMs( handle_t h )
{ return _theBuf.meterMs; }
void cw::audio::buf::setMeterMs( unsigned meterMs )
{ _theBuf.meterMs = std::min(1000u,std::max(10u,meterMs)); }
unsigned cw::audio::buf::channelCount( unsigned devIdx, unsigned flags )
{ {
if( devIdx == kInvalidIdx ) audioBuf_t* p = _handleToPtr(h);
return 0; return p->meterMs;
unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx;
return _theBuf.devArray[devIdx].ioArray[ idx ].chCnt;
} }
void cw::audio::buf::setFlag( unsigned devIdx, unsigned chIdx, unsigned flags ) void cw::audio::buf::setMeterMs( handle_t h, unsigned meterMs )
{ {
audioBuf_t* p = _handleToPtr(h);
p->meterMs = std::min(1000u,std::max(10u,meterMs));
}
unsigned cw::audio::buf::channelCount( handle_t h, unsigned devIdx, unsigned flags )
{
if( devIdx == kInvalidIdx )
return 0;
audioBuf_t* p = _handleToPtr(h);
unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx;
return p->devArray[devIdx].ioArray[ idx ].chCnt;
}
void cw::audio::buf::setFlag( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
{
if( devIdx == kInvalidIdx ) if( devIdx == kInvalidIdx )
return; return;
audioBuf_t* p = _handleToPtr(h);
unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx; unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx;
bool enableFl = flags & kEnableFl ? true : false; bool enableFl = flags & kEnableFl ? true : false;
unsigned i = chIdx != kInvalidIdx ? chIdx : 0; unsigned i = chIdx != kInvalidIdx ? chIdx : 0;
unsigned n = chIdx != kInvalidIdx ? chIdx+1 : _theBuf.devArray[devIdx].ioArray[idx].chCnt; unsigned n = chIdx != kInvalidIdx ? chIdx+1 : p->devArray[devIdx].ioArray[idx].chCnt;
for(; i<n; ++i) for(; i<n; ++i)
{ {
cmApCh* cp = _theBuf.devArray[devIdx].ioArray[idx].chArray + i; cmApCh* cp = p->devArray[devIdx].ioArray[idx].chArray + i;
cp->fl = cwEnaFlag(cp->fl, flags & (kChFl|kToneFl|kMeterFl|kMuteFl|kPassFl), enableFl ); cp->fl = cwEnaFlag(cp->fl, flags & (kChFl|kToneFl|kMeterFl|kMuteFl|kPassFl), enableFl );
} }
} }
bool cw::audio::buf::isFlag( unsigned devIdx, unsigned chIdx, unsigned flags ) bool cw::audio::buf::isFlag( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
{ {
if( devIdx == kInvalidIdx ) if( devIdx == kInvalidIdx )
return false; return false;
audioBuf_t* p = _handleToPtr(h);
unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx; unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx;
return cwIsFlag(_theBuf.devArray[devIdx].ioArray[idx].chArray[chIdx].fl,flags); return cwIsFlag(p->devArray[devIdx].ioArray[idx].chArray[chIdx].fl,flags);
} }
void cw::audio::buf::enableChannel( unsigned devIdx, unsigned chIdx, unsigned flags ) void cw::audio::buf::enableChannel( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
{ setFlag(devIdx,chIdx,flags | kChFl); } { setFlag(h,devIdx,chIdx,flags | kChFl); }
bool cw::audio::buf::isChannelEnabled( unsigned devIdx, unsigned chIdx, unsigned flags ) bool cw::audio::buf::isChannelEnabled( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
{ return isFlag(devIdx, chIdx, flags | kChFl); } { return isFlag(h,devIdx, chIdx, flags | kChFl); }
void cw::audio::buf::enableTone( unsigned devIdx, unsigned chIdx, unsigned flags ) void cw::audio::buf::enableTone( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
{ setFlag(devIdx,chIdx,flags | kToneFl); } { setFlag(h,devIdx,chIdx,flags | kToneFl); }
bool cw::audio::buf::isToneEnabled( unsigned devIdx, unsigned chIdx, unsigned flags ) bool cw::audio::buf::isToneEnabled( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
{ return isFlag(devIdx,chIdx,flags | kToneFl); } { return isFlag(h,devIdx,chIdx,flags | kToneFl); }
void cw::audio::buf::enableMute( unsigned devIdx, unsigned chIdx, unsigned flags ) void cw::audio::buf::enableMute( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
{ setFlag(devIdx,chIdx,flags | kMuteFl); } { setFlag(h,devIdx,chIdx,flags | kMuteFl); }
bool cw::audio::buf::isMuteEnabled( unsigned devIdx, unsigned chIdx, unsigned flags ) bool cw::audio::buf::isMuteEnabled( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
{ return isFlag(devIdx,chIdx,flags | kMuteFl); } { return isFlag(h,devIdx,chIdx,flags | kMuteFl); }
void cw::audio::buf::enablePass( unsigned devIdx, unsigned chIdx, unsigned flags ) void cw::audio::buf::enablePass( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
{ setFlag(devIdx,chIdx,flags | kPassFl); } { setFlag(h,devIdx,chIdx,flags | kPassFl); }
bool cw::audio::buf::isPassEnabled( unsigned devIdx, unsigned chIdx, unsigned flags ) bool cw::audio::buf::isPassEnabled( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
{ return isFlag(devIdx,chIdx,flags | kPassFl); } { return isFlag(h,devIdx,chIdx,flags | kPassFl); }
void cw::audio::buf::enableMeter( unsigned devIdx, unsigned chIdx, unsigned flags ) void cw::audio::buf::enableMeter( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
{ setFlag(devIdx,chIdx,flags | kMeterFl); } { setFlag(h,devIdx,chIdx,flags | kMeterFl); }
bool cw::audio::buf::isMeterEnabled(unsigned devIdx, unsigned chIdx, unsigned flags ) bool cw::audio::buf::isMeterEnabled(handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
{ return isFlag(devIdx,chIdx,flags | kMeterFl); } { return isFlag(h,devIdx,chIdx,flags | kMeterFl); }
cw::audio::buf::sample_t cw::audio::buf::meter(unsigned devIdx, unsigned chIdx, unsigned flags ) cw::audio::buf::sample_t cw::audio::buf::meter(handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
{ {
if( devIdx == kInvalidIdx ) if( devIdx == kInvalidIdx )
return 0; return 0;
audioBuf_t* p = _handleToPtr(h);
unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx; unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx;
const cmApCh* cp = _theBuf.devArray[devIdx].ioArray[idx].chArray + chIdx; const cmApCh* cp = p->devArray[devIdx].ioArray[idx].chArray + chIdx;
return _cmApMeterValue(cp); return _cmApMeterValue(cp);
} }
void cw::audio::buf::setGain( unsigned devIdx, unsigned chIdx, unsigned flags, double gain ) void cw::audio::buf::setGain( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags, double gain )
{ {
if( devIdx == kInvalidIdx ) if( devIdx == kInvalidIdx )
return; return;
audioBuf_t* p = _handleToPtr(h);
unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx; unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx;
unsigned i = chIdx != kInvalidIdx ? chIdx : 0; unsigned i = chIdx != kInvalidIdx ? chIdx : 0;
unsigned n = i + (chIdx != kInvalidIdx ? 1 : _theBuf.devArray[devIdx].ioArray[idx].chCnt); unsigned n = i + (chIdx != kInvalidIdx ? 1 : p->devArray[devIdx].ioArray[idx].chCnt);
for(; i<n; ++i) for(; i<n; ++i)
_theBuf.devArray[devIdx].ioArray[idx].chArray[i].gain = gain; p->devArray[devIdx].ioArray[idx].chArray[i].gain = gain;
} }
double cw::audio::buf::gain( unsigned devIdx, unsigned chIdx, unsigned flags ) double cw::audio::buf::gain( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
{ {
if( devIdx == kInvalidIdx ) if( devIdx == kInvalidIdx )
return 0; return 0;
audioBuf_t* p = _handleToPtr(h);
unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx; unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx;
return _theBuf.devArray[devIdx].ioArray[idx].chArray[chIdx].gain; return p->devArray[devIdx].ioArray[idx].chArray[chIdx].gain;
} }
unsigned cw::audio::buf::getStatus( unsigned devIdx, unsigned flags, double* meterArray, unsigned meterCnt, unsigned* faultCntPtr ) unsigned cw::audio::buf::getStatus( handle_t h, unsigned devIdx, unsigned flags, double* meterArray, unsigned meterCnt, unsigned* faultCntPtr )
{ {
if( devIdx == kInvalidIdx ) if( devIdx == kInvalidIdx )
return 0; return 0;
audioBuf_t* p = _handleToPtr(h);
unsigned ioIdx = cwIsFlag(flags,kInFl) ? kInApIdx : kOutApIdx; unsigned ioIdx = cwIsFlag(flags,kInFl) ? kInApIdx : kOutApIdx;
cmApIO* iop = _theBuf.devArray[devIdx].ioArray + ioIdx; cmApIO* iop = p->devArray[devIdx].ioArray + ioIdx;
unsigned chCnt = std::min(iop->chCnt, meterCnt ); unsigned chCnt = std::min(iop->chCnt, meterCnt );
unsigned i; unsigned i;
@ -636,18 +672,19 @@ unsigned cw::audio::buf::getStatus( unsigned devIdx, unsigned flags, double* met
} }
bool cw::audio::buf::isDeviceReady( unsigned devIdx, unsigned flags ) bool cw::audio::buf::isDeviceReady( handle_t h, unsigned devIdx, unsigned flags )
{ {
//bool iFl = true; //bool iFl = true;
//bool oFl = true; //bool oFl = true;
unsigned i = 0; unsigned i = 0;
if( devIdx == kInvalidIdx ) if( devIdx == kInvalidIdx )
return false; return false;
audioBuf_t* p = _handleToPtr(h);
if( flags & kInFl ) if( flags & kInFl )
{ {
const cmApIO* ioPtr = _theBuf.devArray[devIdx].ioArray + kInApIdx; const cmApIO* ioPtr = p->devArray[devIdx].ioArray + kInApIdx;
for(i=0; i<ioPtr->chCnt; ++i) for(i=0; i<ioPtr->chCnt; ++i)
if( ioPtr->chArray[i].fn < ioPtr->dspFrameCnt ) if( ioPtr->chArray[i].fn < ioPtr->dspFrameCnt )
return false; return false;
@ -657,7 +694,7 @@ bool cw::audio::buf::isDeviceReady( unsigned devIdx, unsigned flags )
if( flags & kOutFl ) if( flags & kOutFl )
{ {
const cmApIO* ioPtr = _theBuf.devArray[devIdx].ioArray + kOutApIdx; const cmApIO* ioPtr = p->devArray[devIdx].ioArray + kOutApIdx;
for(i=0; i<ioPtr->chCnt; ++i) for(i=0; i<ioPtr->chCnt; ++i)
if( (ioPtr->n - ioPtr->chArray[i].fn) < ioPtr->dspFrameCnt ) if( (ioPtr->n - ioPtr->chArray[i].fn) < ioPtr->dspFrameCnt )
@ -674,8 +711,9 @@ bool cw::audio::buf::isDeviceReady( unsigned devIdx, unsigned flags )
// Note that his function returns audio samples but does NOT // Note that his function returns audio samples but does NOT
// change any internal states. // change any internal states.
void cw::audio::buf::get( unsigned devIdx, unsigned flags, sample_t* bufArray[], unsigned bufChCnt ) void cw::audio::buf::get( handle_t h, unsigned devIdx, unsigned flags, sample_t* bufArray[], unsigned bufChCnt )
{ {
audioBuf_t* p = _handleToPtr(h);
unsigned i; unsigned i;
if( devIdx == kInvalidIdx ) if( devIdx == kInvalidIdx )
{ {
@ -685,7 +723,7 @@ void cw::audio::buf::get( unsigned devIdx, unsigned flags, sample_t* bufArray[],
} }
unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx; unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx;
const cmApIO* ioPtr = _theBuf.devArray[devIdx].ioArray + idx; const cmApIO* ioPtr = p->devArray[devIdx].ioArray + idx;
unsigned n = bufChCnt < ioPtr->chCnt ? bufChCnt : ioPtr->chCnt; unsigned n = bufChCnt < ioPtr->chCnt ? bufChCnt : ioPtr->chCnt;
//unsigned offs = flags & kInFl ? ioPtr->oi : ioPtr->ii; //unsigned offs = flags & kInFl ? ioPtr->oi : ioPtr->ii;
cmApCh* cp = ioPtr->chArray; cmApCh* cp = ioPtr->chArray;
@ -698,17 +736,18 @@ void cw::audio::buf::get( unsigned devIdx, unsigned flags, sample_t* bufArray[],
} }
void cw::audio::buf::getIO( unsigned iDevIdx, sample_t* iBufArray[], unsigned iBufChCnt, time::spec_t* iTimeStampPtr, unsigned oDevIdx, sample_t* oBufArray[], unsigned oBufChCnt, time::spec_t* oTimeStampPtr ) void cw::audio::buf::getIO( handle_t h, unsigned iDevIdx, sample_t* iBufArray[], unsigned iBufChCnt, time::spec_t* iTimeStampPtr, unsigned oDevIdx, sample_t* oBufArray[], unsigned oBufChCnt, time::spec_t* oTimeStampPtr )
{ {
get( iDevIdx, kInFl, iBufArray, iBufChCnt ); audioBuf_t* p = _handleToPtr(h);
get( oDevIdx, kOutFl,oBufArray, oBufChCnt ); get( h, iDevIdx, kInFl, iBufArray, iBufChCnt );
get( h, oDevIdx, kOutFl,oBufArray, oBufChCnt );
unsigned i = 0; unsigned i = 0;
if( iDevIdx != kInvalidIdx && oDevIdx != kInvalidIdx ) if( iDevIdx != kInvalidIdx && oDevIdx != kInvalidIdx )
{ {
const cmApIO* ip = _theBuf.devArray[iDevIdx].ioArray + kInApIdx; const cmApIO* ip = p->devArray[iDevIdx].ioArray + kInApIdx;
const cmApIO* op = _theBuf.devArray[oDevIdx].ioArray + kOutApIdx; const cmApIO* op = p->devArray[oDevIdx].ioArray + kOutApIdx;
unsigned minChCnt = std::min(iBufChCnt,oBufChCnt); unsigned minChCnt = std::min(iBufChCnt,oBufChCnt);
unsigned frmCnt = std::min(ip->dspFrameCnt,op->dspFrameCnt); unsigned frmCnt = std::min(ip->dspFrameCnt,op->dspFrameCnt);
unsigned byteCnt = frmCnt * sizeof(sample_t); unsigned byteCnt = frmCnt * sizeof(sample_t);
@ -742,7 +781,7 @@ void cw::audio::buf::getIO( unsigned iDevIdx, sample_t* iBufArray[], unsigned
if( oDevIdx != kInvalidIdx ) if( oDevIdx != kInvalidIdx )
{ {
const cmApIO* op = _theBuf.devArray[oDevIdx].ioArray + kOutApIdx; const cmApIO* op = p->devArray[oDevIdx].ioArray + kOutApIdx;
unsigned byteCnt = op->dspFrameCnt * sizeof(sample_t); unsigned byteCnt = op->dspFrameCnt * sizeof(sample_t);
_theBufCalcTimeStamp(op->srate, &op->timeStamp, op->ioFrameCnt, oTimeStampPtr ); _theBufCalcTimeStamp(op->srate, &op->timeStamp, op->ioFrameCnt, oTimeStampPtr );
@ -753,16 +792,17 @@ void cw::audio::buf::getIO( unsigned iDevIdx, sample_t* iBufArray[], unsigned
} }
} }
void cw::audio::buf::advance( unsigned devIdx, unsigned flags ) void cw::audio::buf::advance( handle_t h, unsigned devIdx, unsigned flags )
{ {
unsigned i; unsigned i;
if( devIdx == kInvalidIdx ) if( devIdx == kInvalidIdx )
return; return;
audioBuf_t* p = _handleToPtr(h);
if( flags & kInFl ) if( flags & kInFl )
{ {
cmApIO* ioPtr = _theBuf.devArray[devIdx].ioArray + kInApIdx; cmApIO* ioPtr = p->devArray[devIdx].ioArray + kInApIdx;
for(i=0; i<ioPtr->chCnt; ++i) for(i=0; i<ioPtr->chCnt; ++i)
{ {
@ -782,7 +822,7 @@ void cw::audio::buf::advance( unsigned devIdx, unsigned flags )
if( flags & kOutFl ) if( flags & kOutFl )
{ {
cmApIO* ioPtr = _theBuf.devArray[devIdx].ioArray + kOutApIdx; cmApIO* ioPtr = p->devArray[devIdx].ioArray + kOutApIdx;
for(i=0; i<ioPtr->chCnt; ++i) for(i=0; i<ioPtr->chCnt; ++i)
{ {
cmApCh* cp = ioPtr->chArray + i; cmApCh* cp = ioPtr->chArray + i;
@ -800,13 +840,14 @@ void cw::audio::buf::advance( unsigned devIdx, unsigned flags )
} }
void cw::audio::buf::inputToOutput( unsigned iDevIdx, unsigned oDevIdx ) void cw::audio::buf::inputToOutput( handle_t h, unsigned iDevIdx, unsigned oDevIdx )
{ {
if( iDevIdx == kInvalidIdx || oDevIdx == kInvalidIdx ) if( iDevIdx == kInvalidIdx || oDevIdx == kInvalidIdx )
return; return;
audioBuf_t* p = _handleToPtr(h);
unsigned iChCnt = channelCount( iDevIdx, kInFl ); unsigned iChCnt = channelCount( h, iDevIdx, kInFl );
unsigned oChCnt = channelCount( oDevIdx, kOutFl ); unsigned oChCnt = channelCount( h, oDevIdx, kOutFl );
unsigned chCnt = iChCnt < oChCnt ? iChCnt : oChCnt; unsigned chCnt = iChCnt < oChCnt ? iChCnt : oChCnt;
unsigned i; unsigned i;
@ -815,17 +856,17 @@ void cw::audio::buf::inputToOutput( unsigned iDevIdx, unsigned oDevIdx )
sample_t* oBufPtrArray[ oChCnt ]; sample_t* oBufPtrArray[ oChCnt ];
while( isDeviceReady( iDevIdx, kInFl ) && isDeviceReady( oDevIdx, kOutFl ) ) while( isDeviceReady( h, iDevIdx, kInFl ) && isDeviceReady( h, oDevIdx, kOutFl ) )
{ {
get( iDevIdx, kInFl, iBufPtrArray, iChCnt ); get( h, iDevIdx, kInFl, iBufPtrArray, iChCnt );
get( oDevIdx, kOutFl, oBufPtrArray, oChCnt ); get( h, oDevIdx, kOutFl, oBufPtrArray, oChCnt );
// Warning: buffer pointers to disabled channels will be set to NULL // Warning: buffer pointers to disabled channels will be set to NULL
for(i=0; i<chCnt; ++i) for(i=0; i<chCnt; ++i)
{ {
cmApIO* ip = _theBuf.devArray[iDevIdx ].ioArray + kInApIdx; cmApIO* ip = p->devArray[iDevIdx].ioArray + kInApIdx;
cmApIO* op = _theBuf.devArray[oDevIdx].ioArray + kOutApIdx; cmApIO* op = p->devArray[oDevIdx].ioArray + kOutApIdx;
cwAssert( ip->dspFrameCnt == op->dspFrameCnt ); cwAssert( ip->dspFrameCnt == op->dspFrameCnt );
@ -842,20 +883,21 @@ void cw::audio::buf::inputToOutput( unsigned iDevIdx, unsigned oDevIdx )
} }
} }
advance( iDevIdx, kInFl ); advance( h, iDevIdx, kInFl );
advance( oDevIdx, kOutFl ); advance( h, oDevIdx, kOutFl );
} }
} }
void cw::audio::buf::report() void cw::audio::buf::report(handle_t h)
{ {
audioBuf_t* p = _handleToPtr(h);
unsigned i,j,k; unsigned i,j,k;
for(i=0; i<_theBuf.devCnt; ++i) for(i=0; i<p->devCnt; ++i)
{ {
for(j=0; j<kIoApCnt; ++j) for(j=0; j<kIoApCnt; ++j)
{ {
cmApIO* ip = _theBuf.devArray[i].ioArray + j; cmApIO* ip = p->devArray[i].ioArray + j;
unsigned ii = 0; unsigned ii = 0;
unsigned oi = 0; unsigned oi = 0;
@ -902,7 +944,8 @@ void cw::audio::buf::test()
sample_t* os = oSig; sample_t* os = oSig;
device::audioPacket_t pkt; device::audioPacket_t pkt;
unsigned i,j; unsigned i,j;
handle_t h;
// create a simulated signal // create a simulated signal
for(i=0; i<sigN; ++i) for(i=0; i<sigN; ++i)
{ {
@ -920,10 +963,10 @@ void cw::audio::buf::test()
// initialize a the audio buffer // initialize a the audio buffer
initialize(devCnt,meterMs); create(h,devCnt,meterMs);
// setup the buffer with the specific parameters to by used by the host audio ports // setup the buffer with the specific parameters to by used by the host audio ports
setup(devIdx,srate,dspFrameCnt,cycleCnt,inChCnt,framesPerCycle,outChCnt,framesPerCycle); setup(h,devIdx,srate,dspFrameCnt,cycleCnt,inChCnt,framesPerCycle,outChCnt,framesPerCycle);
// simulate cylcing through sigN buffer transactions // simulate cylcing through sigN buffer transactions
for(i=0; i<sigN; i+=framesPerCycle*inChCnt) for(i=0; i<sigN; i+=framesPerCycle*inChCnt)
@ -934,16 +977,16 @@ void cw::audio::buf::test()
// simulate a call from the audio port with incoming samples // simulate a call from the audio port with incoming samples
// (fill the audio buffers internal input buffers) // (fill the audio buffers internal input buffers)
update(&pkt,1,NULL,0); update(h,&pkt,1,NULL,0);
// if all devices need to be serviced // if all devices need to be serviced
while( isDeviceReady( devIdx, kInFl | kOutFl )) while( isDeviceReady( h, devIdx, kInFl | kOutFl ))
{ {
// get pointers to full internal input buffers // get pointers to full internal input buffers
get(devIdx, kInFl, inBufArray, bufChCnt ); get(h, devIdx, kInFl, inBufArray, bufChCnt );
// get pointers to empty internal output buffers // get pointers to empty internal output buffers
get(devIdx, kOutFl, outBufArray, bufChCnt ); get(h, devIdx, kOutFl, outBufArray, bufChCnt );
// Warning: pointers to disabled channels will be set to NULL. // Warning: pointers to disabled channels will be set to NULL.
@ -961,14 +1004,14 @@ void cw::audio::buf::test()
// signal the buffer that this cycle is complete. // signal the buffer that this cycle is complete.
// (marks current internal input/output buffer empty/full) // (marks current internal input/output buffer empty/full)
advance( devIdx, kInFl | kOutFl ); advance( h, devIdx, kInFl | kOutFl );
} }
pkt.audioBytesPtr = os; pkt.audioBytesPtr = os;
// simulate a call from the audio port picking up outgoing samples // simulate a call from the audio port picking up outgoing samples
// (empties the audio buffers internal output buffers) // (empties the audio buffers internal output buffers)
update(NULL,0,&pkt,1); update(h,NULL,0,&pkt,1);
os += pkt.audioFramesCnt * pkt.chCnt; os += pkt.audioFramesCnt * pkt.chCnt;
} }
@ -977,7 +1020,7 @@ void cw::audio::buf::test()
cwLogInfo("%f ",oSig[i]); cwLogInfo("%f ",oSig[i]);
cwLogInfo("\n"); cwLogInfo("\n");
finalize(); destroy(h);
} }
/// [cwAudioBufExample] /// [cwAudioBufExample]

View File

@ -41,23 +41,21 @@ namespace cw
//( //(
typedef device::sample_t sample_t; typedef device::sample_t sample_t;
typedef handle<struct audioBuf_str> handle_t;
enum
{
kOkAbRC = 0
};
// Allocate and initialize an audio buffer. // Allocate and initialize an audio buffer.
// devCnt - count of devices this buffer will handle. // devCnt - count of devices this buffer will handle.
// meterMs - length of the meter buffers in milliseconds (automatically limit to the range:10 to 1000) // meterMs - length of the meter buffers in milliseconds (automatically limit to the range:10 to 1000)
rc_t initialize( unsigned devCnt, unsigned meterMs ); rc_t create( handle_t& hRef, unsigned devCnt, unsigned meterMs );
// Deallocate and release any resource held by an audio buffer allocated via initialize(). // Deallocate and release any resource held by an audio buffer allocated via initialize().
rc_t finalize(); rc_t destroy( handle_t& hRef );
// Configure a buffer for a given device. // Configure a buffer for a given device.
rc_t setup( rc_t setup(
handle_t h,
unsigned devIdx, //< device to setup unsigned devIdx, //< device to setup
double srate, //< device sample rate (only required for synthesizing the correct test-tone frequency) double srate, //< device sample rate (only required for synthesizing the correct test-tone frequency)
unsigned dspFrameCnt, // dspFrameCnt - count of samples in channel buffers returned via get() unsigned dspFrameCnt, // dspFrameCnt - count of samples in channel buffers returned via get()
@ -69,10 +67,10 @@ namespace cw
); );
// Prime the buffer with 'audioCycleCnt' * outFramesPerCycle samples ready to be played // Prime the buffer with 'audioCycleCnt' * outFramesPerCycle samples ready to be played
rc_t primeOutput( unsigned devIdx, unsigned audioCycleCnt ); rc_t primeOutput( handle_t h, unsigned devIdx, unsigned audioCycleCnt );
// Notify the audio buffer that a device is being enabled or disabled. // Notify the audio buffer that a device is being enabled or disabled.
void onPortEnable( unsigned devIdx, bool enabelFl ); void onPortEnable( handle_t h, unsigned devIdx, bool enabelFl );
// This function is called asynchronously by the audio device driver to transfer incoming samples to the // This function is called asynchronously by the audio device driver to transfer incoming samples to the
// the buffer and to send outgoing samples to the DAC. This function is // the buffer and to send outgoing samples to the DAC. This function is
@ -91,6 +89,7 @@ namespace cw
// The enable flag has higher precedence than the tone flag therefore disabled channels // The enable flag has higher precedence than the tone flag therefore disabled channels
// will be set to zero even if the tone flag is set. // will be set to zero even if the tone flag is set.
rc_t update( rc_t update(
handle_t h,
device::audioPacket_t* inPktArray, //< full audio packets from incoming audio (from ADC) device::audioPacket_t* inPktArray, //< full audio packets from incoming audio (from ADC)
unsigned inPktCnt, //< count of incoming audio packets unsigned inPktCnt, //< count of incoming audio packets
device::audioPacket_t* outPktArray, //< empty audio packet for outgoing audio (to DAC) device::audioPacket_t* outPktArray, //< empty audio packet for outgoing audio (to DAC)
@ -112,78 +111,78 @@ namespace cw
}; };
// Return the meter window period as set by initialize() // Return the meter window period as set by initialize()
unsigned meterMs(); unsigned meterMs(handle_t h);
// Set the meter update period. THis function limits the value to between 10 and 1000. // Set the meter update period. THis function limits the value to between 10 and 1000.
void setMeterMs( unsigned meterMs ); void setMeterMs( handle_t h, unsigned meterMs );
// Returns the channel count set via setup(). // Returns the channel count set via setup().
unsigned channelCount( unsigned devIdx, unsigned flags ); unsigned channelCount( handle_t h, unsigned devIdx, unsigned flags );
// Set chIdx to -1 to enable all channels on this device. // Set chIdx to -1 to enable all channels on this device.
// Set flags to {kInFl | kOutFl} | {kChFl | kToneFl | kMeterFl} | { kEnableFl=on | 0=off } // Set flags to {kInFl | kOutFl} | {kChFl | kToneFl | kMeterFl} | { kEnableFl=on | 0=off }
void setFlag( unsigned devIdx, unsigned chIdx, unsigned flags ); void setFlag( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags );
// Return true if the the flags is set. // Return true if the the flags is set.
bool isFlag( unsigned devIdx, unsigned chIdx, unsigned flags ); bool isFlag( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags );
// Set chIdx to -1 to enable all channels on this device. // Set chIdx to -1 to enable all channels on this device.
void enableChannel( unsigned devIdx, unsigned chIdx, unsigned flags ); void enableChannel( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags );
// Returns true if an input/output channel is enabled on the specified device. // Returns true if an input/output channel is enabled on the specified device.
bool isChannelEnabled(unsigned devIdx, unsigned chIdx, unsigned flags ); bool isChannelEnabled(handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags );
// Set the state of the tone generator on the specified channel. // Set the state of the tone generator on the specified channel.
// Set chIdx to -1 to apply the change to all channels on this device. // Set chIdx to -1 to apply the change to all channels on this device.
// Set flags to {kInFl | kOutFl} | { kEnableFl=on | 0=off } // Set flags to {kInFl | kOutFl} | { kEnableFl=on | 0=off }
void enableTone( unsigned devIdx, unsigned chIdx, unsigned flags ); void enableTone( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags );
// Returns true if an input/output tone is enabled on the specified device. // Returns true if an input/output tone is enabled on the specified device.
bool isToneEnabled(unsigned devIdx, unsigned chIdx, unsigned flags ); bool isToneEnabled(handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags );
// Mute a specified channel. // Mute a specified channel.
// Set chIdx to -1 to apply the change to all channels on this device. // Set chIdx to -1 to apply the change to all channels on this device.
// Set flags to {kInFl | kOutFl} | { kEnableFl=on | 0=off } // Set flags to {kInFl | kOutFl} | { kEnableFl=on | 0=off }
void enableMute( unsigned devIdx, unsigned chIdx, unsigned flags ); void enableMute( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags );
// Returns true if an input/output channel is muted on the specified device. // Returns true if an input/output channel is muted on the specified device.
bool isMuteEnabled(unsigned devIdx, unsigned chIdx, unsigned flags ); bool isMuteEnabled(handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags );
// Set the specified channel to pass through. // Set the specified channel to pass through.
// Set chIdx to -1 to apply the change to all channels on this device. // Set chIdx to -1 to apply the change to all channels on this device.
// Set flags to {kInFl | kOutFl} | { kEnableFl=on | 0=off } // Set flags to {kInFl | kOutFl} | { kEnableFl=on | 0=off }
void enablePass( unsigned devIdx, unsigned chIdx, unsigned flags ); void enablePass( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags );
// Returns true if pass through is enabled on the specified channel. // Returns true if pass through is enabled on the specified channel.
bool isPassEnabled(unsigned devIdx, unsigned chIdx, unsigned flags ); bool isPassEnabled(handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags );
// Turn meter data collection on and off. // Turn meter data collection on and off.
// Set chIdx to -1 to apply the change to all channels on this device. // Set chIdx to -1 to apply the change to all channels on this device.
// Set flags to {kInFl | kOutFl} | { kEnableFl=on | 0=off } // Set flags to {kInFl | kOutFl} | { kEnableFl=on | 0=off }
void enableMeter( unsigned devIdx, unsigned chIdx, unsigned flags ); void enableMeter( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags );
// Returns true if an input/output tone is enabled on the specified device. // Returns true if an input/output tone is enabled on the specified device.
bool isMeterEnabled(unsigned devIdx, unsigned chIdx, unsigned flags ); bool isMeterEnabled(handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags );
// Return the meter value for the requested channel. // Return the meter value for the requested channel.
// Set flags to kInFl | kOutFl. // Set flags to kInFl | kOutFl.
sample_t meter(unsigned devIdx, unsigned chIdx, unsigned flags ); sample_t meter(handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags );
// Set chIdx to -1 to apply the gain to all channels on the specified device. // Set chIdx to -1 to apply the gain to all channels on the specified device.
void setGain( unsigned devIdx, unsigned chIdx, unsigned flags, double gain ); void setGain( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags, double gain );
// Return the current gain seting for the specified channel. // Return the current gain seting for the specified channel.
double gain( unsigned devIdx, unsigned chIdx, unsigned flags ); double gain( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags );
// Get the meter and fault status of the channel input or output channel array of a device. // Get the meter and fault status of the channel input or output channel array of a device.
// Set 'flags' to { kInFl | kOutFl }. // Set 'flags' to { kInFl | kOutFl }.
// The returns value is the count of channels actually written to meterArray. // The returns value is the count of channels actually written to meterArray.
// If 'faultCntPtr' is non-NULL then it is set to the faultCnt of the associated devices input or output buffer. // If 'faultCntPtr' is non-NULL then it is set to the faultCnt of the associated devices input or output buffer.
unsigned getStatus( unsigned devIdx, unsigned flags, double* meterArray, unsigned meterCnt, unsigned* faultCntPtr ); unsigned getStatus( handle_t h, unsigned devIdx, unsigned flags, double* meterArray, unsigned meterCnt, unsigned* faultCntPtr );
// Do all enabled input/output channels on this device have samples available? // Do all enabled input/output channels on this device have samples available?
// 'flags' can be set to either or both kInFl and kOutFl // 'flags' can be set to either or both kInFl and kOutFl
bool isDeviceReady( unsigned devIdx, unsigned flags ); bool isDeviceReady( handle_t h, unsigned devIdx, unsigned flags );
// This function is called by the application to get full incoming sample buffers and // This function is called by the application to get full incoming sample buffers and
// to fill empty outgoing sample buffers. // to fill empty outgoing sample buffers.
@ -195,7 +194,7 @@ namespace cw
// (see initialize()). // (see initialize()).
// Note that this function just returns audio information it does not // Note that this function just returns audio information it does not
// change any internal states. // change any internal states.
void get( unsigned devIdx, unsigned flags, sample_t* bufArray[], unsigned bufChCnt ); void get( handle_t h, unsigned devIdx, unsigned flags, sample_t* bufArray[], unsigned bufChCnt );
// This function replaces calls to get() and implements pass-through and output // This function replaces calls to get() and implements pass-through and output
// buffer zeroing: // buffer zeroing:
@ -212,22 +211,22 @@ namespace cw
// 3) This function just returns audio information it does not // 3) This function just returns audio information it does not
// change any internal states. // change any internal states.
// 4) The timestamp pointers are optional. // 4) The timestamp pointers are optional.
void getIO( unsigned iDevIdx, sample_t* iBufArray[], unsigned iBufChCnt, time::spec_t* iTimeStampPtr, void getIO( handle_t h, unsigned iDevIdx, sample_t* iBufArray[], unsigned iBufChCnt, time::spec_t* iTimeStampPtr,
unsigned oDevIdx, sample_t* oBufArray[], unsigned oBufChCnt, time::spec_t* oTimeStampPtr ); unsigned oDevIdx, sample_t* oBufArray[], unsigned oBufChCnt, time::spec_t* oTimeStampPtr );
// The application calls this function each time it completes processing of a bufArray[] // The application calls this function each time it completes processing of a bufArray[]
// returned from get(). 'flags' can be set to either or both kInFl and kOutFl. // returned from get(). 'flags' can be set to either or both kInFl and kOutFl.
// This function should only be called from the client thread. // This function should only be called from the client thread.
void advance( unsigned devIdx, unsigned flags ); void advance( handle_t h, unsigned devIdx, unsigned flags );
// Copy all available samples incoming samples from an input device to an output device. // Copy all available samples incoming samples from an input device to an output device.
// The source code for this example is a good example of how an application should use get() // The source code for this example is a good example of how an application should use get()
// and advance(). // and advance().
void inputToOutput( unsigned inDevIdx, unsigned outDevIdx ); void inputToOutput( handle_t h, unsigned inDevIdx, unsigned outDevIdx );
// Print the current buffer state. // Print the current buffer state.
void report(); void report( handle_t h );
// Run a buffer usage simulation to test the class. cmAudioPortTest.c calls this function. // Run a buffer usage simulation to test the class. cmAudioPortTest.c calls this function.
void test(); void test();

View File

@ -34,11 +34,11 @@ namespace cw
while( d != nullptr ) while( d != nullptr )
{ {
drv_t* d0 = d->link; drv_t* d0 = d->link;
memRelease(d); mem::release(d);
d = d0; d = d0;
} }
memRelease(p); mem::release(p);
return kOkRC; return kOkRC;
} }
@ -70,7 +70,7 @@ cw::rc_t cw::audio::device::create( handle_t& hRef )
if((rc = destroy(hRef)) != kOkRC) if((rc = destroy(hRef)) != kOkRC)
return rc; return rc;
device_t* p = memAllocZ<device_t>(); device_t* p = mem::allocZ<device_t>();
hRef.set(p); hRef.set(p);
return rc; return rc;
@ -99,7 +99,7 @@ cw::rc_t cw::audio::device::registerDriver( handle_t h, driver_t* drv )
unsigned n = drv->deviceCount( drv ); unsigned n = drv->deviceCount( drv );
if( n > 0 ) if( n > 0 )
{ {
drv_t* d = memAllocZ<drv_t>(); drv_t* d = mem::allocZ<drv_t>();
d->begIdx = p->nextDrvIdx; d->begIdx = p->nextDrvIdx;
p->nextDrvIdx += n; p->nextDrvIdx += n;

View File

@ -70,7 +70,7 @@ namespace cw
// interacts with data structures also handled by the application. The audio buffer class (\see cwAudioBuf.h) // interacts with data structures also handled by the application. The audio buffer class (\see cwAudioBuf.h)
// is designed to provide a safe and efficient way to communicate between // is designed to provide a safe and efficient way to communicate between
// the audio thread and the application. // the audio thread and the application.
typedef void (*cbFunc_t)( audioPacket_t* inPktArray, unsigned inPktCnt, audioPacket_t* outPktArray, unsigned outPktCnt ); typedef void (*cbFunc_t)( void* cbArg, audioPacket_t* inPktArray, unsigned inPktCnt, audioPacket_t* outPktArray, unsigned outPktCnt );
typedef struct driver_str typedef struct driver_str
{ {

View File

@ -331,7 +331,7 @@ namespace cw
const int reallocN = 5; const int reallocN = 5;
if( p->devCnt == p->devAllocCnt ) if( p->devCnt == p->devAllocCnt )
{ {
p->devArray = memResizeZ<devRecd_t>( p->devArray, p->devAllocCnt + reallocN ); p->devArray = mem::resizeZ<devRecd_t>( p->devArray, p->devAllocCnt + reallocN );
p->devAllocCnt += reallocN; p->devAllocCnt += reallocN;
} }
@ -397,21 +397,21 @@ namespace cw
_devShutdown(p,p->devArray+i,true); _devShutdown(p,p->devArray+i,true);
_devShutdown(p,p->devArray+i,false); _devShutdown(p,p->devArray+i,false);
memRelease(p->devArray[i].iBuf); mem::release(p->devArray[i].iBuf);
memRelease(p->devArray[i].oBuf); mem::release(p->devArray[i].oBuf);
memRelease(p->devArray[i].nameStr); mem::release(p->devArray[i].nameStr);
memRelease(p->devArray[i].descStr); mem::release(p->devArray[i].descStr);
} }
memRelease(p->pollfds); mem::release(p->pollfds);
memRelease(p->pollfdsDesc); mem::release(p->pollfdsDesc);
memRelease(p->devArray); mem::release(p->devArray);
p->devAllocCnt = 0; p->devAllocCnt = 0;
p->devCnt = 0; p->devCnt = 0;
memRelease(p); mem::release(p);
return rc; return rc;
} }
@ -759,7 +759,7 @@ namespace cw
{ {
pkt.audioFramesCnt = err; pkt.audioFramesCnt = err;
drp->cbFunc(&pkt,1,NULL,0 ); // send the samples to the application drp->cbFunc(drp->cbArg,&pkt,1,NULL,0 ); // send the samples to the application
} }
} }
@ -767,7 +767,7 @@ namespace cw
else else
{ {
// callback to fill the buffer // callback to fill the buffer
drp->cbFunc(NULL,0,&pkt,1); drp->cbFunc(drp->cbArg,NULL,0,&pkt,1);
// note that the application may return fewer samples than were requested // note that the application may return fewer samples than were requested
err = _devWriteBuf(drp, pcmH, pkt.audioFramesCnt < frmCnt ? NULL : drp->oBuf,chCnt,frmCnt,drp->oBits,drp->oSigBits); err = _devWriteBuf(drp, pcmH, pkt.audioFramesCnt < frmCnt ? NULL : drp->oBuf,chCnt,frmCnt,drp->oBits,drp->oSigBits);
@ -900,7 +900,7 @@ namespace cw
if((err = _devReadBuf(drp,pcmH,drp->iBuf,chCnt,frmCnt,drp->iBits,drp->oBits)) > 0 ) if((err = _devReadBuf(drp,pcmH,drp->iBuf,chCnt,frmCnt,drp->iBits,drp->oBits)) > 0 )
{ {
pkt.audioFramesCnt = err; pkt.audioFramesCnt = err;
drp->cbFunc(&pkt,1,NULL,0 ); // send the samples to the application drp->cbFunc(drp->cbArg,&pkt,1,NULL,0 ); // send the samples to the application
} }
} }
@ -909,7 +909,7 @@ namespace cw
{ {
// callback to fill the buffer // callback to fill the buffer
drp->cbFunc(NULL,0,&pkt,1); drp->cbFunc(drp->cbArg,NULL,0,&pkt,1);
// note that the application may return fewer samples than were requested // note that the application may return fewer samples than were requested
err = _devWriteBuf(drp, pcmH, pkt.audioFramesCnt < frmCnt ? NULL : drp->oBuf,chCnt,frmCnt,drp->oBits,drp->oSigBits); err = _devWriteBuf(drp, pcmH, pkt.audioFramesCnt < frmCnt ? NULL : drp->oBuf,chCnt,frmCnt,drp->oBits,drp->oSigBits);
@ -1092,7 +1092,7 @@ namespace cw
drp->iSignFl = signFl; drp->iSignFl = signFl;
drp->iSwapFl = swapFl; drp->iSwapFl = swapFl;
drp->iPcmH = pcmH; drp->iPcmH = pcmH;
drp->iBuf = memResizeZ<device::sample_t>( drp->iBuf, actFpC * drp->iChCnt ); drp->iBuf = mem::resizeZ<device::sample_t>( drp->iBuf, actFpC * drp->iChCnt );
drp->iFpC = actFpC; drp->iFpC = actFpC;
} }
else else
@ -1102,7 +1102,7 @@ namespace cw
drp->oSignFl = signFl; drp->oSignFl = signFl;
drp->oSwapFl = swapFl; drp->oSwapFl = swapFl;
drp->oPcmH = pcmH; drp->oPcmH = pcmH;
drp->oBuf = memResizeZ<device::sample_t>( drp->oBuf, actFpC * drp->oChCnt ); drp->oBuf = mem::resizeZ<device::sample_t>( drp->oBuf, actFpC * drp->oChCnt );
drp->oFpC = actFpC; drp->oFpC = actFpC;
} }
@ -1131,8 +1131,8 @@ namespace cw
// if the pollfds[] needs more memroy // if the pollfds[] needs more memroy
if( p->pollfdsCnt + fdsCnt > p->pollfdsAllocCnt ) if( p->pollfdsCnt + fdsCnt > p->pollfdsAllocCnt )
{ {
p->pollfds = memResizeZ<struct pollfd>( p->pollfds, p->pollfdsCnt + fdsCnt ); p->pollfds = mem::resizeZ<struct pollfd>( p->pollfds, p->pollfdsCnt + fdsCnt );
p->pollfdsDesc = memResizeZ<pollfdsDesc_t>( p->pollfdsDesc, p->pollfdsCnt + fdsCnt ); p->pollfdsDesc = mem::resizeZ<pollfdsDesc_t>( p->pollfdsDesc, p->pollfdsCnt + fdsCnt );
p->pollfdsAllocCnt += fdsCnt; p->pollfdsAllocCnt += fdsCnt;
} }
} }
@ -1174,7 +1174,7 @@ cw::rc_t cw::audio::device::alsa::create( handle_t& hRef, struct driver_str*& dr
if((rc = destroy(hRef)) != kOkRC ) if((rc = destroy(hRef)) != kOkRC )
return rc; return rc;
alsa_t* p = memAllocZ<alsa_t>(); alsa_t* p = mem::allocZ<alsa_t>();
// for each sound card // for each sound card
while(1) while(1)
@ -1277,8 +1277,8 @@ cw::rc_t cw::audio::device::alsa::create( handle_t& hRef, struct driver_str*& dr
continue; continue;
// form the device name and desc. string // form the device name and desc. string
dr.nameStr = memPrintf(dr.nameStr,"hw:%i,%i,%i",cardNum,devNum,i); dr.nameStr = mem::printf(dr.nameStr,"hw:%i,%i,%i",cardNum,devNum,i);
dr.descStr = memPrintf(dr.descStr,"%s %s",cardNamePtr,snd_pcm_info_get_name(info)); dr.descStr = mem::printf(dr.descStr,"%s %s",cardNamePtr,snd_pcm_info_get_name(info));
// attempt to open the sub-device // attempt to open the sub-device
if((err = _devOpen(&pcmH,dr.nameStr,inputFl)) < 0 ) if((err = _devOpen(&pcmH,dr.nameStr,inputFl)) < 0 )
@ -1320,8 +1320,8 @@ cw::rc_t cw::audio::device::alsa::create( handle_t& hRef, struct driver_str*& dr
_devAppend(p,&dr); _devAppend(p,&dr);
else else
{ {
memRelease(dr.nameStr); mem::release(dr.nameStr);
memRelease(dr.descStr); mem::release(dr.descStr);
} }
} // sub-dev loop } // sub-dev loop
@ -1343,8 +1343,8 @@ cw::rc_t cw::audio::device::alsa::create( handle_t& hRef, struct driver_str*& dr
{ {
p->pollfdsCnt = 0; p->pollfdsCnt = 0;
p->pollfdsAllocCnt = 2*p->devCnt; p->pollfdsAllocCnt = 2*p->devCnt;
p->pollfds = memAllocZ<struct pollfd>( p->pollfdsAllocCnt ); p->pollfds = mem::allocZ<struct pollfd>( p->pollfdsAllocCnt );
p->pollfdsDesc = memAllocZ<pollfdsDesc_t>(p->pollfdsAllocCnt ); p->pollfdsDesc = mem::allocZ<pollfdsDesc_t>(p->pollfdsAllocCnt );
if((rc = thread::create(p->thH,_threadFunc,p)) != kOkRC ) if((rc = thread::create(p->thH,_threadFunc,p)) != kOkRC )
{ {
@ -1448,8 +1448,7 @@ cw::rc_t cw::audio::device::alsa::deviceSetup( struct driver_str* drv, unsigned
drp->cbArg = cbArg; drp->cbArg = cbArg;
} }
return rc; return rc;
} }
cw::rc_t cw::audio::device::alsa::deviceStart( struct driver_str* drv, unsigned devIdx ) cw::rc_t cw::audio::device::alsa::deviceStart( struct driver_str* drv, unsigned devIdx )

View File

@ -34,229 +34,14 @@ namespace cw
unsigned outDevIdx; // output device index unsigned outDevIdx; // output device index
double srate; // audio sample rate double srate; // audio sample rate
unsigned meterMs; // audio meter buffer length unsigned meterMs; // audio meter buffer length
// param's and state for cmApSynthSine()
unsigned phase; // sine synth phase
double frqHz; // sine synth frequency in Hz
// buffer and state for cmApCopyIn/Out()
sample_t* buf; // buf[bufSmpCnt] - circular interleaved audio buffer
unsigned bufInIdx; // next input buffer index
unsigned bufOutIdx; // next output buffer index
unsigned bufFullCnt; // count of full samples
// debugging log data arrays
unsigned logCnt; // count of elements in log[] and ilong[]
char* log; // log[logCnt]
unsigned* ilog; // ilog[logCnt]
unsigned logIdx; // current log index
unsigned iCbCnt; // count the callback unsigned iCbCnt; // count the callback
unsigned oCbCnt; unsigned oCbCnt;
buf::handle_t audioBufH;
} cmApPortTestRecd; } cmApPortTestRecd;
#ifdef NOT_DEF
// The application can request any block of channels from the device. The packets are provided with the starting
// device channel and channel count. This function converts device channels and channel counts to buffer
// channel indexes and counts.
//
// Example:
// input output
// i,n i n
// App: 0,4 0 1 2 3 -> 2 2
// Pkt 2,8 2 3 4 5 6 7 8 -> 0 2
//
// The return value is the count of application requested channels located in this packet.
//
// input: *appChIdxPtr and appChCnt describe a block of device channels requested by the application.
// *pktChIdxPtr and pktChCnt describe a block of device channels provided to the application
//
// output:*appChIdxPtr and <return value> describe a block of app buffer channels which will send/recv samples.
// *pktChIdxPtr and <return value> describe a block of pkt buffer channels which will send/recv samples
//
unsigned _cmApDeviceToBuffer( unsigned* appChIdxPtr, unsigned appChCnt, unsigned* pktChIdxPtr, unsigned pktChCnt )
{
unsigned abi = *appChIdxPtr;
unsigned aei = abi+appChCnt-1;
unsigned pbi = *pktChIdxPtr;
unsigned pei = pbi+pktChCnt-1;
// if the ch's rqstd by the app do not overlap with this packet - return false.
if( aei < pbi || abi > pei )
return 0;
// if the ch's rqstd by the app overlap with the beginning of the pkt channel block
if( abi < pbi )
{
appChCnt -= pbi - abi;
*appChIdxPtr = pbi - abi;
*pktChIdxPtr = 0;
}
else
{
// the rqstd ch's begin inside the pkt channel block
pktChCnt -= abi - pbi;
*pktChIdxPtr = abi - pbi;
*appChIdxPtr = 0;
}
// if the pkt channels extend beyond the rqstd ch block
if( aei < pei )
pktChCnt -= pei - aei;
else
appChCnt -= aei - pei; // the rqstd ch's extend beyond or coincide with the pkt block
// the returned channel count must always be the same for both the rqstd and pkt
return cmMin(appChCnt,pktChCnt);
}
// synthesize a sine signal into an interleaved audio buffer
unsigned _cmApSynthSine( cmApPortTestRecd* r, float* p, unsigned chIdx, unsigned chCnt, unsigned frmCnt, unsigned phs, double hz )
{
long ph = 0;
unsigned i;
unsigned bufIdx = r->chIdx;
unsigned bufChCnt;
if( (bufChCnt = _cmApDeviceToBuffer( &bufIdx, r->chCnt, &chIdx, chCnt )) == 0)
return phs;
//if( r->cbCnt < 50 )
// printf("ch:%i cnt:%i ch:%i cnt:%i bi:%i bcn:%i\n",r->chIdx,r->chCnt,chIdx,chCnt,bufIdx,bufChCnt);
for(i=bufIdx; i<bufIdx+bufChCnt; ++i)
{
unsigned j;
float* op = p + i;
ph = phs;
for(j=0; j<frmCnt; j++, op+=chCnt, ph++)
{
*op = (float)(0.9 * sin( 2.0 * M_PI * hz * ph / r->srate ));
}
}
return ph;
}
// Copy the audio samples in the interleaved audio buffer sp[srcChCnt*srcFrameCnt]
// to the internal record buffer.
void _cmApCopyIn( cmApPortTestRecd* r, const sample_t* sp, unsigned srcChIdx, unsigned srcChCnt, unsigned srcFrameCnt )
{
unsigned i,j;
unsigned chCnt = cmMin(r->chCnt,srcChCnt);
for(i=0; i<srcFrameCnt; ++i)
{
for(j=0; j<chCnt; ++j)
r->buf[ r->bufInIdx + j ] = sp[ (i*srcChCnt) + j ];
for(; j<r->chCnt; ++j)
r->buf[ r->bufInIdx + j ] = 0;
r->bufInIdx = (r->bufInIdx+r->chCnt) % r->bufFrmCnt;
}
//r->bufFullCnt = (r->bufFullCnt + srcFrameCnt) % r->bufFrmCnt;
r->bufFullCnt += srcFrameCnt;
}
// Copy audio samples out of the internal record buffer into dp[dstChCnt*dstFrameCnt].
void _cmApCopyOut( cmApPortTestRecd* r, sample_t* dp, unsigned dstChIdx, unsigned dstChCnt, unsigned dstFrameCnt )
{
// if there are not enough samples available to fill the destination buffer then zero the dst buf.
if( r->bufFullCnt < dstFrameCnt )
{
printf("Empty Output Buffer\n");
memset( dp, 0, dstFrameCnt*dstChCnt*sizeof(sample_t) );
}
else
{
unsigned i,j;
unsigned chCnt = cmMin(dstChCnt, r->chCnt);
// for each output frame
for(i=0; i<dstFrameCnt; ++i)
{
// copy the first chCnt samples from the internal buf to the output buf
for(j=0; j<chCnt; ++j)
dp[ (i*dstChCnt) + j ] = r->buf[ r->bufOutIdx + j ];
// zero any output ch's for which there is no internal buf channel
for(; j<dstChCnt; ++j)
dp[ (i*dstChCnt) + j ] = 0;
// advance the internal buffer
r->bufOutIdx = (r->bufOutIdx + r->chCnt) % r->bufFrmCnt;
}
r->bufFullCnt -= dstFrameCnt;
}
}
// Audio port callback function called from the audio device thread.
void _cmApPortCb( cmApAudioPacket_t* inPktArray, unsigned inPktCnt, cmApAudioPacket_t* outPktArray, unsigned outPktCnt )
{
unsigned i;
// for each incoming audio packet
for(i=0; i<inPktCnt; ++i)
{
cmApPortTestRecd* r = (cmApPortTestRecd*)inPktArray[i].userCbPtr;
if( inPktArray[i].devIdx == r->inDevIdx )
{
// copy the incoming audio into an internal buffer where it can be picked up by _cpApCopyOut().
_cmApCopyIn( r, (sample_t*)inPktArray[i].audioBytesPtr, inPktArray[i].begChIdx, inPktArray[i].chCnt, inPktArray[i].audioFramesCnt );
}
++r->iCbCnt;
//printf("i %4i in:%4i out:%4i\n",r->bufFullCnt,r->bufInIdx,r->bufOutIdx);
}
unsigned hold_phase = 0;
// for each outgoing audio packet
for(i=0; i<outPktCnt; ++i)
{
cmApPortTestRecd* r = (cmApPortTestRecd*)outPktArray[i].userCbPtr;
if( outPktArray[i].devIdx == r->outDevIdx )
{
// zero the output buffer
memset(outPktArray[i].audioBytesPtr,0,outPktArray[i].chCnt * outPktArray[i].audioFramesCnt * sizeof(sample_t) );
// if the synth is enabled
if( r->synthFl )
{
unsigned tmp_phase = _cmApSynthSine( r, outPktArray[i].audioBytesPtr, outPktArray[i].begChIdx, outPktArray[i].chCnt, outPktArray[i].audioFramesCnt, r->phase, r->frqHz );
// the phase will only change on packets that are actually used
if( tmp_phase != r->phase )
hold_phase = tmp_phase;
}
else
{
// copy the any audio in the internal record buffer to the playback device
_cmApCopyOut( r, (sample_t*)outPktArray[i].audioBytesPtr, outPktArray[i].begChIdx, outPktArray[i].chCnt, outPktArray[i].audioFramesCnt );
}
}
r->phase = hold_phase;
//printf("o %4i in:%4i out:%4i\n",r->bufFullCnt,r->bufInIdx,r->bufOutIdx);
// count callbacks
++r->oCbCnt;
}
}
#endif
// print the usage message for cmAudioPortTest.c // print the usage message for cmAudioPortTest.c
void _cmApPrintUsage() void _cmApPrintUsage()
@ -302,17 +87,19 @@ namespace cw
unsigned _cmGlobalInDevIdx = 0; unsigned _cmGlobalInDevIdx = 0;
unsigned _cmGlobalOutDevIdx = 0; unsigned _cmGlobalOutDevIdx = 0;
void _cmApPortCb2( audioPacket_t* inPktArray, unsigned inPktCnt, audioPacket_t* outPktArray, unsigned outPktCnt ) void _cmApPortCb2( void* arg, audioPacket_t* inPktArray, unsigned inPktCnt, audioPacket_t* outPktArray, unsigned outPktCnt )
{ {
cmApPortTestRecd* p = static_cast<cmApPortTestRecd*>(arg);
for(unsigned i=0; i<inPktCnt; ++i) for(unsigned i=0; i<inPktCnt; ++i)
static_cast<cmApPortTestRecd*>(inPktArray[i].cbArg)->iCbCnt++; static_cast<cmApPortTestRecd*>(inPktArray[i].cbArg)->iCbCnt++;
for(unsigned i=0; i<outPktCnt; ++i) for(unsigned i=0; i<outPktCnt; ++i)
static_cast<cmApPortTestRecd*>(outPktArray[i].cbArg)->oCbCnt++; static_cast<cmApPortTestRecd*>(outPktArray[i].cbArg)->oCbCnt++;
buf::inputToOutput( _cmGlobalInDevIdx, _cmGlobalOutDevIdx ); buf::inputToOutput( p->audioBufH, _cmGlobalInDevIdx, _cmGlobalOutDevIdx );
buf::update( inPktArray, inPktCnt, outPktArray, outPktCnt ); buf::update( p->audioBufH, inPktArray, inPktCnt, outPktArray, outPktCnt );
} }
} }
} }
@ -341,25 +128,11 @@ cw::rc_t cw::audio::device::test( int argc, const char** argv )
r.framesPerCycle = _cmApGetOpt(argc,argv,"-f",512); r.framesPerCycle = _cmApGetOpt(argc,argv,"-f",512);
//r.bufFrmCnt = (r.bufCnt*r.framesPerCycle); //r.bufFrmCnt = (r.bufCnt*r.framesPerCycle);
//r.bufSmpCnt = (r.chCnt * r.bufFrmCnt); //r.bufSmpCnt = (r.chCnt * r.bufFrmCnt);
r.logCnt = 100; //r.logCnt = 100;
r.meterMs = 50; r.meterMs = 50;
sample_t buf[r.bufSmpCnt];
char log[r.logCnt];
unsigned ilog[r.logCnt];
r.inDevIdx = _cmGlobalInDevIdx = _cmApGetOpt(argc,argv,"-i",0); r.inDevIdx = _cmGlobalInDevIdx = _cmApGetOpt(argc,argv,"-i",0);
r.outDevIdx = _cmGlobalOutDevIdx = _cmApGetOpt(argc,argv,"-o",0); r.outDevIdx = _cmGlobalOutDevIdx = _cmApGetOpt(argc,argv,"-o",0);
r.phase = 0;
r.frqHz = 2000;
r.bufInIdx = 0;
r.bufOutIdx = 0;
r.bufFullCnt = 0;
r.logIdx = 0;
r.buf = buf;
r.log = log;
r.ilog = ilog;
r.iCbCnt = 0; r.iCbCnt = 0;
r.oCbCnt = 0; r.oCbCnt = 0;
@ -399,14 +172,14 @@ cw::rc_t cw::audio::device::test( int argc, const char** argv )
if( runFl ) if( runFl )
{ {
// initialize the audio bufer // initialize the audio bufer
buf::initialize( deviceCount(h), r.meterMs ); buf::create( r.audioBufH, deviceCount(h), r.meterMs );
// setup the buffer for the output device // setup the buffer for the output device
buf::setup( r.outDevIdx, r.srate, r.framesPerCycle, r.bufCnt, deviceChannelCount(h,r.outDevIdx,true), r.framesPerCycle, deviceChannelCount(h,r.outDevIdx,false), r.framesPerCycle ); buf::setup( r.audioBufH, r.outDevIdx, r.srate, r.framesPerCycle, r.bufCnt, deviceChannelCount(h,r.outDevIdx,true), r.framesPerCycle, deviceChannelCount(h,r.outDevIdx,false), r.framesPerCycle );
// setup the buffer for the input device // setup the buffer for the input device
//if( r.inDevIdx != r.outDevIdx ) //if( r.inDevIdx != r.outDevIdx )
buf::setup( r.inDevIdx, r.srate, r.framesPerCycle, r.bufCnt, deviceChannelCount(h,r.inDevIdx,true), r.framesPerCycle, deviceChannelCount(h,r.inDevIdx,false), r.framesPerCycle ); buf::setup( r.audioBufH, r.inDevIdx, r.srate, r.framesPerCycle, r.bufCnt, deviceChannelCount(h,r.inDevIdx,true), r.framesPerCycle, deviceChannelCount(h,r.inDevIdx,false), r.framesPerCycle );
// setup an output device // setup an output device
if(deviceSetup(h, r.outDevIdx,r.srate,r.framesPerCycle,_cmApPortCb2,&r) != kOkRC ) if(deviceSetup(h, r.outDevIdx,r.srate,r.framesPerCycle,_cmApPortCb2,&r) != kOkRC )
@ -429,8 +202,8 @@ cw::rc_t cw::audio::device::test( int argc, const char** argv )
cwLogInfo("q=quit O/o output tone, I/i input tone, P/p pass M/m meter s=buf report"); cwLogInfo("q=quit O/o output tone, I/i input tone, P/p pass M/m meter s=buf report");
//buf::enableTone( r.outDevIdx,-1,buf::kOutFl | buf::kEnableFl); // turn on the meters
//buf::enableMeter(r.outDevIdx,-1,buf::kOutFl | buf::kEnableFl); buf::enableMeter(r.audioBufH, r.outDevIdx,-1,buf::kOutFl | buf::kEnableFl);
char c; char c;
while((c=getchar()) != 'q') while((c=getchar()) != 'q')
@ -441,27 +214,27 @@ cw::rc_t cw::audio::device::test( int argc, const char** argv )
{ {
case 'i': case 'i':
case 'I': case 'I':
buf::enableTone(r.inDevIdx,-1,buf::kInFl | (c=='I'?buf::kEnableFl:0)); buf::enableTone(r.audioBufH, r.inDevIdx,-1,buf::kInFl | (c=='I'?buf::kEnableFl:0));
break; break;
case 'o': case 'o':
case 'O': case 'O':
buf::enableTone(r.outDevIdx,-1,buf::kOutFl | (c=='O'?buf::kEnableFl:0)); buf::enableTone(r.audioBufH, r.outDevIdx,-1,buf::kOutFl | (c=='O'?buf::kEnableFl:0));
break; break;
case 'p': case 'p':
case 'P': case 'P':
buf::enablePass(r.outDevIdx,-1,buf::kOutFl | (c=='P'?buf::kEnableFl:0)); buf::enablePass(r.audioBufH, r.outDevIdx,-1,buf::kOutFl | (c=='P'?buf::kEnableFl:0));
break; break;
case 'M': case 'M':
case 'm': case 'm':
buf::enableMeter( r.inDevIdx, -1, buf::kInFl | (c=='M'?buf::kEnableFl:0)); buf::enableMeter( r.audioBufH, r.inDevIdx, -1, buf::kInFl | (c=='M'?buf::kEnableFl:0));
buf::enableMeter( r.outDevIdx, -1, buf::kOutFl | (c=='M'?buf::kEnableFl:0)); buf::enableMeter( r.audioBufH, r.outDevIdx, -1, buf::kOutFl | (c=='M'?buf::kEnableFl:0));
break; break;
case 's': case 's':
buf::report(); buf::report(r.audioBufH);
break; break;
} }
@ -486,7 +259,7 @@ cw::rc_t cw::audio::device::test( int argc, const char** argv )
// release any resources held by the audio port interface // release any resources held by the audio port interface
rc_t rc1 = destroy(h); rc_t rc1 = destroy(h);
rc_t rc2 = buf::finalize(); rc_t rc2 = buf::destroy(r.audioBufH);
//cmApNrtFree(); //cmApNrtFree();
//cmApFileFree(); //cmApFileFree();

View File

@ -43,7 +43,7 @@ namespace cw
} }
// allocate the read target buffer // allocate the read target buffer
if((buf = memAlloc<char>(n+nn)) == nullptr) if((buf = mem::alloc<char>(n+nn)) == nullptr)
{ {
cwLogError(kMemAllocFailRC,"Read buffer allocation failed."); cwLogError(kMemAllocFailRC,"Read buffer allocation failed.");
goto errLabel; goto errLabel;
@ -65,7 +65,7 @@ namespace cw
if( bufByteCntPtr != nullptr ) if( bufByteCntPtr != nullptr )
*bufByteCntPtr = 0; *bufByteCntPtr = 0;
memRelease(buf); mem::release(buf);
return nullptr; return nullptr;
@ -157,7 +157,7 @@ cw::rc_t cw::file::open( handle_t& hRef, const char* fn, unsigned flags )
unsigned byteCnt = sizeof(this_t) + strlen(fn) + 1; unsigned byteCnt = sizeof(this_t) + strlen(fn) + 1;
if((p = memAllocZ<this_t>(byteCnt)) == nullptr ) if((p = mem::allocZ<this_t>(byteCnt)) == nullptr )
return cwLogError(kOpFailRC,"File object allocation failed for file '%s'.",cwStringNullGuard(fn)); return cwLogError(kOpFailRC,"File object allocation failed for file '%s'.",cwStringNullGuard(fn));
p->fnStr = (char*)(p+1); p->fnStr = (char*)(p+1);
@ -171,7 +171,7 @@ cw::rc_t cw::file::open( handle_t& hRef, const char* fn, unsigned flags )
if((p->fp = fopen(fn,mode)) == nullptr ) if((p->fp = fopen(fn,mode)) == nullptr )
{ {
rc_t rc = cwLogSysError(kOpenFailRC,errno,"File open failed on file:'%s'.",cwStringNullGuard(fn)); rc_t rc = cwLogSysError(kOpenFailRC,errno,"File open failed on file:'%s'.",cwStringNullGuard(fn));
memRelease(p); mem::release(p);
return rc; return rc;
} }
} }
@ -193,7 +193,7 @@ cw::rc_t cw::file::close( handle_t& hRef )
if( fclose(p->fp) != 0 ) if( fclose(p->fp) != 0 )
return cwLogSysError(kCloseFailRC,errno,"File close failed on '%s'.", cwStringNullGuard(p->fnStr)); return cwLogSysError(kCloseFailRC,errno,"File close failed on '%s'.", cwStringNullGuard(p->fnStr));
memRelease(p); mem::release(p);
hRef.clear(); hRef.clear();
return kOkRC; return kOkRC;
@ -386,21 +386,21 @@ cw::rc_t cw::file::copy(
char* dstPathFn = nullptr; char* dstPathFn = nullptr;
// form the source path fn // form the source path fn
if((srcPathFn = fileSysMakeFn(srcDir,srcFn,srcExt,nullptr)) == nullptr ) if((srcPathFn = filesys::makeFn(srcDir,srcFn,srcExt,nullptr)) == nullptr )
{ {
rc = cwLogError(kOpFailRC,"The soure file name for dir:%s name:%s ext:%s could not be formed.",cwStringNullGuard(srcDir),cwStringNullGuard(srcFn),cwStringNullGuard(srcExt)); rc = cwLogError(kOpFailRC,"The soure file name for dir:%s name:%s ext:%s could not be formed.",cwStringNullGuard(srcDir),cwStringNullGuard(srcFn),cwStringNullGuard(srcExt));
goto errLabel; goto errLabel;
} }
// form the dest path fn // form the dest path fn
if((dstPathFn = fileSysMakeFn(dstDir,dstFn,dstExt,nullptr)) == nullptr ) if((dstPathFn = filesys::makeFn(dstDir,dstFn,dstExt,nullptr)) == nullptr )
{ {
rc = cwLogError(kOpFailRC,"The destination file name for dir:%s name:%s ext:%s could not be formed.",cwStringNullGuard(dstDir),cwStringNullGuard(dstFn),cwStringNullGuard(dstExt)); rc = cwLogError(kOpFailRC,"The destination file name for dir:%s name:%s ext:%s could not be formed.",cwStringNullGuard(dstDir),cwStringNullGuard(dstFn),cwStringNullGuard(dstExt));
goto errLabel; goto errLabel;
} }
// verify that the source exists // verify that the source exists
if( fileSysIsFile(srcPathFn) == false ) if( filesys::isFile(srcPathFn) == false )
{ {
rc = cwLogError(kOpenFailRC,"The source file '%s' does not exist.",cwStringNullGuard(srcPathFn)); rc = cwLogError(kOpenFailRC,"The source file '%s' does not exist.",cwStringNullGuard(srcPathFn));
goto errLabel; goto errLabel;
@ -418,9 +418,9 @@ cw::rc_t cw::file::copy(
errLabel: errLabel:
// free the buffer // free the buffer
memRelease(buf); mem::release(buf);
memRelease(srcPathFn); mem::release(srcPathFn);
memRelease(dstPathFn); mem::release(dstPathFn);
return rc; return rc;
} }
@ -432,21 +432,21 @@ cw::rc_t cw::file::backup( const char* dir, const char* name, const char* ext )
char* newFn = nullptr; char* newFn = nullptr;
unsigned n = 0; unsigned n = 0;
char* srcFn = nullptr; char* srcFn = nullptr;
fileSysPathPart_t* pp = nullptr; filesys::pathPart_t* pp = nullptr;
// form the name of the backup file // form the name of the backup file
if((srcFn = fileSysMakeFn(dir,name,ext,nullptr)) == nullptr ) if((srcFn = filesys::makeFn(dir,name,ext,nullptr)) == nullptr )
{ {
rc = cwLogError(kOpFailRC,"Backup source file name formation failed."); rc = cwLogError(kOpFailRC,"Backup source file name formation failed.");
goto errLabel; goto errLabel;
} }
// if the src file does not exist then there is nothing to do // if the src file does not exist then there is nothing to do
if( fileSysIsFile(srcFn) == false ) if( filesys::isFile(srcFn) == false )
return rc; return rc;
// break the source file name up into dir/fn/ext. // break the source file name up into dir/fn/ext.
if((pp = fileSysPathParts(srcFn)) == nullptr || pp->fnStr==nullptr) if((pp = filesys::pathParts(srcFn)) == nullptr || pp->fnStr==nullptr)
{ {
rc = cwLogError(kOpFailRC,"The file name '%s' could not be parsed into its parts.",cwStringNullGuard(srcFn)); rc = cwLogError(kOpFailRC,"The file name '%s' could not be parsed into its parts.",cwStringNullGuard(srcFn));
goto errLabel; goto errLabel;
@ -455,20 +455,20 @@ cw::rc_t cw::file::backup( const char* dir, const char* name, const char* ext )
// iterate until a unique file name is found // iterate until a unique file name is found
for(n=0; 1; ++n) for(n=0; 1; ++n)
{ {
memRelease(newFn); mem::release(newFn);
// generate a new file name // generate a new file name
newName = memPrintf(newName,"%s_%i",pp->fnStr,n); newName = mem::printf(newName,"%s_%i",pp->fnStr,n);
// form the new file name into a complete path // form the new file name into a complete path
if((newFn = fileSysMakeFn(pp->dirStr,newName,pp->extStr,nullptr)) == nullptr ) if((newFn = filesys::makeFn(pp->dirStr,newName,pp->extStr,nullptr)) == nullptr )
{ {
rc = cwLogError(kOpFailRC,"A backup file name could not be formed for the file '%s'.",cwStringNullGuard(newName)); rc = cwLogError(kOpFailRC,"A backup file name could not be formed for the file '%s'.",cwStringNullGuard(newName));
goto errLabel; goto errLabel;
} }
// if the new file name is not already in use ... // if the new file name is not already in use ...
if( fileSysIsFile(newFn) == false ) if( filesys::isFile(newFn) == false )
{ {
// .. then duplicate the file // .. then duplicate the file
if((rc = copy(srcFn,nullptr,nullptr,newFn,nullptr,nullptr)) != kOkRC ) if((rc = copy(srcFn,nullptr,nullptr,newFn,nullptr,nullptr)) != kOkRC )
@ -482,10 +482,10 @@ cw::rc_t cw::file::backup( const char* dir, const char* name, const char* ext )
errLabel: errLabel:
memRelease(srcFn); mem::release(srcFn);
memRelease(newFn); mem::release(newFn);
memRelease(newName); mem::release(newName);
memRelease(pp); mem::release(pp);
return rc; return rc;
@ -635,12 +635,12 @@ cw::rc_t cw::file::getLineAuto( handle_t h, char** bufPtrPtr, unsigned* bufByteC
break; break;
case kBufTooSmallRC: case kBufTooSmallRC:
buf = memResizeZ<char>(buf,*bufByteCntPtr); buf = mem::resizeZ<char>(buf,*bufByteCntPtr);
fl = true; fl = true;
break; break;
default: default:
memRelease(buf); mem::release(buf);
break; break;
} }
} }
@ -757,7 +757,7 @@ cw::rc_t cw::file::readStr( handle_t h, char** sRef, unsigned maxCharN )
} }
// allocate a read buffer // allocate a read buffer
char* s = memAllocZ<char>(n+1); char* s = mem::allocZ<char>(n+1);
// fill the buffer from the file // fill the buffer from the file
if((rc = readChar(h,s,n)) != kOkRC ) if((rc = readChar(h,s,n)) != kOkRC )

View File

@ -54,7 +54,7 @@ namespace cw
} }
} }
bool cw::fileSysIsDir( const char* dirStr ) bool cw::filesys::isDir( const char* dirStr )
{ {
struct stat s; struct stat s;
@ -73,7 +73,7 @@ bool cw::fileSysIsDir( const char* dirStr )
return S_ISDIR(s.st_mode); return S_ISDIR(s.st_mode);
} }
bool cw::fileSysIsFile( const char* fnStr ) bool cw::filesys::isFile( const char* fnStr )
{ {
struct stat s; struct stat s;
errno = 0; errno = 0;
@ -93,7 +93,7 @@ bool cw::fileSysIsFile( const char* fnStr )
} }
bool cw::fileSysIsLink( const char* fnStr ) bool cw::filesys::isLink( const char* fnStr )
{ {
struct stat s; struct stat s;
errno = 0; errno = 0;
@ -113,7 +113,7 @@ bool cw::fileSysIsLink( const char* fnStr )
char* cw::fileSysVMakeFn( const char* dir, const char* fn, const char* ext, va_list vl ) char* cw::filesys::vMakeFn( const char* dir, const char* fn, const char* ext, va_list vl )
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;
char* rp = nullptr; char* rp = nullptr;
@ -142,7 +142,7 @@ char* cw::fileSysVMakeFn( const char* dir, const char* fn, const char* ext, va_l
// add 1 for terminating zero and allocate memory // add 1 for terminating zero and allocate memory
if((rp = memAllocZ<char>( n+1 )) == nullptr ) if((rp = mem::allocZ<char>( n+1 )) == nullptr )
{ {
rc = cwLogError(kMemAllocFailRC,"Unable to allocate file name memory."); rc = cwLogError(kMemAllocFailRC,"Unable to allocate file name memory.");
goto errLabel; goto errLabel;
@ -187,31 +187,31 @@ char* cw::fileSysVMakeFn( const char* dir, const char* fn, const char* ext, va_l
errLabel: errLabel:
if( rc != kOkRC && rp != nullptr ) if( rc != kOkRC && rp != nullptr )
memRelease( rp ); mem::release( rp );
return rp; return rp;
} }
char* cw::fileSysMakeFn( const char* dir, const char* fn, const char* ext, ... ) char* cw::filesys::makeFn( const char* dir, const char* fn, const char* ext, ... )
{ {
va_list vl; va_list vl;
va_start(vl,ext); va_start(vl,ext);
char* fnOut = fileSysVMakeFn(dir,fn,ext,vl); char* fnOut = filesys::vMakeFn(dir,fn,ext,vl);
va_end(vl); va_end(vl);
return fnOut; return fnOut;
} }
cw::fileSysPathPart_t* cw::fileSysPathParts( const char* pathStr ) cw::filesys::pathPart_t* cw::filesys::pathParts( const char* pathStr )
{ {
unsigned n = 0; // char's in pathStr unsigned n = 0; // char's in pathStr
unsigned dn = 0; // char's in the dir part unsigned dn = 0; // char's in the dir part
unsigned fn = 0; // char's in the name part unsigned fn = 0; // char's in the name part
unsigned en = 0; // char's in the ext part unsigned en = 0; // char's in the ext part
char* cp = nullptr; char* cp = nullptr;
fileSysPathPart_t* rp = nullptr; pathPart_t* rp = nullptr;
if( pathStr==nullptr ) if( pathStr==nullptr )
@ -283,10 +283,10 @@ cw::fileSysPathPart_t* cw::fileSysPathParts( const char* pathStr )
dn = strlen(cp); dn = strlen(cp);
// get the total size of the returned memory. (add 3 for ecmh possible terminating zero) // get the total size of the returned memory. (add 3 for ecmh possible terminating zero)
n = sizeof(fileSysPathPart_t) + dn + fn + en + 3; n = sizeof(pathPart_t) + dn + fn + en + 3;
// alloc memory // alloc memory
if((rp = memAllocZ<fileSysPathPart_t>( n )) == nullptr ) if((rp = mem::allocZ<pathPart_t>( n )) == nullptr )
{ {
cwLogError( kMemAllocFailRC, "Unable to allocate the file system path part record for '%s'.",pathStr); cwLogError( kMemAllocFailRC, "Unable to allocate the file system path part record for '%s'.",pathStr);
return nullptr; return nullptr;

View File

@ -3,41 +3,43 @@
namespace cw namespace cw
{ {
namespace filesys
// Test the type of a file system object:
//
bool fileSysIsDir( const char* dirStr ); //< Return true if 'dirStr' refers to an existing directory.
bool fileSysIsFile( const char* fnStr ); //< Return true if 'fnStr' refers to an existing file.
bool fileSysIsLink( const char* fnStr ); //< Return true if 'fnStr' refers to a symbolic link.
// Create File Names:
//
// Create a file name by concatenating sub-strings.
//
// Variable arg's. entries are directories inserted between
// 'dirPrefixStr' and the file name.
// Terminate var arg's directory list with a nullptr.
//
// The returned string must be released by a call to memRelease() or memFree().
char* fileSysVMakeFn( const char* dir, const char* fn, const char* ext, va_list vl );
char* fileSysMakeFn( const char* dir, const char* fn, const char* ext, ... );
// Parse a path into its parts:
//
// Return record used by fileSysParts()
typedef struct
{ {
const char* dirStr;
const char* fnStr;
const char* extStr;
} fileSysPathPart_t;
// Given a file name decompose it into a directory string, file name string and file extension string. // Test the type of a file system object:
// The returned record and the strings it points to are contained in a single block of //
// memory which must be released by a call to memRelease() or memFree() bool isDir( const char* dirStr ); //< Return true if 'dirStr' refers to an existing directory.
fileSysPathPart_t* fileSysPathParts( const char* pathNameStr ); bool isFile( const char* fnStr ); //< Return true if 'fnStr' refers to an existing file.
bool isLink( const char* fnStr ); //< Return true if 'fnStr' refers to a symbolic link.
// Create File Names:
//
// Create a file name by concatenating sub-strings.
//
// Variable arg's. entries are directories inserted between
// 'dirPrefixStr' and the file name.
// Terminate var arg's directory list with a nullptr.
//
// The returned string must be released by a call to memRelease() or memFree().
char* vMakeFn( const char* dir, const char* fn, const char* ext, va_list vl );
char* makeFn( const char* dir, const char* fn, const char* ext, ... );
// Parse a path into its parts:
//
// Return record used by pathParts()
typedef struct
{
const char* dirStr;
const char* fnStr;
const char* extStr;
} pathPart_t;
// Given a file name decompose it into a directory string, file name string and file extension string.
// The returned record and the strings it points to are contained in a single block of
// memory which must be released by a call to memRelease() or memFree()
pathPart_t* pathParts( const char* pathNameStr );
}
} }

View File

@ -362,9 +362,9 @@ namespace cw
if( p->mfi == p->mfn ) if( p->mfi == p->mfn )
{ {
int incr_cnt = 10; int incr_cnt = 10;
lexMatcher* np = memAllocZ<lexMatcher>( p->mfn + incr_cnt ); lexMatcher* np = mem::allocZ<lexMatcher>( p->mfn + incr_cnt );
memcpy(np,p->mfp,p->mfi*sizeof(lexMatcher)); memcpy(np,p->mfp,p->mfi*sizeof(lexMatcher));
memRelease(p->mfp); mem::release(p->mfp);
p->mfp = np; p->mfp = np;
p->mfn += incr_cnt; p->mfn += incr_cnt;
} }
@ -378,7 +378,7 @@ namespace cw
if( keyStr != nullptr ) if( keyStr != nullptr )
{ {
// allocate space for the token string and store it // allocate space for the token string and store it
p->mfp[p->mfi].tokenStr = memDuplStr(keyStr); p->mfp[p->mfi].tokenStr = mem::duplStr(keyStr);
} }
@ -442,20 +442,20 @@ cw::rc_t cw::lex::create( handle_t& hRef, const char* cp, unsigned cn, unsigned
if((rc = lex::destroy(hRef)) != kOkRC ) if((rc = lex::destroy(hRef)) != kOkRC )
return rc; return rc;
p = memAllocZ<lex_t>(); p = mem::allocZ<lex_t>();
p->flags = flags; p->flags = flags;
_lexSetTextBuffer( p, cp, cn ); _lexSetTextBuffer( p, cp, cn );
int init_mfn = 10; int init_mfn = 10;
p->mfp = memAllocZ<lexMatcher>( init_mfn ); p->mfp = mem::allocZ<lexMatcher>( init_mfn );
p->mfn = init_mfn; p->mfn = init_mfn;
p->mfi = 0; p->mfi = 0;
p->lineCmtStr = memDuplStr( dfltLineCmt ); p->lineCmtStr = mem::duplStr( dfltLineCmt );
p->blockBegCmtStr = memDuplStr( dfltBlockBegCmt ); p->blockBegCmtStr = mem::duplStr( dfltBlockBegCmt );
p->blockEndCmtStr = memDuplStr( dfltBlockEndCmt ); p->blockEndCmtStr = mem::duplStr( dfltBlockEndCmt );
_lexInstallMatcher( p, kSpaceLexTId, _lexSpaceMatcher, nullptr, nullptr ); _lexInstallMatcher( p, kSpaceLexTId, _lexSpaceMatcher, nullptr, nullptr );
_lexInstallMatcher( p, kRealLexTId, _lexRealMatcher, nullptr, nullptr ); _lexInstallMatcher( p, kRealLexTId, _lexRealMatcher, nullptr, nullptr );
@ -493,21 +493,21 @@ cw::rc_t cw::lex::destroy( handle_t& hRef )
// free the user token strings // free the user token strings
for(; i<p->mfi; ++i) for(; i<p->mfi; ++i)
if( p->mfp[i].tokenStr != nullptr ) if( p->mfp[i].tokenStr != nullptr )
memRelease(p->mfp[i].tokenStr); mem::release(p->mfp[i].tokenStr);
// free the matcher array // free the matcher array
memRelease(p->mfp); mem::release(p->mfp);
p->mfi = 0; p->mfi = 0;
p->mfn = 0; p->mfn = 0;
} }
memRelease(p->lineCmtStr); mem::release(p->lineCmtStr);
memRelease(p->blockBegCmtStr); mem::release(p->blockBegCmtStr);
memRelease(p->blockEndCmtStr); mem::release(p->blockEndCmtStr);
memRelease(p->textBuf); mem::release(p->textBuf);
// free the lexer object // free the lexer object
memRelease(p); mem::release(p);
hRef.set(nullptr); hRef.set(nullptr);
} }
@ -556,7 +556,7 @@ cw::rc_t cw::lex::setFile( handle_t h, const char* fn )
return rc; return rc;
// allocate the text buffer // allocate the text buffer
if((p->textBuf = memResizeZ<char>(p->textBuf, n+1)) == nullptr ) if((p->textBuf = mem::resizeZ<char>(p->textBuf, n+1)) == nullptr )
{ {
rc = cwLogError(kMemAllocFailRC,"Unable to allocate the text file buffer for:'%s'.",fn); rc = cwLogError(kMemAllocFailRC,"Unable to allocate the text file buffer for:'%s'.",fn);
goto errLabel; goto errLabel;

View File

@ -43,7 +43,7 @@ cw::rc_t cw::log::create( handle_t& hRef, unsigned level, logOutputCbFunc_t out
if((rc = destroy(hRef)) != kOkRC) if((rc = destroy(hRef)) != kOkRC)
return rc; return rc;
log_t* p = memAllocZ<log_t>(); log_t* p = mem::allocZ<log_t>();
p->outCbFunc = outCbFunc == nullptr ? defaultOutput : outCbFunc; p->outCbFunc = outCbFunc == nullptr ? defaultOutput : outCbFunc;
p->outCbArg = outCbArg; p->outCbArg = outCbArg;
p->fmtCbFunc = fmtCbFunc == nullptr ? defaultFormatter : fmtCbFunc; p->fmtCbFunc = fmtCbFunc == nullptr ? defaultFormatter : fmtCbFunc;
@ -61,7 +61,7 @@ cw::rc_t cw::log::destroy( handle_t& hRef )
if( hRef.p == nullptr ) if( hRef.p == nullptr )
return rc; return rc;
memRelease( hRef.p ); mem::release( hRef.p );
return rc; return rc;
} }

View File

@ -4,10 +4,7 @@
#include "cwMem.h" #include "cwMem.h"
namespace cw void* cw::mem::_alloc( void* p0, unsigned n, bool zeroFl )
{
void* _memAlloc( void* p0, unsigned n, bool zeroFl )
{ {
void* p = nullptr; // ptr to new block void* p = nullptr; // ptr to new block
unsigned p0N = 0; // size of existing block unsigned p0N = 0; // size of existing block
@ -34,7 +31,7 @@ namespace cw
if( p0 != nullptr ) if( p0 != nullptr )
{ {
memcpy(p,p0_1,p0N); memcpy(p,p0_1,p0N);
memFree(p0); // free the existing block mem::free(p0); // free the existing block
} }
// if requested zero the block // if requested zero the block
@ -50,22 +47,22 @@ namespace cw
return p1+1; return p1+1;
} }
}
unsigned cw::memByteCount( const void* p )
unsigned cw::mem::byteCount( const void* p )
{ {
return p==nullptr ? 0 : static_cast<const unsigned*>(p)[-1]; return p==nullptr ? 0 : static_cast<const unsigned*>(p)[-1];
} }
char* cw::memAllocStr( const char* s ) char* cw::mem::allocStr( const char* s )
{ {
char* s1 = nullptr; char* s1 = nullptr;
if( s != nullptr ) if( s != nullptr )
{ {
unsigned sn = strlen(s); unsigned sn = strlen(s);
s1 = static_cast<char*>(_memAlloc(nullptr,sn+1,false)); s1 = static_cast<char*>(_alloc(nullptr,sn+1,false));
memcpy(s1,s,sn); memcpy(s1,s,sn);
s1[sn] = 0; s1[sn] = 0;
} }
@ -73,27 +70,27 @@ char* cw::memAllocStr( const char* s )
return s1; return s1;
} }
void* cw::memAllocDupl( const void* p0, unsigned byteN ) void* cw::mem::allocDupl( const void* p0, unsigned byteN )
{ {
if( p0 == nullptr || byteN == 0 ) if( p0 == nullptr || byteN == 0 )
return nullptr; return nullptr;
void* p1 = _memAlloc(nullptr,byteN,false); void* p1 = _alloc(nullptr,byteN,false);
memcpy(p1,p0,byteN); memcpy(p1,p0,byteN);
return p1; return p1;
} }
void* cw::memAllocDupl( const void* p ) void* cw::mem::allocDupl( const void* p )
{ {
return memAllocDupl(p,memByteCount(p)); return allocDupl(p,byteCount(p));
} }
void cw::memFree( void* p ) void cw::mem::free( void* p )
{ {
if( p != nullptr) if( p != nullptr)
{ {
free(static_cast<unsigned*>(p)-1); ::free(static_cast<unsigned*>(p)-1);
} }
} }

226
cwMem.h
View File

@ -4,144 +4,146 @@
namespace cw namespace cw
{ {
void* _memAlloc( void* p, unsigned n, bool zeroFl ); namespace mem
{
void* _alloc( void* p, unsigned n, bool zeroFl );
char* memAllocStr( const char* ); char* allocStr( const char* );
void* memAllocDupl( const void* p, unsigned byteN ); void* allocDupl( const void* p, unsigned byteN );
void* memAllocDupl( const void* p ); void* allocDupl( const void* p );
void memFree( void* ); void free( void* );
unsigned memByteCount( const void* p ); unsigned byteCount( const void* p );
template<typename T> template<typename T>
void memRelease(T& p) { memFree(p); p=nullptr; } void release(T& p) { ::cw::mem::free(p); p=nullptr; }
template<typename T> template<typename T>
T* memAllocZ(unsigned n=1) { return static_cast<T*>(_memAlloc(nullptr,n*sizeof(T),true)); } T* allocZ(unsigned n=1) { return static_cast<T*>(_alloc(nullptr,n*sizeof(T),true)); }
template<typename T> template<typename T>
T* memAlloc(unsigned n=1) { return static_cast<T*>(_memAlloc(nullptr,n*sizeof(T),false)); } T* alloc(unsigned n=1) { return static_cast<T*>(_alloc(nullptr,n*sizeof(T),false)); }
template<typename T> template<typename T>
T* memResizeZ(T* p, unsigned n=1) { return static_cast<T*>(_memAlloc(p,n*sizeof(T),true)); } T* resizeZ(T* p, unsigned n=1) { return static_cast<T*>(_alloc(p,n*sizeof(T),true)); }
template<typename T> template<typename T>
size_t _memTextLength(const T* s ) size_t _textLength(const T* s )
{
if( s == nullptr )
return 0;
// get length of source string
size_t n=0;
for(; s[n]; ++n)
{}
return n;
}
template<typename T>
T* memDuplStr( const T* s, size_t n )
{
if( s == nullptr )
return nullptr;
n+=1; // add one for terminating zero
// allocate space for new string
T* s1 = memAlloc<T>(n);
// copy in new string
for(size_t i=0; i<n-1; ++i)
s1[i] = s[i];
s1[n-1] = 0;
return s1;
}
template<typename T>
T* memDuplStr( const T* s )
{
if( s == nullptr )
return nullptr;
return memDuplStr(s,_memTextLength(s));
}
template<typename T>
T* memReallocStr( T* s0, const T* s1 )
{
if( s1 == nullptr )
{ {
memFree(s0); if( s == nullptr )
return nullptr; return 0;
// get length of source string
size_t n=0;
for(; s[n]; ++n)
{}
return n;
}
template<typename T>
T* duplStr( const T* s, size_t n )
{
if( s == nullptr )
return nullptr;
n+=1; // add one for terminating zero
// allocate space for new string
T* s1 = alloc<T>(n);
// copy in new string
for(size_t i=0; i<n-1; ++i)
s1[i] = s[i];
s1[n-1] = 0;
return s1;
} }
if( s0 == nullptr ) template<typename T>
return memDuplStr(s1); T* duplStr( const T* s )
size_t s0n = _memTextLength(s0);
size_t s1n = _memTextLength(s1);
// if s1[] can't fit in space of s0[]
if( s1n > s0n )
{ {
memFree(s0); if( s == nullptr )
return memDuplStr(s1); return nullptr;
return duplStr(s,_textLength(s));
} }
// s1n is <= s0n template<typename T>
// copy s1[] into s0[] T* reallocStr( T* s0, const T* s1 )
size_t i=0;
for(; s1[i]; ++i)
s0[i] = s1[i];
s0[i] = 0;
return s0;
}
template<typename C>
C* memPrintf(C* p0, const char* fmt, va_list vl0 )
{
va_list vl1;
va_copy(vl1,vl0);
size_t bufN = vsnprintf(nullptr,0,fmt,vl0);
if( bufN == 0)
{ {
memFree(p0); if( s1 == nullptr )
return nullptr; {
free(s0);
return nullptr;
}
if( s0 == nullptr )
return duplStr(s1);
size_t s0n = _textLength(s0);
size_t s1n = _textLength(s1);
// if s1[] can't fit in space of s0[]
if( s1n > s0n )
{
free(s0);
return duplStr(s1);
}
// s1n is <= s0n
// copy s1[] into s0[]
size_t i=0;
for(; s1[i]; ++i)
s0[i] = s1[i];
s0[i] = 0;
return s0;
} }
template<typename C>
C* printf(C* p0, const char* fmt, va_list vl0 )
{
va_list vl1;
va_copy(vl1,vl0);
C buf[ bufN + 1 ]; size_t bufN = vsnprintf(nullptr,0,fmt,vl0);
size_t n = vsnprintf(buf,bufN+1,fmt,vl1);
cwAssert(n <= bufN); if( bufN == 0)
{
free(p0);
return nullptr;
}
buf[bufN] = 0;
va_end(vl1); C buf[ bufN + 1 ];
size_t n = vsnprintf(buf,bufN+1,fmt,vl1);
return memReallocStr(p0,buf); cwAssert(n <= bufN);
}
buf[bufN] = 0;
va_end(vl1);
return reallocStr(p0,buf);
}
template<typename C> template<typename C>
C* memPrintf(C* p0, const char* fmt, ... ) C* printf(C* p0, const char* fmt, ... )
{ {
va_list vl; va_list vl;
va_start(vl,fmt); va_start(vl,fmt);
C* p1 = memPrintf(p0,fmt,vl); C* p1 = printf(p0,fmt,vl);
va_end(vl); va_end(vl);
return p1; return p1;
} }
}
} }

View File

@ -358,7 +358,7 @@ namespace cw
p->devCnt += 1; p->devCnt += 1;
// allocate the device array // allocate the device array
p->devArray = memAllocZ<dev_t>(p->devCnt); p->devArray = mem::allocZ<dev_t>(p->devCnt);
// fill in each device record // fill in each device record
snd_seq_client_info_set_client(cip, -1); snd_seq_client_info_set_client(cip, -1);
@ -370,7 +370,7 @@ namespace cw
const char* name = snd_seq_client_info_get_name(cip); const char* name = snd_seq_client_info_get_name(cip);
// initalize the device record // initalize the device record
p->devArray[i].nameStr = memDuplStr(cwStringNullGuard(name)); p->devArray[i].nameStr = mem::duplStr(cwStringNullGuard(name));
p->devArray[i].iPortCnt = 0; p->devArray[i].iPortCnt = 0;
p->devArray[i].oPortCnt = 0; p->devArray[i].oPortCnt = 0;
p->devArray[i].iPortArray = NULL; p->devArray[i].iPortArray = NULL;
@ -396,10 +396,10 @@ namespace cw
// allocate the device port arrays // allocate the device port arrays
if( p->devArray[i].iPortCnt > 0 ) if( p->devArray[i].iPortCnt > 0 )
p->devArray[i].iPortArray = memAllocZ<port_t>(p->devArray[i].iPortCnt); p->devArray[i].iPortArray = mem::allocZ<port_t>(p->devArray[i].iPortCnt);
if( p->devArray[i].oPortCnt > 0 ) if( p->devArray[i].oPortCnt > 0 )
p->devArray[i].oPortArray = memAllocZ<port_t>(p->devArray[i].oPortCnt); p->devArray[i].oPortArray = mem::allocZ<port_t>(p->devArray[i].oPortCnt);
snd_seq_port_info_set_client(pip,client); // set the ports client id snd_seq_port_info_set_client(pip,client); // set the ports client id
@ -417,7 +417,7 @@ namespace cw
{ {
assert(j<p->devArray[i].iPortCnt); assert(j<p->devArray[i].iPortCnt);
p->devArray[i].iPortArray[j].inputFl = true; p->devArray[i].iPortArray[j].inputFl = true;
p->devArray[i].iPortArray[j].nameStr = memDuplStr(cwStringNullGuard(port)); p->devArray[i].iPortArray[j].nameStr = mem::duplStr(cwStringNullGuard(port));
p->devArray[i].iPortArray[j].alsa_type = type; p->devArray[i].iPortArray[j].alsa_type = type;
p->devArray[i].iPortArray[j].alsa_cap = caps; p->devArray[i].iPortArray[j].alsa_cap = caps;
p->devArray[i].iPortArray[j].alsa_addr = addr; p->devArray[i].iPortArray[j].alsa_addr = addr;
@ -439,7 +439,7 @@ namespace cw
{ {
assert(k<p->devArray[i].oPortCnt); assert(k<p->devArray[i].oPortCnt);
p->devArray[i].oPortArray[k].inputFl = false; p->devArray[i].oPortArray[k].inputFl = false;
p->devArray[i].oPortArray[k].nameStr = memDuplStr(cwStringNullGuard(port)); p->devArray[i].oPortArray[k].nameStr = mem::duplStr(cwStringNullGuard(port));
p->devArray[i].oPortArray[k].alsa_type = type; p->devArray[i].oPortArray[k].alsa_type = type;
p->devArray[i].oPortArray[k].alsa_cap = caps; p->devArray[i].oPortArray[k].alsa_cap = caps;
p->devArray[i].oPortArray[k].alsa_addr = addr; p->devArray[i].oPortArray[k].alsa_addr = addr;
@ -513,7 +513,7 @@ cw::rc_t cw::midi::device::initialize( cbFunc_t cbFunc, void* cbArg, unsigned p
return rc; return rc;
// allocate the global root object // allocate the global root object
_cmMpRoot = p = memAllocZ<cmMpRoot_t>(1); _cmMpRoot = p = mem::allocZ<cmMpRoot_t>(1);
p->h = NULL; p->h = NULL;
p->alsa_queue = -1; p->alsa_queue = -1;
@ -541,7 +541,7 @@ cw::rc_t cw::midi::device::initialize( cbFunc_t cbFunc, void* cbArg, unsigned p
// allocate the file descriptors used for polling // allocate the file descriptors used for polling
p->alsa_fdCnt = snd_seq_poll_descriptors_count(p->h, POLLIN); p->alsa_fdCnt = snd_seq_poll_descriptors_count(p->h, POLLIN);
p->alsa_fd = memAllocZ<struct pollfd>(p->alsa_fdCnt); p->alsa_fd = mem::allocZ<struct pollfd>(p->alsa_fdCnt);
snd_seq_poll_descriptors(p->h, p->alsa_fd, p->alsa_fdCnt, POLLIN); snd_seq_poll_descriptors(p->h, p->alsa_fd, p->alsa_fdCnt, POLLIN);
p->cbFunc = cbFunc; p->cbFunc = cbFunc;
@ -620,25 +620,25 @@ cw::rc_t cw::midi::device::finalize()
for(j=0; j<p->devArray[i].iPortCnt; ++j) for(j=0; j<p->devArray[i].iPortCnt; ++j)
{ {
parser::destroy(p->devArray[i].iPortArray[j].parserH); parser::destroy(p->devArray[i].iPortArray[j].parserH);
memRelease( p->devArray[i].iPortArray[j].nameStr ); mem::release( p->devArray[i].iPortArray[j].nameStr );
} }
for(j=0; j<p->devArray[i].oPortCnt; ++j) for(j=0; j<p->devArray[i].oPortCnt; ++j)
{ {
memRelease( p->devArray[i].oPortArray[j].nameStr ); mem::release( p->devArray[i].oPortArray[j].nameStr );
} }
memRelease(p->devArray[i].iPortArray); mem::release(p->devArray[i].iPortArray);
memRelease(p->devArray[i].oPortArray); mem::release(p->devArray[i].oPortArray);
memRelease(p->devArray[i].nameStr); mem::release(p->devArray[i].nameStr);
} }
memRelease(p->devArray); mem::release(p->devArray);
memFree(p->alsa_fd); mem::free(p->alsa_fd);
memRelease(_cmMpRoot); mem::release(_cmMpRoot);
} }

View File

@ -144,17 +144,17 @@ namespace cw
void _destroy( parser_t* p ) void _destroy( parser_t* p )
{ {
memRelease(p->buf); mem::release(p->buf);
cbRecd_t* c = p->cbChain; cbRecd_t* c = p->cbChain;
while(c != NULL) while(c != NULL)
{ {
cbRecd_t* nc = c->linkPtr; cbRecd_t* nc = c->linkPtr;
memRelease(c); mem::release(c);
c = nc; c = nc;
} }
memRelease(p); mem::release(p);
} }
@ -165,7 +165,7 @@ namespace cw
cw::rc_t cw::midi::parser::create( handle_t& hRef, unsigned devIdx, unsigned portIdx, cbFunc_t cbFunc, void* cbDataPtr, unsigned bufByteCnt ) 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; rc_t rc = kOkRC;
parser_t* p = memAllocZ<parser_t>( 1 ); parser_t* p = mem::allocZ<parser_t>( 1 );
p->pkt.devIdx = devIdx; p->pkt.devIdx = devIdx;
@ -176,7 +176,7 @@ cw::rc_t cw::midi::parser::create( handle_t& hRef, unsigned devIdx, unsigned por
//p->cbChain->cbDataPtr = cbDataPtr; //p->cbChain->cbDataPtr = cbDataPtr;
//p->cbChain->linkPtr = NULL; //p->cbChain->linkPtr = NULL;
p->cbChain = NULL; p->cbChain = NULL;
p->buf = memAllocZ<byte_t>( bufByteCnt ); p->buf = mem::allocZ<byte_t>( bufByteCnt );
p->bufByteCnt = bufByteCnt; p->bufByteCnt = bufByteCnt;
p->bufIdx = 0; p->bufIdx = 0;
p->msgCnt = 0; p->msgCnt = 0;
@ -409,7 +409,7 @@ cw::rc_t cw::midi::parser::transmit( handle_t h )
cw::rc_t cw::midi::parser::installCallback( handle_t h, cbFunc_t cbFunc, void* cbDataPtr ) cw::rc_t cw::midi::parser::installCallback( handle_t h, cbFunc_t cbFunc, void* cbDataPtr )
{ {
parser_t* p = _handleToPtr(h); parser_t* p = _handleToPtr(h);
cbRecd_t* newCbPtr = memAllocZ<cbRecd_t>( 1 ); cbRecd_t* newCbPtr = mem::allocZ<cbRecd_t>( 1 );
cbRecd_t* c = p->cbChain; cbRecd_t* c = p->cbChain;
newCbPtr->cbFunc = cbFunc; newCbPtr->cbFunc = cbFunc;
@ -456,7 +456,7 @@ cw::rc_t cw::midi::parser::removeCallback( handle_t h, cbFunc_t cbFunc, vo
else else
c0->linkPtr = c1->linkPtr; c0->linkPtr = c1->linkPtr;
memRelease(c1); mem::release(c1);
return kOkRC; return kOkRC;
} }

View File

@ -18,13 +18,13 @@ namespace cw
MpScNbQueue() MpScNbQueue()
{ {
node_t* stub = memAllocZ<node_t>(); node_t* stub = mem::allocZ<node_t>();
_head = stub; _head = stub;
_tail = stub; _tail = stub;
} }
virtual ~MpScNbQueue() virtual ~MpScNbQueue()
{ memFree(_tail); } { mem::free(_tail); }
MpScNbQueue( const MpScNbQueue& ) = delete; MpScNbQueue( const MpScNbQueue& ) = delete;
MpScNbQueue( const MpScNbQueue&& ) = delete; MpScNbQueue( const MpScNbQueue&& ) = delete;
@ -34,7 +34,7 @@ namespace cw
void push( T* payload ) void push( T* payload )
{ {
node_t* new_node = memAllocZ<node_t>(1); node_t* new_node = mem::allocZ<node_t>(1);
new_node->payload = payload; new_node->payload = payload;
new_node->next.store(nullptr); new_node->next.store(nullptr);
node_t* prev = _head.exchange(new_node,std::memory_order_acq_rel); // append the new node to the list (aquire-release) node_t* prev = _head.exchange(new_node,std::memory_order_acq_rel); // append the new node to the list (aquire-release)
@ -51,7 +51,7 @@ namespace cw
{ {
_tail = next; _tail = next;
payload = next->payload; payload = next->payload;
memFree(t); mem::free(t);
} }
return payload; return payload;

View File

@ -45,11 +45,11 @@ namespace cw
void _objTypeFree( object_t* o ) void _objTypeFree( object_t* o )
{ memRelease(o); } { mem::release(o); }
void _objTypeFreeString( object_t* o ) void _objTypeFreeString( object_t* o )
{ {
memRelease( o->u.str ); mem::release( o->u.str );
_objTypeFree(o); _objTypeFree(o);
} }
@ -237,7 +237,7 @@ namespace cw
} }
} }
object_t* o = memAllocZ<object_t>(); object_t* o = mem::allocZ<object_t>();
o->type = type; o->type = type;
o->parent = parent; o->parent = parent;
return o; return o;
@ -482,7 +482,7 @@ cw::rc_t cw::objectFromString( const char* s, object_t*& objRef )
cnp = _objAppendLeftMostNode( cnp, _objAllocate( kPairTId, cnp )); cnp = _objAppendLeftMostNode( cnp, _objAllocate( kPairTId, cnp ));
char* v = memDuplStr(lex::tokenText(lexH),lex::tokenCharCount(lexH)); char* v = mem::duplStr(lex::tokenText(lexH),lex::tokenCharCount(lexH));
unsigned identFl = lexId == lex::kIdentLexTId ? kIdentFl : 0; unsigned identFl = lexId == lex::kIdentLexTId ? kIdentFl : 0;
@ -523,7 +523,7 @@ cw::rc_t cw::objectFromFile( const char* fn, object_t*& objRef )
if(( buf = file::fnToStr(fn, &bufByteCnt)) != NULL ) if(( buf = file::fnToStr(fn, &bufByteCnt)) != NULL )
{ {
rc = objectFromString( buf, objRef ); rc = objectFromString( buf, objRef );
memRelease(buf); mem::release(buf);
} }
return rc; return rc;

View File

@ -32,7 +32,7 @@ namespace cw
void _setClosedState( port_t* p ) void _setClosedState( port_t* p )
{ {
if( p->_deviceStr != nullptr ) if( p->_deviceStr != nullptr )
cw::memFree(const_cast<char*>(p->_deviceStr)); cw::mem::free(const_cast<char*>(p->_deviceStr));
p->_deviceH = -1; p->_deviceH = -1;
p->_deviceStr = nullptr; p->_deviceStr = nullptr;
@ -103,7 +103,7 @@ namespace cw
_setClosedState(p); _setClosedState(p);
} }
memRelease(p); mem::release(p);
errLabel: errLabel:
return rc; return rc;
@ -123,7 +123,7 @@ cw::rc_t cw::serialPort::create( handle_t& h, const char* deviceStr, unsigned ba
if((rc = destroy(h)) != kOkRC ) if((rc = destroy(h)) != kOkRC )
return rc; return rc;
port_t* p = memAllocZ<port_t>(); port_t* p = mem::allocZ<port_t>();
p->_deviceH = -1; p->_deviceH = -1;
@ -239,7 +239,7 @@ cw::rc_t cw::serialPort::create( handle_t& h, const char* deviceStr, unsigned ba
p->_pollfd.fd = p->_deviceH; p->_pollfd.fd = p->_deviceH;
p->_pollfd.events = POLLIN; p->_pollfd.events = POLLIN;
p->_deviceStr = cw::memAllocStr( deviceStr ); p->_deviceStr = cw::mem::allocStr( deviceStr );
p->_baudRate = baudRate; p->_baudRate = baudRate;
p->_cfgFlags = cfgFlags; p->_cfgFlags = cfgFlags;
p->_cbFunc = cbFunc; p->_cbFunc = cbFunc;

View File

@ -30,7 +30,7 @@ namespace cw
if((rc = thread::destroy(p->threadH)) != kOkRC ) if((rc = thread::destroy(p->threadH)) != kOkRC )
return rc; return rc;
memRelease(p); mem::release(p);
return rc; return rc;
} }
@ -55,7 +55,7 @@ cw::rc_t cw::serialPortSrv::create( handle_t& h, const char* deviceStr, unsigned
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;
this_t* p = memAllocZ<this_t>(); this_t* p = mem::allocZ<this_t>();
if((rc = serialPort::create( p->portH, deviceStr, baudRate, cfgFlags, cbFunc, cbArg )) != kOkRC ) if((rc = serialPort::create( p->portH, deviceStr, baudRate, cfgFlags, cbFunc, cbArg )) != kOkRC )
goto errLabel; goto errLabel;

View File

@ -56,7 +56,7 @@ namespace cw
p->sockH = cwSOCKET_NULL_SOCK; p->sockH = cwSOCKET_NULL_SOCK;
} }
memRelease(p); mem::release(p);
return kOkRC; return kOkRC;
} }
@ -122,7 +122,7 @@ cw::rc_t cw::net::socket::create(
if((rc = destroy(hRef)) != kOkRC ) if((rc = destroy(hRef)) != kOkRC )
return rc; return rc;
socket_t* p = memAllocZ<socket_t>(); socket_t* p = mem::allocZ<socket_t>();
p->sockH = cwSOCKET_NULL_SOCK; p->sockH = cwSOCKET_NULL_SOCK;
// get a handle to the socket // get a handle to the socket

View File

@ -35,8 +35,8 @@ namespace cw
if((rc = socket::destroy(p->sockH)) != kOkRC ) if((rc = socket::destroy(p->sockH)) != kOkRC )
return rc; return rc;
memRelease(p->recvBuf); mem::release(p->recvBuf);
memRelease(p); mem::release(p);
return rc; return rc;
} }
@ -72,7 +72,7 @@ cw::rc_t cw::net::srv::create(
if((rc = destroy(hRef)) != kOkRC ) if((rc = destroy(hRef)) != kOkRC )
return rc; return rc;
socksrv_t* p = memAllocZ<socksrv_t>(); socksrv_t* p = mem::allocZ<socksrv_t>();
if((rc = socket::create( p->sockH, port, socket::kNonBlockingFl, 0, remoteAddr, remotePort )) != kOkRC ) if((rc = socket::create( p->sockH, port, socket::kNonBlockingFl, 0, remoteAddr, remotePort )) != kOkRC )
goto errLabel; goto errLabel;
@ -80,7 +80,7 @@ cw::rc_t cw::net::srv::create(
if((rc = thread::create( p->threadH, _threadFunc, p )) != kOkRC ) if((rc = thread::create( p->threadH, _threadFunc, p )) != kOkRC )
goto errLabel; goto errLabel;
p->recvBuf = memAllocZ<char>( recvBufByteCnt ); p->recvBuf = mem::allocZ<char>( recvBufByteCnt );
p->recvBufByteCnt = recvBufByteCnt; p->recvBufByteCnt = recvBufByteCnt;
p->cbFunc = cbFunc; p->cbFunc = cbFunc;
p->cbArg = cbArg; p->cbArg = cbArg;

View File

@ -32,12 +32,12 @@ cw::rc_t cw::textBuf::create( handle_t& hRef, unsigned initCharN, unsigned expan
if((rc = destroy(hRef)) != kOkRC ) if((rc = destroy(hRef)) != kOkRC )
return rc; return rc;
this_t* p = memAllocZ<this_t>(); this_t* p = mem::allocZ<this_t>();
p->buf = memAllocZ<char>(initCharN); p->buf = mem::allocZ<char>(initCharN);
p->expandCharN = expandCharN; p->expandCharN = expandCharN;
p->allocCharN = initCharN; p->allocCharN = initCharN;
p->boolTrueText = memDuplStr("true"); p->boolTrueText = mem::duplStr("true");
p->boolFalseText = memDuplStr("false"); p->boolFalseText = mem::duplStr("false");
hRef.set(p); hRef.set(p);
return rc; return rc;
} }
@ -62,10 +62,10 @@ cw::rc_t cw::textBuf::destroy(handle_t& hRef )
this_t* p = _handleToPtr(hRef); this_t* p = _handleToPtr(hRef);
memRelease(p->buf); mem::release(p->buf);
memRelease(p->boolTrueText); mem::release(p->boolTrueText);
memRelease(p->boolFalseText); mem::release(p->boolFalseText);
memRelease(p); mem::release(p);
hRef.clear(); hRef.clear();
return rc; return rc;
} }
@ -100,7 +100,7 @@ cw::rc_t cw::textBuf::print( handle_t h, const char* fmt, va_list vl )
unsigned minExpandCharN = (p->endN + n) - p->allocCharN; unsigned minExpandCharN = (p->endN + n) - p->allocCharN;
unsigned expandCharN = std::max(minExpandCharN,p->expandCharN); unsigned expandCharN = std::max(minExpandCharN,p->expandCharN);
p->allocCharN += expandCharN; p->allocCharN += expandCharN;
p->buf = memResizeZ<char>( p->buf, p->allocCharN ); p->buf = mem::resizeZ<char>( p->buf, p->allocCharN );
} }
int m = vsnprintf(p->buf + p->endN, n, fmt, vl ); int m = vsnprintf(p->buf + p->endN, n, fmt, vl );
@ -142,9 +142,9 @@ cw::rc_t cw::textBuf::setBoolFormat( handle_t h, bool v, const char* s)
this_t* p = _handleToPtr(h); this_t* p = _handleToPtr(h);
if( v ) if( v )
p->boolTrueText = memReallocStr(p->boolTrueText,s); p->boolTrueText = mem::reallocStr(p->boolTrueText,s);
else else
p->boolFalseText = memReallocStr(p->boolFalseText,s); p->boolFalseText = mem::reallocStr(p->boolFalseText,s);
return kOkRC; return kOkRC;
} }

View File

@ -109,7 +109,7 @@ cw::rc_t cw::thread::create( handle_t& hRef, cbFunc_t func, void* funcArg, int s
if((rc = destroy(hRef)) != kOkRC ) if((rc = destroy(hRef)) != kOkRC )
return rc; return rc;
thread_t* p = memAllocZ<thread_t>(); thread_t* p = mem::allocZ<thread_t>();
p->func = func; p->func = func;
p->funcArg = funcArg; p->funcArg = funcArg;
@ -155,7 +155,7 @@ cw::rc_t cw::thread::destroy( handle_t& hRef )
if((rc = _waitForState(p,kExitedThId)) != kOkRC ) if((rc = _waitForState(p,kExitedThId)) != kOkRC )
return cwLogError(rc,"Thread timed out waiting for destroy."); return cwLogError(rc,"Thread timed out waiting for destroy.");
memRelease(p); mem::release(p);
hRef.clear(); hRef.clear();
return rc; return rc;

View File

@ -203,8 +203,8 @@ namespace cw
msg_t* t = m1->link; msg_t* t = m1->link;
memFree(m1->msg); mem::free(m1->msg);
memFree(m1); mem::free(m1);
m1 = t; m1 = t;
@ -232,8 +232,8 @@ namespace cw
while((m = p->_q->pop()) != nullptr) while((m = p->_q->pop()) != nullptr)
{ {
memFree(m->msg); mem::free(m->msg);
memFree(m); mem::free(m);
} }
delete p->_q; delete p->_q;
@ -241,7 +241,7 @@ namespace cw
for(int i=0; p->_protocolA!=nullptr and p->_protocolA[i].callback != nullptr; ++i) for(int i=0; p->_protocolA!=nullptr and p->_protocolA[i].callback != nullptr; ++i)
{ {
memFree(const_cast<char*>(p->_protocolA[i].name)); mem::free(const_cast<char*>(p->_protocolA[i].name));
// TODO: delete any msgs in the protocol state here // TODO: delete any msgs in the protocol state here
auto ps = static_cast<protocolState_t*>(p->_protocolA[i].user); auto ps = static_cast<protocolState_t*>(p->_protocolA[i].user);
@ -251,28 +251,28 @@ namespace cw
{ {
msg_t* tmp = m->link; msg_t* tmp = m->link;
memFree(m->msg); mem::free(m->msg);
memFree(m); mem::free(m);
m = tmp; m = tmp;
} }
memFree(ps); mem::free(ps);
} }
memRelease(p->_protocolA); mem::release(p->_protocolA);
p->_protocolN = 0; p->_protocolN = 0;
if( p->_mount != nullptr ) if( p->_mount != nullptr )
{ {
memFree(const_cast<char*>(p->_mount->origin)); mem::free(const_cast<char*>(p->_mount->origin));
memFree(const_cast<char*>(p->_mount->def)); mem::free(const_cast<char*>(p->_mount->def));
memRelease(p->_mount); mem::release(p->_mount);
} }
p->_nextSessionId = 0; p->_nextSessionId = 0;
p->_connSessionN = 0; p->_connSessionN = 0;
memRelease(p); mem::release(p);
return kOkRC; return kOkRC;
@ -296,21 +296,21 @@ cw::rc_t cw::websock::create(
if((rc = destroy(h)) != kOkRC ) if((rc = destroy(h)) != kOkRC )
return rc; return rc;
websock_t* p = memAllocZ<websock_t>(); websock_t* p = mem::allocZ<websock_t>();
int logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE; int logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE;
lws_set_log_level(logs, NULL); lws_set_log_level(logs, NULL);
// Allocate one extra record to act as the end-of-list sentinel. // Allocate one extra record to act as the end-of-list sentinel.
p->_protocolN = protocolN + 1; p->_protocolN = protocolN + 1;
p->_protocolA = memAllocZ<struct lws_protocols>(p->_protocolN); p->_protocolA = mem::allocZ<struct lws_protocols>(p->_protocolN);
// Setup the websocket internal protocol state array // Setup the websocket internal protocol state array
for(unsigned i=0; i<protocolN; ++i) for(unsigned i=0; i<protocolN; ++i)
{ {
// Allocate the application protocol state array where this application can keep protocol related info // Allocate the application protocol state array where this application can keep protocol related info
auto protocolState = memAllocZ<protocolState_t>(1); auto protocolState = mem::allocZ<protocolState_t>(1);
auto dummy = memAllocZ<msg_t>(1); auto dummy = mem::allocZ<msg_t>(1);
protocolState->thisPtr = p; protocolState->thisPtr = p;
protocolState->begMsg = dummy; protocolState->begMsg = dummy;
@ -318,7 +318,7 @@ cw::rc_t cw::websock::create(
// Setup the interal lws_protocols record // Setup the interal lws_protocols record
struct lws_protocols* pr = p->_protocolA + i; struct lws_protocols* pr = p->_protocolA + i;
pr->name = memAllocStr(protocolArgA[i].label); pr->name = mem::allocStr(protocolArgA[i].label);
pr->id = protocolArgA[i].id; pr->id = protocolArgA[i].id;
pr->rx_buffer_size = protocolArgA[i].rcvBufByteN; pr->rx_buffer_size = protocolArgA[i].rcvBufByteN;
pr->tx_packet_size = 0; //protocolArgA[i].xmtBufByteN; pr->tx_packet_size = 0; //protocolArgA[i].xmtBufByteN;
@ -328,11 +328,11 @@ cw::rc_t cw::websock::create(
} }
static const char* slash = {"/"}; static const char* slash = {"/"};
p->_mount = memAllocZ<struct lws_http_mount>(1); p->_mount = mem::allocZ<struct lws_http_mount>(1);
p->_mount->mountpoint = slash; p->_mount->mountpoint = slash;
p->_mount->mountpoint_len = strlen(slash); p->_mount->mountpoint_len = strlen(slash);
p->_mount->origin = memAllocStr(physRootDir); // physical directory assoc'd with http "/" p->_mount->origin = mem::allocStr(physRootDir); // physical directory assoc'd with http "/"
p->_mount->def = memAllocStr(dfltHtmlPageFn); p->_mount->def = mem::allocStr(dfltHtmlPageFn);
p->_mount->origin_protocol= LWSMPRO_FILE; p->_mount->origin_protocol= LWSMPRO_FILE;
memset(&info,0,sizeof(info)); memset(&info,0,sizeof(info));
@ -381,8 +381,8 @@ cw::rc_t cw::websock::send(handle_t h, unsigned protocolId, const void* msg, un
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;
msg_t* m = memAllocZ<msg_t>(1); msg_t* m = mem::allocZ<msg_t>(1);
m->msg = memAllocZ<unsigned char>(byteN); m->msg = mem::allocZ<unsigned char>(byteN);
memcpy(m->msg,msg,byteN); memcpy(m->msg,msg,byteN);
m->msgByteN = byteN; m->msgByteN = byteN;
m->protocolId = protocolId; m->protocolId = protocolId;
@ -442,10 +442,10 @@ cw::rc_t cw::websock::exec( handle_t h, unsigned timeOutMs )
// add the pre-padding bytes to the msg // add the pre-padding bytes to the msg
unsigned char* msg = memAllocZ<unsigned char>(LWS_PRE + m->msgByteN); unsigned char* msg = mem::allocZ<unsigned char>(LWS_PRE + m->msgByteN);
memcpy( msg+LWS_PRE, m->msg, m->msgByteN ); memcpy( msg+LWS_PRE, m->msg, m->msgByteN );
memFree(m->msg); // free the original msg buffer mem::free(m->msg); // free the original msg buffer
m->msg = msg; m->msg = msg;
m->msgId = ps->nextNewMsgId; // set the msg id m->msgId = ps->nextNewMsgId; // set the msg id

View File

@ -30,7 +30,7 @@ namespace cw
if((rc = websock::destroy(p->_websockH)) != kOkRC ) if((rc = websock::destroy(p->_websockH)) != kOkRC )
return rc; return rc;
memRelease(p); mem::release(p);
return rc; return rc;
} }
@ -60,7 +60,7 @@ cw::rc_t cw::websockSrv::create(
if((rc = destroy(h)) != kOkRC ) if((rc = destroy(h)) != kOkRC )
return rc; return rc;
websockSrv_t* p = memAllocZ<websockSrv_t>(); websockSrv_t* p = mem::allocZ<websockSrv_t>();
if((rc = websock::create( p->_websockH, cbFunc, cbArg, physRootDir, dfltHtmlPageFn, port, protocolA, protocolN )) != kOkRC ) if((rc = websock::create( p->_websockH, cbFunc, cbArg, physRootDir, dfltHtmlPageFn, port, protocolA, protocolN )) != kOkRC )
goto errLabel; goto errLabel;

View File

@ -72,18 +72,18 @@ void variadicTplTest( cw::object_t* cfg, int argc, const char* argv[] )
void fileSysTest( cw::object_t* cfg, int argc, const char* argv[] ) void fileSysTest( cw::object_t* cfg, int argc, const char* argv[] )
{ {
cw::fileSysPathPart_t* pp = cw::fileSysPathParts(__FILE__); cw::filesys::pathPart_t* pp = cw::filesys::pathParts(__FILE__);
cwLogInfo("dir:%s",pp->dirStr); cwLogInfo("dir:%s",pp->dirStr);
cwLogInfo("fn: %s",pp->fnStr); cwLogInfo("fn: %s",pp->fnStr);
cwLogInfo("ext:%s",pp->extStr); cwLogInfo("ext:%s",pp->extStr);
char* fn = cw::fileSysMakeFn( pp->dirStr, pp->fnStr, pp->extStr, nullptr ); char* fn = cw::filesys::makeFn( pp->dirStr, pp->fnStr, pp->extStr, nullptr );
cwLogInfo("fn: %s",fn); cwLogInfo("fn: %s",fn);
cw::memRelease(pp); cw::mem::release(pp);
cw::memRelease(fn); cw::mem::release(fn);
} }