Using tracks

The Cinder Track class provides more control over a sound file. Let's go over a couple of features of the Track class that we might use most often.

To loop the loaded audio file, we will need to add a Track object to the Output object and save the returned track reference so that we can use it to control the sound later. Add a variable for storing the track reference in the class declaration:

audio::TrackRef trackRef;

Replace the code in the setup() method implementation with the following:

src = audio::load(loadAsset("sample.wav"));
trackRef = audio::Output::addTrack(src);

This will basically do the same as before for now, except that it will store the audio::Source pointer as a separate track in the audio::Output object. The addTrack() function has another parameter, autoplay, which is automatically set to true if we don't provide the other value. Let's try to set it to false:

trackRef = audio::Output::addTrack(src, false);

As we set the autoplay parameter to false, audio file is not played after it is loaded. We can use this approach if we want the audio file to be played at some other point of the application flow. Use the following code to play it:

trackRef->play();

We used the arrow syntax here because the track reference is just a C++ pointer to the actual Track object. All other properties of the track are accessed in the same manner.

We can add more than one track to the Output object by creating a new audio source object and adding it to the audio::Output pointer. We won't do it now, but keep in mind that this is possible in case we want to create some kind of multilayer audio application.

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

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