See for instance channel separation logic with the creation of a new vector for each channel.
Copilot suggestion (not tested nor verified, possibly wrong or hallucinated):
// Declare reusable channel_samples buffer outside the frame loop
static mut CHANNEL_SAMPLES: Option<Vec<Vec<f32>>> = None;
let channel_samples = unsafe {
CHANNEL_SAMPLES.get_or_insert_with(|| {
let mut v = Vec::with_capacity(track_channels_count as usize);
for _ in 0..track_channels_count {
v.push(Vec::with_capacity(fft_size as usize));
}
v
})
};
// Now channel_samples is a mutable reference to the reusable buffer
let samples: &[f32] = &samples_cache
[start_index..(start_index + (fft_size as u64 * track_channels_count) as usize)];
// Isolate each channel's samples
// Reuse channel_samples buffers to avoid repeated allocations
if channel_samples.len() != track_channels_count as usize {
channel_samples.clear();
for _ in 0..track_channels_count {
channel_samples.push(Vec::with_capacity(fft_size as usize));
}
} else {
for buf in &mut channel_samples {
buf.clear();
}
}
for channel in 0..track_channels_count as usize {
// Fill each channel's buffer
channel_samples[channel].extend(
samples
.iter()
.skip(channel)
.step_by(track_channels_count as usize)
.copied()
);
See for instance channel separation logic with the creation of a new vector for each channel.
Copilot suggestion (not tested nor verified, possibly wrong or hallucinated):