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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use core::ffi::c_char;
use core::ffi::c_float;
use core::ffi::c_int;
use core::ffi::c_void;

use sys::ffi::CString;
use sys::ffi::FilePlayer;
use sys::ffi::sndCallbackProc;

use fs::Path;

use super::Repeat;
use crate::error::ApiError;
use crate::error::Error;

pub mod api;


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


// ctor //

impl<Api> Player<Api> where Api: api::Api {
	pub fn new() -> Result<Player<Api>, Error>
		where Api: Default {
		let api = Api::default();
		Self::new_with(api)
	}

	pub fn new_with(api: Api) -> Result<Player<Api>, Error> {
		let f = api.new_player();
		let player = unsafe { f() };
		if player.is_null() {
			Err(Error::Alloc)
		} else {
			Ok(Player(player, api))
		}
	}
}


impl<Api: api::Api> Drop for Player<Api> {
	fn drop(&mut self) {
		if !self.0.is_null() {
			let f = self.api().free_player();
			unsafe { f(self.0) }
			self.0 = core::ptr::null_mut();
		}
	}
}


// utils //


impl<Api: api::Api> Player<Api> {
	#[inline(always)]
	pub fn api(&self) -> &Api { &self.1 }
}


// impl //

impl<Api> Player<Api> where Api: api::Api {
	/// Prepares player to stream the file at path.
	///
	/// Equivalent to [loadIntoPlayer](sys::ffi::playdate_sound_fileplayer::loadIntoPlayer)
	#[doc(alias = "sys::ffi::playdate_sound_fileplayer::loadIntoPlayer")]
	pub fn load_into_player<P: AsRef<Path>>(&self, path: P) -> Result<(), ApiError> {
		let path_cs = CString::new(path.as_ref())?;
		let path_ptr = path_cs.as_ptr() as *mut c_char;

		let f = self.api().load_into_player();
		let code = unsafe { f(self.0, path_ptr) };
		if code == 1 {
			Ok(())
		} else {
			Err(Error::FileNotExist.into())
		}
	}

	/// Sets the buffer length of player to `len` seconds.
	///
	/// Equivalent to [setBufferLength](sys::ffi::playdate_sound_fileplayer::setBufferLength)
	#[doc(alias = "sys::ffi::playdate_sound_fileplayer::setBufferLength")]
	pub fn set_buffer_length(&self, len: c_float) {
		let f = self.api().set_buffer_length();
		unsafe { f(self.0, len) }
	}


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


	/// Starts playing the file player.
	///
	/// Equivalent to [play](sys::ffi::playdate_sound_fileplayer::play)
	#[doc(alias = "sys::ffi::playdate_sound_fileplayer::play")]
	pub fn play(&self, repeat: Repeat) -> c_int {
		let f = self.api().play();
		unsafe { f(self.0, repeat.into()) }
	}

	/// Stops playing the file.
	///
	/// Equivalent to [stop](sys::ffi::playdate_sound_fileplayer::stop)
	#[doc(alias = "sys::ffi::playdate_sound_fileplayer::stop")]
	pub fn stop(&self) {
		let f = self.api().stop();
		unsafe { f(self.0) }
	}


	/// Gets the left and right channel playback volume for player.
	///
	/// Equivalent to [getVolume](sys::ffi::playdate_sound_fileplayer::getVolume)
	#[doc(alias = "sys::ffi::playdate_sound_fileplayer::getVolume")]
	pub fn volume(&self) -> (c_float, c_float) {
		let (mut left, mut right) = (0.0, 0.0);
		let f = self.api().get_volume();
		unsafe { f(self.0, &mut left, &mut right) };
		(left, right)
	}

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

	/// Returns the length, in seconds, of the file loaded into player.
	///
	/// Equivalent to [getLength](sys::ffi::playdate_sound_fileplayer::getLength)
	#[doc(alias = "sys::ffi::playdate_sound_fileplayer::getLength")]
	pub fn length(&self) -> c_float {
		let f = self.api().get_length();
		unsafe { f(self.0) }
	}

	/// Gets the current offset in seconds for player.
	///
	/// Equivalent to [getOffset](sys::ffi::playdate_sound_fileplayer::getOffset)
	#[doc(alias = "sys::ffi::playdate_sound_fileplayer::getOffset")]
	pub fn offset(&self) -> c_float {
		let f = self.api().get_offset();
		unsafe { f(self.0) }
	}

	/// Sets the current offset in seconds.
	///
	/// Equivalent to [setOffset](sys::ffi::playdate_sound_fileplayer::setOffset)
	#[doc(alias = "sys::ffi::playdate_sound_fileplayer::setOffset")]
	pub fn set_offset(&self, offset: c_float) {
		let f = self.api().set_offset();
		unsafe { f(self.0, offset) }
	}

