RustAudio / dasp

The fundamentals for Digital Audio Signal Processing. Formerly `sample`.
Other
867 stars 63 forks source link

Overflow when using Sinc Interpolation on i16 samples #169

Open lschmierer opened 2 years ago

lschmierer commented 2 years ago

The following code

pub fn sinc<S: Signal<Frame = [i16; 1]>>(
    source: S,
    source_rate: f64,
    target_rate: f64,
) -> impl Signal<Frame = [i16; 1]> {
    let ring_buffer = ring_buffer::Fixed::from([[0]; 100]);
    let sinc = Sinc::new(ring_buffer);
    source.from_hz_to_hz(sinc, source_rate, target_rate)
}

yields

thread 'main' panicked at 'attempt to add with overflow', /Users/lschmierer/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/arith.rs:107:1
stack backtrace:
   0: rust_begin_unwind
             at /rustc/4f20caa6258d4c74ce6b316fd347e3efe81cf557/library/std/src/panicking.rs:493:5
   1: core::panicking::panic_fmt
             at /rustc/4f20caa6258d4c74ce6b316fd347e3efe81cf557/library/core/src/panicking.rs:92:14
   2: core::panicking::panic
             at /rustc/4f20caa6258d4c74ce6b316fd347e3efe81cf557/library/core/src/panicking.rs:50:5
   3: <i16 as core::ops::arith::Add>::add
             at /Users/lschmierer/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/arith.rs:100:45
   4: dasp_sample::Sample::add_amp
             at /Users/lschmierer/.cargo/registry/src/github.com-1ecc6299db9ec823/dasp_sample-0.11.0/src/lib.rs:211:9
   5: <dasp_interpolate::sinc::Sinc<S> as dasp_interpolate::Interpolator>::interpolate::{{closure}}::{{closure}}
             at /Users/lschmierer/.cargo/registry/src/github.com-1ecc6299db9ec823/dasp_interpolate-0.11.0/src/sinc/mod.rs:109:17
   6: <[S; 1] as dasp_frame::Frame>::zip_map::{{closure}}
             at /Users/lschmierer/.cargo/registry/src/github.com-1ecc6299db9ec823/dasp_frame-0.11.0/src/lib.rs:365:29
   7: <[S; 1] as dasp_frame::Frame>::from_fn
             at /Users/lschmierer/.cargo/registry/src/github.com-1ecc6299db9ec823/dasp_frame-0.11.0/src/lib.rs:299:24
   8: <[S; 1] as dasp_frame::Frame>::zip_map
             at /Users/lschmierer/.cargo/registry/src/github.com-1ecc6299db9ec823/dasp_frame-0.11.0/src/lib.rs:355:21
   9: <dasp_interpolate::sinc::Sinc<S> as dasp_interpolate::Interpolator>::interpolate::{{closure}}
             at /Users/lschmierer/.cargo/registry/src/github.com-1ecc6299db9ec823/dasp_interpolate-0.11.0/src/sinc/mod.rs:108:13
  10: core::iter::traits::iterator::Iterator::fold
             at /Users/lschmierer/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:2122:21
  11: <dasp_interpolate::sinc::Sinc<S> as dasp_interpolate::Interpolator>::interpolate
             at /Users/lschmierer/.cargo/registry/src/github.com-1ecc6299db9ec823/dasp_interpolate-0.11.0/src/sinc/mod.rs:91:9
  12: <dasp_signal::interpolate::Converter<S,I> as dasp_signal::Signal>::next
             at /Users/lschmierer/.cargo/registry/src/github.com-1ecc6299db9ec823/dasp_signal-0.11.0/src/interpolate.rs:136:19
  13: james::main
             at ./src/main.rs:24:24
  14: core::ops::function::FnOnce::call_once
             at /Users/lschmierer/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5

When the problem occurred, I tried downsampling from 44,1kHz to 16kHz.

Possibly related to #73 and #167.