Skip to content

Commit

Permalink
Merge branch 'master' of github.com:BlackMIDIDevs/xsynth
Browse files Browse the repository at this point in the history
  • Loading branch information
arduano committed Dec 28, 2024
2 parents d73640e + bff6a12 commit 1bcdaa9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
7 changes: 2 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion soundfonts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "0.1.0"
rubato = "0.15.0"
24 changes: 17 additions & 7 deletions soundfonts/src/sf2/sample.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use super::Sf2ParseError;
use crate::resample::resample_vec;
use soundfont::data::{
hydra::sample::{SampleHeader, SampleLink},
sample_data::SampleData,
use soundfont::raw::{SampleChunk, SampleData, SampleHeader, SampleLink};
use std::{
fs::File,
io::{self, Read, Seek, SeekFrom},
sync::Arc,
};
use std::{fs::File, sync::Arc};

#[derive(Clone, Debug)]
pub struct Sf2Sample {
Expand All @@ -18,14 +19,23 @@ pub struct Sf2Sample {
}

impl Sf2Sample {
fn read_chunk(file: &mut File, chunk: SampleChunk) -> io::Result<Vec<u8>> {
let mut buff = vec![0; chunk.len as usize];

file.seek(SeekFrom::Start(chunk.offset))?;
file.read_exact(&mut buff)?;

Ok(buff)
}

pub fn parse_sf2_samples(
file: &mut File,
headers: Vec<SampleHeader>,
data: SampleData,
sample_rate: u32,
) -> Result<Vec<Self>, Sf2ParseError> {
let smpl = if let Some(data) = data.smpl {
data.read_contents(file).map_err(|_| {
let smpl = if let Some(chunk) = data.smpl {
Self::read_chunk(file, chunk).map_err(|_| {
Sf2ParseError::FailedToParseFile("Error reading sample contents".to_string())
})?
} else {
Expand All @@ -38,7 +48,7 @@ impl Sf2Sample {

if let Some(sm24) = data.sm24 {
// SF2 is 24-bit
let extra = sm24.read_contents(file).map_err(|_| {
let extra = Self::read_chunk(file, sm24).map_err(|_| {
Sf2ParseError::FailedToParseFile("Error reading extra sample contents".to_string())
})?;

Expand Down
9 changes: 7 additions & 2 deletions soundfonts/src/sf2/zone.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::LoopMode;
use soundfont::{data::hydra::generator::GeneratorType, Zone};
use soundfont::{raw::GeneratorType, Zone};
use std::ops::RangeInclusive;

#[derive(Default, Clone, Debug)]
Expand Down Expand Up @@ -38,7 +38,12 @@ impl Sf2Zone {
let mut region = global_region.clone();

for gen in &zone.gen_list {
match gen.ty {
let Ok(gen_ty) = gen.ty.into_result() else {
// Some synths use non-spec generators let's just ignore them.
continue;
};

match gen_ty {
GeneratorType::StartAddrsOffset => region.offset = gen.amount.as_i16().copied(),
GeneratorType::StartAddrsCoarseOffset => {
region.offset_coarse = gen.amount.as_i16().copied()
Expand Down

0 comments on commit 1bcdaa9

Please sign in to comment.