Basic Audio Signal Processing Library
Dependents: unzen_sample_nucleo_f746 skeleton_unzen_nucleo_f746 ifmag_noise_canceller synthesizer_f746
オーディオ信号処理用のライブラリです。
mbed-dspのフィルタ群向けに作ったクラス・ラッパーのほか、以下のクラスを用意しています。
- ヒステリシス
- sin/cosオシレータ
- リミッター
クラスは全て名前空間amakusaに含まれます。
Diff: oscsincos.cpp
- Revision:
- 0:058daa59980d
- Child:
- 2:fbdf03b330ae
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/oscsincos.cpp Sun Dec 11 21:04:06 2016 +0000 @@ -0,0 +1,68 @@ +#include <cstdlib> +#include <cmath> + +#include "amakusa.h" + +#ifndef M_PI +#define M_PI 3.141592653589793F +#endif + +amakusa::OSCSinCos::OSCSinCos( float freq, int Fs ) +{ + this->sampleFreq = Fs; + this->setFrequency( freq ); + this->setPhase( 0.0 ); // must call after setFrequenc() +} + +void amakusa::OSCSinCos::run( float *s, float *c , int count ) +{ + for ( int i=0; i<count; i++ ) + { + // to make the expression simple + float phase = this->deltaPhase * ( i+ this->phaseInSample ); + + s[i] = std::sin( phase ); + // skip cosine if c is NULL + if ( c != NULL ) + c[i] = std::cos( phase ); + } + // update the internal phase + this->phaseInSample += count; + // truncate the phase + while ( this->phaseInSample >= this->sampleFreq ) + this->phaseInSample -= this->sampleFreq; +} + + +void amakusa::OSCSinCos::setFrequency( float freq ) +{ + // convert frequency to the update phase per sample. + this->deltaPhase = 2 * M_PI * freq / this->sampleFreq; +} + + // put the phase into the range [0, 2pi) +void amakusa::OSCSinCos::setPhase( float phase ) +{ + // convert phisical phase to the phase in sample. + this->phaseInSample = phase * this->sampleFreq / ( 2 * M_PI ); + + // truncate phase in to [0,2pi) + while ( this->phaseInSample < 0 ) + this->phaseInSample += this->sampleFreq; + while ( this->phaseInSample >= this->sampleFreq ) + this->phaseInSample -= this->sampleFreq; +} + +float amakusa::OSCSinCos::getFrequency() +{ + // convert sample + return this->deltaPhase * this->sampleFreq / ( 2 * M_PI ); +} + /** + * \brief get the internal phase + * \returns Phase in radian + */ +float amakusa::OSCSinCos::getPhase() +{ + return this->phaseInSample * 2 * M_PI / this->sampleFreq; +}