Visualizing audio

There is one important thing missing in our creative sound application—the visual side of it. Let's create a simple equalizer.

First, we will need to enable the PCM buffering of the audio track.

PCM stands for pulse code modulation and it is a method for digitally representing sampled analog signals. An analog audio signal is the fluctuation of voltage inside a conductor. A digital representation of that are samples or bytes with floating point values usually from -1 to 1.

In the world of audio sampling there is a term, sampling rate, which represents the amount of samples or values being sampled each second. The PCM values and sampling rate of an audio file determine its playback speed.

To enable PCM buffering and be able to read PCM values during application runtime, add the following code in the setup() method's declaration before trackRef->play():

trackRef->enablePcmBuffering( true );

Second, we need a variable that will serve as a pointer to the track's PCM buffer.

A PCM buffer is the secret place in computer memory where the raw values of the sound wave coming out from our speakers reside. Before any sound is sent to the audio output, it is kept in the buffer for some time. When the sound is played, the buffer is cleared and filled again with new audio data.

We will need to access the PCM buffer to read the raw waveform values from it. Add the following line to the class declaration to add a variable that will serve as a reference to it:

audio::PcmBuffer32fRef pcmBuffer;

Third, we need to get a copy of the most recent buffer in each frame. Add the following code to the update() method implementation:

pcmBuffer = trackRef->getPcmBuffer();

Finally, change the draw() method implementation as follows:

void BasicAudioApp::draw() {
  // clear the screen by drawing
  // a semi-transparent black rectangle all over the screen
  gl::enableAlphaBlending();
  gl::color( 0.f, 0.f, 0.f, 0.1f );
  gl::drawSolidRect(getWindowBounds());

  if( !pcmBuffer ) {
    gl::disableAlphaBlending();
    return; // stop here if the buffer is empty
  }

  // get copy of the left channel data from the pcmBuffer
  audio::Buffer32fRef buffer =
  pcmBuffer->getChannelData( audio::CHANNEL_FRONT_LEFT );

  // get buffer length
  uint32_t bufferLength = pcmBuffer->getSampleCount();

  // calculate scale for mapping the buffer data on the screen
  float scale = getWindowWidth() / (float)bufferLength;
  // set color to cyan
  gl::color( 0.f, 1.f ,1.f ,0.8f );

  // loop through current buffer data
  // in steps of 10 and construct waveform
  for( int i=0; i<bufferLength; i+=10 ) {
    // map current x position of buffer value to window width
    float x = i * scale;
    
    // buffer data fluctuates from -1 to +1,
    // map it to window height
    float y = ( (buffer->mData[i]+1) * getWindowHeight()/2 );

    // draw a circle
    gl::drawStrokedCircle( Vec2f(x, y),
    ( abs(buffer->mData[i])*getWindowHeight()/2 ) );
  }

  gl::disableAlphaBlending();
}

Compile and run our application, you should see an image as shown in the following screenshot:

Visualizing audio
..................Content has been hidden....................

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