Using audio input

How about using a microphone or line-in as the audio source? No problem. To make this possible, we will need to import the audio Input library:

#include "cinder/audio/Input.h"

We are going to extend our current application so that we can switch between live input and playback of the loaded file. We will need to declare two new variables for this:

audio::Input input;
bool useInput;

The first variable represents our sound input, and we will use the second one as a switch.

Next, we have to initialize the input variable, so go to the setup() method implementation and add the following line there:

input = audio::Input();

This assigns the default audio input to the input variable.

Let's use the mouseDown method override and fill it with the following code:

useInput = !useInput;
    
if ( useInput ) {
    input.start();
    trackRef->stop();
} else {
    input.stop();
    trackRef->play();
}

These lines of code handle the switching between live and loaded input. Every time the mouse is clicked, the useInput variable is inverted from false to true or vice versa. Depending on the value we get, we enable the live or loaded input.

Finally, replace the code in the update() method implementation as follows:

if ( useInput ) pcmBuffer = input.getPcmBuffer();
else pcmBuffer = trackRef->getPcmBuffer();

These lines of code get the current PCM buffer. If the useInput variable is set to true, we use the PCM buffer of the live input, if not, we use the PCM buffer of the loaded track.

Compile and run the application. Click on the window to enable live input. Click again to return to the loaded sound.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.131.212