Generating a sine oscillator
In this recipe, we will learn how to generatively create a sine wave oscillator by manipulating the sound card's PCM (Pulse-code Modulation ) audio buffer. The frequency of the sine wave will be defined by the mouse's y coordinate.
We will also draw the sine wave for a visual representation.
Getting ready
Include the following files:
#include "cinder/audio/Output.h" #include "cinder/audio/Callback.h" #include "cinder/Rand.h" #include "cinder/CinderMath.h"
And add the following useful using
statements:
using namespace ci; using namespace ci::app; using namespace std;
How to do it…
We will create a sine wave oscillator using the following steps:
Declare the following member variables and the callback method:
void audioCallback( uint64_t inSampleOffset, uint32_t ioSampleCount, audio::Buffer32f *buffer ); float mFrequency; float mPhase, mPhaseAdd; vector<float> mOutput;
In the
setup
module we will initialize the variables and create the audio callback using the following...