Reuse envelopes if they are the same

This commit is contained in:
Gwilym Inzani 2023-08-05 00:58:39 +01:00
parent aabfb1b083
commit ff5d324356
2 changed files with 21 additions and 12 deletions

View file

@ -166,7 +166,7 @@ fixed_width_signed_integer_impl!(i16);
fixed_width_signed_integer_impl!(i32);
/// 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)]
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
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, Hash)]
pub struct Vector2D<T: Number> {
/// The x coordinate
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
pub struct Rect<T: Number> {
/// The position of the rectangle

View file

@ -61,6 +61,7 @@ pub fn parse_module(module: &Module) -> TokenStream {
let mut samples = vec![];
let mut envelopes: Vec<EnvelopeData> = vec![];
let mut existing_envelopes: HashMap<EnvelopeData, usize> = Default::default();
for (instrument_index, instrument) in instruments.iter().enumerate() {
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_id = if envelope.enabled {
envelopes.push(envelope.as_ref().into());
Some(envelopes.len() - 1)
let envelope: EnvelopeData = envelope.as_ref().into();
let id = existing_envelopes
.entry(envelope)
.or_insert_with_key(|envelope| {
envelopes.push(envelope.clone());
envelopes.len() - 1
});
Some(*id)
} else {
None
};
@ -180,10 +188,10 @@ pub fn parse_module(module: &Module) -> TokenStream {
PatternEffect::VolumeSlide(Num::new((slot.volume - 0x70) as i16) / 64)
}
0x80..=0x8F => PatternEffect::FineVolumeSlide(
-Num::new((slot.volume - 0x80) as i16) / 64,
-Num::new((slot.volume - 0x80) as i16) / 128,
),
0x90..=0x9F => PatternEffect::FineVolumeSlide(
Num::new((slot.volume - 0x90) as i16) / 64,
Num::new((slot.volume - 0x90) as i16) / 128,
),
0xC0..=0xCF => PatternEffect::Panning(
Num::new(slot.volume as i16 - (0xC0 + (0xCF - 0xC0) / 2)) / 8,
@ -306,14 +314,14 @@ pub fn parse_module(module: &Module) -> TokenStream {
0x8 => {
PatternEffect::Panning(Num::new(slot.effect_parameter as i16 - 128) / 128)
}
0xA => {
0x5 | 0x6 | 0xA => {
let first = effect_parameter >> 4;
let second = effect_parameter & 0xF;
if first == 0 {
PatternEffect::VolumeSlide(-Num::new(second as i16) / 16)
PatternEffect::VolumeSlide(-Num::new(second as i16) / 64)
} else {
PatternEffect::VolumeSlide(Num::new(first as i16) / 16)
PatternEffect::VolumeSlide(Num::new(first as i16) / 64)
}
}
0xC => {
@ -327,10 +335,10 @@ pub fn parse_module(module: &Module) -> TokenStream {
}
0xE => match slot.effect_parameter >> 4 {
0xA => PatternEffect::FineVolumeSlide(
Num::new((slot.effect_parameter & 0xf) as i16) / 64,
Num::new((slot.effect_parameter & 0xf) as i16) / 128,
),
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()),
_ => PatternEffect::None,
@ -481,6 +489,7 @@ const AMEGA_FREQUENCIES: &[u32] = &[
457,
];
#[derive(PartialEq, Eq, Hash, Clone)]
struct EnvelopeData {
amounts: Vec<Num<i16, 8>>,
sustain: Option<usize>,