hound icon indicating copy to clipboard operation
hound copied to clipboard

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

Open dathinaios opened this issue 2 years ago • 0 comments

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
}

dathinaios avatar Oct 17 '22 20:10 dathinaios