RustAudio / dasp

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

Resample Example does not work. #158

Open patrickvonplaten opened 3 years ago

patrickvonplaten commented 3 years ago

Hey,

thanks a lot for the nice library!

When trying to run the official resampling example - here, I'm getting some import errors.

Here the code:

// An example of using `sample` to efficiently perform decent quality sample rate conversion on a
// WAV file entirely on the stack.

use dasp::{interpolate::sinc::Sinc, ring_buffer, signal, Sample, Signal};
use hound::{WavReader, WavWriter};

fn main() {
    // Find and load the wav.
    let assets = find_folder::Search::ParentsThenKids(5, 5)
        .for_folder("assets")
        .unwrap();
    let reader = WavReader::open(assets.join("two_vowels.wav")).unwrap();

    // Get the wav spec and create a target with the new desired sample rate.
    let spec = reader.spec();
    let mut target = spec;
    target.sample_rate = 10_000;

    // Read the interleaved samples and convert them to a signal.
    let samples = reader
        .into_samples()
        .filter_map(Result::ok)
        .map(i16::to_sample::<f64>);
    let signal = signal::from_interleaved_samples_iter(samples);

    // Convert the signal's sample rate using `Sinc` interpolation.
    let ring_buffer = ring_buffer::Fixed::from([[0.0]; 100]);
    let sinc = Sinc::new(ring_buffer);
    let new_signal = signal.from_hz_to_hz(sinc, spec.sample_rate as f64, target.sample_rate as f64);

    // Write the result to a new file.
    let mut writer = WavWriter::create(assets.join("two_vowels_10k.wav"), target).unwrap();
    for frame in new_signal.until_exhausted() {
        writer.write_sample(frame[0].to_sample::<i16>()).unwrap();
    }
}

which gives:

error[E0433]: failed to resolve: could not find `interpolate` in `dasp`
 --> src/main.rs:4:12
  |
4 | use dasp::{interpolate::sinc::Sinc, ring_buffer, signal, Sample, Signal};
  |            ^^^^^^^^^^^ could not find `interpolate` in `dasp`

error[E0432]: unresolved imports `dasp::ring_buffer`, `dasp::signal`, `dasp::Signal`
 --> src/main.rs:4:37
  |
4 | use dasp::{interpolate::sinc::Sinc, ring_buffer, signal, Sample, Signal};
  |                                     ^^^^^^^^^^^  ^^^^^^          ^^^^^^ no `Signal` in the root
  |                                     |            |
  |                                     |            no `signal` in the root
  |                                     no `ring_buffer` in the root

error[E0433]: failed to resolve: use of undeclared crate or module `find_folder`
 --> src/main.rs:9:18
  |
9 |     let assets = find_folder::Search::ParentsThenKids(5, 5)
  |                  ^^^^^^^^^^^ use of undeclared crate or module `find_folder`

error[E0433]: failed to resolve: use of undeclared type `Sinc`
  --> src/main.rs:28:16
   |
28 |     let sinc = Sinc::new(ring_buffer);
   |                ^^^^ use of undeclared type `Sinc`

When run in main.rs.

Do you know how to successfully fix it?

Jerboas86 commented 3 years ago

Resample example works fine to me.

Are you sure that you didn't forget the features attribute on dasp dependency in your cargo.toml. To help debug, you can compare your cargo.toml with mine.

[package]
name = "dasp"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "^1.0"
cpal = "^0.13"
dasp = {version = "^0.11", features= ["all"]}
hound = "^3.4"
find_folder = "0.3"