mirror of
https://github.com/italicsjenga/agb.git
synced 2024-12-23 08:11:33 +11:00
Reuse envelopes if they are the same
This commit is contained in:
parent
aabfb1b083
commit
ff5d324356
|
@ -166,7 +166,7 @@ fixed_width_signed_integer_impl!(i16);
|
||||||
fixed_width_signed_integer_impl!(i32);
|
fixed_width_signed_integer_impl!(i32);
|
||||||
|
|
||||||
/// A fixed point number represented using `I` with `N` bits of fractional precision
|
/// A fixed point number represented using `I` with `N` bits of fractional precision
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
pub struct Num<I: FixedWidthUnsignedInteger, const N: usize>(I);
|
pub struct Num<I: FixedWidthUnsignedInteger, const N: usize>(I);
|
||||||
|
|
||||||
|
@ -631,7 +631,7 @@ impl<I: FixedWidthUnsignedInteger, const N: usize> Debug for Num<I, N> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A vector of two points: (x, y) represented by integers or fixed point numbers
|
/// A vector of two points: (x, y) represented by integers or fixed point numbers
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, Hash)]
|
||||||
pub struct Vector2D<T: Number> {
|
pub struct Vector2D<T: Number> {
|
||||||
/// The x coordinate
|
/// The x coordinate
|
||||||
pub x: T,
|
pub x: T,
|
||||||
|
@ -880,7 +880,7 @@ impl<I: FixedWidthUnsignedInteger, const N: usize> From<Vector2D<I>> for Vector2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
|
||||||
/// A rectangle with a position in 2d space and a 2d size
|
/// A rectangle with a position in 2d space and a 2d size
|
||||||
pub struct Rect<T: Number> {
|
pub struct Rect<T: Number> {
|
||||||
/// The position of the rectangle
|
/// The position of the rectangle
|
||||||
|
|
|
@ -61,6 +61,7 @@ pub fn parse_module(module: &Module) -> TokenStream {
|
||||||
|
|
||||||
let mut samples = vec![];
|
let mut samples = vec![];
|
||||||
let mut envelopes: Vec<EnvelopeData> = vec![];
|
let mut envelopes: Vec<EnvelopeData> = vec![];
|
||||||
|
let mut existing_envelopes: HashMap<EnvelopeData, usize> = Default::default();
|
||||||
|
|
||||||
for (instrument_index, instrument) in instruments.iter().enumerate() {
|
for (instrument_index, instrument) in instruments.iter().enumerate() {
|
||||||
let InstrumentType::Default(ref instrument) = instrument.instr_type else {
|
let InstrumentType::Default(ref instrument) = instrument.instr_type else {
|
||||||
|
@ -69,8 +70,15 @@ pub fn parse_module(module: &Module) -> TokenStream {
|
||||||
|
|
||||||
let envelope = &instrument.volume_envelope;
|
let envelope = &instrument.volume_envelope;
|
||||||
let envelope_id = if envelope.enabled {
|
let envelope_id = if envelope.enabled {
|
||||||
envelopes.push(envelope.as_ref().into());
|
let envelope: EnvelopeData = envelope.as_ref().into();
|
||||||
Some(envelopes.len() - 1)
|
let id = existing_envelopes
|
||||||
|
.entry(envelope)
|
||||||
|
.or_insert_with_key(|envelope| {
|
||||||
|
envelopes.push(envelope.clone());
|
||||||
|
envelopes.len() - 1
|
||||||
|
});
|
||||||
|
|
||||||
|
Some(*id)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
@ -180,10 +188,10 @@ pub fn parse_module(module: &Module) -> TokenStream {
|
||||||
PatternEffect::VolumeSlide(Num::new((slot.volume - 0x70) as i16) / 64)
|
PatternEffect::VolumeSlide(Num::new((slot.volume - 0x70) as i16) / 64)
|
||||||
}
|
}
|
||||||
0x80..=0x8F => PatternEffect::FineVolumeSlide(
|
0x80..=0x8F => PatternEffect::FineVolumeSlide(
|
||||||
-Num::new((slot.volume - 0x80) as i16) / 64,
|
-Num::new((slot.volume - 0x80) as i16) / 128,
|
||||||
),
|
),
|
||||||
0x90..=0x9F => PatternEffect::FineVolumeSlide(
|
0x90..=0x9F => PatternEffect::FineVolumeSlide(
|
||||||
Num::new((slot.volume - 0x90) as i16) / 64,
|
Num::new((slot.volume - 0x90) as i16) / 128,
|
||||||
),
|
),
|
||||||
0xC0..=0xCF => PatternEffect::Panning(
|
0xC0..=0xCF => PatternEffect::Panning(
|
||||||
Num::new(slot.volume as i16 - (0xC0 + (0xCF - 0xC0) / 2)) / 8,
|
Num::new(slot.volume as i16 - (0xC0 + (0xCF - 0xC0) / 2)) / 8,
|
||||||
|
@ -306,14 +314,14 @@ pub fn parse_module(module: &Module) -> TokenStream {
|
||||||
0x8 => {
|
0x8 => {
|
||||||
PatternEffect::Panning(Num::new(slot.effect_parameter as i16 - 128) / 128)
|
PatternEffect::Panning(Num::new(slot.effect_parameter as i16 - 128) / 128)
|
||||||
}
|
}
|
||||||
0xA => {
|
0x5 | 0x6 | 0xA => {
|
||||||
let first = effect_parameter >> 4;
|
let first = effect_parameter >> 4;
|
||||||
let second = effect_parameter & 0xF;
|
let second = effect_parameter & 0xF;
|
||||||
|
|
||||||
if first == 0 {
|
if first == 0 {
|
||||||
PatternEffect::VolumeSlide(-Num::new(second as i16) / 16)
|
PatternEffect::VolumeSlide(-Num::new(second as i16) / 64)
|
||||||
} else {
|
} else {
|
||||||
PatternEffect::VolumeSlide(Num::new(first as i16) / 16)
|
PatternEffect::VolumeSlide(Num::new(first as i16) / 64)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
0xC => {
|
0xC => {
|
||||||
|
@ -327,10 +335,10 @@ pub fn parse_module(module: &Module) -> TokenStream {
|
||||||
}
|
}
|
||||||
0xE => match slot.effect_parameter >> 4 {
|
0xE => match slot.effect_parameter >> 4 {
|
||||||
0xA => PatternEffect::FineVolumeSlide(
|
0xA => PatternEffect::FineVolumeSlide(
|
||||||
Num::new((slot.effect_parameter & 0xf) as i16) / 64,
|
Num::new((slot.effect_parameter & 0xf) as i16) / 128,
|
||||||
),
|
),
|
||||||
0xB => PatternEffect::FineVolumeSlide(
|
0xB => PatternEffect::FineVolumeSlide(
|
||||||
-Num::new((slot.effect_parameter & 0xf) as i16) / 64,
|
-Num::new((slot.effect_parameter & 0xf) as i16) / 128,
|
||||||
),
|
),
|
||||||
0xC => PatternEffect::NoteCut((slot.effect_parameter & 0xf).into()),
|
0xC => PatternEffect::NoteCut((slot.effect_parameter & 0xf).into()),
|
||||||
_ => PatternEffect::None,
|
_ => PatternEffect::None,
|
||||||
|
@ -481,6 +489,7 @@ const AMEGA_FREQUENCIES: &[u32] = &[
|
||||||
457,
|
457,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
#[derive(PartialEq, Eq, Hash, Clone)]
|
||||||
struct EnvelopeData {
|
struct EnvelopeData {
|
||||||
amounts: Vec<Num<i16, 8>>,
|
amounts: Vec<Num<i16, 8>>,
|
||||||
sustain: Option<usize>,
|
sustain: Option<usize>,
|
||||||
|
|
Loading…
Reference in a new issue