Skip to content

Commit

Permalink
Merge pull request #27 from CorvusPrudens/scratch-alias
Browse files Browse the repository at this point in the history
`ScratchBuffer` type alias
  • Loading branch information
BillyDM authored Feb 24, 2025
2 parents 7741edd + 42973e2 commit ee0c981
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 26 deletions.
11 changes: 9 additions & 2 deletions crates/firewheel-core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub trait AudioNodeProcessor: 'static + Send {
outputs: &mut [&mut [f32]],
events: NodeEventList,
proc_info: &ProcInfo,
scratch_buffers: &mut [&mut [f32]; NUM_SCRATCH_BUFFERS],
scratch_buffers: ScratchBuffers,
) -> ProcessStatus;

/// Called when the audio stream has been stopped.
Expand All @@ -92,6 +92,13 @@ pub trait AudioNodeProcessor: 'static + Send {

pub const NUM_SCRATCH_BUFFERS: usize = 8;

/// A list of extra scratch buffers that can be
/// used for processing. This removes the need for nodes to allocate
/// their own scratch buffers. Each buffer has a length of
/// [`StreamInfo::max_block_frames`]. These buffers are shared across
/// all nodes, so assume that they contain junk data.
pub type ScratchBuffers<'a, 'b> = &'a mut [&'b mut [f32]; NUM_SCRATCH_BUFFERS];