	/// Gets the playback rate for player.
	///
	/// Equivalent to [getRate](sys::ffi::playdate_sound_fileplayer::getRate)
	#[doc(alias = "sys::ffi::playdate_sound_fileplayer::getRate")]
	pub fn rate(&self) -> c_float {
		let f = self.api().get_rate();
		unsafe { f(self.0) }
	}

	/// Sets the playback rate for the player.
	///
	/// `1.0` is normal speed, `0.5` is down an octave, `2.0` is up an octave, etc.
	///
	/// Unlike [`SamplePlayer`](crate::player::SamplePlayer),
	/// [`FilePlayer`](crate::player::FilePlayer)s can’t play in reverse (i.e., rate < 0).
	///
	/// Equivalent to [setRate](sys::ffi::playdate_sound_fileplayer::setRate)
	#[doc(alias = "sys::ffi::playdate_sound_fileplayer::setRate")]
	pub fn set_rate(&self, rate: c_float) {
		let f = self.api().set_rate();
		unsafe { f(self.0, rate) }
	}

	/// Sets the `start` and `end` of the loop region for playback, in seconds.
	///
	/// If end is omitted, the end of the file is used.
	///
	/// Equivalent to [setLoopRange](sys::ffi::playdate_sound_fileplayer::setLoopRange)
	#[doc(alias = "sys::ffi::playdate_sound_fileplayer::setLoopRange")]
	pub fn set_loop_range(&self, start: c_float, end: c_float) {
		let f = self.api().set_loop_range();
		unsafe { f(self.0, start, end) }
	}

	/// Returns `true` if player has underrun, `false` if not.
	///
	/// Equivalent to [didUnderrun](sys::ffi::playdate_sound_fileplayer::didUnderrun)
	#[doc(alias = "sys::ffi::playdate_sound_fileplayer::didUnderrun")]
	pub fn did_underrun(&self) -> bool {
		let f = self.api().did_underrun();
		unsafe { f(self.0) == 1 }
	}

	/// If value is `true`, the player will restart playback (after an audible stutter) as soon as data is available.
	///
	/// Equivalent to [setStopOnUnderrun](sys::ffi::playdate_sound_fileplayer::setStopOnUnderrun)
	#[doc(alias = "sys::ffi::playdate_sound_fileplayer::setStopOnUnderrun")]
	pub fn set_stop_on_underrun(&self, value: bool) {
		let f = self.api().set_stop_on_underrun();
		unsafe { f(self.0, value as _) }
	}


	// callbacks //

	// TODO: rustify this functions

	/// Sets a function to be called when playback has completed.
	///
	/// This is an alias for [`sys::ffi::playdate_sound_source::setFinishCallback`].
	///
	/// Equivalent to [setFinishCallback](sys::ffi::playdate_sound_fileplayer::setFinishCallback)
	#[doc(alias = "sys::ffi::playdate_sound_fileplayer::setFinishCallback")]
	pub fn set_finish_callback(&self, callback: sndCallbackProc) {
		let f = self.api().set_finish_callback();
		// TODO: use userdata
		unsafe { f(self.0, callback, core::ptr::null_mut()) }
	}

	/// Equivalent to [setLoopCallback](sys::ffi::playdate_sound_fileplayer::setLoopCallback)
	#[doc(alias = "sys::ffi::playdate_sound_fileplayer::setLoopCallback")]
	pub fn set_loop_callback(&self, callback: sndCallbackProc) {
		let f = self.api().set_loop_callback();
		// TODO: use userdata
		unsafe { f(self.0, callback, core::ptr::null_mut()) }
	}

	/// Changes the volume of the [`Player`] to `left` and `right` over a length of `len` sample frames,
	/// then calls the provided `callback` (if set).
	///
	/// Equivalent to [fadeVolume](sys::ffi::playdate_sound_fileplayer::fadeVolume)
	#[doc(alias = "sys::ffi::playdate_sound_fileplayer::fadeVolume")]
	// Probably here we can use just FnOnce, because it will dropped after call by proxy.
	pub fn fade_volume(&self, left: c_float, right: c_float, len: i32, finish_callback: sndCallbackProc) {
		let f = self.api().fade_volume();
		// TODO: use userdata
		unsafe { f(self.0, left, right, len, finish_callback, core::ptr::null_mut()) }
	}

	/// Equivalent to [setMP3StreamSource](sys::ffi::playdate_sound_fileplayer::setMP3StreamSource)
	#[doc(alias = "sys::ffi::playdate_sound_fileplayer::setMP3StreamSource")]
	pub fn set_mp3_stream_source(&self,
	                             source: Option<unsafe extern "C" fn(data: *mut u8,
	                                                         bytes: c_int,
	                                                         userdata: *mut c_void)
	                                                         -> c_int>,
	                             userdata: *mut c_void,
	                             buffer_len: c_float) {
		let f = self.api().set_mp3_stream_source();
		unsafe { f(self.0, source, userdata, buffer_len) }
	}
}