Expand description

Use rg3d-sound with SDL’s audio subsystem

This crate allows you to use SDL2’s audio backend to output rendered audio data from rg3d_sound. This provides maximum portability between operating systems as SDL audio works almost everywhere SDL does, and it will also take advantage of newer audio interfaces like Pulseaudio and PipeWire on Linux.

Example

use std::{fs::File, io::BufReader, thread, time::Duration};

use rg3d_sound::{
buffer::{DataSource, SoundBufferResource},
context::SoundContext,
source::{generic::GenericSourceBuilder, Status},
};

let sdl = sdl2::init()?;
let audio = sdl.audio()?;
let (engine, device) = rg3d_sound_sdl::open(&audio, None)?;
device.resume();

let ctx = SoundContext::new();
engine.lock().unwrap().add_context(ctx.clone());

let sound_buffer = SoundBufferResource::new_generic(DataSource::File {
path: "ding.wav".into(),
data: BufReader::new(File::open("ding.wav")?),
})
.expect("Failed to create data source");

let source = GenericSourceBuilder::new()
.with_buffer(sound_buffer)
.with_status(Status::Playing)
.build_source()?;

ctx.state().add_source(source);

thread::sleep(Duration::from_millis(1090));

Structs

An AudioCallback used to feed the SDL audio device with rendered audio from a SoundEngine

Functions

Obtain the desired SDL audio parameters for use with rg3d_sound. This is used internally by open to configure the playback device.

Opens a new audio device.

Converts a slice of f32 values, of even length, to a slice of (f32, f32) tuples. The returned slice will be half the length of the input slice.