A SERVICE OF

logo

176
Chapter 15: Adding Sounds To Virtual Worlds
Manipulating the Audio Samples Directly
csAudioSample::samples() returns the multivalued array eld that contains the actual
audio samples. This handle allows you to directly manipulate the array eld. For
example, to set a sound value, use the following code:
samples->set(index, value);
To set the number of samples and then edit the array directly, use the following code:
samples->setCount(16*44400);
char *samps = samples->edit();
for(i=0;i<16*44400);i++)
samps[i]=DETERMINE_SAMPLE(i);
samples->editDone();
Example Setting a csAudioSamples Node
Example 15-2 sets all of the elds in a csAudioSamples node.
Example 15-2 Setting the Fields in an csAudioSamples Object
// create an audio sample node
csAudioSamples* horn = new csAudioSamples;
// attach the audio sample node to a specific file
horn->setFileName(truck_horn.xxx);
// Set the parameters for the source sound file
horn->setNumFrames(44000.0);
horn->setSampleRate(44000.0);
horn->setSampleSize(2000);
horn->setSampleType(UNSIGNED_INT_SAMPLE_TYPE);
horn->setNumChannels(2);
horn->setSampleScale(1.0);
...
// Load the sound sample by setting the audiosample filename
horn->load();
// Make sure the sound loaded successfully
if (horn->getLoadStatus() == LOAD_FAILED)
abort();
When load() is called, Cosmo 3D reads the samples in the le into the sample eld directly.