Skip to content

Commit

Permalink
Implement Index<usize> on PortChannels, plus some unrelated clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
prokopyl committed Oct 24, 2024
1 parent cd5a9a8 commit a73d316
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
4 changes: 2 additions & 2 deletions host/examples/cpal/src/host/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl StreamAudioProcessor {
self.buffers.ensure_buffer_size_matches(data.len());
let sample_count = self.buffers.cpal_buf_len_to_frame_count(data.len());

let (ins, mut outs) = self.buffers.prepare_plugin_buffers(data.len());
let (ins, outs) = self.buffers.prepare_plugin_buffers(data.len());

let events = if let Some(midi) = self.midi_receiver.as_mut() {
midi.receive_all_events(sample_count as u64)
Expand All @@ -151,7 +151,7 @@ impl StreamAudioProcessor {

match self.audio_processor.process(
&ins,
&mut outs,
&outs,
&events,
&mut OutputEvents::void(),
Some(self.steady_counter),
Expand Down
4 changes: 2 additions & 2 deletions plugin/examples/gain/tests/test_gain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub fn it_works() {
latency: 0,
}]);

let mut output_channels = outputs_descriptors.with_output_buffers([AudioPortBuffer {
let output_channels = outputs_descriptors.with_output_buffers([AudioPortBuffer {
channels: AudioPortBufferType::f32_output_only(
output_buffers.iter_mut().map(|b| b.as_mut_slice()),
),
Expand All @@ -118,7 +118,7 @@ pub fn it_works() {
processor
.process(
&input_channels,
&mut output_channels,
&output_channels,
&input_events.as_input(),
&mut output_events.as_output(),
None,
Expand Down
2 changes: 1 addition & 1 deletion plugin/examples/polysynth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ impl<'a> PluginAudioProcessor<'a, PolySynthPluginShared, PolySynthPluginMainThre

// If somehow the host didn't give us a mono output, we copy the output to all channels
if output_channels.channel_count() > 1 {
let first_channel = output_channels.channel(0).unwrap();
// PANIC: we just checked that channel_count is > 1.
let first_channel = &output_channels[0];

// Copy the first channel into all the other channels.
for other_channel in output_channels.iter().skip(1) {
Expand Down
10 changes: 10 additions & 0 deletions plugin/src/process/audio/port.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::process::audio::{AudioBuffer, BufferError, CelledClapAudioBuffer, SampleType};
use clack_common::process::ConstantMask;
use std::ops::Index;
use std::slice::Iter;

/// An iterator of all the available [`Port`]s from an [`Audio`] struct.
Expand Down Expand Up @@ -212,6 +213,15 @@ impl<'a, S> Clone for PortChannels<'a, S> {

impl<'a, S> Copy for PortChannels<'a, S> {}

impl<'a, S> Index<usize> for PortChannels<'a, S> {
type Output = AudioBuffer<S>;

fn index(&self, index: usize) -> &Self::Output {
// SAFETY: this type guarantees the buffer pointer is valid and of size frames_count
unsafe { AudioBuffer::from_raw_parts(self.data[index], self.frames_count as usize) }
}
}

impl<'a, T> IntoIterator for PortChannels<'a, T> {
type Item = &'a AudioBuffer<T>;
type IntoIter = PortChannelsIter<'a, T>;
Expand Down

0 comments on commit a73d316

Please sign in to comment.