Fix assorted bugs and get this shit workingggg...except maybe being off-key :x

master
Ashley N. 2023-08-26 18:48:01 -04:00
parent a1b9a5dde5
commit f50b974f4b
2 changed files with 42 additions and 27 deletions

View File

@ -293,7 +293,7 @@ fn apply_effects_to_channel( channel: &mut Channel, effects: &LinkedHashSet<Effe
// Add new note cut. Being an effect requiring delay generation, it will generate (and expire) in get_delays // Add new note cut. Being an effect requiring delay generation, it will generate (and expire) in get_delays
channel.active_effects.insert( Effect::NoteCut { after_ticks } ); channel.active_effects.insert( Effect::NoteCut { after_ticks } );
} },
Effect::SetPanning { left, right } => { Effect::SetPanning { left, right } => {
let left = *left; let left = *left;
let right = *right; let right = *right;
@ -332,6 +332,34 @@ fn apply_effects_to_channel( channel: &mut Channel, effects: &LinkedHashSet<Effe
Ok( events ) Ok( events )
} }
fn sequence_to_compacted_delay( current_sequence: &Vec<EchoEvent> ) -> Result<Vec<EchoEvent>, Box<dyn Error>> {
let mut new_events: Vec<EchoEvent> = Vec::new();
let mut cumulative_delay: u16 = 0;
for delay_event in current_sequence {
let mut iter = delay_event.iter();
let delay_type = *iter.next().ok_or( "internal error: empty event" )?;
if delay_type & 0xF0 == ESF_DELAY_SHORT {
cumulative_delay += ( ( delay_type & 0x0F ) as u16 ) + 1;
} else if delay_type == ESF_DELAY_LONG {
cumulative_delay += ( *iter.next().ok_or( "internal error: expected delay byte" )? ) as u16;
}
}
if cumulative_delay > 0 {
// Push amount of full-256 wait events
for _ in 0..cumulative_delay / 256 {
new_events.push( vec![ ESF_DELAY_LONG, 0x00 ] );
}
// Push the remainder under 256 event
new_events.push( get_delay( ( cumulative_delay % 256 ) as u8 )? );
}
Ok( new_events )
}
/** /**
* Replace concurrent sequences of waits with single larger waits. This reduces the ESF stream size. * Replace concurrent sequences of waits with single larger waits. This reduces the ESF stream size.
*/ */
@ -345,32 +373,9 @@ pub fn compact_delays( events: Vec<EchoEvent> ) -> Result<Vec<EchoEvent>, Box<dy
// Continue to push delay events into current_sequence // Continue to push delay events into current_sequence
current_sequence.push( event ); current_sequence.push( event );
} else { } else {
// Encountered a non-delay event - we will always push something here, let compacted_delay = sequence_to_compacted_delay( &current_sequence )?;
// but let's check first if we need to push a current_sequence of waits. if !compacted_delay.is_empty() {
// Coalesce all current delays in current_sequence and push them new_events.extend( compacted_delay.into_iter() );
// onto the new event stream.
let mut cumulative_delay: u16 = 0;
for delay_event in &current_sequence {
let mut iter = delay_event.iter();
let delay_type = *iter.next().ok_or( "internal error: empty event" )?;
if delay_type & 0xF0 == ESF_DELAY_SHORT {
cumulative_delay += ( ( delay_type & 0x0F ) as u16 ) + 1;
} else if delay_type == ESF_DELAY_LONG {
cumulative_delay += ( *iter.next().ok_or( "internal error: expected delay byte" )? ) as u16;
}
}
if cumulative_delay > 0 {
// Push amount of full-256 wait events
for _ in 0..cumulative_delay / 256 {
new_events.push( vec![ ESF_DELAY_LONG, 0x00 ] );
}
// Push the remainder under 256 event
new_events.push( get_delay( ( cumulative_delay % 256 ) as u8 )? );
// Reset the current sequence
current_sequence.clear(); current_sequence.clear();
} }
@ -379,6 +384,8 @@ pub fn compact_delays( events: Vec<EchoEvent> ) -> Result<Vec<EchoEvent>, Box<dy
} }
} }
new_events.extend( sequence_to_compacted_delay( &current_sequence )?.into_iter() );
Ok( new_events ) Ok( new_events )
} }
@ -454,6 +461,12 @@ fn get_note_cut( channel: &mut Channel, note_cut_effect: &Effect, tick: u8 ) ->
if tick >= after_ticks { if tick >= after_ticks {
if channel.active_note.is_some() { if channel.active_note.is_some() {
channel.active_note = None; channel.active_note = None;
// Consume the note cut after we are done with it - it does not remain active
channel.active_effects = channel.active_effects
.clone()
.into_iter()
.filter( | effect | !( matches!( effect, Effect::NoteCut { after_ticks: _ } ) ) )
.collect();
return Ok( vec![ ESF_NOTE_OFF | channel.id ] ); return Ok( vec![ ESF_NOTE_OFF | channel.id ] );
} }
} }

View File

@ -952,6 +952,8 @@ impl EchoFormat for DmfModule {
esf.extend( event ); esf.extend( event );
} }
esf.push( 0xFF ); // Terminate the stream
Ok( esf ) Ok( esf )
} }