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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
#![warn(missing_docs)]
#![doc(issue_tracker_base_url = "https://gitlab.101100.ca/veda/playback-rs/-/issues")]
#![doc = include_str!("../docs.md")]
#![feature(c_variadic)]

use std::collections::VecDeque;
use std::num::Wrapping;
use std::sync::mpsc::{self, Receiver, SyncSender, TryRecvError};
use std::sync::{Arc, Mutex, RwLock};
use std::thread;
use std::time::Duration;

use color_eyre::eyre::{ensure, Report, Result};
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::{
	Device, FrameCount, FromSample, HostId, OutputCallbackInfo, Sample, SampleFormat, SizedSample,
	Stream, StreamConfig, SupportedBufferSize, SupportedStreamConfigRange,
};
use log::{debug, error, info, warn};
use rubato::{InterpolationParameters, InterpolationType, Resampler, SincFixedOut, WindowFunction};
use symphonia::core::audio::SampleBuffer;
use symphonia::core::codecs::DecoderOptions;
use symphonia::core::errors::Error as SymphoniaError;
use symphonia::core::formats::FormatOptions;
use symphonia::core::io::{MediaSource, MediaSourceStream, MediaSourceStreamOptions};
use symphonia::core::meta::MetadataOptions;
use symphonia::default;

pub use symphonia::core::probe::Hint;

#[derive(Debug, Clone, Copy, PartialEq)]
struct SampleRequest {
	frame: Option<(Duration, Wrapping<u8>)>,
	speed: f64,
}

#[derive(Debug, Clone, PartialEq)]
struct SampleResult {
	samples: Vec<f32>,
	end_pos: Duration,
	skip_count: Wrapping<u8>,
	done: bool,
}

#[derive(Debug)]
struct DecodingSong {
	song_length: Duration,
	channel_count: usize,

	requests_channel: SyncSender<SampleRequest>,
	samples_channel: Mutex<Receiver<SampleResult>>,
	frames_per_resample: usize,

	buffer: VecDeque<f32>,
	pending_requests: usize,
	done: bool,
	had_output: bool,
	expected_pos: Duration,
	skip_count: Wrapping<u8>,
}

const MAXIMUM_SPEED_ADJUSTMENT_FACTOR: f64 = 2.0;
const MINIMUM_PLAYBACK_SPEED: f64 = 1.0 / MAXIMUM_SPEED_ADJUSTMENT_FACTOR;
const MAXIMUM_PLAYBACK_SPEED: f64 = 1.0 * MAXIMUM_SPEED_ADJUSTMENT_FACTOR;

