Introduce fixnum::from_f(32|64)

This commit is contained in:
Gwilym Inzani 2023-08-06 20:24:59 +01:00
parent 4f7fb7125b
commit eda11073ed
2 changed files with 19 additions and 5 deletions

View file

@ -376,6 +376,20 @@ impl<I: FixedWidthUnsignedInteger, const N: usize> Num<I, N> {
self.0 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 /// Truncates the fixed point number returning the integral part
/// ```rust /// ```rust
/// # use agb_fixnum::*; /// # use agb_fixnum::*;

View file

@ -95,7 +95,7 @@ pub fn parse_module(module: &Module) -> TokenStream {
usize::MAX 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 { let sample = match &sample.data {
SampleDataType::Depth8(depth8) => depth8 SampleDataType::Depth8(depth8) => depth8
@ -110,7 +110,7 @@ pub fn parse_module(module: &Module) -> TokenStream {
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
}; };
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()); instruments_map.insert((instrument_index, sample_index), samples.len());
samples.push(SampleData { samples.push(SampleData {
@ -477,8 +477,8 @@ fn note_to_speed(
let gba_audio_frequency = 18157f64; let gba_audio_frequency = 18157f64;
let speed: f64 = frequency / gba_audio_frequency; let speed = frequency / gba_audio_frequency;
Num::from_raw((speed * (1 << 12) as f64) as u32) Num::from_f64(speed)
} }
fn note_to_frequency_linear(note: Note, fine_tune: f64, relative_note: i8) -> f64 { 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 second_point = &e.point[index + 1];
let amount = EnvelopePoint::lerp(first_point, second_point, xm_frame) / 64.0; 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); amounts.push(amount);
} }