coalesce delays

master
Ashley N. 2023-08-25 00:46:26 -04:00
parent 00f30dd5c0
commit 8aef9f262d
1 changed files with 49 additions and 1 deletions

View File

@ -344,6 +344,54 @@ fn apply_effects_to_channel( channel: &mut Channel, effects: &LinkedHashSet<Effe
Ok( events ) Ok( events )
} }
/**
* Replace concurrent sequences of waits with single larger waits. This reduces the ESF stream size.
*/
fn compact_delays( events: Vec<EchoEvent> ) -> Result<Vec<EchoEvent>, Box<dyn Error>> {
let mut new_events: Vec<EchoEvent> = Vec::new();
let mut current_sequence: Vec<EchoEvent> = Vec::new();
for event in events {
if let Some( esf_event_type ) = event.iter().next() {
if *esf_event_type == ESF_DELAY_SHORT || *esf_event_type == ESF_DELAY_LONG {
// Continue to push delay events into current_sequence
current_sequence.push( event );
} else {
// Coalesce all current delays in current_sequence and push them
// 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;
} 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();
}
new_events.push( event );
}
}
}
Ok( new_events )
}
/** /**
* For an entire row across all channels, generate the events that apply to the row as a whole. This usually means applying * For an entire row across all channels, generate the events that apply to the row as a whole. This usually means applying
* the waits so that the row can be flushed to Echo and played - ESF ticks are until the nearest wait or stop event. * the waits so that the row can be flushed to Echo and played - ESF ticks are until the nearest wait or stop event.
@ -390,5 +438,5 @@ pub fn get_events_for_row( channels: &mut [Channel], subrows: Vec<&PatternRow>,
events.push( get_delay( ticks_to_wait )? ); events.push( get_delay( ticks_to_wait )? );
} }
Ok( events ) Ok( compact_delays( events )? )
} }