impl DecodingSong {
	fn new(
		song: &Song,
		initial_pos: Duration,
		player_sample_rate: usize,
		player_channel_count: usize,
		expected_buffer_size: usize,
		initial_playback_speed: f64,
	) -> Result<DecodingSong> {
		let frames = song.samples.clone();
		let song_channel_count = song.channel_count;
		if player_channel_count != song_channel_count {
			warn!("Playing song with {song_channel_count} channels while the player has {player_channel_count} channels");
		}
		let total_frames = frames[0].len();
		let frames_per_resample = expected_buffer_size / player_channel_count;
		let volume_adjustment = song.volume_adjustment;

		let (rtx, rrx) = mpsc::sync_channel::<SampleRequest>(10);
		let (stx, srx) = mpsc::channel();
		let song_sample_rate = song.sample_rate as u64;
		let song_length = Self::frame_to_duration(total_frames, song_sample_rate);
		let resample_ratio = player_sample_rate as f64 / song.sample_rate as f64;
		let (etx, erx) = mpsc::channel();
		thread::spawn(move || {
			let sinc_len = 128;
			let f_cutoff = 0.925_914_65;
			let params = InterpolationParameters {
				sinc_len,
				f_cutoff,
				interpolation: InterpolationType::Linear,
				oversampling_factor: 2048,
				window: WindowFunction::Blackman2,
			};
			let mut resampler = match SincFixedOut::<f32>::new(
				resample_ratio,
				MAXIMUM_SPEED_ADJUSTMENT_FACTOR,
				params,
				frames_per_resample, // SincFixedOut theoretically always gives us this much each time we process
				player_channel_count,
			) {
				Ok(resampler) => {
					etx.send(Ok(())).unwrap();
					resampler
				}
				Err(e) => {
					etx.send(Err(e)).unwrap();
					return;
				}
			};
			let mut input_buffer = resampler.input_buffer_allocate();
			let mut output_buffer = resampler.output_buffer_allocate();

			let mut current_frame = 0;
			let mut skip_count = Wrapping(0);
			let mut last_request_speed = 1.0;
			loop {
				let request = match rrx.recv() {
					Ok(request) => request,
					Err(_) => {
						debug!("Ending resampling thread.");
						break;
					}
				};

				// adjust position based on seek
				if let Some((new_pos, new_skip_count)) = request.frame {
					let new_frame = (song_sample_rate * new_pos.as_secs()
						+ song_sample_rate * new_pos.subsec_nanos() as u64 / 1_000_000_000)
						as usize;
					current_frame = new_frame.min(total_frames);
					skip_count = new_skip_count;
				}

				// adjust the speed if it has changed
				if request.speed != last_request_speed {
					resampler
						.set_resample_ratio_relative(1.0 / request.speed)
						.unwrap();
					last_request_speed = request.speed;
				}

				// determine which samples to pass in to the converter
				let frames_wanted_by_resampler = resampler.input_frames_next();
				let last_frame = (current_frame + frames_wanted_by_resampler).min(total_frames);
				let frames_we_have = last_frame - current_frame;
				for i in 0..player_channel_count {
					input_buffer[i].clear();
					for j in 0..frames_wanted_by_resampler {
						if current_frame + j < total_frames {
							input_buffer[i].push(frames[i % song_channel_count][current_frame + j]);
						} else {
							input_buffer[i].push(0.0);
						}
					}
				}
				current_frame = last_frame;
				let end_pos = Self::frame_to_duration(current_frame, song_sample_rate);

				// resample the frames and convert into interleaved samples
				let processed_samples =
					match resampler.process_into_buffer(&input_buffer, &mut output_buffer, None) {
						Ok(()) => {
							let frame_count = if frames_we_have < frames_wanted_by_resampler {
								frames_per_resample * frames_we_have / frames_wanted_by_resampler
							} else {
								frames_per_resample
							};
							let mut samples = vec![0.0; player_channel_count * frame_count];
							for chan in 0..player_channel_count {
								if chan < 2 || chan < output_buffer.len() {
									for sample in 0..frame_count {
										samples[sample * player_channel_count + chan] =
											output_buffer[chan % output_buffer.len()][sample]
												* volume_adjustment
									}
								};
							}
							samples
						}
						Err(e) => {
							error!("Error converting sample rate: {e}");
							vec![0.0; expected_buffer_size]
						}
					};

				// send the data out over the channel
				// Dropping the other end of the channel will cause this to error, which will stop decoding.
				if stx
					.send(SampleResult {
						samples: processed_samples,
						skip_count,
						end_pos,
						done: frames_we_have < frames_wanted_by_resampler,
					})
					.is_err()
				{
					debug!("Ending resampling thread.");
					break;
				}
			}
		});
		erx.recv()??;
		let skip_count = Wrapping(0);
		rtx.send(SampleRequest {
			speed: initial_playback_speed,
			frame: Some((initial_pos, skip_count)),
		})?;
		Ok(DecodingSong {
			song_length,
			channel_count: player_channel_count,
			requests_channel: rtx,
			samples_channel: Mutex::new(srx),
			frames_per_resample,
			buffer: VecDeque::new(),
			pending_requests: 1,
			done: false,
			had_output: false,
			expected_pos: initial_pos,
			skip_count,
		})
	}
	fn read_samples(
		&mut self,
		pos: Duration,
		count: usize,
		playback_speed: f64,
	) -> (Vec<f32>, Duration, bool) {
		// if they want another position, we're seeking, so reset the buffer
		if pos != self.expected_pos {
			self.had_output = false;
			self.done = false;
			self.buffer.clear();
			self.skip_count += 1;
			self.requests_channel
				.send(SampleRequest {
					speed: playback_speed,
					frame: Some((pos, self.skip_count)),
				})
				.unwrap(); // This shouldn't be able to fail unless the thread stops which shouldn't be able to happen.
			self.pending_requests = 1;
		}

		while count
			> self.buffer.len()
				+ self.pending_requests * self.frames_per_resample * self.channel_count
		{
			if self
				.requests_channel
				.send(SampleRequest {
					speed: playback_speed,
					frame: None,
				})
				.is_err()
			{
				break;
			}

			self.pending_requests += 1;
		}
		let channel = self.samples_channel.lock().unwrap();
		if !self.done {
			// Fetch samples until there are none left to fetch and we have enough.
			let mut sent_warning = !self.had_output;
			loop {
				let got = channel.try_recv();
				match got {
					Ok(SampleResult {
						samples,
						skip_count,
						end_pos,
						done,
					}) => {
						if self.skip_count == skip_count {
							self.pending_requests -= 1;
							self.buffer.append(&mut VecDeque::from(samples));
							self.expected_pos = end_pos;
							if done {
								self.done = true;
								break;
							}
							if self.buffer.len() >= count {
								break;
							}
						}
					}
					Err(TryRecvError::Disconnected) => {
						self.done = true;
						break;
					}
					Err(TryRecvError::Empty) => {
						if self.buffer.len() >= count {
							break;
						} else if !sent_warning {
							warn!("Waiting on resampler, this could cause audio choppyness. If you are a developer and this happens repeatedly in release mode please file an issue on playback-rs.");
							sent_warning = true;
						}
					}
				}
			}
		}
		let mut vec = Vec::new();
		let mut done = false;
		for _i in 0..count {
			if let Some(sample) = self.buffer.pop_front() {
				vec.push(sample);
			} else {
				done = true;
				break;
			}
		}

		(vec, self.expected_pos, done)
	}
	fn frame_to_duration(frame: usize, song_sample_rate: u64) -> Duration {
		let sub_second_samples = frame as u64 % song_sample_rate;
		Duration::new(
			frame as u64 / song_sample_rate,
			(1_000_000_000 * sub_second_samples / song_sample_rate) as u32,
		)
	}
}

