AngryLawyer/rust-sdl2 — pull request #75 by andelf (2014-04-11 17:15:48+00:00)
Title: Add sdl audio support.

this fixes #14 .
* implement sdl_audio in audio.rs (except deprecated APIs)
* fix RWops usage in surface.rs ( unhandled unwrap() )

TODO: add example code

usage: (code needs clean up)
```rust
#![feature(globs, macro_rules)]

extern crate rand;
extern crate sdl2;
extern crate native;
extern crate libc;

use libc::c_void;
use sdl2::audio;

macro_rules! trying(
($e:expr) => (match $e { Ok(e) => e, Err(e) => fail!("failed: {}", e) })
)

fn main() {
sdl2::init([sdl2::InitAudio, sdl2::InitTimer]);

let n = audio::get_num_audio_drivers();
println!("SDL_GetNumAudioDrivers: {}", n);
for i in range(0, n) {
let name = audio::get_audio_driver(i);
println!("| {} => {:?}", i, name);
println!("| init => {}", audio::audio_init(name));
}

// not dummy
audio::audio_init("coreaudio");

// current SDL2 don't support recording device
let n = audio::get_num_audio_devices(1);
println!("SDL_GetNumAudioDevices(recording): {}", n);
let n = audio::get_num_audio_devices(0);
println!("SDL_GetNumAudioDevices(plaback): {}", n);

for i in range(0, n) {
let name = audio::get_audio_device_name(i, 0);
println!("| {} => {:?}", i, name);
}

//let (mut spec, music) = audio::AudioSpec::load_wav(&Path::new("./ring.wav")).unwrap();
let (mut spec, music) = audio::AudioSpec::load_wav(&Path::new("./ccheer.wav")).unwrap();

let cvt = audio::AudioCVT::new(audio::AudioU8, 1, 11025,
audio::AudioF32LSB, 1, 44100).unwrap();

let ret = cvt.convert(music);
let music = ret.unwrap();
spec.format = audio::AudioF32LSB;
spec.freq = 44100;

let mut calls = 0;
let mut pos = 0;
let blk = |v: &mut [u8]| {
let l = v.len();
println!("got l : {}", l);
println!("buf size: {} pos: {}, music len: {}", l, pos, music.len());

for i in range(0, l) {
v[i] = if calls > 100 || pos + i >= music.len() {
0u8
} else {
music.as_slice()[pos + i]
};
}
pos += l;
calls += 1;
};

spec.callback = &blk;

match audio::AudioDevice::open("Built-in Output", 0, spec) {
Err(e) => println!("open error: {}", e),
Ok((dev, spec)) => {
println!("debug dev ={:?}", dev);
println!("spec = {:?}", spec);
println!("status => {}", dev.get_status());
dev.resume();
}
}

println!("SDL_GetCurrentAudioDriver: {:?}", audio::get_current_audio_driver());

sdl2::timer::delay(20000);
audio::audio_quit();
sdl2::quit();
}
```