use crate::{util::Ref, PLAYDATE};
use super::{AudioSample, SoundSource};
pub(crate) struct PlaydateSamplePlayer {
handle: *const sys::playdate_sound_sampleplayer,
}
impl PlaydateSamplePlayer {
pub(crate) fn new(handle: *const sys::playdate_sound_sampleplayer) -> Self {
Self { handle }
}
}
pub struct SamplePlayer {
handle: *mut sys::SamplePlayer,
}
unsafe impl Send for SamplePlayer {}
unsafe impl Sync for SamplePlayer {}
impl Default for SamplePlayer {
fn default() -> Self {
Self::new()
}
}
impl SamplePlayer {
pub fn new() -> Self {
Self {
handle: unsafe { (*(*PLAYDATE.sound.handle).sampleplayer).newPlayer.unwrap()() },
}
}
fn new_ref<'a>(handle: *mut sys::SamplePlayer) -> Ref<'a, Self> {
Ref::new(Self { handle })
}
pub(crate) fn as_sound_source(&self) -> Ref<SoundSource> {
SoundSource::new_ref(self.handle as *mut sys::SoundSource)
}
pub fn get_length(&self) -> f32 {
unsafe { (*PLAYDATE.sound.sample_player.handle).getLength.unwrap()(self.handle) }
}
pub fn is_playing(&self) -> bool {
unsafe { (*PLAYDATE.sound.sample_player.handle).isPlaying.unwrap()(self.handle) != 0 }
}
pub fn play(&self, repeat: usize, rate: f32) {
unsafe {
(*PLAYDATE.sound.sample_player.handle).play.unwrap()(self.handle, repeat as _, rate)
};
}
pub fn set_finish_callback(&self, callback: impl Send + FnOnce(&Self) + 'static) {
self.as_sound_source().set_finish_callback(move |x| {
callback(&Self::new_ref(x.handle as *mut sys::SamplePlayer));
});
}
pub fn set_offset(&self, offset: f32) {
unsafe { (*PLAYDATE.sound.sample_player.handle).setOffset.unwrap()(self.handle, offset) }
}
pub fn get_offset(&self) -> f32 {
unsafe { (*PLAYDATE.sound.sample_player.handle).getOffset.unwrap()(self.handle) }
}
pub fn set_playing_range(&self, start: usize, end: usize) {
unsafe {
(*PLAYDATE.sound.sample_player.handle).setPlayRange.unwrap()(
self.handle,
start as _,
end as _,
)
}
}
pub fn set_paused(&self, paused: bool) {
unsafe {
(*PLAYDATE.sound.sample_player.handle).setPaused.unwrap()(self.handle, paused as _)
}
}
pub fn set_rate(&self, rate: f32) {
unsafe { (*PLAYDATE.sound.sample_player.handle).setRate.unwrap()(self.handle, rate) }
}
pub fn get_rate(&self) -> f32 {
unsafe { (*PLAYDATE.sound.sample_player.handle).getRate.unwrap()(self.handle) }
}
pub fn set_sample<'a, 'b: 'a>(&'a self, sample: &'b AudioSample) {
unsafe {
(*PLAYDATE.sound.sample_player.handle).setSample.unwrap()(self.handle, sample.handle)
}
}
pub fn set_volume(&self, left: f32, right: f32) {
unsafe {
(*PLAYDATE.sound.sample_player.handle).setVolume.unwrap()(self.handle, left, right)
}
}
pub fn get_volume(&self) -> (f32, f32) {
let mut left = 0.0;
let mut right = 0.0;
unsafe {
(*PLAYDATE.sound.sample_player.handle).getVolume.unwrap()(
self.handle,
&mut left,
&mut right,
)
}
(left, right)
}
pub fn stop(&self) {
unsafe { (*PLAYDATE.sound.sample_player.handle).stop.unwrap()(self.handle) }
}
}
impl Drop for SamplePlayer {
fn drop(&mut self) {
self.as_sound_source().drop_callbacks();
unsafe { (*(*PLAYDATE.sound.handle).sampleplayer).freePlayer.unwrap()(self.handle) }
}
}