type PlaybackState = (DecodingSong, Duration);

#[derive(Clone)]
struct PlayerState {
	playback: Arc<RwLock<Option<PlaybackState>>>,
	next_samples: Arc<RwLock<Option<PlaybackState>>>,
	playing: Arc<RwLock<bool>>,
	channel_count: usize,
	sample_rate: usize,
	buffer_size: u32,
	playback_speed: Arc<RwLock<f64>>,
}

impl PlayerState {
	fn new(channel_count: u32, sample_rate: u32, buffer_size: FrameCount) -> Result<PlayerState> {
		Ok(PlayerState {
			playback: Arc::new(RwLock::new(None)),
			next_samples: Arc::new(RwLock::new(None)),
			playing: Arc::new(RwLock::new(true)),
			channel_count: channel_count as usize,
			sample_rate: sample_rate as usize,
			buffer_size,
			playback_speed: Arc::new(RwLock::new(1.0)),
		})
	}
	fn write_samples<T>(&self, data: &mut [T], _info: &OutputCallbackInfo)
	where
		T: Sample + FromSample<f32>,
	{
		for sample in data.iter_mut() {
			*sample = Sample::EQUILIBRIUM;
		}
		if *self.playing.read().unwrap() {
			let playback_speed = *self.playback_speed.read().unwrap();
			let mut playback = self.playback.write().unwrap();
			if playback.is_none() {
				if let Some((new_samples, new_pos)) = self.next_samples.write().unwrap().take() {
					*playback = Some((new_samples, new_pos));
				}
			}
			let mut done = false;
			if let Some((decoding_song, sample_pos)) = playback.as_mut() {
				let mut neg_offset = 0;
				let data_len = data.len();
				let (mut samples, mut new_pos, mut is_final) =
					decoding_song.read_samples(*sample_pos, data_len, playback_speed);
				for (i, sample) in data.iter_mut().enumerate() {
					if i >= samples.len() {
						if let Some((next_samples, next_pos)) =
							self.next_samples.write().unwrap().take()
						{
							*decoding_song = next_samples;
							neg_offset = i;
							*sample_pos = next_pos;
							(samples, new_pos, is_final) = decoding_song.read_samples(
								*sample_pos,
								data_len - neg_offset,
								playback_speed,
							);
						} else {
							break;
						}
					}
					*sample = T::from_sample(samples[i - neg_offset]);
				}
				*sample_pos = new_pos;
				done = is_final;
			}
			if done {
				*playback = None;
			}
		}
	}
	fn decode_song(&self, song: &Song, initial_pos: Duration) -> Result<DecodingSong> {
		DecodingSong::new(
			song,
			initial_pos,
			self.sample_rate,
			self.channel_count,
			self.buffer_size as usize,
			*self.playback_speed.read().unwrap(),
		)
	}
	fn set_playback_speed(&self, speed: f64) {
		*self.playback_speed.write().unwrap() =
			speed.clamp(MINIMUM_PLAYBACK_SPEED, MAXIMUM_PLAYBACK_SPEED);
	}
	fn stop(&self) {
		*self.next_samples.write().unwrap() = None;
		*self.playback.write().unwrap() = None;
	}
	fn skip(&self) {
		*self.playback.write().unwrap() = None;
	}
	fn play_song(&self, song: &Song, time: Option<Duration>) -> Result<()> {
		let initial_pos = time.unwrap_or_default();
		let samples = self.decode_song(song, initial_pos)?;
		*self.next_samples.write().unwrap() = Some((samples, initial_pos));
		Ok(())
	}
	fn set_playing(&self, playing: bool) {
		*self.playing.write().unwrap() = playing;
	}
	fn get_position(&self) -> Option<(Duration, Duration)> {
		self.playback
			.read()
			.unwrap()
			.as_ref()
			.map(|(samples, pos)| (*pos, samples.song_length))
	}
	fn seek(&self, time: Duration) -> bool {
		let (mut playback, mut next_song) = (
			self.playback.write().unwrap(),
			self.next_samples.write().unwrap(),
		);
		if let Some((_, pos)) = playback.as_mut() {
			*pos = time;
			true
		} else if let Some((_, pos)) = next_song.as_mut() {
			*pos = time;
			true
		} else {
			false
		}
	}
	fn force_remove_next_song(&self) {
		let (mut playback, mut next_song) = (
			self.playback.write().unwrap(),
			self.next_samples.write().unwrap(),
		);
		if next_song.is_some() {
			*next_song = None;
		} else {
			*playback = None;
		}
	}
}

