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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// Copyright (c) 2014 by SiegeLord
//
// All rights reserved. Distributed under ZLib. For full terms see the file LICENSE.

use addon::AudioAddon;
use allegro::c_bool;
use allegro_audio_sys::*;
use internal::{AttachToMixerImpl, Connection};

use libc::*;

use mixer::AttachToMixer;
use properties::*;
use std::ffi::CString;
use std::mem;
use std::ptr;
use std::slice::{from_raw_parts, from_raw_parts_mut};
use std::sync::Arc;
use std::sync::Mutex;

pub trait DataSample
where
	Self: Sized,
{
	fn get_depth(_: Option<Self>) -> AudioDepth;
}

macro_rules! data_sample_impl {
	($t:ty, $d:path) => {
		impl DataSample for $t
		{
			fn get_depth(_: Option<$t>) -> AudioDepth
			{
				$d
			}
		}
	};
}

data_sample_impl!(i8, AudioDepth::I8);
data_sample_impl!(i16, AudioDepth::I16);
data_sample_impl!(u8, AudioDepth::U8);
data_sample_impl!(u16, AudioDepth::U16);
data_sample_impl!(f32, AudioDepth::F32);

// TODO: ALLEGRO_SAMPLE and ALLEGRO_SAMPLE_INSTANCE can probably race on each other...
// consider adding mutexes (maybe Allegro's mutexes prevent everything bad already)

pub struct Sample
{
	allegro_sample: *mut ALLEGRO_SAMPLE,
	// This will inform sample instances that this sample got dropped
	sample_valid: Arc<Mutex<bool>>,
}

impl Sample
{
	pub fn load(_: &AudioAddon, filename: &str) -> Result<Sample, ()>
	{
		let filename = CString::new(filename.as_bytes()).unwrap();
		let samp = unsafe { al_load_sample(filename.as_ptr()) };
		if samp.is_null()
		{
			Err(())
		}
		else
		{
			Ok(Sample {
				allegro_sample: samp,
				sample_valid: Arc::new(Mutex::new(true)),
			})
		}
	}

	pub fn create_instance(&self) -> Result<SampleInstance, ()>
	{
		let inst = SampleInstance::new_raw();
		inst.and_then(|mut inst| {
			if_ok!(inst.set_sample(self));
			Ok(inst)
		})
	}

	pub fn get_frequency(&self) -> usize
	{
		unsafe { al_get_sample_frequency(self.allegro_sample as *const _) as usize }
	}

	pub fn get_length(&self) -> usize
	{
		unsafe { al_get_sample_length(self.allegro_sample as *const _) as usize }
	}

	pub fn get_byte_length(&self) -> usize
	{
		self.get_length()
			* self.get_channels().get_num_channels()
			* self.get_depth().get_byte_size()
	}

	pub fn get_depth(&self) -> AudioDepth
	{
		unsafe { AudioDepth::from_allegro(al_get_sample_depth(self.allegro_sample as *const _)) }
	}

	pub fn get_channels(&self) -> ChannelConf
	{
		unsafe {
			ChannelConf::from_allegro(al_get_sample_channels(self.allegro_sample as *const _))
		}
	}

	pub fn get_raw_data<'l>(&'l self) -> &'l [u8]
	{
		let len = self.get_byte_length();
		unsafe {
			from_raw_parts(
				al_get_sample_data(self.allegro_sample as *const _) as *const _,
				len,
			)
		}
	}

	pub fn get_data<'l, T: DataSample>(&'l self) -> Result<&'l [T], ()>
	{
		if self.get_depth() == DataSample::get_depth(None::<T>)
		{
			let len = self.get_byte_length() / mem::size_of::<T>();
			Ok(unsafe {
				from_raw_parts(
					al_get_sample_data(self.allegro_sample as *const _) as *const _,
					len,
				)
			})
		}
		else
		{
			Err(())
		}
	}

	pub fn get_data_mut<'l, T: DataSample>(&'l mut self) -> Result<&'l mut [T], ()>
	{
		if self.get_depth() == DataSample::get_depth(None::<T>)
		{
			let len = self.get_byte_length() / mem::size_of::<T>();
			Ok(unsafe {
				from_raw_parts_mut(
					al_get_sample_data(self.allegro_sample as *const _) as *mut _,
					len,
				)
			})
		}
		else
		{
			Err(())
		}
	}

	pub fn get_raw_data_mut<'l>(&'l mut self) -> &'l mut [u8]
	{
		let len = self.get_byte_length();
		unsafe {
			from_raw_parts_mut(
				al_get_sample_data(self.allegro_sample as *const _) as *mut _,
				len,
			)
		}
	}

	pub fn get_allegro_sample(&self) -> *mut ALLEGRO_SAMPLE
	{
		self.allegro_sample
	}
}

