Chapter 9. Enter Sound – Adding Sound and Audio

In this chapter, we will talk a bit about the concept of sound in creative coding.

In this chapter we will learn the following:

  • How to load and play sound in Cinder
  • How to modify sound in real time
  • How to use audio data to draw and animate
  • How to make use of live sound input

Loading and playing a sound file

There are several ways of using sound in creative coding. One way is to use audio samples, another one is to use live input, and then there is the possibility of generating sound from scratch. Nevertheless, a lot more possibilities emerge when you start to combine all of these approaches.

In this chapter, we will learn to load, play, and visualize audio files and capture live input. To get started, open TinderBox and make another project with the name BasicAudio. Open xcode/BasicAudio.xcodeproj (vc10BasicAudio.sln on Windows). Open BasicAudioApp.cpp in the editor.

To use the basic audio features of Cinder, we have to import the appropriate libraries:

#include "cinder/audio/Output.h"

This particular one contains all the code that is needed to load and play back an audio file. Go and find your own audio file (mp3 or wav will work), and place it in the project's assets folder.

We will need an audio::SourceRef object for storing the reference to the audio source that we will use in our code. Add the following line to your class declaration:

audio::SourceRef src;

Add the following code snippet into the setup() method implementation:

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

This will load the audio file into the computer memory as the audio::Source object and save a reference in the src variable. Then, we can play the sound by using the audio::Output::play() class method.

Compile and run our application. You should hear the sound. There are some limitations with this way of playing the sound back. It is not possible to apply the loop control to it in any other way (except for setting and getting the volume).

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

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