cmDspBuiltIn.c:Rewrote _cmDspAudioFileOutExec().

This commit is contained in:
kevin 2013-03-01 17:35:35 -08:00
parent 4463d9bbe3
commit b467c7abd4

View File

@ -1298,47 +1298,44 @@ cmDspRC_t _cmDspAudioFileOutReset(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDs
return rc; return rc;
} }
cmDspRC_t _cmDspAudioFileOutExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt ) cmDspRC_t _cmDspAudioFileOutExec(cmDspCtx_t* ctx, cmDspInst_t* inst, const cmDspEvt_t* evt )
{ {
cmDspRC_t rc = kOkDspRC; cmDspRC_t rc = kOkDspRC;
cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;; cmDspAudioFileOut_t* p = (cmDspAudioFileOut_t*)inst;;
unsigned fpc = cmDspSamplesPerCycle(ctx); unsigned chCnt = cmMin(2,cmDspUInt(inst,kChCntAofId));
unsigned chCnt = cmDspUInt(inst,kChCntAofId); unsigned smpCnt = 0;
cmSample_t* chArray[chCnt]; cmSample_t* chArray[chCnt];
unsigned i; unsigned i,j;
// Assume that both channls have the same number of samples. for(i=0,j=0; i<chCnt; ++i)
// (cmAudioWriteFile() has the same constraint )
int sn = cmDspAudioBufSmpCount(ctx,inst,kIn0AofId,0);
// This code can handle the case where the input channels contain
// more than or less than cmDspSamplesPerCycle().
while(sn>0)
{ {
// we can process at most 'fpc' samples on one iteration unsigned chVarId = i == 0 ? kIn0AofId : kIn1AofId; // get audio buf var id for this ch
unsigned n = cmMin(sn,fpc); unsigned iSmpCnt = cmDspVarRows(inst,chVarId);
sn -= n;
// apply output gain if( iSmpCnt == 0 )
for(i=0; i<chCnt; ++i)
{ {
chArray[i] = p->smpBuf + i*fpc; chArray[j] = NULL;
const cmSample_t* sp = i==0 ? cmDspAudioBuf(ctx,inst,kIn0AofId,0) : cmDspAudioBuf(ctx,inst,kIn1AofId,0); }
cmSample_t gain = i==0 ? cmDspDouble(inst,kGain0AofId) : cmDspDouble(inst,kGain1AofId); else
{
cmSample_t gain = cmDspSample(inst,i==0?kGain0AofId:kGain1AofId); // get ch gain
cmVOS_MultVVS(chArray[i], n, sp, (cmSample_t)gain); chArray[j] = cmDspAudioBuf(ctx,inst,chVarId,i); // get incoming audio buf ptr
if( gain != 1.0 )
cmVOS_MultVVS(chArray[j], iSmpCnt, chArray[j], gain); // apply gain
++j; // incr chArray[] index
assert( smpCnt==0 || iSmpCnt==smpCnt);
smpCnt = iSmpCnt; // set outgoing sample count
}
} }
// write the samples // write the samples
if( cmAudioFileWriteSample(p->afH, n, chCnt, chArray ) != kOkAfRC ) if( cmAudioFileWriteSample(p->afH, smpCnt, j, chArray ) != kOkAfRC )
rc = cmDspClassErr(ctx,inst->classPtr,kFileWriteFailDspRC,"An audio output file write failed."); rc = cmDspClassErr(ctx,inst->classPtr,kFileWriteFailDspRC,"An audio output file write failed.");
}
return rc; return rc;
} }
@ -5020,6 +5017,8 @@ cmDspClassConsFunc_t _cmDspClassBuiltInArray[] =
cmMidiFilePlayClassCons, cmMidiFilePlayClassCons,
cmScFolClassCons, cmScFolClassCons,
cmScModClassCons, cmScModClassCons,
cmGSwitchClassCons,
NULL, NULL,
}; };