1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//! Playdate sound-source API

use core::ffi::c_float;

use sys::ffi::sndCallbackProc;
use sys::ffi::SoundSource as OpaqueSoundSource;
use sys::traits::AsRaw;


#[cfg_attr(feature = "bindings-derive-debug", derive(Debug))]
pub struct SoundSource<Api = api::Default>(*mut OpaqueSoundSource, Api);

impl<Api> AsRaw for SoundSource<Api> {
	type Type = OpaqueSoundSource;
	unsafe fn as_raw(&self) -> *mut Self::Type { self.0 }
}

impl<Api: Default> From<*mut OpaqueSoundSource> for SoundSource<Api> {
	fn from(ptr: *mut OpaqueSoundSource) -> Self { Self(ptr, Default::default()) }
}

impl<Api: api::Api> SoundSource<Api> {
	pub fn from_with(api: Api, ptr: *mut OpaqueSoundSource) -> Self { Self(ptr, api) }

	/// Returns `true` if the source is currently playing.
	///
	/// Equivalent to [`sys::ffi::playdate_sound_source::isPlaying`]
	#[doc(alias = "sys::ffi::playdate_sound_source::isPlaying")]
	pub fn is_playing(&self) -> bool {
		let f = self.1.is_playing();
		unsafe { f(self.0) == 1 }
	}

	/// Gets the playback volume (`0.0` - `1.0`) for `left` and `right` channels of the source.
	///
	/// Equivalent to [`sys::ffi::playdate_sound_source::getVolume`]
	#[doc(alias = "sys::ffi::playdate_sound_source::getVolume")]
	pub fn get_volume(&self) -> (c_float, c_float) {
		let mut l = 0.;
		let mut r = 0.;
		let f = self.1.get_volume();
		unsafe { f(self.0, &mut l, &mut r) };
		(l, r)
	}

	/// Sets the playback volume (`0.0` - `1.0`) for `left` and `right` channels of the source.
	///
	/// Equivalent to [`sys::ffi::playdate_sound_source::setVolume`]
	#[doc(alias = "sys::ffi::playdate_sound_source::setVolume")]
	pub fn set_volume(&self, left: c_float, right: c_float) {
		let f = self.1.set_volume();
		unsafe { f(self.0, left, right) }
	}


	/// Equivalent to [`sys::ffi::playdate_sound_source::setFinishCallback`]
	#[doc(alias = "sys::ffi::playdate_sound_source::setFinishCallback")]
	pub fn set_finish_callback_raw(&self, callback: sndCallbackProc) {
		let f = self.1.set_finish_callback();
		unsafe { f(self.0, callback) }
	}
}


pub mod api {
	use core::ffi::c_float;
	use core::ptr::NonNull;

	use sys::ffi::SoundSource;
	use sys::ffi::sndCallbackProc;
	use sys::ffi::playdate_sound_source;

	/// Default sound source api end-point, ZST.
	///
	/// All calls approximately costs ~4 derefs.
	#[derive(Debug, Clone, Copy, core::default::Default)]
	pub struct Default;
	impl Api for Default {}


	/// Cached sound source api end-point.
	///
	/// Stores one reference, so size on stack is eq `usize`.
	///
	/// All calls approximately costs ~1 deref.
	#[derive(Clone, Copy)]
	#[cfg_attr(feature = "bindings-derive-debug", derive(Debug))]
	pub struct Cache(&'static playdate_sound_source);

	impl core::default::Default for Cache {
		fn default() -> Self { Self(sys::api!(sound.source)) }
	}

	impl From<*const playdate_sound_source> for Cache {
		#[inline(always)]
		fn from(ptr: *const playdate_sound_source) -> Self { Self(unsafe { ptr.as_ref() }.expect("snd.src")) }
	}

	impl From<&'static playdate_sound_source> for Cache {
		#[inline(always)]
		fn from(r: &'static playdate_sound_source) -> Self { Self(r) }
	}

	impl From<NonNull<playdate_sound_source>> for Cache {
		#[inline(always)]
		fn from(ptr: NonNull<playdate_sound_source>) -> Self { Self(unsafe { ptr.as_ref() }) }
	}

	impl From<&'_ NonNull<playdate_sound_source>> for Cache {
		#[inline(always)]
		fn from(ptr: &NonNull<playdate_sound_source>) -> Self { Self(unsafe { ptr.as_ref() }) }
	}


	impl Api for Cache {
		#[inline(always)]
		fn set_volume(&self) -> unsafe extern "C" fn(c: *mut SoundSource, lvol: c_float, rvol: c_float) {
			self.0.setVolume.expect("setVolume")
		}

		#[inline(always)]
		fn get_volume(&self) -> unsafe extern "C" fn(c: *mut SoundSource, outl: *mut c_float, outr: *mut c_float) {
			self.0.getVolume.expect("getVolume")
		}

		#[inline(always)]
		fn is_playing(&self) -> unsafe extern "C" fn(c: *mut SoundSource) -> core::ffi::c_int {
			self.0.isPlaying.expect("isPlaying")
		}

		#[inline(always)]
		fn set_finish_callback(&self) -> unsafe extern "C" fn(c: *mut SoundSource, callback: sndCallbackProc) {
			self.0.setFinishCallback.expect("setFinishCallback")
		}
	}


	pub trait Api {
		/// Returns [`sys::ffi::playdate_sound_source::setVolume`]
		#[doc(alias = "sys::ffi::playdate_sound_source::setVolume")]
		#[inline(always)]
		fn set_volume(&self) -> unsafe extern "C" fn(c: *mut SoundSource, lvol: c_float, rvol: c_float) {
			*sys::api!(sound.source.setVolume)
		}

		/// Returns [`sys::ffi::playdate_sound_source::getVolume`]
		#[doc(alias = "sys::ffi::playdate_sound_source::getVolume")]
		#[inline(always)]
		fn get_volume(&self) -> unsafe extern "C" fn(c: *mut SoundSource, outl: *mut c_float, outr: *mut c_float) {
			*sys::api!(sound.source.getVolume)
		}

		/// Returns [`sys::ffi::playdate_sound_source::isPlaying`]
		#[doc(alias = "sys::ffi::playdate_sound_source::isPlaying")]
		#[inline(always)]
		fn is_playing(&self) -> unsafe extern "C" fn(c: *mut SoundSource) -> core::ffi::c_int {
			*sys::api!(sound.source.isPlaying)
		}

		/// Returns [`sys::ffi::playdate_sound_source::setFinishCallback`]
		#[doc(alias = "sys::ffi::playdate_sound_source::setFinishCallback")]
		#[inline(always)]
		fn set_finish_callback(&self) -> unsafe extern "C" fn(c: *mut SoundSource, callback: sndCallbackProc) {
			*sys::api!(sound.source.setFinishCallback)
		}
	}
}