diff --git a/src/sdl2/audio.rs b/src/sdl2/audio.rs index 1fec594f..c6616095 100644 --- a/src/sdl2/audio.rs +++ b/src/sdl2/audio.rs @@ -255,7 +255,7 @@ impl, CB: AudioCallback> AudioSpecDesired { } } - pub fn open_audio_device(self, device: Option<&str>, iscapture: bool) -> SdlResult> { + pub fn open_audio_device(self, device: Option<&str>, iscapture: bool) -> SdlResult>> { use std::mem::uninitialized; use std::mem::transmute; use std::ptr::null; @@ -287,12 +287,12 @@ impl, CB: AudioCallback> AudioSpecDesired { false => AudioDeviceID::PlaybackDevice(id) }; - let (spec, callback) = AudioSpec::convert_from_ll(obtained); + let (spec, callback) = AudioSpec::convert_from_ll_box(obtained); Ok(AudioDevice { device_id: device_id, spec: spec, - callback: callback + callback_data: callback }) } } @@ -303,6 +303,8 @@ impl, CB: AudioCallback> AudioSpecDesired { #[deriving(Clone, Show)] pub struct AudioSpec { pub freq: i32, + // TODO: Showing format should be prettier + pub format: AudioFormat, pub channels: u8, pub silence: u8, pub samples: u16, @@ -310,12 +312,12 @@ pub struct AudioSpec { } impl AudioSpec { - fn convert_from_ll, CB: AudioCallback>(spec: ll::SDL_AudioSpec) - -> (AudioSpec, Box) { + fn convert_from_ll_box(spec: ll::SDL_AudioSpec) -> (AudioSpec, Box) { use std::mem::transmute; unsafe { (AudioSpec { freq: spec.freq, + format: spec.format, channels: spec.channels, silence: spec.silence, samples: spec.samples, @@ -346,16 +348,16 @@ impl Drop for AudioDeviceID { } } -/// Wraps SDL_AudioDeviceID and owns the callback used by the audio device. -pub struct AudioDevice, CB: AudioCallback> { +/// Wraps SDL_AudioDeviceID and owns the callback data used by the audio device. +pub struct AudioDevice { device_id: AudioDeviceID, /// Every audio device corresponds to an SDL_AudioSpec. spec: AudioSpec, /// Store the callback to keep it alive for the entire duration of `AudioDevice`. - callback: Box + callback_data: CB } -impl, CB: AudioCallback> AudioDevice { +impl AudioDevice { pub fn get_status(&self) -> AudioStatus { unsafe { let status = ll::SDL_GetAudioDeviceStatus(self.device_id.id()); @@ -378,34 +380,34 @@ impl, CB: AudioCallback> AudioDevice { /// When the returned lock guard is dropped, `SDL_UnlockAudioDevice` is /// called. /// Use this method to read and mutate callback data. - pub fn lock<'a>(&'a mut self) -> AudioDeviceLockGuard<'a, T, CB> { + pub fn lock<'a>(&'a mut self) -> AudioDeviceLockGuard<'a, CB> { unsafe { ll::SDL_LockAudioDevice(self.device_id.id()) }; AudioDeviceLockGuard { device: self } } - pub fn close_and_get_callback(self) -> Box { + pub fn close_and_get_callback(self) -> CB { drop(self.device_id); - self.callback + self.callback_data } } /// Similar to `std::sync::MutexGuard`, but for use with `AudioDevice::lock()`. -pub struct AudioDeviceLockGuard<'a, T: AudioFormatNum, CB: AudioCallback + 'a> { - device: &'a mut AudioDevice +pub struct AudioDeviceLockGuard<'a, CB: 'a> { + device: &'a mut AudioDevice } -impl<'a, T: AudioFormatNum, CB: AudioCallback> Deref for AudioDeviceLockGuard<'a, T, CB> { - fn deref(&self) -> &CB { &*self.device.callback } +impl<'a, CB> Deref for AudioDeviceLockGuard<'a, CB> { + fn deref(&self) -> &CB { &self.device.callback_data } } -impl<'a, T: AudioFormatNum, CB: AudioCallback> DerefMut for AudioDeviceLockGuard<'a, T, CB> { - fn deref_mut(&mut self) -> &mut CB { &mut *self.device.callback } +impl<'a, CB> DerefMut for AudioDeviceLockGuard<'a, CB> { + fn deref_mut(&mut self) -> &mut CB { &mut self.device.callback_data } } #[unsafe_destructor] -impl<'a, T: AudioFormatNum, CB: AudioCallback> Drop for AudioDeviceLockGuard<'a, T, CB> { +impl<'a, CB> Drop for AudioDeviceLockGuard<'a, CB> { fn drop(&mut self) { unsafe { ll::SDL_UnlockAudioDevice(self.device.device_id.id()) } }