Lesson 9 : AudioBuffers


Lesson 8 : Decoding MPEG P and B Frames | Lesson 10 : More About Audio Buffers | Contents


We need to load the Basic package first.

cd c:/Dali/doc/tutorial/
package require DvmBasic

As with ByteImage, we need to allocate memory for a new Audio Buffer.

set audio [audio_16_new 10000]

Let's set the initialize the audio buffer by setting all samples to 0, offset 0, and stride 1 (see documentaion for definition of the last two parameters).

audio_16_set_some $audio 0 0 1

After that we have a perfectly silent Audio buffer with all samples being 0. But this is boring! Let's try to make some noise in the middle. We can clip a part of audio by the following commands.

set clip [audio_16_clip $audio 2000 5000]

This clips the 5000 samples from audio starting from its 2000th sample. Let's set that 5000 samples to 1.

audio_16_set_some $clip 1 0 1

Remeber clip shares memory with audio. clip is located at the middle of memory allocated for audio. The contents of audio is illustrated in the following diagram.

wpe7.jpg (18858 bytes)

We want to copy the yellow portion out to another Audio buffer. We first need to allocate memory for the 5000 samples.

set dest [audio_16_new 5000]

Then we clip the part we want to copy out from.

set yellow [audio_16_clip $audio 5000 5000]

Now we can copy it.

audio_16_copy $yellow $dest

The Audio buffer dest is then containing the exactly the same samples as the yellow portion.

What if we want to copy the yellow portion to the blue portion of the following Audio buffer?

set dest2 [audio_16_new 10000]

wpe6.jpg (9483 bytes)

Similarly, we clip the blue portion out from the destination buffer before doing the copying.

set blue [audio_16_clip $audio 2500 5000]
audio_16_copy $yellow $blue

One last thing, remember to free up the resources when you're done with them.

audio_free $audio
audio_free $dest
audio_free $yellow
audio_free $dest2
audiO_free $blue

Code for this lesson: l9.tcl.


Lesson 8 : Decoding MPEG P and B Frames | Lesson 10 : More About Audio Buffers | Contents


Last Updated :