mirror of
https://github.com/RustAudio/rodio
synced 2024-12-13 13:42:34 +00:00
Revert Arc stuff
This commit is contained in:
parent
ca43ad0885
commit
80277e03f6
3 changed files with 20 additions and 51 deletions
36
src/sink.rs
36
src/sink.rs
|
@ -1,4 +1,4 @@
|
||||||
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
|
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
@ -12,28 +12,6 @@ use crate::stream::{OutputStreamHandle, PlayError};
|
||||||
use crate::{queue, source::Done, Sample, Source};
|
use crate::{queue, source::Done, Sample, Source};
|
||||||
use cpal::FromSample;
|
use cpal::FromSample;
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct AtomicF64 {
|
|
||||||
storage: AtomicU64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AtomicF64 {
|
|
||||||
pub fn new(value: f64) -> Self {
|
|
||||||
let as_u64 = value.to_bits();
|
|
||||||
Self {
|
|
||||||
storage: AtomicU64::new(as_u64),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn store(&self, value: f64, ordering: Ordering) {
|
|
||||||
let as_u64 = value.to_bits();
|
|
||||||
self.storage.store(as_u64, ordering)
|
|
||||||
}
|
|
||||||
pub fn load(&self, ordering: Ordering) -> f64 {
|
|
||||||
let as_u64 = self.storage.load(ordering);
|
|
||||||
f64::from_bits(as_u64)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Handle to a device that outputs sounds.
|
/// Handle to a device that outputs sounds.
|
||||||
///
|
///
|
||||||
/// Dropping the `Sink` stops all sounds. You can use `detach` if you want the sounds to continue
|
/// Dropping the `Sink` stops all sounds. You can use `detach` if you want the sounds to continue
|
||||||
|
@ -44,7 +22,6 @@ pub struct Sink {
|
||||||
|
|
||||||
controls: Arc<Controls>,
|
controls: Arc<Controls>,
|
||||||
sound_count: Arc<AtomicUsize>,
|
sound_count: Arc<AtomicUsize>,
|
||||||
position: Arc<AtomicF64>,
|
|
||||||
|
|
||||||
detached: bool,
|
detached: bool,
|
||||||
}
|
}
|
||||||
|
@ -87,6 +64,7 @@ struct Controls {
|
||||||
speed: Mutex<f32>,
|
speed: Mutex<f32>,
|
||||||
to_clear: Mutex<u32>,
|
to_clear: Mutex<u32>,
|
||||||
seek: Mutex<Option<SeekOrder>>,
|
seek: Mutex<Option<SeekOrder>>,
|
||||||
|
position: Mutex<f64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sink {
|
impl Sink {
|
||||||
|
@ -113,9 +91,9 @@ impl Sink {
|
||||||
speed: Mutex::new(1.0),
|
speed: Mutex::new(1.0),
|
||||||
to_clear: Mutex::new(0),
|
to_clear: Mutex::new(0),
|
||||||
seek: Mutex::new(None),
|
seek: Mutex::new(None),
|
||||||
|
position: Mutex::new(0.0),
|
||||||
}),
|
}),
|
||||||
sound_count: Arc::new(AtomicUsize::new(0)),
|
sound_count: Arc::new(AtomicUsize::new(0)),
|
||||||
position: Arc::new(AtomicF64::new(0.0)),
|
|
||||||
detached: false,
|
detached: false,
|
||||||
};
|
};
|
||||||
(sink, queue_rx)
|
(sink, queue_rx)
|
||||||
|
@ -143,7 +121,7 @@ impl Sink {
|
||||||
|
|
||||||
let source = source
|
let source = source
|
||||||
.speed(1.0)
|
.speed(1.0)
|
||||||
.trackable(self.position.clone())
|
.trackable()
|
||||||
.pausable(false)
|
.pausable(false)
|
||||||
.amplify(1.0)
|
.amplify(1.0)
|
||||||
.skippable()
|
.skippable()
|
||||||
|
@ -152,12 +130,16 @@ impl Sink {
|
||||||
.periodic_access(Duration::from_millis(5), move |src| {
|
.periodic_access(Duration::from_millis(5), move |src| {
|
||||||
if controls.stopped.load(Ordering::SeqCst) {
|
if controls.stopped.load(Ordering::SeqCst) {
|
||||||
src.stop();
|
src.stop();
|
||||||
|
*controls.position.lock().unwrap() = 0.0;
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
let mut to_clear = controls.to_clear.lock().unwrap();
|
let mut to_clear = controls.to_clear.lock().unwrap();
|
||||||
if *to_clear > 0 {
|
if *to_clear > 0 {
|
||||||
src.inner_mut().skip();
|
src.inner_mut().skip();
|
||||||
*to_clear -= 1;
|
*to_clear -= 1;
|
||||||
|
*controls.position.lock().unwrap() = 0.0;
|
||||||
|
} else {
|
||||||
|
*controls.position.lock().unwrap() = src.inner().inner().inner().inner().get_pos();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let amp = src.inner_mut().inner_mut();
|
let amp = src.inner_mut().inner_mut();
|
||||||
|
@ -339,7 +321,7 @@ impl Sink {
|
||||||
/// Returns the position of the sound that's being played.
|
/// Returns the position of the sound that's being played.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get_pos(&self) -> f64 {
|
pub fn get_pos(&self) -> f64 {
|
||||||
self.position.load(Ordering::Relaxed)
|
*self.controls.position.lock().unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
//! Sources of sound and various filters.
|
//! Sources of sound and various filters.
|
||||||
|
|
||||||
use std::sync::Arc;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use cpal::FromSample;
|
use cpal::FromSample;
|
||||||
|
|
||||||
use crate::sink::AtomicF64;
|
|
||||||
use crate::Sample;
|
use crate::Sample;
|
||||||
|
|
||||||
pub use self::amplify::Amplify;
|
pub use self::amplify::Amplify;
|
||||||
|
@ -337,11 +335,11 @@ where
|
||||||
skippable::skippable(self)
|
skippable::skippable(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn trackable(self, position: Arc<AtomicF64>) -> TrackPosition<Self>
|
fn trackable(self) -> TrackPosition<Self>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
position::trackable(self, position)
|
position::trackable(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Applies a low-pass filter to the source.
|
/// Applies a low-pass filter to the source.
|
||||||
|
|
|
@ -1,19 +1,15 @@
|
||||||
use std::{
|
use std::time::Duration;
|
||||||
sync::{atomic::Ordering, Arc},
|
|
||||||
time::Duration,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{sink::AtomicF64, Sample, Source};
|
use crate::{Sample, Source};
|
||||||
|
|
||||||
use super::SeekError;
|
use super::SeekError;
|
||||||
|
|
||||||
/// Internal function that builds a `TrackPosition` object.
|
/// Internal function that builds a `TrackPosition` object.
|
||||||
pub fn trackable<I>(source: I, position: Arc<AtomicF64>) -> TrackPosition<I> {
|
pub fn trackable<I>(source: I) -> TrackPosition<I> {
|
||||||
TrackPosition {
|
TrackPosition {
|
||||||
input: source,
|
input: source,
|
||||||
samples_counted: 0,
|
samples_counted: 0,
|
||||||
offset_duration: 0.0,
|
offset_duration: 0.0,
|
||||||
position,
|
|
||||||
current_frame_sample_rate: 0,
|
current_frame_sample_rate: 0,
|
||||||
current_frame_channels: 0,
|
current_frame_channels: 0,
|
||||||
current_frame_len: None,
|
current_frame_len: None,
|
||||||
|
@ -25,7 +21,6 @@ pub struct TrackPosition<I> {
|
||||||
input: I,
|
input: I,
|
||||||
samples_counted: usize,
|
samples_counted: usize,
|
||||||
offset_duration: f64,
|
offset_duration: f64,
|
||||||
position: Arc<AtomicF64>,
|
|
||||||
current_frame_sample_rate: u32,
|
current_frame_sample_rate: u32,
|
||||||
current_frame_channels: u16,
|
current_frame_channels: u16,
|
||||||
current_frame_len: Option<usize>,
|
current_frame_len: Option<usize>,
|
||||||
|
@ -58,7 +53,7 @@ where
|
||||||
{
|
{
|
||||||
/// Returns the position of the source.
|
/// Returns the position of the source.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn get_pos(&self) -> f64 {
|
pub fn get_pos(&self) -> f64 {
|
||||||
self.samples_counted as f64 / self.input.sample_rate() as f64 / self.input.channels() as f64
|
self.samples_counted as f64 / self.input.sample_rate() as f64 / self.input.channels() as f64
|
||||||
+ self.offset_duration
|
+ self.offset_duration
|
||||||
}
|
}
|
||||||
|
@ -101,7 +96,6 @@ where
|
||||||
self.set_current_frame();
|
self.set_current_frame();
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
self.position.store(self.get_pos(), Ordering::Relaxed);
|
|
||||||
item
|
item
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,7 +139,6 @@ where
|
||||||
// starts again at the beginning of a frame. Which is the case with
|
// starts again at the beginning of a frame. Which is the case with
|
||||||
// symphonia.
|
// symphonia.
|
||||||
self.samples_counted = 0;
|
self.samples_counted = 0;
|
||||||
self.position.store(self.get_pos(), Ordering::Relaxed);
|
|
||||||
}
|
}
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
@ -153,28 +146,24 @@ where
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::sync::atomic::Ordering;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use crate::buffer::SamplesBuffer;
|
use crate::buffer::SamplesBuffer;
|
||||||
use crate::sink::AtomicF64;
|
|
||||||
use crate::source::Source;
|
use crate::source::Source;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_position() {
|
fn test_position() {
|
||||||
let inner = SamplesBuffer::new(1, 1, vec![10i16, -10, 10, -10, 20, -20]);
|
let inner = SamplesBuffer::new(1, 1, vec![10i16, -10, 10, -10, 20, -20]);
|
||||||
let position = Arc::new(AtomicF64::new(0.0));
|
let mut source = inner.trackable();
|
||||||
let mut source = inner.trackable(position.clone());
|
|
||||||
|
|
||||||
assert_eq!(position.load(Ordering::Relaxed), 0.0);
|
assert_eq!(source.get_pos(), 0.0);
|
||||||
source.next();
|
source.next();
|
||||||
assert_eq!(position.load(Ordering::Relaxed), 1.0);
|
assert_eq!(source.get_pos(), 1.0);
|
||||||
|
|
||||||
source.next();
|
source.next();
|
||||||
assert_eq!(position.load(Ordering::Relaxed), 2.0);
|
assert_eq!(source.get_pos(), 2.0);
|
||||||
|
|
||||||
assert_eq!(source.try_seek(Duration::new(1, 0)).is_ok(), true);
|
assert_eq!(source.try_seek(Duration::new(1, 0)).is_ok(), true);
|
||||||
assert_eq!(position.load(Ordering::Relaxed), 1.0);
|
assert_eq!(source.get_pos(), 1.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue