7.9.2. AUDIO-02 — Playing Audio Files
This tutorial covers loading audio from files on disk, decoding compressed formats to PCM, and looping playback.
7.9.2.1. Playing from File
play_sound_from_file loads and decodes a file directly on the audio
thread. It supports WAV, MP3, and FLAC. You specify the expected sample
rate and channel count:
let path = "{get_das_root()}/examples/media/audio/gong.wav"
let sid = play_sound_from_file(path, MA_SAMPLE_RATE, 2)
The function returns a SID immediately. Decoding happens in the background — there may be a brief delay before audio is audible for large files.
7.9.2.2. Decoding Audio
For more control, decode the file to an array<float> of interleaved PCM
samples using decode_audio. This lets you inspect properties (channels,
sample rate, duration) before playback, or process the data.
The file must first be loaded into memory with fopen / fmap:
var channels : int
var rate : int
var inscope samples : array<float>
fopen(path, "rb") <| $(f) {
if (f == null) {
print("ERROR: Could not open file\n")
return
}
fmap(f) <| $(data) {
samples <- decode_audio(data, channels, rate)
}
}
decode_audio takes the raw file bytes (via fmap) and writes the
channel count and sample rate to out-parameters. The returned array
contains interleaved samples — for stereo audio, samples alternate
left/right.
After decoding you can compute the duration:
let num_frames = length(samples) / channels
let duration = float(num_frames) / float(rate)
Then play with play_sound_from_pcm as in the previous tutorial:
let sid = play_sound_from_pcm(rate, channels, samples)
7.9.2.3. Looping Playback
play_sound_loop_from_pcm plays samples in an infinite loop until
explicitly stopped. This is useful for background music, ambient sounds,
or drones:
let half_second = MA_SAMPLE_RATE / 2
var samples <- [for (x in range(half_second));
sin(2.0 * PI * 330.0 * float(x) / float(MA_SAMPLE_RATE))]
let sid = play_sound_loop_from_pcm(MA_SAMPLE_RATE, 1, samples)
// Let it loop for 2 seconds, then stop
sleep(2000u)
stop(sid, 0.0)
stop(sid, 0.0) stops the sound immediately. A non-zero second argument
fades out over that many seconds (covered in the next tutorial).
7.9.2.4. Running the Tutorial
daslang.exe tutorials/dasAudio/02_playing_files.das
The tutorial plays a WAV file from disk, decodes and plays an MP3 (first 3 seconds), then loops a short generated tone for 2 seconds before stopping.
See also
Full source: tutorials/dasAudio/02_playing_files.das
Next tutorial: AUDIO-03 — Sound Control