Changing track parameters

Next thing that we want to do is to make the audio file play again after it has finished playing. It is possible by setting the looping of the track to true:

trackRef->setLooping(true);

This will make the track loop infinite.

Another thing that we might want to do is to change the track volume. You can think of the Output volume as the master volume and the Track volumes as individual track volumes of a mixer.

To change the volume, use the following code:

trackRef->setVolume(0.5f); // set track volume
audio::Output::setVolume(0.5f); // set master volume

We can use variables to dynamically change the volume of the sound. Let's do that for both master and track volumes.

Add the following lines to the class declaration of the app:

float masterVol, trackVol;
void mouseMove(MouseEvent event);

With these lines of code, we declare two floats that will store the master and track volumes and a mouseMove method override of the AppBasic class that will let us respond to mouse movements.

Let's go to the setup() method implementation and initialize the masterVol and trackVol variables, additionally change the raw values of trackRef->setVolume() and audio::Output::setVolume():

void BasicAudioApp::setup() {
  masterVol = trackVol = 0.5f;
  src = audio::load(loadAsset("sample.wav"));
  trackRef = audio::Output::addTrack(src, false);
  trackRef->setLooping(true);
  trackRef->setVolume(trackVol); // set track volume
  audio::Output::setVolume(masterVol); // set master volume
  trackRef->play();
}

Add the implementation of the mouseMove method at the end of the file, before the CINDER_APP_BASIC() call:

void BasicAudioApp::mouseMove(MouseEvent event) {
    masterVol = (float)event.getX()/getWindowWidth();
    trackVol = (float)event.getY()/getWindowHeight();
    audio::Output::setVolume(masterVol);
    trackRef->setVolume(trackVol);
}

An event object of the type MouseEvent is passed to this method. We can get the current location of the mouse cursor at the time of the event as well as many other parameters that the event object holds. We will stick to the mouse x and y coordinates for now and use them for changing the volume.

For changing the master volume, we are using the x coordinate. We are using the y coordinate for the track volume.

As volume of the sound in Cinder is defined in a range from 0.0f to 1.0f, we need to transform the mouse positions in this kind of form. To do that, we need to divide the actual mouse position with its maximum range that is the width and height of the window. So, if the mouse x position is 0 and window width is 100, after dividing the coordinate with the window width (0/100) we get 0. If the mouse x position is 100, we get 1. Additionally, if the mouse x position is 50, we get 0.5 (50/100).

As the mouse positions and window dimensions are returned as int values, we will need to cast one of the values to float for a successful floating point number operation. That is why there is (float) before the event.getX()/getWindowWidth() and event.getY()/getWindowHeight() parts of the code, it casts the int value returned by event.getX() to float. Finally, we set the master volume and track volume with audio::Output::setVolume(masterVol); and trackRef->setVolume(trackVol);.

Compile and run our application. Move the mouse, you should hear how the volume of the sound is changing.

Next useful feature that we might want to learn is jumping to a certain position of the time of the track. To do that, we will use the mouseDrag method of the AppBasic class. Let's declare an override of it by adding the following line to the class declaration:

void mouseDrag(MouseEvent event);

Let's implement that. Add the following code at the end of the file, before the CINDER_APP_BASIC() call:

void BasicAudioApp::mouseDrag(MouseEvent event) {
    double time =
    (double)event.getX()/getWindowWidth()*src->getDuration();
    trackRef->setTime( time );
}

In this example, the application window's width is being mapped to the duration of the track. The x position of the mouse is being transformed to a specific time in the audio track. The trackRef->setTime(time) part of the code sets the position of the track playhead.

Compile and run our application. You should be able to do live seeking now. Click and drag to try that out.

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

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