cwFlowProc.cpp : Fixed various problems with 'sine_tone' proc.

This commit is contained in:
kevin 2021-12-19 12:13:45 -05:00
parent 6578f69369
commit 5c069612f9

View File

@ -507,33 +507,41 @@ namespace cw
typedef struct typedef struct
{ {
real_t gain; real_t *phaseA;
real_t hz;
real_t phase;
srate_t srate;
unsigned chCnt;
} inst_t; } inst_t;
rc_t create( instance_t* ctx ) rc_t create( instance_t* ctx )
{ {
rc_t rc = kOkRC; rc_t rc = kOkRC;
inst_t* inst = mem::allocZ<inst_t>();
inst_t* inst = mem::allocZ<inst_t>(); srate_t srate = 0;
unsigned chCnt = 0;
real_t gain;
real_t hz;
ctx->userPtr = inst; ctx->userPtr = inst;
// Register variables and get their current value // Register variables and get their current value
if((rc = var_register_and_get( ctx, kAnyChIdx, if((rc = var_register_and_get( ctx, kAnyChIdx, kChCntPid, "chCnt", chCnt)) != kOkRC )
kSratePId, "srate", inst->srate,
kChCntPid, "chCnt", inst->chCnt,
kFreqHzPId, "hz", inst->hz,
kGainPId, "gain", inst->gain)) != kOkRC )
{ {
goto errLabel; goto errLabel;
} }
// register each oscillator variable
for(unsigned i=0; i<chCnt; ++i)
if((rc = var_register_and_get( ctx, i,
kSratePId, "srate", srate,
kFreqHzPId, "hz", hz,
kGainPId, "gain", gain)) != kOkRC )
{
goto errLabel;
}
// create one output audio buffer // create one output audio buffer
rc = var_register_and_set( ctx, "out", kOutPId, kAnyChIdx, inst->srate, inst->chCnt, ctx->ctx->framesPerCycle ); rc = var_register_and_set( ctx, "out", kOutPId, kAnyChIdx, srate, chCnt, ctx->ctx->framesPerCycle );
inst->phaseA = mem::allocZ<real_t>( chCnt );
errLabel: errLabel:
return rc; return rc;
@ -562,7 +570,6 @@ namespace cw
inst_t* inst = (inst_t*)ctx->userPtr; inst_t* inst = (inst_t*)ctx->userPtr;
abuf_t* abuf = nullptr; abuf_t* abuf = nullptr;
// get the output signal buffer // get the output signal buffer
if((rc = var_get(ctx,kOutPId,kAnyChIdx,abuf)) != kOkRC ) if((rc = var_get(ctx,kOutPId,kAnyChIdx,abuf)) != kOkRC )
{ {
@ -570,14 +577,23 @@ namespace cw
} }
else else
{ {
for(unsigned i=0; i<inst->chCnt; ++i) for(unsigned i=0; i<abuf->chN; ++i)
{ {
sample_t* v = abuf->buf + (i*abuf->frameN); real_t gain;
for(unsigned j=0; j<abuf->frameN; ++j) real_t hz;
v[j] = inst->gain * (sample_t)sin( inst->phase + (j*inst->srate/inst->hz)); srate_t srate;
}
inst->phase += abuf->frameN*inst->srate/inst->hz; var_get( ctx, kFreqHzPId, i, hz );
var_get( ctx, kGainPId, i, gain );
var_get( ctx, kSratePId, i, srate );
sample_t* v = abuf->buf + (i*abuf->frameN);
for(unsigned j=0; j<abuf->frameN; ++j)
v[j] = (sample_t)(gain * sin( inst->phaseA[i] + (2.0 * M_PI * j * hz/srate)));
inst->phaseA[i] += 2.0 * M_PI * abuf->frameN * hz/srate;
}
} }
return rc; return rc;