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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
use std::{
mem,
sync::{Arc, atomic::Ordering},
time::{Duration, Instant},
};
use cpal::{
Device, Devices, OutputCallbackInfo, SampleFormat, SampleRate, Stream,
SupportedOutputConfigs, SupportedStreamConfig,
traits::{DeviceTrait, HostTrait, StreamTrait},
};
use crate::{
BufferSize, CallbackInfo, OptionBox, SharedData, Timestamp,
err::{Error, Result},
mixer::Mixer,
sample_buffer::SampleBufferMut,
source::{DeviceConfig, Source},
};
/// A player that can play `Source`
pub struct Sink {
/// Data shared with the playback loop ([`Mixer`])
shared: Arc<SharedData>,
// The stream is never read, it just stays alive so that the audio plays
/// The stream, if you drop this the playbakck loop will stop
stream: Option<Stream>,
/// Info about the current device configuration
info: DeviceConfig,
/// Prefered device set by the user
device: Option<Device>,
/// Sink will try to get the buffer size to be this
preferred_buffer_size: BufferSize,
}
impl Sink {
/// Creates the output stream and starts the playback loop.
/// `config` is preffered device configuration, [`None`] = choose
/// default.
fn build_out_stream(
&mut self,
config: Option<DeviceConfig>,
) -> Result<()> {
let mut device =
self.device.take().map(Ok).unwrap_or_else(|| -> Result<_> {
cpal::default_host()
.default_output_device()
.ok_or(Error::NoOutDevice)
})?;
let sup = if let Ok(c) = device.supported_output_configs() {
c
} else {
device = cpal::default_host()
.default_output_device()
.ok_or(Error::NoOutDevice)?;
device.supported_output_configs()?
};
let supported_config = match config {
Some(c) => select_config(c, sup)
.unwrap_or(device.default_output_config()?),
None => device.default_output_config()?,
};
self.info = DeviceConfig {
channel_count: supported_config.channels() as u32,
sample_rate: supported_config.sample_rate().0,
sample_format: supported_config.sample_format(),
};
let shared = self.shared.clone();
let mut mixer = Mixer::new(shared.clone(), self.info.clone());
let mut config = supported_config.config();
config.buffer_size = self
.preferred_buffer_size
.to_cpal(supported_config.buffer_size(), config.sample_rate.0);
macro_rules! arm {
($t:ident, $e:ident) => {
device.build_output_stream(
&config,
move |d: &mut [$t], info| {
mixer.mix(
&mut SampleBufferMut::$e(d),
get_play_time(info),
)
},
move |e| {
_ = shared.invoke_err_callback(e.into());
},
//Some(Duration::from_millis(5)),
None,
)
};
}
let stream = match self.info.sample_format {
SampleFormat::I8 => arm!(i8, I8),
SampleFormat::I16 => arm!(i16, I16),
SampleFormat::I32 => arm!(i32, I32),
SampleFormat::I64 => arm!(i64, I64),
SampleFormat::U8 => arm!(u8, U8),
SampleFormat::U16 => arm!(u16, U16),
SampleFormat::U32 => arm!(u32, U32),
SampleFormat::U64 => arm!(u64, U64),
SampleFormat::F32 => arm!(f32, F32),
SampleFormat::F64 => arm!(f64, F64),
_ => {
// TODO: select other format when this is not supported
return Err(Error::UnsupportedSampleFormat);
}
}?;
self.device = Some(device);
self.stream = Some(stream);
Ok(())
}
/// Sets the callback function. Returns previous callback function.
///
/// The function is called when playback event occurs. For example when
/// source ends.
///
/// The function is called from another thread.
///
/// # Errors
/// - another user of one of the used mutexes panicked while using it
/// - source fails to init
///
/// # Panics
/// - the current thread already locked one of the used mutexes and didn't
/// release them
pub fn on_callback(
&self,
callback: Box<dyn FnMut(CallbackInfo) + Send>,
) -> Result<OptionBox<dyn FnMut(CallbackInfo) + Send>> {
self.shared.callback().set(callback)
}
/// Sets the error callback method. Returns previous error callback
/// function.
///
/// The funciton is called when an error occures on another thread.
///
/// The function is called from another thread.
///
/// # Errors
/// - another user of one of the used mutexes panicked while using it
/// - source fails to init
///
/// # Panics
/// - the current thread already locked one of the used mutexes and didn't
/// release them
pub fn on_err_callback(
&self,
callback: Box<dyn FnMut(Error) + Send>,
) -> Result<OptionBox<dyn FnMut(Error) + Send>> {
self.shared.err_callback().set(callback)
}
/// Discards the old source and sets the new source. Starts playing if
/// `play` is set to true.
///
/// # Errors
/// - another user of one of the used mutexes panicked while using it
/// - source fails to select preferred configuration.
///
/// # Panics
/// - the current thread already locked one of the used mutexes and didn't
/// release them
pub fn load(&mut self, src: Box<dyn Source>, play: bool) -> Result<()> {
self.try_load(&mut Some(src), play)
}
/// Tries to load the given source. If loading of the source fails, it is
/// not taken. If it it succeeds, it will be removed from the option.
///
/// `src` MUST NOT BE [`None`].
///
/// There is option where this will return error, but the source will be
/// taken. In that case the source is not dropped, but already loaded
/// internaly and the operation can be retried by calling [`Self::play`].
///
/// # Errors
/// - another user of one of the used mutexes panicked while using it
/// - source fails to select preferred configuration.
///
/// # Panics
/// - `src` was [`None`].
/// - the current thread already locked one of the used mutexes and didn't
/// release them
pub fn try_load(
&mut self,
src: &mut Option<Box<dyn Source>>,
play: bool,
) -> Result<()> {
let srcr = src.as_mut().expect("Sink::try_load() called with None");
srcr.set_err_callback(self.shared.err_callback());
let config = srcr.preferred_config();
let new_stream = if self.device.is_none()
|| config.as_ref().map(|c| *c != self.info).unwrap_or_default()
{
self.build_out_stream(config)?;
true
} else {
false
};
let mut controls = self.shared.controls()?;
let mut source = self.shared.source()?;
srcr.init(&self.info)?;
controls.play = play;
*source = src.take();
if !new_stream {
self.do_prefetch_notify(true);
}
if let Some(s) = &self.stream {
if play {
s.play()?;
}
}
Ok(())
}
/// Loads the prefetched source.
///
/// # Errors
/// - There is no prefetched source.
/// - Another user of one of the used mutexes panicked while using it
/// - Source fails to select preferred configuration.
///
/// # Panics
/// - the current thread already locked one of the used mutexes and didn't
/// release them
pub fn load_prefetched(&mut self, play: bool) -> Result<()> {
let src = self.shared.prefech_src()?.take();
if let Some(src) = src {
let mut src = Some(src);
let res = self.try_load(&mut src, play);
if src.is_some() {
*self.shared.prefech_src()? = src;
}
res
} else {
Err(Error::NoPrefetchedSource)
}
}
/// Resumes the playback of the current source if `play` is true, otherwise
/// pauses the playback.
///
/// # Errors
/// - another user of one of the used mutexes panicked while using it
///
/// # Panics
/// - the current thread already locked one of the used mutexes and didn't
/// release them
pub fn play(&self, play: bool) -> Result<()> {
self.shared.controls()?.play = play;
if let Some(s) = &self.stream {
s.play()?;
}
Ok(())
}
/// Pauses the loop that is feeding new samples. This can be used to reduce
/// cpu usage, but it is very different from the normal pause.
///
/// It doesn't ignores fade play/pause.
pub fn hard_pause(&self) -> Result<()> {
if let Some(s) = &self.stream {
s.pause()?;
}
Ok(())
}
/// Pauses the playback of the current source
///
/// # Errors
/// - another user of one of the used mutexes panicked while using it
///
/// # Panics
/// - the current thread already locked one of the used mutexes and didn't
/// release them
pub fn pause(&self) -> Result<()> {
self.play(false)
}
/// Resumes the playback of the current source
///
/// # Errors
/// - another user of one of the used mutexes panicked while using it
///
/// # Panics
/// - the current thread already locked one of the used mutexes and didn't
/// release them
pub fn resume(&self) -> Result<()> {
self.play(true)
}
/// Sets the volume of the playback, 0 = mute, 1 = full volume.
///
/// The value is not clipped so the caller should make sure that the volume
/// is in the bounds or the audio may have clipping.
///
/// # Errors
/// - another user of one of the used mutexes panicked while using it
///
/// # Panics
/// - the current thread already locked one of the used mutexes and didn't
/// release them
pub fn volume(&self, volume: f32) -> Result<()> {
self.shared.controls()?.volume = volume;
Ok(())
}
/// Gets the volume of the playback, 0 = mute, 1 = full volume.
///
/// The value may not be in the range.
///
/// # Errors
/// - another user of one of the used mutexes panicked while using it
///
/// # Panics
/// - the current thread already locked one of the used mutexes and didn't
/// release them
pub fn get_volume(&self) -> Result<f32> {
Ok(self.shared.controls()?.volume)
}
/// Returns true if the source is playing, otherwise returns false
///
/// # Errors
/// - another user of one of the used mutexes panicked while using it
///
/// # Panics
/// - the current thread already locked one of the used mutexes and didn't
/// release them
pub fn is_playing(&self) -> Result<bool> {
Ok(self.shared.controls()?.play)
}
/// Seeks to the given position
///
/// # Errors
/// - no source is playing
/// - the source doesn't support this
/// - failed to seek
pub fn seek_to(&mut self, timestamp: Duration) -> Result<Timestamp> {
Ok(self
.shared
.source()?
.as_mut()
.ok_or(Error::NoSourceIsPlaying)?
.seek(timestamp)?)
}
/// Seeks by the given amount. If `forward` is true, seeks forward,
/// otherwise seeks backward
pub fn seek_by(
&mut self,
time: Duration,
forward: bool,
) -> Result<Timestamp> {
Ok(self
.shared
.source()?
.as_mut()
.ok_or(Error::NoSourceIsPlaying)?
.seek_by(time, forward)?)
}
/// Gets the current timestamp and the total length of the currently
/// playing source.
///
/// # Errors
/// - no source is playing
/// - the source doesn't support this
pub fn get_timestamp(&self) -> Result<Timestamp> {
self.shared
.source()?
.as_ref()
.ok_or(Error::NoSourceIsPlaying)?
.get_time()
.ok_or(Error::Unsupported {
component: "Source",
feature: "getting current timestamp",
})
}
/// Sets the fade-in/fade-out time for play/pause. Returns the previous
/// fade length.
///
/// # Errors
/// - another user of one of the used mutexes panicked while using it
/// - source fails to select preferred configuration.
///
/// # Panics
/// - the current thread already locked one of the used mutexes and didn't
/// release them
pub fn set_fade_len(&mut self, fade: Duration) -> Result<Duration> {
Ok(mem::replace(
&mut self.shared.controls()?.fade_duration,
fade,
))
}
/// Gets the current fade-in/fade-out time for play/pause.
///
/// # Errors
/// - another user of one of the used mutexes panicked while using it
/// - source fails to select preferred configuration.
///
/// # Panics
/// - the current thread already locked one of the used mutexes and didn't
/// release them
pub fn get_fade_len(&self) -> Result<Duration> {
Ok(self.shared.controls()?.fade_duration)
}
/// Sets the preferred buffer size. None means, use default size.
///
/// Set to small values (such as 1024 or even less) for low latency.
/// Set to large values (such as 16384) for better performace efficiency.
pub fn set_buffer_size(&mut self, size: BufferSize) {
self.preferred_buffer_size = size;
}
/// Gets the preferred buffer size set by you.
pub fn get_preferred_buffer_size(&self) -> BufferSize {
self.preferred_buffer_size
}
/// Gets info about the configuration of the output device that is
/// currently playing
pub fn get_info(&self) -> &DeviceConfig {
&self.info
}
/// Gets iterator over all available devices
pub fn list_devices() -> Result<Devices> {
Ok(cpal::default_host().devices()?)
}
/// Sets the device to be used. If `device` is [`None`], default device
/// will be selected. Returns the current device.
///
/// This change will be applied the next time that stream will need to
/// rebuild or by calling [`Self::restart_stream`].
pub fn set_device(&mut self, device: Option<Device>) -> Option<Device> {
mem::replace(&mut self.device, device)
}
/// Gets the currently selected playback device.
pub fn get_device(&self) -> &Option<Device> {
&self.device
}
/// Resets the device and restarts the stream. If device is [`None`],
/// default device will be selected.
///
/// You may want to call this if [`Self::load`] returns with
/// `Error::Cpal(CpalError::BuildStream(BuildStreamError::DeviceNotAvailable))`.
///
/// # Errors
/// - another user of one of the used mutexes panicked while using it
/// - source fails to select preferred configuration.
///
/// # Panics
/// - the current thread already locked one of the used mutexes and didn't
/// release them
pub fn restart_device(&mut self, device: Option<Device>) -> Result<()> {
self.set_device(device);
self.restart_stream()
}
/// Rebuilds the stream. Playback is resumed right after restarting the
/// stream if it was playing.
///
/// # Errors
/// - another user of one of the used mutexes panicked while using it
/// - source fails to select preferred configuration.
///
/// # Panics
/// - the current thread already locked one of the used mutexes and didn't
/// release them
pub fn restart_stream(&mut self) -> Result<()> {
self.stream = None;
let src = self.shared.source()?.take();
if let Some(src) = src {
let play = self.is_playing()?;
self.load(src, play)?;
}
Ok(())
}
/// Removes the callback function and returns it.
///
/// # Panics
/// - If locking mutex returns error.
pub fn take_callback(
&self,
) -> Option<Box<dyn FnMut(CallbackInfo) + Send>> {
self.shared.callback().take()
}
/// Removes the error callback function and returns it.
///
/// # Panics
/// - If locking mutex returns error.
pub fn take_err_callback(&self) -> Option<Box<dyn FnMut(Error) + Send>> {
self.shared.err_callback().take()
}
/// Prefetch the next song. Return the previous value of prefetch if any.
///
/// # Errors
/// - another user of one of the used mutexes panicked while using it.
///
/// # Panics
/// - the current thread already locked one of the used mutexes and didn't
/// release them
pub fn prefetch(
&self,
mut src: Option<Box<dyn Source>>,
) -> Result<Option<Box<dyn Source>>> {
if let Some(src) = &mut src {
src.set_err_callback(self.shared.err_callback());
}
Ok(mem::replace(&mut *self.shared.prefech_src()?, src))
}
/// Sets how long before source ends should notification about the source
/// ending be sent. Setting this to [`Duration::ZERO`] will disable this
/// feature.
///
/// If the remaining length of source is less than `rem`, notification
/// will be sent using the callback function with
/// [`CallbackInfo::PrefetchTime`] with the remaining time.
///
/// # Errors
/// - another user of one of the used mutexes panicked while using it.
///
/// # Panics
/// - the current thread already locked one of the used mutexes and didn't
/// release them
pub fn prefetch_notify(&self, rem: Duration) -> Result<()> {
self.shared.controls()?.prefetch = rem;
Ok(())
}
/// true - Makes the source notify of prefetch even if that notification
/// has already been sent.
///
/// false - Don't sent notify for the current source.
pub fn do_prefetch_notify(&self, val: bool) {
self.shared.prefetch_notify.store(val, Ordering::Relaxed);
}
}
impl Default for Sink {
fn default() -> Self {
Self {
shared: Arc::new(SharedData::new()),
stream: None,
info: DeviceConfig {
channel_count: 0,
sample_rate: 0,
sample_format: SampleFormat::F32,
},
device: None,
preferred_buffer_size: BufferSize::Auto,
}
}
}
/// Selects config based on the prefered configuration
fn select_config(
prefered: DeviceConfig,
configs: SupportedOutputConfigs,
) -> Option<SupportedStreamConfig> {
let mut selected = None;
for c in configs {
if c.min_sample_rate().0 <= prefered.sample_rate
&& c.max_sample_rate().0 >= prefered.sample_rate
{
if c.channels() as u32 == prefered.channel_count {
if c.sample_format() == prefered.sample_format {
selected = Some(c);
break;
} else if selected.is_none()
|| selected.as_ref().unwrap().channels() as u32
!= prefered.channel_count
{
selected = Some(c)
}
} else if selected.is_none() {
selected = Some(c)
}
}
}
selected.map(|s| s.with_sample_rate(SampleRate(prefered.sample_rate)))
}
impl std::fmt::Debug for Sink {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Sink")
.field("shared", &self.shared)
.field("info", &self.info)
.finish()
}
}
fn get_play_time(info: &OutputCallbackInfo) -> Instant {
let now = Instant::now();
now + info
.timestamp()
.playback
.duration_since(&info.timestamp().callback)
.unwrap_or_default()
}