Skip to content
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

Getting error "data chunk length is not a multiple of sample size" for some files #63

Open
dathinaios opened this issue Oct 17, 2022 · 0 comments

Comments

@dathinaios
Copy link

Hello,

I am getting this error for certain audio files only. The code works fine with other files! Could you point me to the right direction on what might be the cause?

This is the function that I am using:

pub fn load_fundsp_wav(path: &str) -> Vec<Arc<fundsp::hacker::Wave64>> {
    let paths = fs::read_dir(path).unwrap();
    let mut paths_vec = Vec::new();
    for path in paths {
        let mut reader = hound::WavReader::open(path.unwrap().path()).unwrap();
        let spec = reader.spec();
        nih_dbg!("{:?}", spec);

        let mut samples: Vec<f64> = Vec::new();
        if spec.bits_per_sample == 16 {
            let read = reader.samples().collect::<Result<Vec<i32>, hound::Error>>();
            if let Ok(result) = read {
                const I16_MAX: i16 = std::i16::MAX;
                samples = result
                    .iter()
                    .map(|val| *val as f64 / I16_MAX as f64)
                    .collect::<Vec<f64>>();
            }
        } else if spec.bits_per_sample == 24 {
            let read = reader.samples().collect::<Result<Vec<i32>, hound::Error>>();
            if let Ok(result) = read {
                const I24_MAX: i32 = 2_i32.pow(23) - 1;
                samples = result
                    .iter()
                    .map(|val| *val as f64 / I24_MAX as f64)
                    .collect::<Vec<f64>>();
            }
        } else {
            nih_error!("There was no compatible file found")
        };

        let mut wavefile =
            Wave64::with_capacity(spec.channels.into(), spec.sample_rate.into(), samples.len());

        wavefile.resize(samples.len());

        for (pos, s) in samples.iter().enumerate() {
            wavefile.set(0, pos, *s);
        }

        paths_vec.push(Arc::new(wavefile));
    }
    paths_vec
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant