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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
// Copyright (C) 2017,2018 Sebastian Dröge <sebastian@centricular.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use glib;
use glib::subclass;
use glib::subclass::prelude::*;
use gst;
use gst::prelude::*;
use gst::subclass::prelude::*;
use gst_audio;
use gst_base;
use gst_base::subclass::prelude::*;
use std::sync::Mutex;
use std::{cmp, i32, iter, u64};
use byte_slice_cast::*;
use num_traits::cast::{FromPrimitive, ToPrimitive};
use num_traits::float::Float;
const DEFAULT_MAX_DELAY: u64 = gst::SECOND_VAL;
const DEFAULT_DELAY: u64 = 500 * gst::MSECOND_VAL;
const DEFAULT_INTENSITY: f64 = 0.5;
const DEFAULT_FEEDBACK: f64 = 0.0;
#[derive(Debug, Clone, Copy)]
struct Settings {
pub max_delay: u64,
pub delay: u64,
pub intensity: f64,
pub feedback: f64,
}
impl Default for Settings {
fn default() -> Self {
Settings {
max_delay: DEFAULT_MAX_DELAY,
delay: DEFAULT_DELAY,
intensity: DEFAULT_INTENSITY,
feedback: DEFAULT_FEEDBACK,
}
}
}
struct State {
info: gst_audio::AudioInfo,
buffer: RingBuffer,
}
struct AudioEcho {
cat: gst::DebugCategory,
settings: Mutex<Settings>,
state: Mutex<Option<State>>,
}
static PROPERTIES: [subclass::Property; 4] = [
subclass::Property("max-delay", |name| {
glib::ParamSpec::uint64(name,
"Maximum Delay",
"Maximum delay of the echo in nanoseconds (can't be changed in PLAYING or PAUSED state)",
0, u64::MAX,
DEFAULT_MAX_DELAY,
glib::ParamFlags::READWRITE,
)
}),
subclass::Property("delay", |name| {
glib::ParamSpec::uint64(
name,
"Delay",
"Delay of the echo in nanoseconds",
0,
u64::MAX,
DEFAULT_DELAY,
glib::ParamFlags::READWRITE,
)
}),
subclass::Property("intensity", |name| {
glib::ParamSpec::double(
name,
"Intensity",
"Intensity of the echo",
0.0,
1.0,
DEFAULT_INTENSITY,
glib::ParamFlags::READWRITE,
)
}),
subclass::Property("feedback", |name| {
glib::ParamSpec::double(
name,
"Feedback",
"Amount of feedback",
0.0,
1.0,
DEFAULT_FEEDBACK,
glib::ParamFlags::READWRITE,
)
}),
];
impl AudioEcho {
fn process<F: Float + ToPrimitive + FromPrimitive>(
data: &mut [F],
state: &mut State,
settings: &Settings,
) {
let delay_frames = (settings.delay as usize)
* (state.info.channels() as usize)
* (state.info.rate() as usize)
/ (gst::SECOND_VAL as usize);
for (i, (o, e)) in data.iter_mut().zip(state.buffer.iter(delay_frames)) {
let inp = (*i).to_f64().unwrap();
let out = inp + settings.intensity * e;
*o = inp + settings.feedback * e;
*i = FromPrimitive::from_f64(out).unwrap();
}
}
}
impl ObjectSubclass for AudioEcho {
const NAME: &'static str = "RsAudioEcho";
type ParentType = gst_base::BaseTransform;
type Instance = gst::subclass::ElementInstanceStruct<Self>;
type Class = subclass::simple::ClassStruct<Self>;
glib_object_subclass!();
fn new() -> Self {
Self {
cat: gst::DebugCategory::new(
"rsaudioecho",
gst::DebugColorFlags::empty(),
"Rust audioecho effect",
),
settings: Mutex::new(Default::default()),
state: Mutex::new(None),
}
}
fn class_init(klass: &mut subclass::simple::ClassStruct<Self>) {
klass.set_metadata(
"Audio echo",
"Filter/Effect/Audio",
"Adds an echo or reverb effect to an audio stream",
"Sebastian Dröge <sebastian@centricular.com>",
);
let caps = gst::Caps::new_simple(
"audio/x-raw",
&[
(
"format",
&gst::List::new(&[
&gst_audio::AUDIO_FORMAT_F32.to_string(),
&gst_audio::AUDIO_FORMAT_F64.to_string(),
]),
),
("rate", &gst::IntRange::<i32>::new(0, i32::MAX)),
("channels", &gst::IntRange::<i32>::new(0, i32::MAX)),
("layout", &"interleaved"),
],
);
let src_pad_template = gst::PadTemplate::new(
"src",
gst::PadDirection::Src,
gst::PadPresence::Always,
&caps,
)
.unwrap();
klass.add_pad_template(src_pad_template);
let sink_pad_template = gst::PadTemplate::new(
"sink",
gst::PadDirection::Sink,
gst::PadPresence::Always,
&caps,
)
.unwrap();
klass.add_pad_template(sink_pad_template);
klass.install_properties(&PROPERTIES);
klass.configure(
gst_base::subclass::BaseTransformMode::AlwaysInPlace,
false,
false,
);
}
}
impl ObjectImpl for AudioEcho {
glib_object_impl!();
fn set_property(&self, _obj: &glib::Object, id: usize, value: &glib::Value) {
let prop = &PROPERTIES[id];
match *prop {
subclass::Property("max-delay", ..) => {
let mut settings = self.settings.lock().unwrap();
if self.state.lock().unwrap().is_none() {
settings.max_delay = value.get().unwrap();
}
}
subclass::Property("delay", ..) => {
let mut settings = self.settings.lock().unwrap();
settings.delay = value.get().unwrap();
}
subclass::Property("intensity", ..) => {
let mut settings = self.settings.lock().unwrap();
settings.intensity = value.get().unwrap();
}
subclass::Property("feedback", ..) => {
let mut settings = self.settings.lock().unwrap();
settings.feedback = value.get().unwrap();
}
_ => unimplemented!(),
}
}
fn get_property(&self, _obj: &glib::Object, id: usize) -> Result<glib::Value, ()> {
let prop = &PROPERTIES[id];
match *prop {
subclass::Property("max-delay", ..) => {
let settings = self.settings.lock().unwrap();
Ok(settings.max_delay.to_value())
}
subclass::Property("delay", ..) => {
let settings = self.settings.lock().unwrap();
Ok(settings.delay.to_value())
}
subclass::Property("intensity", ..) => {
let settings = self.settings.lock().unwrap();
Ok(settings.intensity.to_value())
}
subclass::Property("feedback", ..) => {
let settings = self.settings.lock().unwrap();
Ok(settings.feedback.to_value())
}
_ => unimplemented!(),
}
}
}
impl ElementImpl for AudioEcho {}
impl BaseTransformImpl for AudioEcho {
fn transform_ip(
&self,
_element: &gst_base::BaseTransform,
buf: &mut gst::BufferRef,
) -> Result<gst::FlowSuccess, gst::FlowError> {
let mut settings = *self.settings.lock().unwrap();
settings.delay = cmp::min(settings.max_delay, settings.delay);
let mut state_guard = self.state.lock().unwrap();
let state = state_guard.as_mut().ok_or(gst::FlowError::NotNegotiated)?;
let mut map = buf.map_writable().ok_or(gst::FlowError::Error)?;
match state.info.format() {
gst_audio::AUDIO_FORMAT_F64 => {
let data = map.as_mut_slice_of::<f64>().unwrap();
Self::process(data, state, &settings);
}
gst_audio::AUDIO_FORMAT_F32 => {
let data = map.as_mut_slice_of::<f32>().unwrap();
Self::process(data, state, &settings);
}
_ => return Err(gst::FlowError::NotNegotiated),
}
Ok(gst::FlowSuccess::Ok)
}
fn set_caps(
&self,
_element: &gst_base::BaseTransform,
incaps: &gst::Caps,
outcaps: &gst::Caps,
) -> bool {
if incaps != outcaps {
return false;
}
let info = match gst_audio::AudioInfo::from_caps(incaps) {
None => return false,
Some(info) => info,
};
let max_delay = self.settings.lock().unwrap().max_delay;
let size = max_delay * (info.rate() as u64) / gst::SECOND_VAL;
let buffer_size = size * (info.channels() as u64);
*self.state.lock().unwrap() = Some(State {
info,
buffer: RingBuffer::new(buffer_size as usize),
});
true
}
fn stop(&self, _element: &gst_base::BaseTransform) -> Result<(), gst::ErrorMessage> {
// Drop state
let _ = self.state.lock().unwrap().take();
Ok(())
}
}
pub fn register(plugin: &gst::Plugin) -> Result<(), glib::BoolError> {
gst::Element::register(plugin, "rsaudioecho", 0, AudioEcho::get_type())
}
struct RingBuffer {
buffer: Box<[f64]>,
pos: usize,
}
impl RingBuffer {
fn new(size: usize) -> Self {
let mut buffer = Vec::with_capacity(size as usize);
buffer.extend(iter::repeat(0.0).take(size as usize));
Self {
buffer: buffer.into_boxed_slice(),
pos: 0,
}
}
fn iter(&mut self, delay: usize) -> RingBufferIter {
RingBufferIter::new(self, delay)
}
}
struct RingBufferIter<'a> {
buffer: &'a mut [f64],
buffer_pos: &'a mut usize,
read_pos: usize,
write_pos: usize,
}
impl<'a> RingBufferIter<'a> {
fn new(buffer: &'a mut RingBuffer, delay: usize) -> RingBufferIter<'a> {
let size = buffer.buffer.len();
assert!(size >= delay);
assert_ne!(size, 0);
let read_pos = (size - delay + buffer.pos) % size;
let write_pos = buffer.pos % size;
let buffer_pos = &mut buffer.pos;
let buffer = &mut buffer.buffer;
RingBufferIter {
buffer,
buffer_pos,
read_pos,
write_pos,
}
}
}
impl<'a> Iterator for RingBufferIter<'a> {
type Item = (&'a mut f64, f64);
fn next(&mut self) -> Option<Self::Item> {
let res = unsafe {
let r = *self.buffer.get_unchecked(self.read_pos);
let w = self.buffer.get_unchecked_mut(self.write_pos);
// Cast needed to get from &mut f64 to &'a mut f64
(&mut *(w as *mut f64), r)
};
let size = self.buffer.len();
self.write_pos = (self.write_pos + 1) % size;
self.read_pos = (self.read_pos + 1) % size;
Some(res)
}
}
impl<'a> Drop for RingBufferIter<'a> {
fn drop(&mut self) {
*self.buffer_pos = self.write_pos;
}
}
|