Module audio
Available on crate feature
dep_sdl3
only.Expand description
Audio Functions
§Example
use sdl3::audio::{AudioCallback, AudioFormat, AudioSpec};
use std::time::Duration;//!
use sdl3::sys;
struct SquareWave {
phase_inc: f32,
phase: f32,
volume: f32
}
impl AudioCallback<f32> for SquareWave {
fn callback(&mut self, out: &mut [f32]) {
// Generate a square wave
for x in out.iter_mut() {
*x = if self.phase <= 0.5 {
self.volume
} else {
-self.volume
};
self.phase = (self.phase + self.phase_inc) % 1.0;
}
}
}
let sdl_context = sdl3::init().unwrap();
let audio_subsystem = sdl_context.audio().unwrap();
let desired_spec = AudioSpec {
freq: Some(44100),
channels: Some(1), // mono
format: Some(AudioFormat::S16BE) // signed 16 bit samples
};
let device = audio_subsystem.open_playback_stream(&desired_spec, |spec| {
// initialize the audio callback
SquareWave {
phase_inc: 440.0 / spec.freq as f32,
phase: 0.0,
volume: 0.25
}
}).unwrap();
// Start playback
device.resume().expect("Failed to start playback");
// Play for 2 seconds
std::thread::sleep(Duration::from_millis(2000));
Structs§
- Audio
Device - Represents an open audio device (playback or recording).
- Audio
Spec - Audio
SpecWAV - Audio
Stream - Audio
Stream With Callback - Driver
Iterator
Enums§
Traits§
- Audio
Callback - Audio
Format Num - A phantom type for retrieving the
SDL_AudioFormat
of a given generic type. All format types are returned as native-endian. - Audio
Recording Callback
Functions§
- drivers
- Gets an iterator of all audio drivers compiled into the SDL2 library.