Skip to content

Commit

Permalink
A kinda hacky stereo linking implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
MyBlackMIDIScore committed Jun 15, 2024
1 parent f585339 commit d65c2a3
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 113 deletions.
36 changes: 6 additions & 30 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 core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ simdeez = "2.0.0-dev3"
proc-macro2 = "1.0.63"

[dev-dependencies]
midi-toolkit-rs = { git = "https://github.com/arduano/midi-toolkit-rs", rev = "a54f198" }
midi-toolkit-rs = { git = "https://github.com/arduano/midi-toolkit-rs", rev = "26747a3" }
rand = "0.8.5"
criterion = "0.4.0"

Expand Down
27 changes: 4 additions & 23 deletions core/src/soundfont/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
AudioStreamParams, ChannelCount,
};

use soundfonts::{FilterType, LoopMode};
use soundfonts::{convert_sample_index, FilterType, LoopMode};

pub mod audio;
mod voice_spawners;
Expand Down Expand Up @@ -182,10 +182,6 @@ pub enum LoadSfError {
Unsupported,
}

fn convert_sample_index(idx: u32, old_sample_rate: u32, new_sample_rate: u32) -> u32 {
(new_sample_rate as f32 * idx as f32 / old_sample_rate as f32).round() as u32
}

impl SampleSoundfont {
pub fn new(
path: impl Into<PathBuf>,
Expand Down Expand Up @@ -412,29 +408,15 @@ impl SampleSoundfont {

let pan = ((region.pan as f32 / 500.0) + 1.0) / 2.0;

let sample_rate = region.sample_rate;

let loop_params = LoopParams {
mode: if region.loop_start == region.loop_end {
LoopMode::NoLoop
} else {
region.loop_mode
},
offset: convert_sample_index(
region.offset,
sample_rate,
stream_params.sample_rate,
),
start: convert_sample_index(
region.loop_start,
sample_rate,
stream_params.sample_rate,
),
end: convert_sample_index(
region.loop_end,
sample_rate,
stream_params.sample_rate,
),
offset: region.offset,
start: region.loop_start,
end: region.loop_end,
};

let mut region_samples = region.sample.clone();
Expand All @@ -444,7 +426,6 @@ impl SampleSoundfont {
region_samples =
Arc::new([region_samples[0].clone(), region_samples[0].clone()]);
}
// FIXME: Stereo linked samples

let spawner_params = Arc::new(SampleVoiceSpawnerParams {
pan,
Expand Down
2 changes: 1 addition & 1 deletion realtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ wav = "1.0.0"
core = { path = "../core", package = "xsynth-core" }

[dev-dependencies]
midi-toolkit-rs = { git = "https://github.com/arduano/midi-toolkit-rs.git", rev = "a54f198" }
midi-toolkit-rs = { git = "https://github.com/arduano/midi-toolkit-rs.git", rev = "26747a3" }
gen-iter = { git = "https://github.com/arduano/gen-iter.git", rev = "64e28bc" }

[build-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ core = { path = "../core", package = "xsynth-core" }
crossbeam-channel = "0.5.1"
hound = "3.5.0"
rayon = "1.5.3"
midi-toolkit-rs = { git = "https://github.com/arduano/midi-toolkit-rs", rev = "a54f198" }
midi-toolkit-rs = { git = "https://github.com/arduano/midi-toolkit-rs", rev = "26747a3" }
spin_sleep = "1.0.0"
atomic_float = "0.1.0"
thiserror = "1.0.31"
4 changes: 4 additions & 0 deletions soundfonts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ pub enum LoopMode {
LoopContinuous,
LoopSustain,
}

pub fn convert_sample_index(idx: u32, old_sample_rate: u32, new_sample_rate: u32) -> u32 {
(new_sample_rate as f32 * idx as f32 / old_sample_rate as f32).round() as u32
}
44 changes: 22 additions & 22 deletions soundfonts/src/resample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ pub fn resample_vecs(
new_sample_rate: f32,
) -> Arc<[Arc<[f32]>]> {
vecs.into_iter()
.map(|samples| {
let params = SincInterpolationParameters {
sinc_len: 32,
f_cutoff: 0.95,
interpolation: SincInterpolationType::Linear,
oversampling_factor: 128,
window: WindowFunction::BlackmanHarris2,
};

let len = samples.len();
let mut resampler = SincFixedIn::<f32>::new(
new_sample_rate as f64 / sample_rate as f64,
2.0,
params,
len,
1,
)
.unwrap();
resampler.process(&[samples], None).unwrap()[0]
.clone()
.into()
})
.map(|samples| resample_vec(samples, sample_rate, new_sample_rate))
.collect()
}

pub fn resample_vec(vec: Vec<f32>, sample_rate: f32, new_sample_rate: f32) -> Arc<[f32]> {
let params = SincInterpolationParameters {
sinc_len: 32,
f_cutoff: 0.95,
interpolation: SincInterpolationType::Linear,
oversampling_factor: 128,
window: WindowFunction::BlackmanHarris2,
};

let len = vec.len();
let mut resampler = SincFixedIn::<f32>::new(
new_sample_rate as f64 / sample_rate as f64,
2.0,
params,
len,
1,
)
.unwrap();
resampler.process(&[vec], None).unwrap()[0].clone().into()
}
2 changes: 2 additions & 0 deletions soundfonts/src/sf2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct Sf2Zone {
pub root_override: Option<i16>,
}

#[derive(Clone)]
pub struct Sf2Region {
pub sample: Arc<[Arc<[f32]>]>,
pub sample_rate: u32,
Expand Down Expand Up @@ -95,5 +96,6 @@ pub fn load_soundfont(
sample_data,
instruments,
presets,
sample_rate,
))
}
Loading

0 comments on commit d65c2a3

Please sign in to comment.