6GZTCB6VJEBACZR2KCGIXBOQ3JFHTBJQJNPAGQNNWQFDB6VRFEUAC
use std::time::Duration;
use cpal::StreamConfig;
use dasp::{
frame::Stereo,
interpolate::sinc::Sinc,
ring_buffer,
signal::{interpolate::Converter, IntoInterleavedSamples},
Signal,
};
use crate::Source;
const INTERPOLATOR_SIZE: usize = 100;
pub enum Event {
Play,
Pause,
Stop,
Seek(Duration),
SetSource(Source),
}
pub struct Player {
receiver: crossbeam_channel::Receiver<Event>,
config: StreamConfig,
playing: bool,
_duration: Duration,
position: f64,
source:
Option<IntoInterleavedSamples<Converter<Source, Sinc<[Stereo<f32>; INTERPOLATOR_SIZE]>>>>,
}
impl Player {
pub fn new(config: StreamConfig, receiver: crossbeam_channel::Receiver<Event>) -> Self {
Self {
receiver,
config,
playing: false,
_duration: Duration::SECOND,
position: 0.0,
source: None,
}
}
pub fn process_events(&mut self) {
while let Ok(event) = self.receiver.try_recv() {
match event {
Event::Play => self.playing = true,
Event::Pause => self.playing = false,
Event::Stop => self.playing = false,
Event::Seek(position) => self.position = position.as_secs_f64(),
Event::SetSource(source) => {
let ring_buffer = ring_buffer::Fixed::from([[0.0, 0.0]; INTERPOLATOR_SIZE]);
let source_sample_rate = source.sample_rate;
self.source = Some(
source
.from_hz_to_hz(
Sinc::new(ring_buffer),
source_sample_rate,
self.config.sample_rate.0 as f64,
)
.into_interleaved_samples(),
);
}
}
}
}
pub fn process(&mut self, data: &mut [f32]) {
self.process_events();
match &mut self.source {
Some(source) if self.playing => {
for d in data {
*d = source.next_sample();
}
}
_ => {}
}
}
}
let frame = [
self.sample_buf.samples()[(self.i + 0) as usize],
self.sample_buf.samples()[(self.i + 1) as usize],
];
self.i += 2;
if self.i == self.sample_buf.len() as u32 {
self.decode_next();
self.i = 0;
for b in buf {
b[0] = self.sample_buf.samples()[(self.i + 0) as usize];
b[1] = self.sample_buf.samples()[(self.i + 1) as usize];
self.i += 2;
if self.i == self.sample_buf.len() as u32 {
self.decode_next();
self.i = 0;
}
// impl Signal for LocalSource {
// type Frame = Stereo<f32>;
// fn is_exhausted(&self) -> bool {
// self.ended
// }
// fn next(&mut self) -> Self::Frame {
// if self.empty || self.ended {
// return [0.0, 0.0];
// }
// let frame = [
// self.sample_buf.samples()[(self.i + 0) as usize],
// self.sample_buf.samples()[(self.i + 1) as usize],
// ];
// self.i += 2;
// if self.i == self.sample_buf.len() as u32 {
// self.decode_next();
// self.i = 0;
// }
// frame
// }
// }
]
[[package]]
name = "dasp"
version = "0.11.0"
source = "git+https://github.com/RustAudio/dasp#66e8b83a5c926fde3ec7feab5dbe5982e6ce7b81"
dependencies = [
"dasp_envelope",
"dasp_frame",
"dasp_interpolate",
"dasp_peak",
"dasp_ring_buffer",
"dasp_rms",
"dasp_sample",
"dasp_signal",
"dasp_slice",
"dasp_window",
]
[[package]]
name = "dasp_envelope"
version = "0.11.0"
source = "git+https://github.com/RustAudio/dasp#66e8b83a5c926fde3ec7feab5dbe5982e6ce7b81"
dependencies = [
"dasp_frame",
"dasp_peak",
"dasp_ring_buffer",
"dasp_rms",
"dasp_sample",
]
[[package]]
name = "dasp_frame"
version = "0.11.0"
source = "git+https://github.com/RustAudio/dasp#66e8b83a5c926fde3ec7feab5dbe5982e6ce7b81"
dependencies = [
"dasp_sample",
]
[[package]]
name = "dasp_interpolate"
version = "0.11.0"
source = "git+https://github.com/RustAudio/dasp#66e8b83a5c926fde3ec7feab5dbe5982e6ce7b81"
dependencies = [
"dasp_frame",
"dasp_ring_buffer",
"dasp_sample",
name = "dasp_peak"
version = "0.11.0"
source = "git+https://github.com/RustAudio/dasp#66e8b83a5c926fde3ec7feab5dbe5982e6ce7b81"
dependencies = [
"dasp_frame",
"dasp_sample",
]
[[package]]
name = "dasp_ring_buffer"
version = "0.11.0"
source = "git+https://github.com/RustAudio/dasp#66e8b83a5c926fde3ec7feab5dbe5982e6ce7b81"
[[package]]
name = "dasp_rms"
version = "0.11.0"
source = "git+https://github.com/RustAudio/dasp#66e8b83a5c926fde3ec7feab5dbe5982e6ce7b81"
dependencies = [
"dasp_frame",
"dasp_ring_buffer",
"dasp_sample",
]
[[package]]
name = "dasp_sample"
version = "0.11.0"
source = "git+https://github.com/RustAudio/dasp#66e8b83a5c926fde3ec7feab5dbe5982e6ce7b81"
[[package]]
name = "dasp_signal"
version = "0.11.0"
source = "git+https://github.com/RustAudio/dasp#66e8b83a5c926fde3ec7feab5dbe5982e6ce7b81"
dependencies = [
"dasp_envelope",
"dasp_frame",
"dasp_interpolate",
"dasp_peak",
"dasp_ring_buffer",
"dasp_rms",
"dasp_sample",
"dasp_window",
]
[[package]]
name = "dasp_slice"
version = "0.11.0"
source = "git+https://github.com/RustAudio/dasp#66e8b83a5c926fde3ec7feab5dbe5982e6ce7b81"
dependencies = [
"dasp_frame",
"dasp_sample",
]
[[package]]
name = "dasp_window"
version = "0.11.0"
source = "git+https://github.com/RustAudio/dasp#66e8b83a5c926fde3ec7feab5dbe5982e6ce7b81"
dependencies = [
"dasp_sample",
]
[[package]]
name = "rustfft"
version = "6.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1d089e5c57521629a59f5f39bca7434849ff89bd6873b521afe389c1c602543"
dependencies = [
"num-complex",
"num-integer",
"num-traits",
"primal-check",
"strength_reduce",
"transpose",
]
[[package]]