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

Add support for the tune opcode #62

Merged
merged 3 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions core/src/soundfont/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ fn key_vel_to_index(key: u8, vel: u8) -> usize {
(key as usize) * 128 + (vel as usize)
}

fn cents_factor(cents: f32) -> f32 {
2.0f32.powf(cents / 1200.0)
}

pub struct SampleSoundfont {
spawner_params_list: Vec<Vec<Arc<SampleVoiceSpawnerParams>>>,
stream_params: AudioStreamParams,
Expand Down Expand Up @@ -417,7 +421,8 @@ impl SampleSoundfont {
for key in region.keyrange.clone() {
for vel in region.velrange.clone() {
let index = key_vel_to_index(key, vel);
let speed_mult = get_speed_mult_from_keys(key, region.pitch_keycenter);
let speed_mult = get_speed_mult_from_keys(key, region.pitch_keycenter)
* cents_factor(region.tune as f32);

let envelope_params = unique_envelope_params
.iter()
Expand All @@ -433,7 +438,7 @@ impl SampleSoundfont {
let cents = vel as f32 / 127.0 * region.fil_veltrack as f32
+ (key as f32 - region.fil_keycenter as f32)
* region.fil_keytrack as f32;
cutoff_t *= 2.0f32.powf(cents / 1200.0);
cutoff_t *= cents_factor(cents);
cutoff = Some(
cutoff_t
.clamp(1.0, stream_params.sample_rate as f32 / 2.0 - 100.0),
Expand Down
5 changes: 5 additions & 0 deletions soundfonts/src/sfz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub struct RegionParamsBuilder {
fil_keytrack: i16,
filter_type: FilterType,
ampeg_envelope: AmpegEnvelopeParams,
tune: i16,
}

impl Default for RegionParamsBuilder {
Expand All @@ -99,6 +100,7 @@ impl Default for RegionParamsBuilder {
fil_keytrack: 0,
filter_type: FilterType::default(),
ampeg_envelope: AmpegEnvelopeParams::default(),
tune: 0,
}
}
}
Expand Down Expand Up @@ -131,6 +133,7 @@ impl RegionParamsBuilder {
SfzOpcode::FilterType(val) => self.filter_type = val,
SfzOpcode::DefaultPath(val) => self.default_path = Some(val),
SfzOpcode::AmpegEnvelope(flag) => self.ampeg_envelope.update_from_flag(flag),
SfzOpcode::Tune(val) => self.tune = val,
}
}

Expand Down Expand Up @@ -165,6 +168,7 @@ impl RegionParamsBuilder {
fil_keytrack: self.fil_keytrack.clamp(0, 1200),
filter_type: self.filter_type,
ampeg_envelope: self.ampeg_envelope,
tune: self.tune,
})
}
}
Expand All @@ -188,6 +192,7 @@ pub struct RegionParams {
pub fil_keytrack: i16,
pub filter_type: FilterType,
pub ampeg_envelope: AmpegEnvelopeParams,
pub tune: i16,
}

fn get_group_level(group_type: SfzGroupType) -> Option<usize> {
Expand Down
2 changes: 2 additions & 0 deletions soundfonts/src/sfz/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub enum SfzOpcode {
FilKeytrack(i16),
FilterType(FilterType),
DefaultPath(String),
Tune(i16),
AmpegEnvelope(SfzAmpegEnvelope),
}

Expand Down Expand Up @@ -251,6 +252,7 @@ fn parse_sfz_opcode(
"loop_end" | "loopend" => parse_u32_in_range(val, 0..=u32::MAX).map(LoopEnd),
"offset" => parse_u32_in_range(val, 0..=u32::MAX).map(Offset),
"default_path" => Some(DefaultPath(val.replace('\\', "/"))),
"tune" => parse_i16_in_range(val, -2400..=2400).map(Tune),

"ampeg_delay" => parse_float_in_range(val, 0.0..=100.0)
.map(AmpegDelay)
Expand Down
Loading