/// Manages playback of [Song]s through [cpal] and sample conversion through [rubato].
pub struct Player {
	_stream: Box<dyn StreamTrait>,
	player_state: PlayerState,
}

impl Player {
	/// Creates a new [Player] to play [Song]s. If specified, the player will attempt to use one of
	/// the specified sampling rates. If not specified or the list is empty, the preferred rates
	/// are 48000 and 44100.
	///
	/// If none of the preferred sampling rates are available, the closest available rate to the
	/// first preferred rate will be selected.
	///
	/// On Linux, this prefers `pipewire`, `jack`, and `pulseaudio` devices over `alsa`.
	pub fn new(preferred_sampling_rates: Option<Vec<u32>>) -> Result<Player> {
		let device = {
			let mut selected_host = cpal::default_host();
			for host in cpal::available_hosts() {
				if host.name().to_lowercase().contains("jack") {
					selected_host = cpal::host_from_id(host)?;
				}
			}
			info!("Selected Host: {:?}", selected_host.id());
			#[cfg(any(
				target_os = "linux",
				target_os = "dragonfly",
				target_os = "freebsd",
				target_os = "netbsd"
			))]
			{
				if selected_host.id() == HostId::Alsa {
					block_alsa_output();
				}
			}
			let mut selected_device = selected_host
				.default_output_device()
				.ok_or_else(|| Report::msg("No output device found."))?;
			for device in selected_host.output_devices()? {
				if let Ok(name) = device.name().map(|s| s.to_lowercase()) {
					if name.contains("pipewire") || name.contains("pulse") || name.contains("jack")
					{
						selected_device = device;
					}
				}
			}
			info!(
				"Selected Device: {}",
				selected_device
					.name()
					.unwrap_or_else(|_| "Unknown".to_string())
			);
			selected_device
		};
		let mut supported_configs = device.supported_output_configs()?.collect::<Vec<_>>();
		let preferred_sampling_rates = preferred_sampling_rates
			.filter(|given_rates| !given_rates.is_empty())
			.unwrap_or(vec![48000, 44100]);
		let preferred_sampling_rate = preferred_sampling_rates[0];
		let rank_supported_config = |config: &SupportedStreamConfigRange| {
			let chans = config.channels() as u32;
			let channel_rank = match chans {
				0 => 0,
				1 => 1,
				2 => 4,
				4 => 3,
				_ => 2,
			};
			let min_sample_rank = if config.min_sample_rate().0 <= preferred_sampling_rate {
				3
			} else {
				0
			};
			let max_sample_rank = if config.max_sample_rate().0 >= preferred_sampling_rate {
				3
			} else {
				0
			};
			let sample_format_rank = if config.sample_format() == SampleFormat::F32 {
				4
			} else {
				0
			};
			channel_rank + min_sample_rank + max_sample_rank + sample_format_rank
		};
		supported_configs.sort_by_key(|c_2| std::cmp::Reverse(rank_supported_config(c_2)));

		let supported_config = supported_configs
			.into_iter()
			.next()
			.ok_or_else(|| Report::msg("No supported output config."))?;

		let sample_rate_range =
			supported_config.min_sample_rate().0..supported_config.max_sample_rate().0;
		let supported_config = if let Some(selected_rate) = preferred_sampling_rates
			.into_iter()
			.find(|rate| sample_rate_range.contains(rate))
		{
			supported_config.with_sample_rate(cpal::SampleRate(selected_rate))
		} else if sample_rate_range.end <= preferred_sampling_rate {
			supported_config.with_sample_rate(cpal::SampleRate(sample_rate_range.end))
		} else {
			supported_config.with_sample_rate(cpal::SampleRate(sample_rate_range.start))
		};
		let sample_format = supported_config.sample_format();
		let sample_rate = supported_config.sample_rate().0;
		let channel_count = supported_config.channels();
		let buffer_size = match supported_config.buffer_size() {
			SupportedBufferSize::Range { min, .. } => (*min).max(1024) * 2,
			SupportedBufferSize::Unknown => 1024 * 2,
		};
		let config = supported_config.into();
		let player_state = PlayerState::new(channel_count as u32, sample_rate, buffer_size)?;
		info!(
			"SR, CC, SF: {}, {}, {:?}",
			sample_rate, channel_count, sample_format
		);
		fn build_stream<T>(
			device: &Device,
			config: &StreamConfig,
			player_state: PlayerState,
		) -> Result<Stream>
		where
			T: SizedSample + FromSample<f32>,
		{
			let err_fn = |err| error!("A playback error has occurred! {}", err);
			let stream = device.build_output_stream(
				config,
				move |data, info| player_state.write_samples::<T>(data, info),
				err_fn,
				None,
			)?;
			// Not all platforms (*cough cough* windows *cough*) automatically run the stream upon creation, so do that here.
			stream.play()?;
			Ok(stream)
		}
		let stream = {
			let player_state = player_state.clone();
			match sample_format {
				SampleFormat::I8 => build_stream::<i8>(&device, &config, player_state)?,
				SampleFormat::I16 => build_stream::<i16>(&device, &config, player_state)?,
				SampleFormat::I32 => build_stream::<i32>(&device, &config, player_state)?,
				SampleFormat::I64 => build_stream::<i64>(&device, &config, player_state)?,
				SampleFormat::U8 => build_stream::<u8>(&device, &config, player_state)?,
				SampleFormat::U16 => build_stream::<u16>(&device, &config, player_state)?,
				SampleFormat::U32 => build_stream::<u32>(&device, &config, player_state)?,
				SampleFormat::U64 => build_stream::<u64>(&device, &config, player_state)?,
				SampleFormat::F32 => build_stream::<f32>(&device, &config, player_state)?,
				SampleFormat::F64 => build_stream::<f64>(&device, &config, player_state)?,
				sample_format => Err(Report::msg(format!(
					"Unsupported sample format '{sample_format}'"
				)))?,
			}
		};
		Ok(Player {
			_stream: Box::new(stream),
			player_state,
		})
	}
	/// Set the playback speed (This will also affect song pitch)
	pub fn set_playback_speed(&self, speed: f64) {
		self.player_state.set_playback_speed(speed);
	}
	/// Set the song that will play after the current song is over (or immediately if no song is currently playing), optionally start playing in the middle of the song.
	pub fn play_song_next(&self, song: &Song, start_time: Option<Duration>) -> Result<()> {
		self.player_state.play_song(song, start_time)
	}
	/// Start playing a song immediately, while discarding any song that might have been queued to play next. Optionally start playing in the middle of the song.
	pub fn play_song_now(&self, song: &Song, start_time: Option<Duration>) -> Result<()> {
		self.player_state.stop();
		self.player_state.play_song(song, start_time)?;
		Ok(())
	}
	/// Used to replace the next song, or the current song if there is no next song. Optionally start playing in the middle of the song.
	///
	/// This will remove the current song if no next song exists to avoid a race condition in case the current song ends after you have determined that the next song must be replaced but before you call this function.
	/// See also [`force_remove_next_song`](Player::force_remove_next_song)
	pub fn force_replace_next_song(&self, song: &Song, start_time: Option<Duration>) -> Result<()> {
		self.player_state.force_remove_next_song();
		self.player_state.play_song(song, start_time)?;
		Ok(())
	}
	/// Used to remove the next song, or the current song if there is no next song.
	///
	/// This will remove the current song if no next song exists to avoid a race condition in case the current song ends after you have determined that the next song must be replaced but before you call this function.
	/// See also [`force_replace_next_song`](Player::force_replace_next_song)
	pub fn force_remove_next_song(&self) -> Result<()> {
		self.player_state.force_remove_next_song();
		Ok(())
	}
	/// Stop playing any songs and remove a next song if it has been queued.
	///
	/// Note that this does not pause playback (use [`set_playing`](Player::set_playing)), meaning new songs will play upon adding them.
	pub fn stop(&self) {
		self.player_state.stop();
	}
	/// Skip the currently playing song (i.e. stop playing it immediately.
	///
	/// This will immediately start playing the next song if it exists.
	pub fn skip(&self) {
		self.player_state.skip();
	}
	/// Return the current playback position, if there is currently a song playing (see [`has_current_song`](Player::has_current_song))
	///
	/// See also [`seek`](Player::seek)
	pub fn get_playback_position(&self) -> Option<(Duration, Duration)> {
		self.player_state.get_position()
	}
	/// Set the current playback position if there is a song playing or a song queued to be played next.
	///
	/// Returns whether the seek was successful (whether there was a song to seek).
	/// Note that seeking past the end of the song will be successful and will cause playback to begin at the _beginning_ of the next song.
	///
	/// See also [`get_playback_position`](Player::get_playback_position)
	pub fn seek(&self, time: Duration) -> bool {
		self.player_state.seek(time)
	}
	/// Sets whether playback is enabled or not, without touching the song queue.
	///
	/// See also [`is_playing`](Player::is_playing)
	pub fn set_playing(&self, playing: bool) {
		self.player_state.set_playing(playing);
	}
	/// Returns whether playback is currently paused.
	///
	/// See also [`set_playing`](Player::set_playing)
	pub fn is_playing(&self) -> bool {
		*self.player_state.playing.read().unwrap()
	}
	/// Returns whether there is a song queued to play next after the current song has finished
	///
	/// If you want to check whether there is currently a song playing, use [`has_current_song`][Player::has_current_song] and [`is_playing`][Player::is_playing].
	/// This should always be queried before calling [`play_song_next`](Player::play_song_next) if you do not intend on replacing the song currently in the queue.
	pub fn has_next_song(&self) -> bool {
		self.player_state
			.next_samples
			.read()
			.expect("Next song mutex poisoned.")
			.is_some()
	}
	/// Returns whether there is a song currently playing (or about to start playing next audio frame)
	///
	/// Note that this **does not** indicate whether the current song is actively being played or paused, for that functionality you can use [is_playing](Self::is_playing).
	pub fn has_current_song(&self) -> bool {
		self.player_state
			.playback
			.read()
			.expect("Current song mutex poisoned.")
			.is_some() || self
			.player_state
			.next_samples
			.read()
			.expect("Next song mutex poisoned.")
			.is_some()
	}
}

