Small refactor

master
Ashley N. 2023-08-25 12:56:45 -04:00
parent c91dc3d230
commit 18e45bc59d
1 changed files with 24 additions and 14 deletions

View File

@ -433,25 +433,13 @@ fn compact_delays( events: Vec<EchoEvent> ) -> Result<Vec<EchoEvent>, Box<dyn Er
}
/**
* 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.
* Get the delays due at the end of a row. These delays are what flushes the tick to Echo so that it can play.
*/
pub fn get_events_for_row( channels: &mut [Channel], subrows: Vec<&PatternRow>, ticks_to_wait: u8 ) -> Result<Vec<EchoEvent>, Box<dyn Error>> {
fn get_delays( channels: &mut [Channel], ticks_to_wait: u8 ) -> Result<Vec<EchoEvent>, Box<dyn Error>> {
let mut events: Vec<EchoEvent> = Vec::new();
// Get events for each subrow (part of the total row for each channel)
for i in 0..channels.len() {
// Apply effects to channel's current state
events.extend( apply_effects_to_channel( &mut channels[ i ], &subrows[ i ].effects )? );
// Get the ESF events for this channel's part of the row
events.extend( get_events_for_channel( &mut channels[ i ], subrows[ i ] )? );
}
// All portamento effects deploy per tick, not per row. So we need to aggregate all portamentos across all
// channels for this row, then flush them once per `ticks_to_wait` for this row.
// So let's say portamentos are defined on FM1 and FM5 and ticks_to_wait is 3. The generated events are:
// [ freq_shift_fm1, freq_shift_fm5, wait 1 tick, freq_shift_fm1, freq_shift_fm5, wait 1 tick, freq_shift_fm1, freq_shift_fm5, wait 1 tick ]
let mut active_portamentos: Vec<(usize, Effect)> = Vec::new();
for channel_id in 0..channels.len() {
let channel = &channels[ channel_id ];
@ -464,6 +452,9 @@ pub fn get_events_for_row( channels: &mut [Channel], subrows: Vec<&PatternRow>,
}
}
// Do we have any active portamentos? If so, push them according to the portamento wait pattern.
// So let's say portamentos are defined on FM1 and FM5 and ticks_to_wait is 3. The generated events are:
// [ freq_shift_fm1, freq_shift_fm5, wait 1 tick, freq_shift_fm1, freq_shift_fm5, wait 1 tick, freq_shift_fm1, freq_shift_fm5, wait 1 tick ]
if !active_portamentos.is_empty() {
for _tick in 0..ticks_to_wait {
for ( channel_id, portamento ) in &active_portamentos {
@ -480,3 +471,22 @@ pub fn get_events_for_row( channels: &mut [Channel], subrows: Vec<&PatternRow>,
Ok( compact_delays( events )? )
}
/**
* 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.
*/
pub fn get_events_for_row( channels: &mut [Channel], subrows: Vec<&PatternRow>, ticks_to_wait: u8 ) -> Result<Vec<EchoEvent>, Box<dyn Error>> {
let mut events: Vec<EchoEvent> = Vec::new();
// Get events for each subrow (part of the total row for each channel)
for i in 0..channels.len() {
// Apply effects to channel's current state
events.extend( apply_effects_to_channel( &mut channels[ i ], &subrows[ i ].effects )? );
// Get the ESF events for this channel's part of the row
events.extend( get_events_for_channel( &mut channels[ i ], subrows[ i ] )? );
}
Ok( get_delays( channels, ticks_to_wait )? )
}