From 18e45bc59dc49f4d704971355307b06880a9a157 Mon Sep 17 00:00:00 2001 From: ashley Date: Fri, 25 Aug 2023 12:56:45 -0400 Subject: [PATCH] Small refactor --- src/reskit/soundtrack/engines/echo.rs | 38 +++++++++++++++++---------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/reskit/soundtrack/engines/echo.rs b/src/reskit/soundtrack/engines/echo.rs index e9a7c32..8a678af 100644 --- a/src/reskit/soundtrack/engines/echo.rs +++ b/src/reskit/soundtrack/engines/echo.rs @@ -433,25 +433,13 @@ fn compact_delays( events: Vec ) -> Result, Box, ticks_to_wait: u8 ) -> Result, Box> { +fn get_delays( channels: &mut [Channel], ticks_to_wait: u8 ) -> Result, Box> { let mut events: Vec = 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 { @@ -479,4 +470,23 @@ 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, Box> { + let mut events: Vec = 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 )? ) } \ No newline at end of file