/// Additional information for processing audio
pub struct ProcInfo<'a> {
/// The number of samples (in a single channel of audio) in this
Expand Down Expand Up @@ -234,7 +241,7 @@ impl AudioNodeProcessor for DummyProcessor {
_outputs: &mut [&mut [f32]],
_events: NodeEventList,
_proc_info: &ProcInfo,
_scratch_buffers: &mut [&mut [f32]; NUM_SCRATCH_BUFFERS],
_scratch_buffers: ScratchBuffers,
) -> ProcessStatus {
ProcessStatus::Bypass
}
Expand Down
4 changes: 2 additions & 2 deletions crates/firewheel-cpal/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use firewheel_core::{
event::{NodeEventList, NodeEventType},
node::{
AudioNodeConstructor, AudioNodeInfo, AudioNodeProcessor, ProcInfo, ProcessStatus,
NUM_SCRATCH_BUFFERS,
ScratchBuffers,
},
sync_wrapper::SyncWrapper,
SilenceMask, StreamInfo,
Expand Down Expand Up @@ -486,7 +486,7 @@ impl AudioNodeProcessor for Processor {
outputs: &mut [&mut [f32]],
mut events: NodeEventList,
proc_info: &ProcInfo,
_scratch_buffers: &mut [&mut [f32]; NUM_SCRATCH_BUFFERS],
_scratch_buffers: ScratchBuffers,
) -> ProcessStatus {
events.for_each(|event| {
if let NodeEventType::Custom(event) = event {
Expand Down
4 changes: 2 additions & 2 deletions crates/firewheel-nodes/src/beep_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use firewheel_core::{
event::NodeEventList,
node::{
AudioNodeConstructor, AudioNodeInfo, AudioNodeProcessor, ProcInfo, ProcessStatus,
NUM_SCRATCH_BUFFERS,
ScratchBuffers,
},
};

Expand Down Expand Up @@ -92,7 +92,7 @@ impl AudioNodeProcessor for Processor {
outputs: &mut [&mut [f32]],
events: NodeEventList,
_proc_info: &ProcInfo,
_scratch_buffers: &mut [&mut [f32]; NUM_SCRATCH_BUFFERS],
_scratch_buffers: ScratchBuffers,
) -> ProcessStatus {
let Some(out) = outputs.first_mut() else {
return ProcessStatus::ClearAllOutputs;
Expand Down
4 changes: 2 additions & 2 deletions crates/firewheel-nodes/src/sampler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use firewheel_core::{
event::{NodeEventList, NodeEventType, SequenceCommand},
node::{
AudioNodeConstructor, AudioNodeInfo, AudioNodeProcessor, ProcInfo, ProcessStatus,
NUM_SCRATCH_BUFFERS,
ScratchBuffers,
},
sample_resource::SampleResource,
SilenceMask, StreamInfo,
Expand Down Expand Up @@ -770,7 +770,7 @@ impl AudioNodeProcessor for SamplerProcessor {
outputs: &mut [&mut [f32]],
mut events: NodeEventList,
proc_info: &ProcInfo,
_scratch_buffers: &mut [&mut [f32]; NUM_SCRATCH_BUFFERS],
_scratch_buffers: ScratchBuffers,
) -> ProcessStatus {
events.for_each(|event| match event {
NodeEventType::SequenceCommand(command) => {
Expand Down
4 changes: 2 additions & 2 deletions crates/firewheel-nodes/src/spatial_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use firewheel_core::{
event::{NodeEventList, Vec3},
node::{
AudioNodeConstructor, AudioNodeInfo, AudioNodeProcessor, ProcInfo, ProcessStatus,
NUM_SCRATCH_BUFFERS,
ScratchBuffers,
},
param::smoother::{SmoothedParam, SmootherConfig},
SilenceMask,
Expand Down Expand Up @@ -279,7 +279,7 @@ impl AudioNodeProcessor for Processor {
outputs: &mut [&mut [f32]],
events: NodeEventList,
proc_info: &ProcInfo,
_scratch_buffers: &mut [&mut [f32]; NUM_SCRATCH_BUFFERS],
_scratch_buffers: ScratchBuffers,
) -> ProcessStatus {
if self.params.patch_list(events) {
let computed_values = self.params.compute_values();
Expand Down
4 changes: 2 additions & 2 deletions crates/firewheel-nodes/src/stereo_to_mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use firewheel_core::{
event::NodeEventList,
node::{
AudioNodeConstructor, AudioNodeInfo, AudioNodeProcessor, ProcInfo, ProcessStatus,
NUM_SCRATCH_BUFFERS,
ScratchBuffers,
},
};

Expand Down Expand Up @@ -39,7 +39,7 @@ impl AudioNodeProcessor for StereoToMonoProcessor {
outputs: &mut [&mut [f32]],
_events: NodeEventList,
proc_info: &ProcInfo,
_scratch_buffers: &mut [&mut [f32]; NUM_SCRATCH_BUFFERS],
_scratch_buffers: ScratchBuffers,
) -> ProcessStatus {
if proc_info.in_silence_mask.all_channels_silent(2)
|| inputs.len() < 2
Expand Down
4 changes: 2 additions & 2 deletions crates/firewheel-nodes/src/stream/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use firewheel_core::{
event::{NodeEventList, NodeEventType},
node::{
AudioNodeConstructor, AudioNodeInfo, AudioNodeProcessor, ProcInfo, ProcessStatus,
NUM_SCRATCH_BUFFERS,
ScratchBuffers,
},
sync_wrapper::SyncWrapper,
StreamInfo,
Expand Down Expand Up @@ -406,7 +406,7 @@ impl AudioNodeProcessor for Processor {
_outputs: &mut [&mut [f32]],
mut events: NodeEventList,
proc_info: &ProcInfo,
_scratch_buffers: &mut [&mut [f32]; NUM_SCRATCH_BUFFERS],
_scratch_buffers: ScratchBuffers,
) -> ProcessStatus {
events.for_each(|event| {
if let NodeEventType::Custom(event) = event {
Expand Down
4 changes: 2 additions & 2 deletions crates/firewheel-nodes/src/stream/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use firewheel_core::{
event::{NodeEventList, NodeEventType},
node::{
AudioNodeConstructor, AudioNodeInfo, AudioNodeProcessor, ProcInfo, ProcessStatus,
NUM_SCRATCH_BUFFERS,
ScratchBuffers,
},
sync_wrapper::SyncWrapper,
SilenceMask, StreamInfo,
Expand Down Expand Up @@ -388,7 +388,7 @@ impl AudioNodeProcessor for Processor {
outputs: &mut [&mut [f32]],
mut events: NodeEventList,
proc_info: &ProcInfo,
_scratch_buffers: &mut [&mut [f32]; NUM_SCRATCH_BUFFERS],
_scratch_buffers: ScratchBuffers,
) -> ProcessStatus {
events.for_each(|event| {
if let NodeEventType::Custom(event) = event {
Expand Down
4 changes: 2 additions & 2 deletions crates/firewheel-nodes/src/volume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use firewheel_core::{
event::NodeEventList,
node::{
AudioNodeConstructor, AudioNodeInfo, AudioNodeProcessor, ProcInfo, ProcessStatus,
NUM_SCRATCH_BUFFERS,
ScratchBuffers,
},
param::smoother::{SmoothedParam, SmootherConfig},
SilenceMask,
Expand Down Expand Up @@ -129,7 +129,7 @@ impl AudioNodeProcessor for VolumeProcessor {
outputs: &mut [&mut [f32]],
events: NodeEventList,
proc_info: &ProcInfo,
scratch_buffers: &mut [&mut [f32]; NUM_SCRATCH_BUFFERS],
scratch_buffers: ScratchBuffers,
) -> ProcessStatus {
if self.params.patch_list(events) {
self.gain.set_value(self.params.normalized_volume);
Expand Down
4 changes: 2 additions & 2 deletions crates/firewheel-nodes/src/volume_pan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use firewheel_core::{
event::NodeEventList,
node::{
AudioNodeConstructor, AudioNodeInfo, AudioNodeProcessor, ProcInfo, ProcessStatus,
NUM_SCRATCH_BUFFERS,
ScratchBuffers,
},
param::smoother::{SmoothedParam, SmootherConfig},
SilenceMask,
Expand Down Expand Up @@ -147,7 +147,7 @@ impl AudioNodeProcessor for Processor {
outputs: &mut [&mut [f32]],
events: NodeEventList,
proc_info: &ProcInfo,
_scratch_buffers: &mut [&mut [f32]; NUM_SCRATCH_BUFFERS],
_scratch_buffers: ScratchBuffers,
) -> ProcessStatus {
if self.params.patch_list(events) {
let (gain_l, gain_r) = self.params.compute_gains();
Expand Down
4 changes: 2 additions & 2 deletions examples/custom_nodes/src/nodes/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use firewheel::{
event::NodeEventList,
node::{
AudioNodeConstructor, AudioNodeInfo, AudioNodeProcessor, ProcInfo, ProcessStatus,
NUM_SCRATCH_BUFFERS,
ScratchBuffers,
},
param::smoother::{SmoothedParam, SmoothedParamBuffer},
SilenceMask, StreamInfo,
Expand Down Expand Up @@ -135,7 +135,7 @@ impl AudioNodeProcessor for Processor {
// Additional information about the process.
proc_info: &ProcInfo,
// Optional scratch buffers that can be used for processing.
_scratch_buffers: &mut [&mut [f32]; NUM_SCRATCH_BUFFERS],
_scratch_buffers: ScratchBuffers,
) -> ProcessStatus {
// Process the events.
let enabled = self.params.enabled;
Expand Down
4 changes: 2 additions & 2 deletions examples/custom_nodes/src/nodes/noise_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use firewheel::{
event::NodeEventList,
node::{
AudioNodeConstructor, AudioNodeInfo, AudioNodeProcessor, ProcInfo, ProcessStatus,
NUM_SCRATCH_BUFFERS,
ScratchBuffers,
},
SilenceMask, StreamInfo,
};
Expand Down Expand Up @@ -111,7 +111,7 @@ impl AudioNodeProcessor for Processor {
// Additional information about the process.
_proc_info: &ProcInfo,
// Optional scratch buffers that can be used for processing.
_scratch_buffers: &mut [&mut [f32]; NUM_SCRATCH_BUFFERS],
_scratch_buffers: ScratchBuffers,
) -> ProcessStatus {
// Process the events.
if self.params.patch_list(events) {
Expand Down
4 changes: 2 additions & 2 deletions examples/custom_nodes/src/nodes/rms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use firewheel::{
event::NodeEventList,
node::{
AudioNodeConstructor, AudioNodeInfo, AudioNodeProcessor, ProcInfo, ProcessStatus,
NUM_SCRATCH_BUFFERS,
ScratchBuffers,
},
StreamInfo,
};
Expand Down Expand Up @@ -157,7 +157,7 @@ impl AudioNodeProcessor for Processor {
// Additional information about the process.
proc_info: &ProcInfo,
// Optional scratch buffers that can be used for processing.
_scratch_buffers: &mut [&mut [f32]; NUM_SCRATCH_BUFFERS],
_scratch_buffers: ScratchBuffers,
) -> ProcessStatus {
if !self.shared_state.enabled.load(Ordering::Relaxed) {
self.shared_state.rms_value.store(0.0, Ordering::Relaxed);
Expand Down

0 comments on commit ee0c981

Please sign in to comment.