impl Drop for Sample
{
	fn drop(&mut self)
	{
		let mut valid = self.sample_valid.lock().unwrap();
		*valid = false;
		unsafe {
			al_destroy_sample(self.allegro_sample);
		}
	}
}

unsafe impl Send for Sample {}
unsafe impl Sync for Sample {}

pub struct SampleInstance
{
	parent: Option<Connection>,
	// I think when the sample is invalid, it is unsafe to resume it
	sample_valid: Arc<Mutex<bool>>,
	allegro_sample_instance: *mut ALLEGRO_SAMPLE_INSTANCE,
}

macro_rules! check_or_else {
	($self_:ident, $valid:expr, $invalid:expr) => {{
		let valid = $self_.sample_valid.lock().unwrap();
		if *valid
			{
			unsafe { $valid }
			}
		else
			{
			$invalid
			}
		}};
}

macro_rules! set_impl {
	($self_:ident, $c_func:ident, $var:expr) => {
		check_or_else!(
			$self_,
			if $c_func($self_.allegro_sample_instance, $var) != 0
				{
				Ok(())
				}
			else
				{
				Err(())
				},
			Err(())
			)
	};
}

macro_rules! get_opt_impl {
	($self_:ident, $c_func:ident, $dest_ty:ty) => {
		check_or_else!(
			$self_,
			Ok($c_func($self_.allegro_sample_instance as *const _) as $dest_ty),
			Err(())
			)
	};
}

macro_rules! get_conv_impl {
	($self_:ident, $c_func:ident, $conv:path) => {
		check_or_else!(
			$self_,
			Ok($conv($c_func($self_.allegro_sample_instance as *const _))),
			Err(())
			)
	};
}

macro_rules! get_bool_impl {
	($self_:ident, $c_func:ident) => {
		check_or_else!(
			$self_,
			Ok($c_func($self_.allegro_sample_instance as *const _) != 0),
			Err(())
			)
	};
}

impl SampleInstance
{
	pub fn new(_: &AudioAddon) -> Result<SampleInstance, ()>
	{
		SampleInstance::new_raw()
	}

	fn new_raw() -> Result<SampleInstance, ()>
	{
		let inst = unsafe { al_create_sample_instance(ptr::null_mut()) };
		if inst.is_null()
		{
			Err(())
		}
		else
		{
			Ok(SampleInstance {
				parent: None,
				sample_valid: Arc::new(Mutex::new(false)),
				allegro_sample_instance: inst,
			})
		}
	}

	fn detach(allegro_sample_instance: *mut c_void)
	{
		unsafe {
			al_detach_sample_instance(mem::transmute(allegro_sample_instance));
		}
	}

	pub fn set_sample(&mut self, sample: &Sample) -> Result<(), ()>
	{
		if unsafe { al_set_sample(self.allegro_sample_instance, sample.allegro_sample) != 0 }
		{
			self.sample_valid = sample.sample_valid.clone();
			Ok(())
		}
		else
		{
			self.sample_valid = Arc::new(Mutex::new(false));
			// As per docs of al_set_sample
			self.parent = None;
			Err(())
		}
	}

	pub fn set_position(&self, position: u32) -> Result<(), ()>
	{
		set_impl!(self, al_set_sample_instance_position, position as c_uint)
	}

	pub fn set_length(&self, length: u32) -> Result<(), ()>
	{
		set_impl!(self, al_set_sample_instance_length, length as c_uint)
	}