/// Represents a single song that has been decoded into memory, can be played in a <Player> struct.
///
/// The data in the song is stored in an <Arc> so cloning a song is a lightweight operation.
#[derive(Debug, Clone)]
pub struct Song {
	samples: Arc<Vec<Vec<f32>>>,
	sample_rate: u32,
	channel_count: usize,
	volume_adjustment: f32,
}

impl Song {
	/// Creates a new song using a reader of some kind and a type hint (the Symphonia hint type has been reexported at the crate root for convenience), as well as an optional volume adjustment (used for e.g. replay gain).
	pub fn new(
		reader: Box<dyn MediaSource>,
		hint: &Hint,
		volume_adjustment: Option<f32>,
	) -> Result<Song> {
		let media_source_stream =
			MediaSourceStream::new(reader, MediaSourceStreamOptions::default());
		let mut probe_result = default::get_probe().format(
			hint,
			media_source_stream,
			&FormatOptions {
				enable_gapless: true,
				..FormatOptions::default()
			},
			&MetadataOptions::default(),
		)?;
		let mut decoder = default::get_codecs().make(
			&probe_result
				.format
				.default_track()
				.ok_or_else(|| Report::msg("No default track in media file."))?
				.codec_params,
			&DecoderOptions::default(),
		)?;
		let mut song: Option<(Vec<Vec<f32>>, u32, usize)> = None;
		let mut bad_packet = false;
		loop {
			match probe_result.format.next_packet() {
				Ok(packet) => {
					let decoded = match decoder.decode(&packet) {
						Ok(decoded) => decoded,
						Err(symphonia::core::errors::Error::DecodeError(err)) => {
							// The example playback code doesn't treat decode errors as fatal errors,
							// so just log this at most once per file.
							if !bad_packet {
								bad_packet = true;
								warn!("Bad packet: {err:?}");
							}
							continue;
						}
						Err(err) => {
							return Err(Report::new(err));
						}
					};
					let spec = *decoded.spec();
					let song_samples =
						if let Some((samples, sample_rate, channel_count)) = &mut song {
							ensure!(
								spec.rate == *sample_rate,
								"Sample rate of decoded does not match previous sample rate."
							);
							ensure!(
								spec.channels.count() == *channel_count,
								"Channel count of decoded does not match previous channel count."
							);
							samples
						} else {
							song = Some((
								vec![Vec::new(); spec.channels.count()],
								spec.rate,
								spec.channels.count(),
							));
							&mut song.as_mut().unwrap().0
						};
					if decoded.frames() > 0 {
						let mut samples = SampleBuffer::new(decoded.frames() as u64, spec);
						samples.copy_interleaved_ref(decoded);
						for frame in samples.samples().chunks(spec.channels.count()) {
							for (chan, sample) in frame.iter().enumerate() {
								song_samples[chan].push(*sample)
							}
						}
					} else {
						warn!("Empty packet encountered while loading song!");
					}
				}
				Err(SymphoniaError::IoError(_)) => break,
				Err(e) => return Err(e.into()),
			}
		}
		song.map(|(samples, sample_rate, channel_count)| Song {
			samples: Arc::new(samples),
			sample_rate,
			channel_count,
			volume_adjustment: volume_adjustment.unwrap_or(1.0),
		})
		.ok_or_else(|| Report::msg("No song data decoded."))
	}
	/// Creates a [Song] by reading data from a file and using the file's extension as a format type hint. Takes an optional volume adjustment (used for e.g. replay gain)
	pub fn from_file<P: AsRef<std::path::Path>>(
		path: P,
		volume_adjustment: Option<f32>,
	) -> Result<Song> {
		let mut hint = Hint::new();
		if let Some(extension) = path.as_ref().extension().and_then(|s| s.to_str()) {
			hint.with_extension(extension);
		}
		Self::new(
			Box::new(std::fs::File::open(path)?),
			&hint,
			volume_adjustment,
		)
	}
}

