Skip to content

Commit

Permalink
Add benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwellmattryan committed Oct 2, 2024
1 parent 01029f7 commit 2e48983
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ Cargo.lock
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# Profiler output
# Flamegraph output
**/perf.data
**/perf.data.old

**/flamegraph.svg

## Test coverage output
Expand Down
4 changes: 2 additions & 2 deletions bbx_dsp/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ use crate::context::Context;
pub const DEFAULT_CONTEXT: Context = Context {
sample_rate: 44100,
num_channels: 2,
buffer_size: 256,
max_num_graph_nodes: 384,
buffer_size: 128,
max_num_graph_nodes: 1024,
};
2 changes: 1 addition & 1 deletion bbx_dsp/src/effectors/overdrive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl Process for OverdriveEffector {
sum_audio_inputs(inputs, output);

for channel_buffer in output.iter_mut() {
channel_buffer.apply(|s| s - (s.powi(3) / 3.0))
channel_buffer.apply(|s| s - (s.powi(3) / 3.0));
}
}
}
25 changes: 25 additions & 0 deletions bbx_dsp/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ impl Graph {

#[cfg(test)]
mod tests {
use test::Bencher;

use super::*;
use crate::{context::Context, effector::Effector, generator::Generator, generators::wave_table::Waveform};

Expand Down Expand Up @@ -323,4 +325,27 @@ mod tests {
let result = graph.evaluate();
assert_eq!(result.len(), 6); // Assuming 2 channels for this context
}

#[bench]
fn bench_evaluate(b: &mut Bencher) {
let context = Context::new(44100, 2, 128, 2048);
let mut graph = Graph::new(context);

let mixer = graph.add_effector(Effector::Mixer);
for n in 0..256 {
let osc = graph.add_generator(Generator::WaveTable {
frequency: 110.0 * (n + 1) as f32,
waveform: Waveform::Sine,
});
graph.create_connection(osc, mixer);
}
let overdrive = graph.add_effector(Effector::Overdrive);
graph.create_connection(mixer, overdrive);

graph.prepare_for_playback();

b.iter(|| {
graph.evaluate();
});
}
}
3 changes: 3 additions & 0 deletions bbx_dsp/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![feature(test)]
extern crate test;

pub mod effectors;
pub mod generators;

Expand Down
8 changes: 5 additions & 3 deletions bbx_sandbox/examples/03_n_osc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use bbx_dsp::{
};
use bbx_sandbox::{player::Player, signal::Signal};

const NUM_OSCILLATORS: usize = 128;
const NUM_OSCILLATORS: usize = 6;

const BASE_FREQUENCY: f32 = 55.0;
const MAX_FREQUENCY: f32 = 999.0;

fn main() {
// Create a `Graph` with the default context
Expand All @@ -17,8 +18,9 @@ fn main() {

// Create a number of oscillator nodes and connect to the mixer
for n in 0..NUM_OSCILLATORS {
let frequency = (n as f32 / NUM_OSCILLATORS as f32) * MAX_FREQUENCY + BASE_FREQUENCY;
let oscillator = graph.add_generator(Generator::WaveTable {
frequency: BASE_FREQUENCY * (n + 1) as f32,
frequency,
waveform: Waveform::Sine,
});
graph.create_connection(oscillator, mixer);
Expand All @@ -29,7 +31,7 @@ fn main() {
graph.create_connection(mixer, overdrive);

// Add a filter because it sounds harsh
let filter = graph.add_effector(Effector::Filter(DEFAULT_CONTEXT, 1500.0, 1.5));
let filter = graph.add_effector(Effector::Filter(DEFAULT_CONTEXT, MAX_FREQUENCY / 2.0, 1.5));
graph.create_connection(overdrive, filter);

// Prepare the graph for playback
Expand Down

0 comments on commit 2e48983

Please sign in to comment.