perf: remove allocation from sampler callback

This commit is contained in:
🪞👃🪞 2024-07-05 21:33:50 +03:00
parent 2a96c19069
commit 4204ac4462

View file

@ -68,14 +68,8 @@ impl Sampler {
} }
// Emit next chunk of each currently playing voice, // Emit next chunk of each currently playing voice,
// dropping voices that have reached their ends. // dropping voices that have reached their ends.
let mut voices = vec![];
std::mem::swap(&mut voices, &mut self.voices);
let gain = 0.5; let gain = 0.5;
loop { self.voices.retain_mut(|voice|{
if voices.len() < 1 {
break
}
let mut voice = voices.swap_remove(0);
if let Some(chunk) = voice.chunk(scope.n_frames() as usize) { if let Some(chunk) = voice.chunk(scope.n_frames() as usize) {
for (i, channel) in chunk.iter().enumerate() { for (i, channel) in chunk.iter().enumerate() {
let buffer = &mut mixed[i % channel_count]; let buffer = &mut mixed[i % channel_count];
@ -83,9 +77,11 @@ impl Sampler {
buffer[i] += sample * gain; buffer[i] += sample * gain;
} }
} }
self.voices.push(voice); true
} else {
false
} }
} });
// Write output buffer to output ports. // Write output buffer to output ports.
for (i, port) in self.ports.audio_outs.values_mut().enumerate() { for (i, port) in self.ports.audio_outs.values_mut().enumerate() {
let buffer = &mixed[i]; let buffer = &mixed[i];