My Project
Loading...
Searching...
No Matches
Audio

Introduction

An interactive application would be incomplete without some kind of audio, be it background music or sound effects. CHAI3D's audio classes incorporate the flexible and powerful OpenAL framework. CHAI3D currently support the WAV audio file format and has sophisticated features for playing sounds in 3D space associated with tools and material properties of objects.

Basic Concepts

In real life, sounds are emitted by objects and heard by listeners. The way a sound is perceived depends on a number of factors. A listener can tell roughly which direction a sound is coming from and may also get some sense of its distance from its loudness and quality. A fast-moving sound source (like a aircraft or a passing police car) will change in pitch as it moves as a result of the Doppler Effect.

Audio Sources and Listener.

To simulate the effects of position, CHAI3D requires sounds to originate from Audio Sources (cAudioSource ) attached to objects. The sounds emitted are then picked up by an Audio Listener (cAudioDevice ) attached to another object, most often the main camera. CHAI3D can then simulate the effects of a source's distance and position from the listener object and play them to the user accordingly. The relative speed of the source and listener objects can also be used to simulate the Doppler Effect for added realism.

using namespace chai3d;
// create an audio device to play sounds
cAudioDevice* audioDevice = new cAudioDevice();
// attach audio device to camera
camera->attachAudioDevice(audioDevice);
// create an audio buffer
cAudioBuffer* audioBuffer = audioDevice->newAudioBuffer();
// load a WAV file
audioBuffer->loadFromFile("sound.wav");
// create an audio source
cAudioSource* audioSource = audioDevice->newAudioSource();
// assign audio buffer to audio source
audioSource->setAudioBuffer(audioBuffer);
// loop playing of sound
audioSource->setLoop(true);
// set audio gain
audioSource->setGain(1.0);
// set audio pitch
audioSource->setPitch(0.2);
// play sound
audioSource->play();
This class implements an audio buffer.
Definition CAudioBuffer.h:79
bool loadFromFile(const std::string &a_filename)
This method loads an audio file by passing the filename as argument.
Definition CAudioBuffer.cpp:201
This class implements an audio device context.
Definition CAudioDevice.h:82
This class implements an audio source.
Definition CAudioSource.h:79
bool play()
This method plays the audio buffer.
Definition CAudioSource.cpp:116
bool setGain(const float a_gain)
This method sets the audio gain value. Set value to 0.0 to disable sound.
Definition CAudioSource.cpp:309
bool setAudioBuffer(cAudioBuffer *a_audioBuffer)
This method sets the audio buffer.
Definition CAudioSource.cpp:98
bool setPitch(const float a_pitch)
This method sets the audio pitch. The default value is 1.0.
Definition CAudioSource.cpp:287
bool setLoop(const bool a_loop)
This method enables or disables loop playing.
Definition CAudioSource.cpp:384
Definition CAudioBuffer.cpp:57

The manual for OpenAL can be found in the CHAI3D documentation folder and gives more information about the many options and parameters available for getting effects just right.