19 Audio
This section covers audio playback using SDL3’s stream-based audio model.
Open an audio device (or use the default)
Create audio streams with source/destination formats
Bind streams to devices
Push audio data into streams
SDL automatically mixes and plays
19.1 Audio Specs
Audio specs describe the format of audio data.
procedure
(make-audio-spec format channels freq) → SDL_AudioSpec?
format : exact-nonnegative-integer? channels : exact-positive-integer? freq : exact-positive-integer?
SDL_AUDIO_U8 —
Unsigned 8-bit SDL_AUDIO_S8 —
Signed 8-bit SDL_AUDIO_S16 —
Signed 16-bit SDL_AUDIO_S32 —
Signed 32-bit SDL_AUDIO_F32 —
32-bit float
channels is 1 for mono, 2 for stereo.
freq is the sample rate in Hz (e.g., 44100, 48000).
(define spec (make-audio-spec SDL_AUDIO_S16 2 44100))
procedure
spec : SDL_AudioSpec?
procedure
spec : SDL_AudioSpec?
procedure
spec : SDL_AudioSpec?
19.2 Device Enumeration
procedure
→ (listof (cons/c exact-nonnegative-integer? string?))
Each element is (cons device-id device-name).
(for ([dev (audio-playback-devices)]) (printf "Device ~a: ~a~n" (car dev) (cdr dev)))
procedure
→ (listof (cons/c exact-nonnegative-integer? string?))
procedure
(audio-device-name device-id) → (or/c string? #f)
device-id : exact-nonnegative-integer?
19.3 Device Management
procedure
(open-audio-device [device-id spec]) → exact-nonnegative-integer?
device-id : (or/c exact-nonnegative-integer? #f) = #f spec : (or/c SDL_AudioSpec? #f) = #f
If device-id is #f, opens the default playback device. If spec is #f, uses the system default format.
Returns the device ID.
;; Open default device with default format (define dev (open-audio-device)) ;; Open default device with specific format (define spec (make-audio-spec SDL_AUDIO_S16 2 44100)) (define dev2 (open-audio-device #f spec))
procedure
(close-audio-device! device) → void?
device : exact-nonnegative-integer?
procedure
(pause-audio-device! device) → void?
device : exact-nonnegative-integer?
procedure
(resume-audio-device! device) → void?
device : exact-nonnegative-integer?
procedure
(audio-device-paused? device) → boolean?
device : exact-nonnegative-integer?
procedure
(audio-device-format [device-id])
→
SDL_AudioSpec? exact-nonnegative-integer?
device-id : exact-nonnegative-integer? = SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK
procedure
(audio-device-gain device-id) → real?
device-id : exact-nonnegative-integer?
procedure
(set-audio-device-gain! device-id gain) → void?
device-id : exact-nonnegative-integer? gain : real?
19.4 Audio Streams
Audio streams handle format conversion and buffering.
procedure
(make-audio-stream src-spec [dst-spec]) → cpointer?
src-spec : SDL_AudioSpec? dst-spec : (or/c SDL_AudioSpec? #f) = #f
If dst-spec is #f, uses the same format as the source.
(define src-spec (make-audio-spec SDL_AUDIO_S16 2 44100)) (define stream (make-audio-stream src-spec))
procedure
(open-audio-device-stream [ device-id spec #:callback callback #:userdata userdata]) → cpointer?
device-id : exact-nonnegative-integer? = SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK spec : (or/c SDL_AudioSpec? #f) = #f callback : (or/c procedure? #f) = #f userdata : any/c = #f
The device starts paused. Call resume-audio-stream-device! to start.
4 args: (userdata stream additional-bytes total-bytes)
3 args: (stream additional-bytes total-bytes)
2 args: (additional-bytes total-bytes)
;; Simple device+stream setup (define stream (open-audio-device-stream)) (resume-audio-stream-device! stream)
procedure
(destroy-audio-stream! stream) → void?
stream : cpointer?
procedure
(audio-stream-device stream) → exact-nonnegative-integer?
stream : cpointer?
procedure
(bind-audio-stream! device stream) → void?
device : exact-nonnegative-integer? stream : cpointer?
procedure
(unbind-audio-stream! stream) → void?
stream : cpointer?
procedure
(pause-audio-stream-device! stream) → void?
stream : cpointer?
procedure
(resume-audio-stream-device! stream) → void?
stream : cpointer?
procedure
(audio-stream-device-paused? stream) → boolean?
stream : cpointer?
19.5 Stream Operations
procedure
(audio-stream-put! stream data length) → void?
stream : cpointer? data : cpointer? length : exact-nonnegative-integer?
data is a pointer to the audio data. length is the number of bytes.
procedure
(audio-stream-available stream) → exact-nonnegative-integer?
stream : cpointer?
procedure
(audio-stream-clear! stream) → void?
stream : cpointer?
procedure
(audio-stream-flush! stream) → void?
stream : cpointer?
procedure
(play-audio! stream data length) → void?
stream : cpointer? data : cpointer? length : exact-nonnegative-integer?
Same as audio-stream-put!.
19.6 WAV Loading
procedure
(load-wav source) →
SDL_AudioSpec? cpointer? exact-nonnegative-integer? source : (or/c string? path? bytes? input-port?)
Returns (values audio-spec audio-data length).
The audio-data pointer must be freed with free-audio-data!.
(define-values (spec data len) (load-wav "sound.wav")) ;; Play the sound (audio-stream-put! stream data len) ;; Free when done (free-audio-data! data)
procedure
(free-audio-data! data) → void?
data : cpointer?
19.7 Audio Mixing and Conversion
procedure
(mix-audio! dst src format length [volume]) → void?
dst : cpointer? src : cpointer? format : exact-nonnegative-integer? length : exact-nonnegative-integer? volume : real? = 1.0
volume is a multiplier (1.0 = full volume).
procedure
(convert-audio-samples src-spec src-data src-length dst-spec)
→
cpointer? exact-nonnegative-integer? src-spec : SDL_AudioSpec? src-data : cpointer? src-length : exact-nonnegative-integer? dst-spec : SDL_AudioSpec?
Returns (values dst-data dst-length).
The returned data must be freed with free-audio-data!.
procedure
(audio-format-name format) → string?
format : exact-nonnegative-integer?
19.8 Audio Format Constants
19.9 Example: Playing a Sound
Here’s a complete example of loading and playing a WAV file:
#lang racket/base (require sdl3) (with-sdl ;; Initialize audio (sdl-init! 'audio) ;; Open default audio device and stream (define stream (open-audio-device-stream)) ;; Load a WAV file (define-values (spec data len) (load-wav "sound.wav")) ;; Start audio playback (resume-audio-stream-device! stream) ;; Play the sound (audio-stream-put! stream data len) ;; Wait for sound to finish (let loop () (when (> (audio-stream-available stream) 0) (delay! 100) (loop))) ;; Cleanup (free-audio-data! data) (destroy-audio-stream! stream))