-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmultichannel.rs
More file actions
156 lines (133 loc) · 4.26 KB
/
multichannel.rs
File metadata and controls
156 lines (133 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use core::slice;
use crossbeam_channel::{bounded, Receiver, Sender};
use nih_plug::buffer::Buffer;
use nih_plug::nih_dbg;
use nih_plug::prelude::AtomicF32;
use std::sync::atomic::Ordering;
use std::sync::{atomic, Arc, RwLock, Weak};
use super::*;
/// A bus for multi-channel data.
#[derive(Clone)]
pub struct MultiChannelBus<const C: usize> {
dispatchers: Arc<RwLock<Vec<Weak<dyn Fn(slice::Iter<'_, [f32; C]>) + Sync + Send>>>>,
channel: (Sender<[f32; C]>, Receiver<[f32; C]>),
sample_rate: Arc<AtomicF32>,
}
impl<const C: usize> MultiChannelBus<C> {
pub fn new(size: usize) -> Self {
let channel = bounded(size);
Self {
dispatchers: RwLock::new(vec![]).into(),
channel,
sample_rate: Arc::new(f32::NAN.into()),
}
}
}
impl<const C: usize> Default for MultiChannelBus<C> {
fn default() -> Self {
Self::new(4096)
}
}
impl<const C: usize> MultiChannelBus<C> {
/// Sends the latest audio data.
///
/// This operation will silently fail if the Bus is congested.
#[inline]
pub fn send_buffer(&self, buffer: &mut Buffer) {
for mut x in buffer.iter_samples() {
let mut array = [0.0; C];
for i in 0..C {
if let Some(sample) = x.get_mut(i) {
array[i] = sample.clone();
} else {
break;
}
}
self.send(array);
}
}
/// Sends a single sample.
///
/// This operation will silently fail if the Bus is congested.
#[inline]
pub fn send(&self, value: [f32; C]) {
let _ = self.channel.0.try_send(value);
}
/// Creates a mono bus, given a downmixer.
///
/// See [`IntoMonoBus`].
pub fn into_mono<D>(&self, downmixer: D) -> Arc<IntoMonoBus<C, D>>
where
for<'a> D: Fn(&'a [f32; C]) -> &'a f32 + 'static + Copy + Clone + Send + Sync,
{
IntoMonoBus {
bus: self.clone(),
downmixer,
}
.into()
}
// /// Creates a mono bus, by summing samples.
// ///
// /// See [`IntoMonoBus`].
// pub fn into_mono_summing(
// &self,
// ) -> Arc<IntoMonoBus<C, impl Fn(&[f32; C]) -> &f32 + 'static + Copy + Clone + Send + Sync>>
// {
// fn downmixer<'a, const C: usize>(sample: &'a [f32; C]) -> &'a f32 {
// let mut x = 0.0f32;
// for s in sample {
// x += s;
// }
// &x
// }
// self.into_mono(downmixer::<C>)
// }
pub fn into_mono_from_channel<const CI: usize>(
&self,
) -> Arc<IntoMonoBus<C, impl Fn(&[f32; C]) -> &f32 + 'static + Copy + Clone + Send + Sync>>
{
fn downmixer<'a, const C: usize, const CI: usize>(sample: &'a [f32; C]) -> &'a f32 {
&sample[CI]
}
self.into_mono(downmixer::<C, CI>)
}
}
impl<const C: usize> Bus<[f32; C]> for MultiChannelBus<C> {
type I<'a> = slice::Iter<'a, [f32; C]>;
type O<'a> = Self::I<'a>;
fn register_dispatcher<F: for<'a> Fn(Self::I<'a>) + Sync + Send + 'static>(
&self,
dispatcher: F,
) -> Arc<dyn for<'a> Fn(Self::I<'a>) + Sync + Send> {
let dispatcher: Arc<dyn for<'a> Fn(Self::I<'a>) + Sync + Send> = Arc::new(dispatcher);
let downgraded = Arc::downgrade(&dispatcher);
let mut dispatchers = self.dispatchers.write().unwrap();
if let Some(pos) = dispatchers.iter().position(|d| d.upgrade().is_none()) {
dispatchers[pos] = downgraded;
dispatchers.retain(|d| d.upgrade().is_some());
} else {
dispatchers.push(downgraded);
}
dispatcher
}
fn update(&self, cx: &mut ContextProxy) {
let samples = self.channel.1.try_iter().collect::<Vec<_>>();
if samples.is_empty() {
return;
}
self.dispatchers
.read()
.unwrap()
.iter()
.filter_map(|d| d.upgrade())
.for_each(|d| d(samples.iter()));
cx.redraw();
}
fn set_sample_rate(&self, sample_rate: f32) {
self.sample_rate
.store(sample_rate, atomic::Ordering::Relaxed);
}
fn sample_rate(&self) -> f32 {
self.sample_rate.load(Ordering::Relaxed)
}
}