Skip to content

replace current_span_length in favor of parameters_changed #706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 25 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7cf2caf
find replace current_span_len with parameters changed
dvdsk Feb 24, 2025
0eac7bc
implement `parameters_changed` for simple and medium complexity sources
dvdsk Feb 24, 2025
0e40dbf
(re)add tests for skip_duration, fixes skip duration
dvdsk Feb 24, 2025
5fc542d
rewrite and test take_duration similar to skip_duration
dvdsk Feb 25, 2025
25cfb9e
refactor take_duration
dvdsk Feb 25, 2025
e5f1b1a
Adds test to blt_filter. Notes removal of source factories in changelog
dvdsk Feb 26, 2025
7dacb2e
fixes position and provides `frame_change` test for it
dvdsk Feb 26, 2025
5b40b66
rename peekable -> peekablesource, document it, add test file
dvdsk Feb 26, 2025
e1253f3
refactor buffered + fix size_hint for TestSource
dvdsk Feb 26, 2025
af6b92e
note parameters_changed behaviour before first next
dvdsk Feb 26, 2025
92eb10a
move crossfade tests to integration tests
dvdsk Feb 26, 2025
f923115
adds integration tests for repeat
dvdsk Feb 26, 2025
0168f24
add test and fix take_span
dvdsk Feb 27, 2025
6902aaa
test and fix take_duration fadeout
dvdsk Feb 27, 2025
9090c55
implement parameters_changed for wav decoder
dvdsk Feb 27, 2025
c9e7795
updates pipeline bench to new API changes
dvdsk Feb 27, 2025
1fddd89
removes left over debug prints
dvdsk Feb 27, 2025
5e7d8d8
This makes ChannelCount NonZero<u16> and channels not zero asserts
dvdsk Feb 28, 2025
6447d9e
adds test for reverb
dvdsk Feb 28, 2025
58cd766
fix buffered SpanData::End crashing other sources + remove dbg!'s
dvdsk Feb 28, 2025
23af4e4
adds tests for queue and channel vol. + work on queue
dvdsk Mar 1, 2025
32f2039
update docs for peekable
dvdsk Mar 2, 2025
3634210
partially implements lazy parameters_changed for testsource & uniform
dvdsk Mar 2, 2025
756ba51
write part of alternative lock-free buffered
dvdsk Mar 2, 2025
ea2c3b6
more buffered2 work
dvdsk Mar 3, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Minimal builds without `cpal` audio output are now supported.
See `README.md` for instructions. (#349)
- Added `Sample::is_zero()` method for checking zero samples.
- Added `TakeDuration::with_fadeout` similar to `TakeDuration::fadeout` but returns `Self`

### Changed
- Breaking: `OutputStreamBuilder` should now be used to initialize an audio output stream.
Expand All @@ -31,6 +32,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Breaking: The term 'frame' was renamed to 'span' in the crate and documentation.
- Breaking: Sources now use `f32` samples. To convert to and from other types of samples use functions from
`dasp_sample` crate. For example `DaspSample::from_sample(sample)`. Remove `integer-decoder` feature.
- Breaking: Sources wrapping an existing source had a public factory method
which is now removed. Something like: `source::amplify(unamplified, 1.2)` must now be
written as `unamplified.amplify(1.2)`.
- Breaking: `TakeDuration::set_filter_fadeout()` has been removed. Use `TakeDuration.fadeout(true)`.
- `SignalGenerator`'s `Function` is now Copy.


### Fixed
Expand Down
79 changes: 78 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ tracing = { version = "0.1.40", optional = true }

atomic_float = { version = "1.1.0", optional = true }
num-rational = "0.4.2"
append-only-vec = "0.1.7"

[features]
default = ["playback", "flac", "vorbis", "wav", "mp3"]
Expand Down Expand Up @@ -66,6 +67,8 @@ rstest_reuse = "0.6.0"
approx = "0.5.1"
dasp_sample = "0.11.0"
divan = "0.1.14"
spectrum-analyzer = "1.6.0"
itertools = "0.14.0"

[[bench]]
name = "effects"
Expand Down
11 changes: 6 additions & 5 deletions benches/pipeline.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::time::Duration;

use divan::Bencher;
use rodio::ChannelCount;
use rodio::{source::UniformSourceIterator, Source};

mod shared;
Expand All @@ -13,7 +14,7 @@ fn main() {
#[divan::bench]
fn long(bencher: Bencher) {
bencher.with_inputs(|| music_wav()).bench_values(|source| {
let mut take_dur = source
let effects_applied = source
.high_pass(300)
.amplify(1.2)
.speed(0.9)
Expand All @@ -25,13 +26,13 @@ fn long(bencher: Bencher) {
)
.delay(Duration::from_secs_f32(0.5))
.fade_in(Duration::from_secs_f32(2.0))
.take_duration(Duration::from_secs(10));
take_dur.set_filter_fadeout();
let effects_applied = take_dur
.take_duration(Duration::from_secs(10))
.with_fadeout(true)
.buffered()
.reverb(Duration::from_secs_f32(0.05), 0.3)
.skippable();
let resampled = UniformSourceIterator::new(effects_applied, 2, 40_000);
let resampled =
UniformSourceIterator::new(effects_applied, ChannelCount::new(2).unwrap(), 40_000);
resampled.for_each(divan::black_box_drop)
})
}
Expand Down
6 changes: 3 additions & 3 deletions benches/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rodio::{ChannelCount, Sample, SampleRate, Source};

pub struct TestSource {
samples: vec::IntoIter<Sample>,
channels: u16,
channels: ChannelCount,
sample_rate: u32,
total_duration: Duration,
}
Expand All @@ -29,8 +29,8 @@ impl ExactSizeIterator for TestSource {

impl Source for TestSource {
#[inline]
fn current_span_len(&self) -> Option<usize> {
None // forever
fn parameters_changed(&self) -> bool {
false
}

#[inline]
Expand Down
3 changes: 2 additions & 1 deletion examples/mix_multiple_sources.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use rodio::mixer;
use rodio::source::{SineWave, Source};
use std::error::Error;
use std::num::NonZero;
use std::time::Duration;

fn main() -> Result<(), Box<dyn Error>> {
// Construct a dynamic controller and mixer, stream_handle, and sink.
let (controller, mixer) = mixer::mixer(2, 44_100);
let (controller, mixer) = mixer::mixer(NonZero::new(2).unwrap(), 44_100);
let stream_handle = rodio::OutputStreamBuilder::open_default_stream()?;
let sink = rodio::Sink::connect_new(&stream_handle.mixer());

Expand Down
36 changes: 16 additions & 20 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ impl SamplesBuffer {
///
/// # Panic
///
/// - Panics if the number of channels is zero.
/// - Panics if the samples rate is zero.
/// - Panics if the length of the buffer is larger than approximately 16 billion elements.
/// This is because the calculation of the duration would overflow.
Expand All @@ -39,13 +38,12 @@ impl SamplesBuffer {
where
D: Into<Vec<Sample>>,
{
assert!(channels >= 1);
assert!(sample_rate >= 1);

let data = data.into();
let duration_ns = 1_000_000_000u64.checked_mul(data.len() as u64).unwrap()
/ sample_rate as u64
/ channels as u64;
/ channels.get() as u64;
let duration = Duration::new(
duration_ns / 1_000_000_000,
(duration_ns % 1_000_000_000) as u32,
Expand All @@ -63,8 +61,8 @@ impl SamplesBuffer {

impl Source for SamplesBuffer {
#[inline]
fn current_span_len(&self) -> Option<usize> {
None
fn parameters_changed(&self) -> bool {
false
}

#[inline]
Expand All @@ -89,14 +87,14 @@ impl Source for SamplesBuffer {
// and due to the constant sample_rate we can jump to the right
// sample directly.

let curr_channel = self.pos % self.channels() as usize;
let new_pos = pos.as_secs_f32() * self.sample_rate() as f32 * self.channels() as f32;
let curr_channel = self.pos % self.channels().get() as usize;
let new_pos = pos.as_secs_f32() * self.sample_rate() as f32 * self.channels().get() as f32;
// saturate pos at the end of the source
let new_pos = new_pos as usize;
let new_pos = new_pos.min(self.data.len());

// make sure the next sample is for the right channel
let new_pos = new_pos.next_multiple_of(self.channels() as usize);
let new_pos = new_pos.next_multiple_of(self.channels().get() as usize);
let new_pos = new_pos - curr_channel;

self.pos = new_pos;
Expand All @@ -123,36 +121,31 @@ impl Iterator for SamplesBuffer {
#[cfg(test)]
mod tests {
use crate::buffer::SamplesBuffer;
use crate::math::ch;
use crate::source::Source;

#[test]
fn basic() {
let _ = SamplesBuffer::new(1, 44100, vec![0.0, 0.0, 0.0, 0.0, 0.0, 0.0]);
}

#[test]
#[should_panic]
fn panic_if_zero_channels() {
SamplesBuffer::new(0, 44100, vec![0.0, 0.0, 0.0, 0.0, 0.0, 0.0]);
let _ = SamplesBuffer::new(ch!(1), 44100, vec![0.0, 0.0, 0.0, 0.0, 0.0, 0.0]);
}

#[test]
#[should_panic]
fn panic_if_zero_sample_rate() {
SamplesBuffer::new(1, 0, vec![0.0, 0.0, 0.0, 0.0, 0.0, 0.0]);
SamplesBuffer::new(ch!(1), 0, vec![0.0, 0.0, 0.0, 0.0, 0.0, 0.0]);
}

#[test]
fn duration_basic() {
let buf = SamplesBuffer::new(2, 2, vec![0.0, 0.0, 0.0, 0.0, 0.0, 0.0]);
let buf = SamplesBuffer::new(ch!(2), 2, vec![0.0, 0.0, 0.0, 0.0, 0.0, 0.0]);
let dur = buf.total_duration().unwrap();
assert_eq!(dur.as_secs(), 1);
assert_eq!(dur.subsec_nanos(), 500_000_000);
}

#[test]
fn iteration() {
let mut buf = SamplesBuffer::new(1, 44100, vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
let mut buf = SamplesBuffer::new(ch!(1), 44100, vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
assert_eq!(buf.next(), Some(1.0));
assert_eq!(buf.next(), Some(2.0));
assert_eq!(buf.next(), Some(3.0));
Expand All @@ -172,7 +165,7 @@ mod tests {
#[test]
fn channel_order_stays_correct() {
const SAMPLE_RATE: SampleRate = 100;
const CHANNELS: ChannelCount = 2;
const CHANNELS: ChannelCount = ch!(2);
let mut buf = SamplesBuffer::new(
CHANNELS,
SAMPLE_RATE,
Expand All @@ -182,7 +175,10 @@ mod tests {
.collect::<Vec<_>>(),
);
buf.try_seek(Duration::from_secs(5)).unwrap();
assert_eq!(buf.next(), Some(5.0 * SAMPLE_RATE as f32 * CHANNELS as f32));
assert_eq!(
buf.next(),
Some(5.0 * SAMPLE_RATE as f32 * CHANNELS.get() as f32)
);

assert!(buf.next().is_some_and(|s| s.trunc() as i32 % 2 == 1));
assert!(buf.next().is_some_and(|s| s.trunc() as i32 % 2 == 0));
Expand Down
6 changes: 4 additions & 2 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::num::NonZero;

/// Stream sample rate (a frame rate or samples per second per channel).
pub type SampleRate = u32;

/// Number of channels in a stream.
pub type ChannelCount = u16;
/// Number of channels in a stream. Can never be Zero
pub type ChannelCount = NonZero<u16>;

/// Represents value of a single sample.
/// Silence corresponds to the value `0.0`. The expected amplitude range is -1.0...1.0.
Expand Down
Loading
Loading