From dfcf5be512b52cf7494082ff14309f8bbf2f7ef6 Mon Sep 17 00:00:00 2001 From: kevin Date: Tue, 15 Dec 2020 15:30:45 -0500 Subject: [PATCH] cwAudioDeviceTest.h/cpp : test() now takes a cfg object and added test_tone(). --- cwAudioDeviceTest.cpp | 151 +++++++++++++++++++++++------------------- cwAudioDeviceTest.h | 3 +- 2 files changed, 84 insertions(+), 70 deletions(-) diff --git a/cwAudioDeviceTest.cpp b/cwAudioDeviceTest.cpp index 7ac0ad8..2f2bb57 100644 --- a/cwAudioDeviceTest.cpp +++ b/cwAudioDeviceTest.cpp @@ -7,6 +7,7 @@ #include "cwAudioDevice.h" #include "cwAudioBuf.h" #include "cwAudioDeviceAlsa.h" +#include "cwObject.h" #include "cwAudioDeviceTest.h" namespace cw @@ -24,62 +25,45 @@ namespace cw // audio port callback functions. typedef struct { + const char* inDevLabel; // Input audio device label + const char* outDevLabel; // Output audio device label unsigned bufCnt; // 2=double buffering 3=triple buffering unsigned framesPerCycle; // DSP frames per cycle - unsigned inDevIdx; // input device index - unsigned outDevIdx; // output device index double srate; // audio sample rate unsigned meterMs; // audio meter buffer length + unsigned inDevIdx; // input device index + unsigned outDevIdx; // output device index + unsigned iCbCnt; // count the callback unsigned oCbCnt; + double amHz; // ampl. modulation frequency + double amPhs; // phase + double amMaxGain; // max gain. + buf::handle_t audioBufH; } cmApPortTestRecd; - - // print the usage message for cmAudioPortTest.c - void _cmApPrintUsage() + rc_t _cmApGetCfg( cmApPortTestRecd* r, const object_t* cfg ) { - char msg[] = - "cmApPortTest() command switches\n" - "-r -c -b -f -i -o -t -p -h \n" - "\n" - "-r = sample rate\n" - "-b = count of buffers\n" - "-f = count of samples per buffer\n" - "-i = input device index\n" - "-o = output device index\n" - "-p = print report but do not start audio devices\n" - "-h = print this usage message\n"; + rc_t rc; - cwLogInfo(msg); + r->bufCnt = 3; + r->srate = 48000; + r->framesPerCycle = 512; + r->meterMs = 50; + r->amHz = 0; + r->amMaxGain = 0.8; + + if((rc = cfg->getv_opt("inDev",r->inDevLabel,"outDev",r->outDevLabel,"srate",r->srate,"bufN",r->bufCnt,"framesPerCycle",r->framesPerCycle,"meterMs",r->meterMs,"amHz",r->amHz,"amMaxGain",r->amMaxGain)) != kOkRC ) + return cwLogError(rc,"The audio device configuration is invalid."); + + return rc; } - // Get a command line option. Note that if 'boolFl' is set to 'true' then the function simply - // returns '1'. This is used to handle arguments whose presense indicates a positive boolean - // flag. For example -h (help) indicates that the usage data should be printed - it needs no other argument. - int _cmApGetOpt( int argc, const char* argv[], const char* label, int defaultVal, bool boolFl=false ) - { - int i = 0; - for(; i(outPktArray[i].cbArg)->oCbCnt++; + + if( p->amHz > 0 && outPktCnt > 0 ) + { + unsigned sampleFrameN = outPktArray[0].audioFramesCnt; + + double amGain = p->amMaxGain * (cos( p->amPhs ) + 1.0) / 2; + p->amPhs += p->amHz * sampleFrameN * M_PI / p->srate; + buf::setGain( p->audioBufH, p->outDevIdx, -1, buf::kOutFl, amGain); + } - buf::inputToOutput( p->audioBufH, _cmGlobalInDevIdx, _cmGlobalOutDevIdx ); + buf::inputToOutput( p->audioBufH, p->inDevIdx, p->outDevIdx ); buf::update( p->audioBufH, inPktArray, inPktCnt, outPktArray, outPktCnt ); } @@ -100,7 +93,7 @@ namespace cw } // Audio Port testing function -cw::rc_t cw::audio::device::test( int argc, const char** argv ) +cw::rc_t cw::audio::device::test( const object_t* cfg ) { cmApPortTestRecd r; unsigned i; @@ -110,49 +103,59 @@ cw::rc_t cw::audio::device::test( int argc, const char** argv ) alsa::handle_t alsaH; bool runFl = true; - if( _cmApGetOpt(argc,argv,"-h",0,true) ) - _cmApPrintUsage(); - - runFl = _cmApGetOpt(argc,argv,"-p",0,true) ? false : true; - r.srate = _cmApGetOpt(argc,argv,"-r",44100); - r.bufCnt = _cmApGetOpt(argc,argv,"-b",3); - r.framesPerCycle = _cmApGetOpt(argc,argv,"-f",512); - r.meterMs = 50; - - r.inDevIdx = _cmGlobalInDevIdx = _cmApGetOpt(argc,argv,"-i",0); - r.outDevIdx = _cmGlobalOutDevIdx = _cmApGetOpt(argc,argv,"-o",0); - r.iCbCnt = 0; - r.oCbCnt = 0; + r.oCbCnt = 0; + r.iCbCnt = 0; + r.amPhs = 0; - //cwLogInfo("Program cfg: %s in:%i out:%i chidx:%i chs:%i bufs=%i frm=%i rate=%f",runFl?"exec":"rpt",r.inDevIdx,r.outDevIdx,r.chIdx,r.chCnt,r.bufCnt,r.framesPerCycle,r.srate); - // initialize the audio device interface if((rc = create(h)) != kOkRC ) { - cwLogInfo("Initialize failed."); + cwLogError(rc,"Initialize failed."); goto errLabel; } // initialize the ALSA device driver interface if((rc = alsa::create(alsaH, drv )) != kOkRC ) { - cwLogInfo("ALSA initialize failed."); + cwLogError(rc,"ALSA initialize failed."); goto errLabel; } // register the ALSA device driver with the audio interface if((rc = registerDriver( h, drv )) != kOkRC ) { - cwLogInfo("ALSA driver registration failed."); + cwLogError(rc,"ALSA driver registration failed."); goto errLabel; } - + // report the current audio device configuration for(i=0; i