2019-12-24 15:05:24 +00:00
|
|
|
#include "cwCommon.h"
|
|
|
|
#include "cwLog.h"
|
|
|
|
#include "cwCommonImpl.h"
|
|
|
|
#include "cwMem.h"
|
|
|
|
#include "cwTime.h"
|
|
|
|
#include "cwTextBuf.h"
|
2019-12-26 02:44:14 +00:00
|
|
|
#include "cwAudioDevice.h"
|
2019-12-24 15:05:24 +00:00
|
|
|
#include "cwAudioBuf.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
This API is called by two types of threads:
|
|
|
|
audio device threads and the client thread. There
|
|
|
|
may be multiple device threads, however, there is only
|
|
|
|
one client thread.
|
|
|
|
|
|
|
|
The audio device threads only call update().
|
|
|
|
update() is never called by any other threads.
|
|
|
|
A call from the audio update threads targets specific channels
|
|
|
|
(cmApCh records). The variables within each channels that
|
|
|
|
it modifies are confined to:
|
|
|
|
on input channels: increments ii and increments fn (data is entering the ch. buffers)
|
|
|
|
on output channels: increments oi and decrements fn (data is leaving the ch. buffers)
|
|
|
|
|
|
|
|
The client picks up incoming audio and provides outgoing audio via
|
|
|
|
get(). It then informs the buf that it has completed
|
|
|
|
the audio data transfer by calling advance().
|
|
|
|
|
|
|
|
advance() modifies the following internal variables:
|
|
|
|
on input channels: increments oi and decrements fn (data has left the ch buffer)
|
|
|
|
on output channels: increments ii and increments fn (data has enterned the ch. buffer)
|
|
|
|
|
|
|
|
Based on the above scenario the channel ii and oi variables are always thread-safe
|
|
|
|
because they are only changed by a single thread.
|
|
|
|
|
|
|
|
ii oi fn
|
|
|
|
------ ----- ----
|
|
|
|
input ch: audio client both
|
|
|
|
output ch: client audio both
|
|
|
|
|
|
|
|
The fn variable however is not thread-safe and therefore care must be taken as
|
|
|
|
to how it is read and updated.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2019-12-27 21:52:45 +00:00
|
|
|
#define atomicUIntIncr( vRef, dVal ) std::atomic_fetch_add<unsigned>(vRef,dVal)
|
|
|
|
#define atomicUIntDecr( vRef, dVal ) std::atomic_fetch_sub<unsigned>(vRef,dVal)
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
namespace cw
|
|
|
|
{
|
|
|
|
namespace audio
|
|
|
|
{
|
|
|
|
namespace buf
|
|
|
|
{
|
|
|
|
|
|
|
|
enum { kInApIdx=0, kOutApIdx=1, kIoApCnt=2 };
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
unsigned fl; // kChFl|kToneFl|kMeterFl ...
|
|
|
|
sample_t* b; // b[n]
|
|
|
|
unsigned ii; // next in
|
|
|
|
unsigned oi; // next out
|
|
|
|
std::atomic<unsigned> fn; // full cnt - count of samples currently in the buffer - incr'd by incoming, decr'd by outgoing
|
|
|
|
unsigned phs; // tone phase
|
|
|
|
double hz; // tone frequency
|
|
|
|
double gain; // channel gain
|
|
|
|
sample_t* m; // m[mn] meter sample sum
|
|
|
|
unsigned mn; // length of m[]
|
|
|
|
unsigned mi; // next ele of m[] to rcv sum
|
|
|
|
} cmApCh;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
unsigned chCnt;
|
|
|
|
cmApCh* chArray;
|
2019-12-28 02:51:28 +00:00
|
|
|
unsigned n; // length of b[] (multiple of dspFrameCnt) bufCnt*framesPerCycle
|
|
|
|
double srate; // device sample rate;
|
2019-12-24 15:05:24 +00:00
|
|
|
unsigned faultCnt;
|
|
|
|
unsigned framesPerCycle;
|
|
|
|
unsigned dspFrameCnt;
|
2019-12-28 02:51:28 +00:00
|
|
|
time::spec_t timeStamp; // base (starting) time stamp for this device
|
2019-12-24 15:05:24 +00:00
|
|
|
std::atomic<unsigned> ioFrameCnt; // count of frames input or output for this device
|
|
|
|
|
|
|
|
} cmApIO;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
// ioArray[] always contains 2 elements - one for input the other for output.
|
2019-12-28 02:51:28 +00:00
|
|
|
cmApIO ioArray[kIoApCnt];
|
2019-12-24 15:05:24 +00:00
|
|
|
} cmApDev;
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
typedef struct audioBuf_str
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
cmApDev* devArray;
|
|
|
|
unsigned devCnt;
|
|
|
|
unsigned meterMs;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2019-12-27 21:52:45 +00:00
|
|
|
sample_t* zeroBuf; // buffer of zeros
|
|
|
|
unsigned zeroBufCnt; // max of all dspFrameCnt for all devices.
|
2019-12-28 02:51:28 +00:00
|
|
|
} audioBuf_t;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
inline audioBuf_t* _handleToPtr( handle_t h ) { return handleToPtr<handle_t,audioBuf_t>(h); }
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
sample_t _cmApMeterValue( const cmApCh* cp )
|
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
double sum = 0;
|
2019-12-24 15:05:24 +00:00
|
|
|
unsigned i;
|
|
|
|
for(i=0; i<cp->mn; ++i)
|
2019-12-28 02:51:28 +00:00
|
|
|
sum += cp->m[i];
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
return cp->mn == 0 ? 0 : (sample_t)sqrt(sum/cp->mn);
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void _cmApSine( cmApCh* cp, sample_t* b0, unsigned n0, sample_t* b1, unsigned n1, unsigned stride, float srate )
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for(i=0; i<n0; ++i,++cp->phs)
|
|
|
|
b0[i*stride] = (float)(cp->gain * sin( 2.0 * M_PI * cp->hz * cp->phs / srate ));
|
|
|
|
|
|
|
|
for(i=0; i<n1; ++i,++cp->phs)
|
|
|
|
b1[i*stride] = (float)(cp->gain * sin( 2.0 * M_PI * cp->hz * cp->phs / srate ));
|
|
|
|
}
|
|
|
|
|
|
|
|
sample_t _cmApMeter( const sample_t* b, unsigned bn, unsigned stride )
|
|
|
|
{
|
|
|
|
const sample_t* ep = b + bn;
|
|
|
|
sample_t sum = 0;
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
for(; b<ep; b += stride)
|
|
|
|
sum += *b * *b;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
return sum / bn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _cmApChFinalize( cmApCh* chPtr )
|
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
mem::release( chPtr->b );
|
|
|
|
mem::release( chPtr->m );
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
// n = buf sample cnt mn=meter buf smp cnt
|
2019-12-24 15:05:24 +00:00
|
|
|
void _cmApChInitialize( cmApCh* chPtr, unsigned n, unsigned mn )
|
|
|
|
{
|
|
|
|
_cmApChFinalize(chPtr);
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
chPtr->b = n==0 ? NULL : mem::allocZ<sample_t>( n );
|
2019-12-24 15:05:24 +00:00
|
|
|
chPtr->ii = 0;
|
|
|
|
chPtr->oi = 0;
|
|
|
|
chPtr->fn = 0;
|
|
|
|
chPtr->fl = (n!=0 ? kChFl : 0);
|
|
|
|
chPtr->hz = 1000;
|
|
|
|
chPtr->gain = 1.0;
|
|
|
|
chPtr->mn = mn;
|
2019-12-28 02:51:28 +00:00
|
|
|
chPtr->m = mem::allocZ<sample_t>(mn);
|
2019-12-24 15:05:24 +00:00
|
|
|
chPtr->mi = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _cmApIoFinalize( cmApIO* ioPtr )
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
for(i=0; i<ioPtr->chCnt; ++i)
|
|
|
|
_cmApChFinalize( ioPtr->chArray + i );
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
mem::release(ioPtr->chArray);
|
2019-12-24 15:05:24 +00:00
|
|
|
ioPtr->chCnt = 0;
|
|
|
|
ioPtr->n = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _cmApIoInitialize( cmApIO* ioPtr, double srate, unsigned framesPerCycle, unsigned chCnt, unsigned n, unsigned meterBufN, unsigned dspFrameCnt )
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
_cmApIoFinalize(ioPtr);
|
|
|
|
|
|
|
|
n += (n % dspFrameCnt); // force buffer size to be a multiple of dspFrameCnt
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
ioPtr->chArray = chCnt==0 ? NULL : mem::allocZ<cmApCh>(chCnt );
|
2019-12-24 15:05:24 +00:00
|
|
|
ioPtr->chCnt = chCnt;
|
|
|
|
ioPtr->n = n;
|
|
|
|
ioPtr->faultCnt = 0;
|
|
|
|
ioPtr->framesPerCycle = framesPerCycle;
|
|
|
|
ioPtr->srate = srate;
|
|
|
|
ioPtr->dspFrameCnt = dspFrameCnt;
|
|
|
|
ioPtr->timeStamp.tv_sec = 0;
|
|
|
|
ioPtr->timeStamp.tv_nsec = 0;
|
|
|
|
ioPtr->ioFrameCnt = 0;
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
for(i = 0; i<chCnt; ++i )
|
2019-12-24 15:05:24 +00:00
|
|
|
_cmApChInitialize( ioPtr->chArray + i, n, meterBufN );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void _cmApDevFinalize( cmApDev* dp )
|
|
|
|
{
|
|
|
|
unsigned i;
|
2019-12-28 02:51:28 +00:00
|
|
|
for(i = 0; i<kIoApCnt; ++i)
|
2019-12-24 15:05:24 +00:00
|
|
|
_cmApIoFinalize( dp->ioArray+i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _cmApDevInitialize( cmApDev* dp, double srate, unsigned iFpC, unsigned iChCnt, unsigned iBufN, unsigned oFpC, unsigned oChCnt, unsigned oBufN, unsigned meterBufN, unsigned dspFrameCnt )
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
_cmApDevFinalize(dp);
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
for(i = 0; i<kIoApCnt; ++i)
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
|
|
|
unsigned chCnt = i==kInApIdx ? iChCnt : oChCnt;
|
|
|
|
unsigned bufN = i==kInApIdx ? iBufN : oBufN;
|
|
|
|
unsigned fpc = i==kInApIdx ? iFpC : oFpC;
|
|
|
|
_cmApIoInitialize( dp->ioArray+i, srate, fpc, chCnt, bufN, meterBufN, dspFrameCnt );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void _theBufCalcTimeStamp( double srate, const time::spec_t* baseTimeStamp, unsigned frmCnt, time::spec_t* retTimeStamp )
|
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
if( retTimeStamp == NULL )
|
2019-12-24 15:05:24 +00:00
|
|
|
return;
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
double secs = frmCnt / srate;
|
|
|
|
unsigned int_secs = floor(secs);
|
|
|
|
double frac_secs = secs - int_secs;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
retTimeStamp->tv_nsec = floor(baseTimeStamp->tv_nsec + frac_secs * 1000000000);
|
2019-12-28 02:51:28 +00:00
|
|
|
retTimeStamp->tv_sec = baseTimeStamp->tv_sec + int_secs;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
if( retTimeStamp->tv_nsec > 1000000000 )
|
|
|
|
{
|
|
|
|
retTimeStamp->tv_nsec -= 1000000000;
|
|
|
|
retTimeStamp->tv_sec += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
cw::rc_t cw::audio::buf::create( handle_t& hRef, unsigned devCnt, unsigned meterMs )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
|
|
|
rc_t rc;
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
if((rc = destroy(hRef)) != kOkRC )
|
2019-12-24 15:05:24 +00:00
|
|
|
return rc;
|
2019-12-28 02:51:28 +00:00
|
|
|
|
|
|
|
audioBuf_t* p = mem::allocZ<audioBuf_t>();
|
|
|
|
|
|
|
|
p->devArray = mem::allocZ<cmApDev>(devCnt );
|
|
|
|
p->devCnt = devCnt;
|
|
|
|
|
|
|
|
hRef.set(p);
|
|
|
|
|
|
|
|
setMeterMs(hRef,meterMs);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
return kOkRC;
|
|
|
|
}
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
cw::rc_t cw::audio::buf::destroy( handle_t& hRef )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
rc_t rc = kOkRC;
|
|
|
|
|
|
|
|
if( !hRef.isValid() )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
audioBuf_t* p = _handleToPtr(hRef);
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
unsigned i;
|
2019-12-28 02:51:28 +00:00
|
|
|
for(i=0; i<p->devCnt; ++i)
|
|
|
|
_cmApDevFinalize(p->devArray + i);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
mem::release( p->devArray );
|
|
|
|
mem::release( p->zeroBuf );
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
p->devCnt = 0;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2021-01-20 18:05:33 +00:00
|
|
|
mem::release( p );
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
return kOkRC;
|
|
|
|
}
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
cw::rc_t cw::audio::buf::setup(
|
|
|
|
handle_t h,
|
2019-12-24 15:05:24 +00:00
|
|
|
unsigned devIdx,
|
|
|
|
double srate,
|
|
|
|
unsigned dspFrameCnt,
|
|
|
|
unsigned bufCnt,
|
|
|
|
unsigned inChCnt,
|
|
|
|
unsigned inFramesPerCycle,
|
|
|
|
unsigned outChCnt,
|
|
|
|
unsigned outFramesPerCycle)
|
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
audioBuf_t* p = _handleToPtr(h);
|
|
|
|
cmApDev* devPtr = p->devArray + devIdx;
|
2019-12-24 15:05:24 +00:00
|
|
|
unsigned iBufN = bufCnt * inFramesPerCycle;
|
|
|
|
unsigned oBufN = bufCnt * outFramesPerCycle;
|
2019-12-28 02:51:28 +00:00
|
|
|
unsigned meterBufN = std::max(1.0,floor(srate * p->meterMs / (1000.0 * outFramesPerCycle)));
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
_cmApDevInitialize( devPtr, srate, inFramesPerCycle, inChCnt, iBufN, outFramesPerCycle, outChCnt, oBufN, meterBufN, dspFrameCnt );
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
if( inFramesPerCycle > p->zeroBufCnt || outFramesPerCycle > p->zeroBufCnt )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
p->zeroBufCnt = std::max(inFramesPerCycle,outFramesPerCycle);
|
|
|
|
p->zeroBuf = mem::resizeZ<sample_t>(p->zeroBuf,p->zeroBufCnt);
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return kOkRC;
|
|
|
|
}
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
cw::rc_t cw::audio::buf::primeOutput( handle_t h, unsigned devIdx, unsigned audioCycleCnt )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
audioBuf_t* p = _handleToPtr(h);
|
|
|
|
cmApIO* iop = p->devArray[devIdx].ioArray + kOutApIdx;
|
2019-12-24 15:05:24 +00:00
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for(i=0; i<iop->chCnt; ++i)
|
|
|
|
{
|
|
|
|
cmApCh* cp = iop->chArray + i;
|
|
|
|
unsigned bn = iop->n * sizeof(sample_t);
|
|
|
|
memset(cp->b,0,bn);
|
|
|
|
cp->oi = 0;
|
|
|
|
cp->ii = iop->framesPerCycle * audioCycleCnt;
|
|
|
|
cp->fn = iop->framesPerCycle * audioCycleCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return kOkRC;
|
|
|
|
}
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
void cw::audio::buf::onPortEnable( handle_t h, unsigned devIdx, bool enableFl )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
audioBuf_t* p = _handleToPtr(h);
|
2019-12-24 15:05:24 +00:00
|
|
|
if( devIdx == kInvalidIdx || enableFl==false)
|
|
|
|
return;
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
cmApIO* iop = p->devArray[devIdx].ioArray + kOutApIdx;
|
2019-12-24 15:05:24 +00:00
|
|
|
iop->timeStamp.tv_sec = 0;
|
|
|
|
iop->timeStamp.tv_nsec = 0;
|
|
|
|
iop->ioFrameCnt = 0;
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
iop = p->devArray[devIdx].ioArray + kInApIdx;
|
2019-12-24 15:05:24 +00:00
|
|
|
iop->timeStamp.tv_sec = 0;
|
|
|
|
iop->timeStamp.tv_nsec = 0;
|
|
|
|
iop->ioFrameCnt = 0;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
cw::rc_t cw::audio::buf::update(
|
2019-12-28 02:51:28 +00:00
|
|
|
handle_t h,
|
2019-12-24 15:05:24 +00:00
|
|
|
device::audioPacket_t* inPktArray,
|
|
|
|
unsigned inPktCnt,
|
|
|
|
device::audioPacket_t* outPktArray,
|
|
|
|
unsigned outPktCnt )
|
|
|
|
{
|
|
|
|
unsigned i,j;
|
2019-12-28 02:51:28 +00:00
|
|
|
audioBuf_t* p = _handleToPtr(h);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
// copy samples from the packet to the buffer
|
|
|
|
if( inPktArray != NULL )
|
|
|
|
{
|
|
|
|
for(i=0; i<inPktCnt; ++i)
|
|
|
|
{
|
|
|
|
device::audioPacket_t* pp = inPktArray + i;
|
2019-12-28 02:51:28 +00:00
|
|
|
cmApIO* ip = p->devArray[pp->devIdx].ioArray + kInApIdx; // dest io recd
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
// if the base time stamp has not yet been set - then set it
|
|
|
|
if( ip->timeStamp.tv_sec==0 && ip->timeStamp.tv_nsec==0 )
|
|
|
|
ip->timeStamp = pp->timeStamp;
|
|
|
|
|
|
|
|
// for each source packet channel and enabled dest channel
|
|
|
|
for(j=0; j<pp->chCnt; ++j)
|
|
|
|
{
|
|
|
|
cmApCh* cp = ip->chArray + pp->begChIdx +j; // dest ch
|
|
|
|
unsigned n0 = ip->n - cp->ii; // first dest segment
|
|
|
|
unsigned n1 = 0; // second dest segment
|
|
|
|
|
|
|
|
cwAssert(pp->begChIdx + j < ip->chCnt );
|
|
|
|
|
|
|
|
// if the incoming samples would overflow the buffer then ignore them
|
|
|
|
if( cp->fn + pp->audioFramesCnt > ip->n )
|
|
|
|
{
|
|
|
|
++ip->faultCnt; // record input overflow
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the incoming samples would go off the end of the buffer then
|
|
|
|
// copy in the samples in two segments (one at the end and another at begin of dest channel)
|
|
|
|
if( n0 < pp->audioFramesCnt )
|
|
|
|
n1 = pp->audioFramesCnt-n0;
|
|
|
|
else
|
|
|
|
n0 = pp->audioFramesCnt;
|
|
|
|
|
|
|
|
bool enaFl = cwIsFlag(cp->fl,kChFl) && cwIsFlag(cp->fl,kMuteFl)==false;
|
2019-12-28 02:51:28 +00:00
|
|
|
const sample_t* sp = enaFl ? ((sample_t*)pp->audioBytesPtr) + j : p->zeroBuf;
|
2019-12-24 15:05:24 +00:00
|
|
|
unsigned ssn = enaFl ? pp->chCnt : 1; // stride (packet samples are interleaved)
|
|
|
|
sample_t* dp = cp->b + cp->ii;
|
|
|
|
const sample_t* ep = dp + n0;
|
|
|
|
|
|
|
|
|
|
|
|
// update the meter
|
|
|
|
if( cwIsFlag(cp->fl,kMeterFl) )
|
|
|
|
{
|
|
|
|
cp->m[cp->mi] = _cmApMeter(sp,pp->audioFramesCnt,pp->chCnt);
|
|
|
|
cp->mi = (cp->mi + 1) % cp->mn;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the test tone is enabled on this input channel
|
|
|
|
if( enaFl && cwIsFlag(cp->fl,kToneFl) )
|
|
|
|
{
|
|
|
|
_cmApSine(cp, dp, n0, cp->b, n1, 1, ip->srate );
|
|
|
|
}
|
|
|
|
else // otherwise copy samples from the packet to the buffer
|
|
|
|
{
|
|
|
|
// copy the first segment
|
|
|
|
for(; dp < ep; sp += ssn )
|
|
|
|
*dp++ = cp->gain * *sp;
|
|
|
|
|
|
|
|
// if there is a second segment
|
|
|
|
if( n1 > 0 )
|
|
|
|
{
|
|
|
|
// copy the second segment
|
|
|
|
dp = cp->b;
|
|
|
|
ep = dp + n1;
|
|
|
|
for(; dp<ep; sp += ssn )
|
|
|
|
*dp++ = cp->gain * *sp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// advance the input channel buffer
|
|
|
|
cp->ii = n1>0 ? n1 : cp->ii + n0;
|
|
|
|
//cp->fn += pp->audioFramesCnt;
|
2019-12-27 21:52:45 +00:00
|
|
|
atomicUIntIncr(&cp->fn,pp->audioFramesCnt);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy samples from the buffer to the packet
|
|
|
|
if( outPktArray != NULL )
|
|
|
|
{
|
|
|
|
for(i=0; i<outPktCnt; ++i)
|
|
|
|
{
|
|
|
|
device::audioPacket_t* pp = outPktArray + i;
|
2019-12-28 02:51:28 +00:00
|
|
|
cmApIO* op = p->devArray[pp->devIdx].ioArray + kOutApIdx; // dest io recd
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
// if the base timestamp has not yet been set then set it.
|
|
|
|
if( op->timeStamp.tv_sec==0 && op->timeStamp.tv_nsec==0 )
|
|
|
|
op->timeStamp = pp->timeStamp;
|
|
|
|
|
|
|
|
// for each dest packet channel and enabled source channel
|
|
|
|
for(j=0; j<pp->chCnt; ++j)
|
|
|
|
{
|
2019-12-27 21:52:45 +00:00
|
|
|
cmApCh* cp = op->chArray + pp->begChIdx + j; // dest ch
|
|
|
|
unsigned n0 = op->n - cp->oi; // first src segment
|
|
|
|
unsigned n1 = 0; // second src segment
|
|
|
|
volatile unsigned fn = cp->fn; // store fn because it may be changed by the client thread
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
// if the outgoing samples will underflow the buffer
|
|
|
|
if( pp->audioFramesCnt > fn )
|
|
|
|
{
|
|
|
|
++op->faultCnt; // record an output underflow
|
|
|
|
|
|
|
|
// if the buffer is empty - zero the packet and return
|
|
|
|
if( fn == 0 )
|
|
|
|
{
|
|
|
|
memset( pp->audioBytesPtr, 0, pp->audioFramesCnt*sizeof(sample_t));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ... otherwise decrease the count of returned samples
|
|
|
|
pp->audioFramesCnt = fn;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the outgong segments would go off the end of the buffer then
|
|
|
|
// arrange to wrap to the begining of the buffer
|
|
|
|
if( n0 < pp->audioFramesCnt )
|
|
|
|
n1 = pp->audioFramesCnt-n0;
|
|
|
|
else
|
|
|
|
n0 = pp->audioFramesCnt;
|
|
|
|
|
|
|
|
sample_t* dp = ((sample_t*)pp->audioBytesPtr) + j;
|
|
|
|
bool enaFl = cwIsFlag(cp->fl,kChFl) && cwIsFlag(cp->fl,kMuteFl)==false;
|
|
|
|
|
|
|
|
// if the tone is enabled on this channel
|
|
|
|
if( enaFl && cwIsFlag(cp->fl,kToneFl) )
|
|
|
|
{
|
|
|
|
_cmApSine(cp, dp, n0, dp + n0*pp->chCnt, n1, pp->chCnt, op->srate );
|
|
|
|
}
|
|
|
|
else // otherwise copy samples from the output buffer to the packet
|
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
const sample_t* sp = enaFl ? cp->b + cp->oi : p->zeroBuf;
|
2019-12-24 15:05:24 +00:00
|
|
|
const sample_t* ep = sp + n0;
|
|
|
|
|
|
|
|
// copy the first segment
|
|
|
|
for(; sp < ep; dp += pp->chCnt )
|
|
|
|
*dp = cp->gain * *sp++;
|
|
|
|
|
|
|
|
// if there is a second segment
|
|
|
|
if( n1 > 0 )
|
|
|
|
{
|
|
|
|
// copy the second segment
|
2019-12-28 02:51:28 +00:00
|
|
|
sp = enaFl ? cp->b : p->zeroBuf;
|
2019-12-24 15:05:24 +00:00
|
|
|
ep = sp + n1;
|
|
|
|
for(; sp<ep; dp += pp->chCnt )
|
|
|
|
*dp = cp->gain * *sp++;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the meter
|
|
|
|
if( cwIsFlag(cp->fl,kMeterFl) )
|
|
|
|
{
|
|
|
|
cp->m[cp->mi] = _cmApMeter(((sample_t*)pp->audioBytesPtr)+j,pp->audioFramesCnt,pp->chCnt);
|
|
|
|
cp->mi = (cp->mi + 1) % cp->mn;
|
|
|
|
}
|
|
|
|
|
|
|
|
// advance the output channel buffer
|
|
|
|
cp->oi = n1>0 ? n1 : cp->oi + n0;
|
|
|
|
//cp->fn -= pp->audioFramesCnt;
|
2019-12-27 21:52:45 +00:00
|
|
|
atomicUIntDecr(&cp->fn,pp->audioFramesCnt);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return kOkRC;
|
|
|
|
}
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
unsigned cw::audio::buf::meterMs( handle_t h )
|
|
|
|
{
|
|
|
|
audioBuf_t* p = _handleToPtr(h);
|
|
|
|
return p->meterMs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cw::audio::buf::setMeterMs( handle_t h, unsigned meterMs )
|
|
|
|
{
|
|
|
|
audioBuf_t* p = _handleToPtr(h);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
p->meterMs = std::min(1000u,std::max(10u,meterMs));
|
|
|
|
}
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
unsigned cw::audio::buf::channelCount( handle_t h, unsigned devIdx, unsigned flags )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
if( devIdx == kInvalidIdx )
|
|
|
|
return 0;
|
2019-12-28 02:51:28 +00:00
|
|
|
audioBuf_t* p = _handleToPtr(h);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx;
|
2019-12-28 02:51:28 +00:00
|
|
|
return p->devArray[devIdx].ioArray[ idx ].chCnt;
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
void cw::audio::buf::setFlag( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
if( devIdx == kInvalidIdx )
|
|
|
|
return;
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
audioBuf_t* p = _handleToPtr(h);
|
2019-12-24 15:05:24 +00:00
|
|
|
unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx;
|
|
|
|
bool enableFl = flags & kEnableFl ? true : false;
|
|
|
|
unsigned i = chIdx != kInvalidIdx ? chIdx : 0;
|
2019-12-28 02:51:28 +00:00
|
|
|
unsigned n = chIdx != kInvalidIdx ? chIdx+1 : p->devArray[devIdx].ioArray[idx].chCnt;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
for(; i<n; ++i)
|
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
cmApCh* cp = p->devArray[devIdx].ioArray[idx].chArray + i;
|
2019-12-24 15:05:24 +00:00
|
|
|
cp->fl = cwEnaFlag(cp->fl, flags & (kChFl|kToneFl|kMeterFl|kMuteFl|kPassFl), enableFl );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
bool cw::audio::buf::isFlag( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
|
|
|
if( devIdx == kInvalidIdx )
|
|
|
|
return false;
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
audioBuf_t* p = _handleToPtr(h);
|
2019-12-24 15:05:24 +00:00
|
|
|
unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx;
|
2019-12-28 02:51:28 +00:00
|
|
|
return cwIsFlag(p->devArray[devIdx].ioArray[idx].chArray[chIdx].fl,flags);
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
void cw::audio::buf::enableChannel( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
|
|
|
|
{ setFlag(h,devIdx,chIdx,flags | kChFl); }
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
bool cw::audio::buf::isChannelEnabled( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
|
|
|
|
{ return isFlag(h,devIdx, chIdx, flags | kChFl); }
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
void cw::audio::buf::enableTone( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
|
|
|
|
{ setFlag(h,devIdx,chIdx,flags | kToneFl); }
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
bool cw::audio::buf::isToneEnabled( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
|
|
|
|
{ return isFlag(h,devIdx,chIdx,flags | kToneFl); }
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
void cw::audio::buf::enableMute( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
|
|
|
|
{ setFlag(h,devIdx,chIdx,flags | kMuteFl); }
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
bool cw::audio::buf::isMuteEnabled( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
|
|
|
|
{ return isFlag(h,devIdx,chIdx,flags | kMuteFl); }
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
void cw::audio::buf::enablePass( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
|
|
|
|
{ setFlag(h,devIdx,chIdx,flags | kPassFl); }
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
bool cw::audio::buf::isPassEnabled( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
|
|
|
|
{ return isFlag(h,devIdx,chIdx,flags | kPassFl); }
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
void cw::audio::buf::enableMeter( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
|
|
|
|
{ setFlag(h,devIdx,chIdx,flags | kMeterFl); }
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
bool cw::audio::buf::isMeterEnabled(handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
|
|
|
|
{ return isFlag(h,devIdx,chIdx,flags | kMeterFl); }
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
cw::audio::buf::sample_t cw::audio::buf::meter(handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
|
|
|
if( devIdx == kInvalidIdx )
|
|
|
|
return 0;
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
audioBuf_t* p = _handleToPtr(h);
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx;
|
2019-12-28 02:51:28 +00:00
|
|
|
const cmApCh* cp = p->devArray[devIdx].ioArray[idx].chArray + chIdx;
|
2019-12-24 15:05:24 +00:00
|
|
|
return _cmApMeterValue(cp);
|
|
|
|
}
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
void cw::audio::buf::setGain( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags, double gain )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
|
|
|
if( devIdx == kInvalidIdx )
|
|
|
|
return;
|
2019-12-28 02:51:28 +00:00
|
|
|
audioBuf_t* p = _handleToPtr(h);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx;
|
|
|
|
unsigned i = chIdx != kInvalidIdx ? chIdx : 0;
|
2019-12-28 02:51:28 +00:00
|
|
|
unsigned n = i + (chIdx != kInvalidIdx ? 1 : p->devArray[devIdx].ioArray[idx].chCnt);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
for(; i<n; ++i)
|
2019-12-28 02:51:28 +00:00
|
|
|
p->devArray[devIdx].ioArray[idx].chArray[i].gain = gain;
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
double cw::audio::buf::gain( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
|
|
|
if( devIdx == kInvalidIdx )
|
|
|
|
return 0;
|
2019-12-28 02:51:28 +00:00
|
|
|
audioBuf_t* p = _handleToPtr(h);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx;
|
2019-12-28 02:51:28 +00:00
|
|
|
return p->devArray[devIdx].ioArray[idx].chArray[chIdx].gain;
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
unsigned cw::audio::buf::getStatus( handle_t h, unsigned devIdx, unsigned flags, double* meterArray, unsigned meterCnt, unsigned* faultCntPtr )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
|
|
|
if( devIdx == kInvalidIdx )
|
|
|
|
return 0;
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
audioBuf_t* p = _handleToPtr(h);
|
2019-12-24 15:05:24 +00:00
|
|
|
unsigned ioIdx = cwIsFlag(flags,kInFl) ? kInApIdx : kOutApIdx;
|
2019-12-28 02:51:28 +00:00
|
|
|
cmApIO* iop = p->devArray[devIdx].ioArray + ioIdx;
|
2019-12-24 15:05:24 +00:00
|
|
|
unsigned chCnt = std::min(iop->chCnt, meterCnt );
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
if( faultCntPtr != NULL )
|
|
|
|
*faultCntPtr = iop->faultCnt;
|
|
|
|
|
|
|
|
for(i=0; i<chCnt; ++i)
|
|
|
|
meterArray[i] = _cmApMeterValue(iop->chArray + i);
|
|
|
|
return chCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
bool cw::audio::buf::isDeviceReady( handle_t h, unsigned devIdx, unsigned flags )
|
|
|
|
{
|
2019-12-24 15:05:24 +00:00
|
|
|
//bool iFl = true;
|
|
|
|
//bool oFl = true;
|
|
|
|
unsigned i = 0;
|
|
|
|
|
|
|
|
if( devIdx == kInvalidIdx )
|
|
|
|
return false;
|
2019-12-28 02:51:28 +00:00
|
|
|
audioBuf_t* p = _handleToPtr(h);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
if( flags & kInFl )
|
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
const cmApIO* ioPtr = p->devArray[devIdx].ioArray + kInApIdx;
|
2019-12-24 15:05:24 +00:00
|
|
|
for(i=0; i<ioPtr->chCnt; ++i)
|
|
|
|
if( ioPtr->chArray[i].fn < ioPtr->dspFrameCnt )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
//iFl = ioPtr->fn > ioPtr->dspFrameCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( flags & kOutFl )
|
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
const cmApIO* ioPtr = p->devArray[devIdx].ioArray + kOutApIdx;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
for(i=0; i<ioPtr->chCnt; ++i)
|
|
|
|
if( (ioPtr->n - ioPtr->chArray[i].fn) < ioPtr->dspFrameCnt )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
//oFl = (ioPtr->n - ioPtr->fn) > ioPtr->dspFrameCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
//return iFl & oFl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Note that his function returns audio samples but does NOT
|
|
|
|
// change any internal states.
|
2019-12-28 02:51:28 +00:00
|
|
|
void cw::audio::buf::get( handle_t h, unsigned devIdx, unsigned flags, sample_t* bufArray[], unsigned bufChCnt )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
audioBuf_t* p = _handleToPtr(h);
|
2019-12-24 15:05:24 +00:00
|
|
|
unsigned i;
|
|
|
|
if( devIdx == kInvalidIdx )
|
|
|
|
{
|
|
|
|
for(i=0; i<bufChCnt; ++i)
|
|
|
|
bufArray[i] = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx;
|
2019-12-28 02:51:28 +00:00
|
|
|
const cmApIO* ioPtr = p->devArray[devIdx].ioArray + idx;
|
2019-12-24 15:05:24 +00:00
|
|
|
unsigned n = bufChCnt < ioPtr->chCnt ? bufChCnt : ioPtr->chCnt;
|
|
|
|
//unsigned offs = flags & kInFl ? ioPtr->oi : ioPtr->ii;
|
|
|
|
cmApCh* cp = ioPtr->chArray;
|
|
|
|
|
|
|
|
for(i=0; i<n; ++i,++cp)
|
|
|
|
{
|
|
|
|
unsigned offs = flags & kInFl ? cp->oi : cp->ii;
|
|
|
|
bufArray[i] = cwIsFlag(cp->fl,kChFl) ? cp->b + offs : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
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 )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
audioBuf_t* p = _handleToPtr(h);
|
|
|
|
get( h, iDevIdx, kInFl, iBufArray, iBufChCnt );
|
|
|
|
get( h, oDevIdx, kOutFl,oBufArray, oBufChCnt );
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
unsigned i = 0;
|
|
|
|
|
|
|
|
if( iDevIdx != kInvalidIdx && oDevIdx != kInvalidIdx )
|
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
const cmApIO* ip = p->devArray[iDevIdx].ioArray + kInApIdx;
|
|
|
|
const cmApIO* op = p->devArray[oDevIdx].ioArray + kOutApIdx;
|
2019-12-24 15:05:24 +00:00
|
|
|
unsigned minChCnt = std::min(iBufChCnt,oBufChCnt);
|
|
|
|
unsigned frmCnt = std::min(ip->dspFrameCnt,op->dspFrameCnt);
|
|
|
|
unsigned byteCnt = frmCnt * sizeof(sample_t);
|
|
|
|
|
|
|
|
_theBufCalcTimeStamp(ip->srate, &ip->timeStamp, ip->ioFrameCnt, iTimeStampPtr );
|
|
|
|
_theBufCalcTimeStamp(op->srate, &op->timeStamp, op->ioFrameCnt, oTimeStampPtr );
|
|
|
|
|
|
|
|
for(i=0; i<minChCnt; ++i)
|
|
|
|
{
|
|
|
|
cmApCh* ocp = op->chArray + i;
|
|
|
|
cmApCh* icp = ip->chArray + i;
|
|
|
|
|
|
|
|
if( oBufArray[i] != NULL )
|
|
|
|
{
|
|
|
|
// if either the input or output channel is marked for pass-through
|
|
|
|
if( cwAllFlags(ocp->fl,kPassFl) || cwAllFlags(icp->fl,kPassFl) )
|
|
|
|
{
|
|
|
|
memcpy( oBufArray[i], iBufArray[i], byteCnt );
|
|
|
|
|
|
|
|
// set the output buffer to NULL to prevent it being over written by the client
|
|
|
|
oBufArray[i] = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// zero the output buffer
|
|
|
|
memset(oBufArray[i],0,byteCnt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( oDevIdx != kInvalidIdx )
|
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
const cmApIO* op = p->devArray[oDevIdx].ioArray + kOutApIdx;
|
2019-12-24 15:05:24 +00:00
|
|
|
unsigned byteCnt = op->dspFrameCnt * sizeof(sample_t);
|
|
|
|
|
|
|
|
_theBufCalcTimeStamp(op->srate, &op->timeStamp, op->ioFrameCnt, oTimeStampPtr );
|
|
|
|
|
|
|
|
for(; i<oBufChCnt; ++i)
|
|
|
|
if( oBufArray[i] != NULL )
|
|
|
|
memset( oBufArray[i], 0, byteCnt );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
void cw::audio::buf::advance( handle_t h, unsigned devIdx, unsigned flags )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
if( devIdx == kInvalidIdx )
|
|
|
|
return;
|
2019-12-28 02:51:28 +00:00
|
|
|
audioBuf_t* p = _handleToPtr(h);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
if( flags & kInFl )
|
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
cmApIO* ioPtr = p->devArray[devIdx].ioArray + kInApIdx;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
for(i=0; i<ioPtr->chCnt; ++i)
|
|
|
|
{
|
|
|
|
cmApCh* cp = ioPtr->chArray + i;
|
|
|
|
cp->oi = (cp->oi + ioPtr->dspFrameCnt) % ioPtr->n;
|
2019-12-27 21:52:45 +00:00
|
|
|
atomicUIntDecr(&cp->fn,ioPtr->dspFrameCnt);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// count the number of samples input from this device
|
|
|
|
if( ioPtr->timeStamp.tv_sec!=0 && ioPtr->timeStamp.tv_nsec!=0 )
|
|
|
|
{
|
2019-12-27 21:52:45 +00:00
|
|
|
atomicUIntIncr(&ioPtr->ioFrameCnt,ioPtr->dspFrameCnt);
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( flags & kOutFl )
|
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
cmApIO* ioPtr = p->devArray[devIdx].ioArray + kOutApIdx;
|
2019-12-24 15:05:24 +00:00
|
|
|
for(i=0; i<ioPtr->chCnt; ++i)
|
|
|
|
{
|
|
|
|
cmApCh* cp = ioPtr->chArray + i;
|
|
|
|
cp->ii = (cp->ii + ioPtr->dspFrameCnt) % ioPtr->n;
|
2019-12-27 21:52:45 +00:00
|
|
|
atomicUIntIncr(&cp->fn,ioPtr->dspFrameCnt);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// count the number of samples output from this device
|
|
|
|
if( ioPtr->timeStamp.tv_sec!=0 && ioPtr->timeStamp.tv_nsec!=0 )
|
|
|
|
{
|
2019-12-27 21:52:45 +00:00
|
|
|
atomicUIntIncr(&ioPtr->ioFrameCnt,ioPtr->dspFrameCnt);
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
void cw::audio::buf::inputToOutput( handle_t h, unsigned iDevIdx, unsigned oDevIdx )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
|
|
|
if( iDevIdx == kInvalidIdx || oDevIdx == kInvalidIdx )
|
|
|
|
return;
|
2019-12-28 02:51:28 +00:00
|
|
|
audioBuf_t* p = _handleToPtr(h);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
unsigned iChCnt = channelCount( h, iDevIdx, kInFl );
|
|
|
|
unsigned oChCnt = channelCount( h, oDevIdx, kOutFl );
|
2019-12-24 15:05:24 +00:00
|
|
|
unsigned chCnt = iChCnt < oChCnt ? iChCnt : oChCnt;
|
|
|
|
unsigned i;
|
|
|
|
|
2020-12-15 20:31:49 +00:00
|
|
|
if( chCnt == 0 )
|
|
|
|
{
|
|
|
|
cwLogWarning("Both input and output devices must have a non-zero channel count in the call to audio::buf::inputToOutput().");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
sample_t* iBufPtrArray[ iChCnt ];
|
|
|
|
sample_t* oBufPtrArray[ oChCnt ];
|
|
|
|
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
while( isDeviceReady( h, iDevIdx, kInFl ) && isDeviceReady( h, oDevIdx, kOutFl ) )
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
get( h, iDevIdx, kInFl, iBufPtrArray, iChCnt );
|
|
|
|
get( h, oDevIdx, kOutFl, oBufPtrArray, oChCnt );
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
// Warning: buffer pointers to disabled channels will be set to NULL
|
|
|
|
|
|
|
|
for(i=0; i<chCnt; ++i)
|
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
cmApIO* ip = p->devArray[iDevIdx].ioArray + kInApIdx;
|
|
|
|
cmApIO* op = p->devArray[oDevIdx].ioArray + kOutApIdx;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
cwAssert( ip->dspFrameCnt == op->dspFrameCnt );
|
|
|
|
|
|
|
|
unsigned byteCnt = ip->dspFrameCnt * sizeof(sample_t);
|
|
|
|
|
|
|
|
if( oBufPtrArray[i] != NULL )
|
|
|
|
{
|
|
|
|
// the input channel is not disabled
|
|
|
|
if( iBufPtrArray[i]!=NULL )
|
|
|
|
memcpy(oBufPtrArray[i],iBufPtrArray[i],byteCnt);
|
|
|
|
else
|
|
|
|
// the input channel is disabled but the output is not - so fill the output with zeros
|
|
|
|
memset(oBufPtrArray[i],0,byteCnt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
advance( h, iDevIdx, kInFl );
|
|
|
|
advance( h, oDevIdx, kOutFl );
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
void cw::audio::buf::report(handle_t h)
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
audioBuf_t* p = _handleToPtr(h);
|
2019-12-24 15:05:24 +00:00
|
|
|
unsigned i,j,k;
|
2019-12-28 02:51:28 +00:00
|
|
|
for(i=0; i<p->devCnt; ++i)
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
|
|
|
for(j=0; j<kIoApCnt; ++j)
|
|
|
|
{
|
2019-12-28 02:51:28 +00:00
|
|
|
cmApIO* ip = p->devArray[i].ioArray + j;
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
unsigned ii = 0;
|
|
|
|
unsigned oi = 0;
|
|
|
|
unsigned fn = 0;
|
2019-12-27 21:52:45 +00:00
|
|
|
sample_t m = 0;
|
2019-12-24 15:05:24 +00:00
|
|
|
for(k=0; k<ip->chCnt; ++k)
|
|
|
|
{
|
2020-12-15 20:31:49 +00:00
|
|
|
cmApCh* cp = ip->chArray + k;
|
2019-12-24 15:05:24 +00:00
|
|
|
ii += cp->ii;
|
|
|
|
oi += cp->oi;
|
|
|
|
fn += cp->fn;
|
2019-12-27 21:52:45 +00:00
|
|
|
m += _cmApMeterValue(cp);
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
2019-12-27 21:52:45 +00:00
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
|
2019-12-27 21:52:45 +00:00
|
|
|
cwLogInfo("%i : %s - i:%7i o:%7i f:%7i n:%7i err %s:%7i meter:%f",
|
2019-12-26 02:44:14 +00:00
|
|
|
i,j==0?"IN ":"OUT",
|
2019-12-27 21:52:45 +00:00
|
|
|
ii,oi,fn,ip->n, (j==0?"over ":"under"), ip->faultCnt, m/ip->chCnt);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// [cwAudioBufExample]
|
|
|
|
|
2020-09-27 14:27:32 +00:00
|
|
|
cw::rc_t cw::audio::buf::test()
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
2020-09-27 14:27:32 +00:00
|
|
|
rc_t rc = kOkRC;
|
2019-12-24 15:05:24 +00:00
|
|
|
unsigned devIdx = 0;
|
|
|
|
unsigned devCnt = 1 ;
|
|
|
|
unsigned dspFrameCnt = 10;
|
|
|
|
unsigned cycleCnt = 3;
|
|
|
|
unsigned framesPerCycle = 25;
|
|
|
|
unsigned inChCnt = 2;
|
|
|
|
unsigned outChCnt = inChCnt;
|
|
|
|
unsigned sigN = cycleCnt*framesPerCycle*inChCnt;
|
|
|
|
double srate = 44100.0;
|
|
|
|
unsigned meterMs = 50;
|
|
|
|
|
|
|
|
unsigned bufChCnt = inChCnt;
|
|
|
|
sample_t* inBufArray[ bufChCnt ];
|
|
|
|
sample_t* outBufArray[ bufChCnt ];
|
|
|
|
sample_t iSig[ sigN ];
|
|
|
|
sample_t oSig[ sigN ];
|
|
|
|
sample_t* os = oSig;
|
|
|
|
device::audioPacket_t pkt;
|
|
|
|
unsigned i,j;
|
2019-12-28 02:51:28 +00:00
|
|
|
handle_t h;
|
|
|
|
|
2019-12-24 15:05:24 +00:00
|
|
|
// create a simulated signal
|
|
|
|
for(i=0; i<sigN; ++i)
|
|
|
|
{
|
|
|
|
iSig[i] = i;
|
|
|
|
oSig[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pkt.devIdx = 0;
|
|
|
|
pkt.begChIdx = 0;
|
|
|
|
pkt.chCnt = inChCnt;
|
|
|
|
pkt.audioFramesCnt = framesPerCycle;
|
|
|
|
pkt.bitsPerSample = 32;
|
|
|
|
pkt.flags = 0;
|
|
|
|
time::get(pkt.timeStamp);
|
|
|
|
|
|
|
|
|
|
|
|
// initialize a the audio buffer
|
2019-12-28 02:51:28 +00:00
|
|
|
create(h,devCnt,meterMs);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
// setup the buffer with the specific parameters to by used by the host audio ports
|
2019-12-28 02:51:28 +00:00
|
|
|
setup(h,devIdx,srate,dspFrameCnt,cycleCnt,inChCnt,framesPerCycle,outChCnt,framesPerCycle);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
// simulate cylcing through sigN buffer transactions
|
|
|
|
for(i=0; i<sigN; i+=framesPerCycle*inChCnt)
|
|
|
|
{
|
|
|
|
// setup an incoming audio packet
|
|
|
|
pkt.audioFramesCnt = framesPerCycle;
|
|
|
|
pkt.audioBytesPtr = iSig+i;
|
|
|
|
|
|
|
|
// simulate a call from the audio port with incoming samples
|
|
|
|
// (fill the audio buffers internal input buffers)
|
2019-12-28 02:51:28 +00:00
|
|
|
update(h,&pkt,1,NULL,0);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
// if all devices need to be serviced
|
2019-12-28 02:51:28 +00:00
|
|
|
while( isDeviceReady( h, devIdx, kInFl | kOutFl ))
|
2019-12-24 15:05:24 +00:00
|
|
|
{
|
|
|
|
// get pointers to full internal input buffers
|
2019-12-28 02:51:28 +00:00
|
|
|
get(h, devIdx, kInFl, inBufArray, bufChCnt );
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
// get pointers to empty internal output buffers
|
2019-12-28 02:51:28 +00:00
|
|
|
get(h, devIdx, kOutFl, outBufArray, bufChCnt );
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
// Warning: pointers to disabled channels will be set to NULL.
|
|
|
|
|
|
|
|
// simulate a play through by copying the incoming samples to the outgoing buffers.
|
|
|
|
for(j=0; j<bufChCnt; ++j)
|
|
|
|
if( outBufArray[j] != NULL )
|
|
|
|
{
|
|
|
|
// if the input is disabled - but the output is not then zero the output buffer
|
|
|
|
if( inBufArray[j] == NULL )
|
|
|
|
memset( outBufArray[j], 0, dspFrameCnt*sizeof(sample_t));
|
|
|
|
else
|
|
|
|
// copy the input to the output
|
|
|
|
memcpy( outBufArray[j], inBufArray[j], dspFrameCnt*sizeof(sample_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
// signal the buffer that this cycle is complete.
|
|
|
|
// (marks current internal input/output buffer empty/full)
|
2019-12-28 02:51:28 +00:00
|
|
|
advance( h, devIdx, kInFl | kOutFl );
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pkt.audioBytesPtr = os;
|
|
|
|
|
|
|
|
// simulate a call from the audio port picking up outgoing samples
|
|
|
|
// (empties the audio buffers internal output buffers)
|
2019-12-28 02:51:28 +00:00
|
|
|
update(h,NULL,0,&pkt,1);
|
2019-12-24 15:05:24 +00:00
|
|
|
|
|
|
|
os += pkt.audioFramesCnt * pkt.chCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0; i<sigN; ++i)
|
|
|
|
cwLogInfo("%f ",oSig[i]);
|
|
|
|
cwLogInfo("\n");
|
|
|
|
|
2019-12-28 02:51:28 +00:00
|
|
|
destroy(h);
|
2020-09-27 14:27:32 +00:00
|
|
|
|
|
|
|
return rc;
|
2019-12-24 15:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// [cwAudioBufExample]
|
|
|
|
|
|
|
|
|