	pub fn set_playing(&self, playing: bool) -> Result<(), ()>
	{
		set_impl!(self, al_set_sample_instance_playing, playing as c_bool)
	}

	pub fn set_gain(&self, gain: f32) -> Result<(), ()>
	{
		set_impl!(self, al_set_sample_instance_gain, gain as c_float)
	}

	pub fn set_pan(&self, pan: Option<f32>) -> Result<(), ()>
	{
		set_impl!(
			self,
			al_set_sample_instance_pan,
			match pan
			{
				Some(p) => p as c_float,
				None => ALLEGRO_AUDIO_PAN_NONE,
			}
		)
	}

	pub fn set_speed(&self, speed: f32) -> Result<(), ()>
	{
		set_impl!(self, al_set_sample_instance_speed, speed as c_float)
	}

	pub fn set_playmode(&self, playmode: Playmode) -> Result<(), ()>
	{
		set_impl!(self, al_set_sample_instance_playmode, playmode.get())
	}

	pub fn get_frequency(&self) -> Result<u32, ()>
	{
		get_opt_impl!(self, al_get_sample_instance_frequency, u32)
	}

	pub fn get_length(&self) -> Result<u32, ()>
	{
		get_opt_impl!(self, al_get_sample_instance_length, u32)
	}

	pub fn get_position(&self) -> Result<u32, ()>
	{
		get_opt_impl!(self, al_get_sample_instance_position, u32)
	}

	pub fn get_speed(&self) -> Result<f32, ()>
	{
		get_opt_impl!(self, al_get_sample_instance_speed, f32)
	}

	pub fn get_gain(&self) -> Result<f32, ()>
	{
		get_opt_impl!(self, al_get_sample_instance_gain, f32)
	}

	pub fn get_pan(&self) -> Result<f32, ()>
	{
		get_opt_impl!(self, al_get_sample_instance_pan, f32)
	}

	pub fn get_time(&self) -> Result<f32, ()>
	{
		get_opt_impl!(self, al_get_sample_instance_time, f32)
	}

	pub fn get_playmode(&self) -> Result<Playmode, ()>
	{
		get_conv_impl!(
			self,
			al_get_sample_instance_playmode,
			Playmode::from_allegro
		)
	}

	pub fn get_channels(&self) -> Result<ChannelConf, ()>
	{
		get_conv_impl!(
			self,
			al_get_sample_instance_channels,
			ChannelConf::from_allegro
		)
	}

	pub fn get_depth(&self) -> Result<AudioDepth, ()>
	{
		get_conv_impl!(self, al_get_sample_instance_depth, AudioDepth::from_allegro)
	}

	pub fn get_playing(&self) -> Result<bool, ()>
	{
		get_bool_impl!(self, al_get_sample_instance_playing)
	}

	pub fn get_attached(&self) -> Result<bool, ()>
	{
		get_bool_impl!(self, al_get_sample_instance_attached)
	}

	pub fn get_allegro_sample_instance(&self) -> *mut ALLEGRO_SAMPLE_INSTANCE
	{
		self.allegro_sample_instance
	}
}

impl Drop for SampleInstance
{
	fn drop(&mut self)
	{
		self.detach();
		unsafe {
			al_destroy_sample_instance(self.allegro_sample_instance);
		}
	}
}

unsafe impl Send for SampleInstance {}
unsafe impl Sync for SampleInstance {}

impl AttachToMixerImpl for SampleInstance
{
	fn create_connection(&mut self, allegro_mixer: *mut ALLEGRO_MIXER) -> Result<Connection, ()>
	{
		if unsafe {
			al_attach_sample_instance_to_mixer(self.allegro_sample_instance, allegro_mixer) == 0
		}
		{
			Err(())
		}
		else
		{
			let (c1, c2) = Connection::new(
				unsafe { mem::transmute(self.allegro_sample_instance) },
				SampleInstance::detach,
			);
			self.parent = Some(c1);
			Ok(c2)
		}
	}
}

impl AttachToMixer for SampleInstance
{
	fn detach(&mut self)
	{
		self.parent = None;
	}
}