diff --git a/agb-fixnum/src/lib.rs b/agb-fixnum/src/lib.rs index 722cb98b..3ba3a47f 100644 --- a/agb-fixnum/src/lib.rs +++ b/agb-fixnum/src/lib.rs @@ -376,6 +376,20 @@ impl Num { self.0 } + /// Lossily transforms an f32 into a fixed point representation. This is not const + /// because you cannot currently do floating point operations in const contexts, so + /// you should use the `num!` macro from agb-macros if you want a const from_f32/f64 + pub fn from_f32(input: f32) -> Self { + Self::from_raw(I::from_as_i32((input * (1 << N) as f32) as i32)) + } + + /// Lossily transforms an f64 into a fixed point representation. This is not const + /// because you cannot currently do floating point operations in const contexts, so + /// you should use the `num!` macro from agb-macros if you want a const from_f32/f64 + pub fn from_f64(input: f64) -> Self { + Self::from_raw(I::from_as_i32((input * (1 << N) as f64) as i32)) + } + /// Truncates the fixed point number returning the integral part /// ```rust /// # use agb_fixnum::*; diff --git a/tracker/agb-xm-core/src/lib.rs b/tracker/agb-xm-core/src/lib.rs index b2f53192..dd7c0636 100644 --- a/tracker/agb-xm-core/src/lib.rs +++ b/tracker/agb-xm-core/src/lib.rs @@ -95,7 +95,7 @@ pub fn parse_module(module: &Module) -> TokenStream { usize::MAX }; - let volume = Num::from_raw((sample.volume * (1 << 8) as f32) as i16); + let volume = Num::from_f32(sample.volume); let sample = match &sample.data { SampleDataType::Depth8(depth8) => depth8 @@ -110,7 +110,7 @@ pub fn parse_module(module: &Module) -> TokenStream { .collect::>(), }; - let fadeout = Num::from_raw((instrument.volume_fadeout * ((1 << 8) as f32)) as i32); + let fadeout = Num::from_f32(instrument.volume_fadeout); instruments_map.insert((instrument_index, sample_index), samples.len()); samples.push(SampleData { @@ -477,8 +477,8 @@ fn note_to_speed( let gba_audio_frequency = 18157f64; - let speed: f64 = frequency / gba_audio_frequency; - Num::from_raw((speed * (1 << 12) as f64) as u32) + let speed = frequency / gba_audio_frequency; + Num::from_f64(speed) } fn note_to_frequency_linear(note: Note, fine_tune: f64, relative_note: i8) -> f64 { @@ -536,7 +536,7 @@ impl From<&xmrs::envelope::Envelope> for EnvelopeData { let second_point = &e.point[index + 1]; let amount = EnvelopePoint::lerp(first_point, second_point, xm_frame) / 64.0; - let amount = Num::from_raw((amount * (1 << 8) as f32) as i16); + let amount = Num::from_f32(amount); amounts.push(amount); }