#[cfg(any(
	target_os = "linux",
	target_os = "dragonfly",
	target_os = "freebsd",
	target_os = "netbsd"
))]
fn block_alsa_output() {
	use std::os::raw::{c_char, c_int};

	use alsa_sys::snd_lib_error_set_handler;
	use log::trace;

	unsafe extern "C" fn error_handler(
		file: *const c_char,
		line: c_int,
		function: *const c_char,
		err: c_int,
		format: *const c_char,
		mut format_args: ...
	) {
		use std::ffi::CStr;
		let file = String::from_utf8_lossy(CStr::from_ptr(file).to_bytes());
		let function = String::from_utf8_lossy(CStr::from_ptr(function).to_bytes());
		let format = String::from_utf8_lossy(CStr::from_ptr(format).to_bytes());
		// FIXME: This should really be better, but it works for alsa so
		let mut last_m = 0;
		let formatted: String = format
			.match_indices("%s")
			.flat_map(|(m, s)| {
				let res = [
					format[last_m..m].to_string(),
					String::from_utf8_lossy(
						CStr::from_ptr(format_args.arg::<*const c_char>()).to_bytes(),
					)
					.to_string(),
				];
				last_m = m + s.len();
				res
			})
			.collect();
		trace!("ALSA Error: {err}: {file} ({line}): {function}: {formatted}");
	}

	unsafe {
		snd_lib_error_set_handler(Some(error_handler));
	}
}