diff --git a/Cargo.lock b/Cargo.lock index 2994586..874cff1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1272,8 +1272,7 @@ dependencies = [ [[package]] name = "soundfont" version = "0.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88f3c4ee9be1386dc5d4f33fb1df6f813eb2f49510cb24e8bfd30784f4853fce" +source = "git+https://github.com/PolyMeilex/OxiSynth?tag=0.0.5#c2e95ab7156f0be617b0aa960ab3d049c84b2530" dependencies = [ "riff", ] diff --git a/soundfonts/Cargo.toml b/soundfonts/Cargo.toml index d60dc3d..1f24ca2 100644 --- a/soundfonts/Cargo.toml +++ b/soundfonts/Cargo.toml @@ -19,5 +19,5 @@ lazy-regex = "3.2.0" regex-bnf = "0.1.2" simdeez = "2.0.0-dev3" thiserror = "1.0.63" -soundfont = "0.0.3" +soundfont = { git = "https://github.com/PolyMeilex/OxiSynth", tag = "0.0.5" } rubato = "0.15.0" diff --git a/soundfonts/src/sf2/mod.rs b/soundfonts/src/sf2/mod.rs index 394c683..1c4be4f 100644 --- a/soundfonts/src/sf2/mod.rs +++ b/soundfonts/src/sf2/mod.rs @@ -15,11 +15,11 @@ pub enum Sf2ParseError { FailedToReadFile(PathBuf), #[error("Failed to parse file")] - FailedToParseFile, + FailedToParseFile(String), } /// Structure that holds the generator and modulator parameters of an SF2 region. -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct Sf2Region { pub sample: Arc<[Arc<[f32]>]>, pub sample_rate: u32, @@ -40,6 +40,7 @@ pub struct Sf2Region { } /// Structure that holds the parameters of an SF2 preset. +#[derive(Clone, Debug)] pub struct Sf2Preset { pub bank: u16, pub preset: u16, @@ -59,7 +60,7 @@ pub fn load_soundfont( .map_err(|_| Sf2ParseError::FailedToReadFile(sf2_path.clone()))?; let file = &mut file; let sf2 = soundfont::SoundFont2::load(file) - .map_err(|_| Sf2ParseError::FailedToParseFile)? + .map_err(|e| Sf2ParseError::FailedToParseFile(format!("{:#?}", e)))? .sort_presets(); let sample_data = sample::Sf2Sample::parse_sf2_samples( diff --git a/soundfonts/src/sf2/sample.rs b/soundfonts/src/sf2/sample.rs index 518f161..bcb1256 100644 --- a/soundfonts/src/sf2/sample.rs +++ b/soundfonts/src/sf2/sample.rs @@ -25,24 +25,32 @@ impl Sf2Sample { sample_rate: u32, ) -> Result, Sf2ParseError> { let smpl = if let Some(data) = data.smpl { - data.read_contents(file) - .map_err(|_| Sf2ParseError::FailedToParseFile)? + data.read_contents(file).map_err(|_| { + Sf2ParseError::FailedToParseFile("Error reading sample contents".to_string()) + })? } else { - return Err(Sf2ParseError::FailedToParseFile); + return Err(Sf2ParseError::FailedToParseFile( + "Soundfont does not contain samples".to_string(), + )); }; let mut samples = Vec::new(); if let Some(sm24) = data.sm24 { // SF2 is 24-bit - let extra = sm24 - .read_contents(file) - .map_err(|_| Sf2ParseError::FailedToParseFile)?; - if smpl.len() / 2 != extra.len() { - return Err(Sf2ParseError::FailedToParseFile); + let extra = sm24.read_contents(file).map_err(|_| { + Sf2ParseError::FailedToParseFile("Error reading extra sample contents".to_string()) + })?; + + let smpllen = smpl.len() / 2; + let extralen = extra.len() - (smpllen % 2); + if smpllen != extralen { + return Err(Sf2ParseError::FailedToParseFile( + "Invalid sample length".to_string(), + )); } - for i in 0..extra.len() { + for i in 0..extralen { let n0 = 0; let n1 = extra[i]; let n2 = smpl[i * 2];