diff --git a/src/oscillator/wavetable.rs b/src/oscillator/wavetable.rs index 4afa136..11044af 100644 --- a/src/oscillator/wavetable.rs +++ b/src/oscillator/wavetable.rs @@ -19,7 +19,18 @@ struct Table { harmonic: f64, } -impl Table { +/// サンプル読出しに必要な位置と長さ。帯域選択時に静的記述から複写する。 +#[derive(Clone, Copy)] +struct ReadTable { + offset: usize, + length: f64, +} + +impl ReadTable { + const fn new(table: &Table) -> Self { + Self { offset: table.offset, length: table.length } + } + #[inline] fn read_sample(&self, phase: f64) -> f64 { let position = phase * self.length; @@ -47,8 +58,8 @@ fn tables(wave: Waveform) -> &'static [Table] { #[derive(Clone, Copy)] pub(super) struct Selection { /// 周波数変更時に選ぶ倍音の多い帯域と、その隣の帯域。 - hi: &'static Table, - lo: &'static Table, + hi: ReadTable, + lo: ReadTable, /// 直前の帯域(倍音の少ないテーブル)への混合率。 mix: f64, /// Nyquist直前で基音を0へ下げる倍率。他の帯域は1。 @@ -57,8 +68,8 @@ pub(super) struct Selection { impl Selection { pub const EMPTY: Self = Self { - hi: &data::SAW[0], - lo: &data::SAW[0], + hi: ReadTable::new(&data::SAW[0]), + lo: ReadTable::new(&data::SAW[0]), mix: 0.0, gain: 0.0, }; @@ -81,8 +92,8 @@ impl Selection { let gain = if index == 0 { 1.0 - mix } else { 1.0 }; let mix = if index == 0 { 0.0 } else { mix }; Self { - hi: rich, - lo: &tables[index.saturating_sub(1)], + hi: ReadTable::new(rich), + lo: ReadTable::new(&tables[index.saturating_sub(1)]), mix, gain, } @@ -118,7 +129,7 @@ mod tests { .partition_point(|t| t.harmonic * increment < 0.5) .saturating_sub(1); assert!( - std::ptr::eq(Selection::new(wave, increment).hi, &tables(wave)[reference]), + Selection::new(wave, increment).hi.offset == tables(wave)[reference].offset, "increment={increment:?}" ); }; @@ -152,7 +163,7 @@ mod tests { assert_eq!(table.length.fract(), 0.0); assert!(table.length * end <= 65536.0); for phase in [0.0, end / 2.0, end] { - assert!(table.read_sample(phase).is_finite()); + assert!(ReadTable::new(table).read_sample(phase).is_finite()); } } }