Skip to content

Commit

Permalink
fix: Add precise handling of export buffer size
Browse files Browse the repository at this point in the history
  • Loading branch information
tphbrok committed Jul 17, 2024
1 parent f4c2ee6 commit 70c6976
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
.DS_Store
71 changes: 42 additions & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,16 @@ impl Plugin for Beatrec {
let is_playing = transport.playing;
let sample_rate = transport.sample_rate;

let range_samples;
let range_samples: usize;

match context.transport().loop_range_samples() {
// If a loop is active in the transport, set range_samples to the amount of samples in that loop
Some((start, end)) => {
range_samples = (end - start) as f32;
range_samples = (end - start) as usize;
}
// Otherwise, take a single beat (dependent on the transport tempo)
None => {
range_samples = sample_rate / (tempo / 60.0);
range_samples = (sample_rate / (tempo / 60.0)).round() as usize;
}
}

Expand Down Expand Up @@ -180,48 +181,60 @@ impl Plugin for Beatrec {
.push(channel_samples.into_iter().map(|s| s.to_f32()).collect());

self.recording_progress
.set(self.recording_buffer.len() as f32 / range_samples);
.set(self.recording_buffer.len() as f32 / range_samples as f32);

if self.recording_buffer.len() >= range_samples {
let mut temp_buffer = self.recording_buffer.clone();
temp_buffer.truncate(range_samples);

let mut remainder = self.recording_buffer.clone();
remainder.reverse();
remainder.truncate(self.recording_buffer.len() - range_samples);
remainder.reverse();

if self.recording_buffer.len() as f32 >= range_samples {
self.export_buffer.clear();
self.export_buffer.append(&mut self.recording_buffer);
self.export_buffer.append(&mut temp_buffer);
self.recording_buffer.clear();
self.recording_buffer.append(&mut remainder);
}
} else {
self.recording_buffer.clear();
}
}

let average_frame_size = (range_samples / 2400.0).round() as usize;
if is_playing {
let average_frame_size = (range_samples / 2400) as usize;
let chunks = self.export_buffer.chunks_exact(average_frame_size);

let chunks = self.export_buffer.chunks_exact(average_frame_size);
let averages = chunks
.map(|chunk| {
let mut average = 0.0;

let averages = chunks
.map(|chunk| {
let mut average = 0.0;

for channel_samples in chunk {
average += channel_samples.iter().sum::<f32>() / channel_samples.len() as f32;
}
for channel_samples in chunk {
average +=
channel_samples.iter().sum::<f32>() / channel_samples.len() as f32;
}

(average / average_frame_size as f32).clamp(-1.0, 1.0).neg()
})
.collect();
(average / average_frame_size as f32).clamp(-1.0, 1.0).neg()
})
.collect();

self.waveform_buffer_input.write(averages);
self.waveform_buffer_input.write(averages);

if self.output_buffer.len() > 0 {
let output_slice: Vec<_> = self
.output_buffer
.drain(0..buffer.samples().min(self.output_buffer.len() - 1))
.collect();
if self.output_buffer.len() > 0 {
let output_slice: Vec<_> = self
.output_buffer
.drain(0..buffer.samples().min(self.output_buffer.len() - 1))
.collect();

if output_slice.len() > 0 {
for (i, channel_samples) in buffer.iter_samples().enumerate() {
let channel_output_slice = output_slice[i.min(output_slice.len() - 1)].clone();
if output_slice.len() > 0 {
for (i, channel_samples) in buffer.iter_samples().enumerate() {
let channel_output_slice =
output_slice[i.min(output_slice.len() - 1)].clone();

for (j, sample) in channel_samples.into_iter().enumerate() {
*sample = channel_output_slice[j];
for (j, sample) in channel_samples.into_iter().enumerate() {
*sample = channel_output_slice[j];
}
}
}
}
Expand Down

0 comments on commit 70c6976

Please sign in to comment.