From b9f9082aee79c67b036ecd828df567e0a040c864 Mon Sep 17 00:00:00 2001 From: kevin Date: Sun, 19 Dec 2021 12:08:10 -0500 Subject: [PATCH] cwDspTransforms.h/cpp : Added 'recorder'. --- cwDspTransforms.cpp | 94 +++++++++++++++++++++++++++++++++++++++++++++ cwDspTransforms.h | 24 ++++++++++-- 2 files changed, 115 insertions(+), 3 deletions(-) diff --git a/cwDspTransforms.cpp b/cwDspTransforms.cpp index f444658..26e3250 100644 --- a/cwDspTransforms.cpp +++ b/cwDspTransforms.cpp @@ -161,3 +161,97 @@ void cw::dsp::compressor::set_rms_wnd_ms( obj_t* p, real_t ms ) if( p->rmsWndCnt > p->rmsWndAllocCnt ) p->rmsWndCnt = p->rmsWndAllocCnt; } + + + +cw::rc_t cw::dsp::recorder::create( obj_t*& pRef, real_t srate, real_t max_secs, unsigned chN ) +{ + obj_t* p = mem::allocZ(); + p->srate = srate; + p->maxFrameN = (unsigned)(max_secs * srate); + p->chN = chN; + p->buf = mem::allocZ( p->maxFrameN * p->chN ); + + pRef = p; + + return kOkRC; +} + +cw::rc_t cw::dsp::recorder::destroy( obj_t*& pRef) +{ + obj_t* p = pRef; + + if( p != nullptr ) + { + mem::release(p->buf); + mem::release(p); + } + + pRef = nullptr; + return kOkRC; +} + +cw::rc_t cw::dsp::recorder::exec( obj_t* p, const sample_t* buf, unsigned chN, unsigned frameN ) +{ + const sample_t* chA[ chN ]; + for(unsigned i=0; ichN ); + + // + if( p->frameIdx + frameN > p->maxFrameN ) + frameN = p->maxFrameN - p->frameIdx; + + for(unsigned i=0; ibuf[ i*p->maxFrameN + p->frameIdx + j ] = chA[i][j]; + + p->frameIdx += frameN; + + return kOkRC; +} + +cw::rc_t cw::dsp::recorder::write( obj_t* p, const char* fname ) +{ + file::handle_t h; + cw::rc_t rc; + + if((rc = file::open(h,fname, file::kWriteFl )) != kOkRC ) + { + rc = cwLogError(rc,"Recorder file open failed on '%s'.", cwStringNullGuard(fname)); + goto errLabel; + } + + file::printf(h,"{\n"); + + file::printf(h,"\"srate\":%f,\n",p->srate); + file::printf(h,"\"maxFrameN\":%i,\n",p->frameIdx); + + for(unsigned i=0; ichN; ++i) + { + file::printf(h,"\"%i\":[",i); + + for(unsigned j=0; jframeIdx; ++j) + file::printf(h,"%f%c\n",p->buf[ p->maxFrameN*i + j ], j+1==p->frameIdx ? ' ' : ','); + + file::printf(h,"]\n"); + } + + file::printf(h,"}\n"); + + errLabel: + + if((rc = file::close(h)) != kOkRC ) + { + rc = cwLogError(rc,"Recorder file close failed on '%s'.", cwStringNullGuard(fname)); + goto errLabel; + } + + return rc; +} diff --git a/cwDspTransforms.h b/cwDspTransforms.h index 7ef92c9..1ce6db1 100644 --- a/cwDspTransforms.h +++ b/cwDspTransforms.h @@ -45,9 +45,27 @@ namespace cw void set_thresh_db( obj_t* p, real_t thresh ); void set_rms_wnd_ms( obj_t* p, real_t ms ); } - - } - + + namespace recorder + { + typedef struct + { + real_t srate; // + unsigned maxFrameN; // + unsigned chN; // channel count + unsigned frameIdx; // next frame to write + sample_t* buf; // [ [maxFrameN] [maxFrameN] ] + } obj_t; // ch0 ch1 + + rc_t create( obj_t*& pRef, real_t srate, real_t max_secs, unsigned chN ); + rc_t destroy( obj_t*& pRef); + + rc_t exec( obj_t* p, const sample_t* buf, unsigned chN, unsigned frameN ); + rc_t exec( obj_t* p, const sample_t* chA[], unsigned chN, unsigned frameN ); + rc_t write( obj_t* p, const char* fname ); + + } + } } #endif