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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
use {
crate::{juce, Result, JUCE},
std::{
ops::{Index, IndexMut},
pin::Pin,
},
};
pub struct InputAudioSampleBuffer<'a> {
buffer: &'a juce::AudioSampleBuffer,
}
impl<'a> InputAudioSampleBuffer<'a> {
pub(crate) fn new(buffer: &'a juce::AudioSampleBuffer) -> Self {
Self { buffer }
}
pub fn channels(&self) -> usize {
self.buffer.get_num_channels() as usize
}
pub fn samples(&self) -> usize {
self.buffer.get_num_samples() as usize
}
}
impl Index<usize> for InputAudioSampleBuffer<'_> {
type Output = [f32];
fn index(&self, channel: usize) -> &Self::Output {
if self.channels() < channel {
panic!("channel out of bounds");
}
let ptr = self.buffer.get_read_pointer(channel as i32);
let len = self.samples();
unsafe { std::slice::from_raw_parts(ptr, len) }
}
}
pub struct OutputAudioSampleBuffer<'a> {
buffer: Pin<&'a mut juce::AudioSampleBuffer>,
}
impl<'a> OutputAudioSampleBuffer<'a> {
pub(crate) fn new(buffer: Pin<&'a mut juce::AudioSampleBuffer>) -> Self {
Self { buffer }
}
pub fn channels(&self) -> usize {
self.buffer.get_num_channels() as usize
}
pub fn samples(&self) -> usize {
self.buffer.get_num_samples() as usize
}
pub fn clear(&mut self) {
self.buffer.as_mut().clear();
}
}
impl Index<usize> for OutputAudioSampleBuffer<'_> {
type Output = [f32];
fn index(&self, channel: usize) -> &Self::Output {
if self.channels() < channel {
panic!("channel out of bounds");
}
let ptr = self.buffer.get_read_pointer(channel as i32);
let len = self.samples();
unsafe { std::slice::from_raw_parts(ptr, len) }
}
}
impl IndexMut<usize> for OutputAudioSampleBuffer<'_> {
fn index_mut(&mut self, channel: usize) -> &mut Self::Output {
if self.channels() < channel {
panic!("channel out of bounds");
}
let ptr = self.buffer.as_mut().get_write_pointer(channel as i32);
let len = self.samples();
unsafe { std::slice::from_raw_parts_mut(ptr, len) }
}
}
pub struct AudioDeviceSetup(cxx::UniquePtr<juce::AudioDeviceSetup>);
unsafe impl Send for AudioDeviceSetup {}
impl Default for AudioDeviceSetup {
fn default() -> Self {
Self(juce::create_audio_device_setup())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChannelCount {
Default,
Custom(i32),
}
unsafe impl Send for ChannelCount {}
impl AudioDeviceSetup {
pub fn output_device_name(&self) -> &str {
self.0.output_device_name()
}
pub fn with_output_device_name(mut self, name: impl AsRef<str>) -> Self {
self.0.pin_mut().set_output_device_name(name.as_ref());
self
}
pub fn input_device_name(&self) -> &str {
self.0.input_device_name()
}
pub fn with_input_device_name(mut self, name: impl AsRef<str>) -> Self {
self.0.pin_mut().set_input_device_name(name.as_ref());
self
}
pub fn sample_rate(&self) -> f64 {
self.0.sample_rate()
}
pub fn with_sample_rate(mut self, sample_rate: f64) -> Self {
self.0.pin_mut().set_sample_rate(sample_rate);
self
}
pub fn buffer_size(&self) -> usize {
self.0.buffer_size() as usize
}
pub fn with_buffer_size(mut self, buffer_size: usize) -> Self {
self.0.pin_mut().set_buffer_size(buffer_size as i32);
self
}
pub fn input_channels(&self) -> ChannelCount {
if self.0.using_default_input_channels() {
ChannelCount::Default
} else {
ChannelCount::Custom(self.0.number_of_input_channels())
}
}
pub fn with_input_channels(mut self, channels: ChannelCount) -> Self {
match channels {
ChannelCount::Default => {
self.0.pin_mut().use_default_input_channels(true);
}
ChannelCount::Custom(count) => {
self.0.pin_mut().use_default_input_channels(false);
self.0.pin_mut().set_number_of_input_channels(count);
}
}
self
}
pub fn output_channels(&self) -> ChannelCount {
if self.0.using_default_output_channels() {
ChannelCount::Default
} else {
ChannelCount::Custom(self.0.number_of_output_channels())
}
}
pub fn with_output_channels(mut self, channels: ChannelCount) -> Self {
match channels {
ChannelCount::Default => {
self.0.pin_mut().use_default_output_channels(true);
}
ChannelCount::Custom(count) => {
self.0.pin_mut().use_default_output_channels(false);
self.0.pin_mut().set_number_of_output_channels(count);
}
}
self
}
}
pub struct AudioDeviceManager {
device_manager: cxx::UniquePtr<juce::AudioDeviceManager>,
_juce: JUCE,
}
unsafe impl Send for AudioDeviceManager {}
impl Default for AudioDeviceManager {
fn default() -> Self {
Self::new()
}
}
impl AudioDeviceManager {
pub fn new() -> Self {
let juce = JUCE::initialise();
Self {
device_manager: juce::create_audio_device_manager(),
_juce: juce,
}
}
pub fn initialise(&mut self, input_channels: usize, output_channels: usize) -> Result<()> {
self.device_manager
.pin_mut()
.initialise_with_default_devices(input_channels as i32, output_channels as i32)
}
pub fn audio_device_setup(&self) -> AudioDeviceSetup {
AudioDeviceSetup(self.device_manager.get_audio_device_setup())
}
pub fn set_audio_device_setup(&mut self, setup: &AudioDeviceSetup) {
self.device_manager
.pin_mut()
.set_audio_device_setup(&setup.0);
}
pub fn play_test_sound(&mut self) {
self.device_manager.pin_mut().play_test_sound();
}
pub fn device_types(&mut self) -> Vec<impl AudioIODeviceType + '_> {
let available_device_types = self.device_manager.pin_mut().get_available_device_types();
(0..available_device_types.size())
.map(|i| available_device_types.get_unchecked(i))
.collect()
}
pub fn current_device_type(&self) -> Option<impl AudioIODeviceType + '_> {
let device_type = self.device_manager.get_current_device_type_object();
if !device_type.is_null() {
Some(device_type)
} else {
None
}
}
pub fn current_device(&self) -> Option<impl AudioIODevice + '_> {
let current_device = self.device_manager.get_current_audio_device();
if !current_device.is_null() {
Some(current_device)
} else {
None
}
}
pub fn add_audio_callback(
&mut self,
callback: impl AudioIODeviceCallback + 'static,
) -> AudioCallbackHandle<'_> {
let callback = Box::new(callback);
AudioCallbackHandle(
self.device_manager
.pin_mut()
.add_audio_callback(Box::new(callback)),
)
}
pub fn add_audio_device_type(&mut self, device_type: impl AudioIODeviceType + 'static) {
let device_type = Box::new(device_type);
self.device_manager
.pin_mut()
.add_audio_device_type(Box::new(device_type));
}
pub fn set_current_audio_device_type(&mut self, device_type: &str) {
self.device_manager
.pin_mut()
.set_current_audio_device_type(device_type);
}
}
pub trait AudioIODeviceCallback: Send {
fn about_to_start(&mut self, device: &mut dyn AudioIODevice);
fn process_block(
&mut self,
input: &InputAudioSampleBuffer<'_>,
output: &mut OutputAudioSampleBuffer<'_>,
);
fn stopped(&mut self);
}
pub(crate) type BoxedAudioIODeviceCallback = Box<dyn AudioIODeviceCallback>;
pub(crate) type BoxedAudioIODeviceType = Box<dyn AudioIODeviceType>;
pub(crate) type BoxedAudioIODevice = Box<dyn AudioIODevice>;
#[must_use]
pub struct AudioCallbackHandle<'a>(cxx::UniquePtr<juce::AudioCallbackHandle<'a>>);
pub trait AudioIODeviceType {
fn name(&self) -> String;
fn scan_for_devices(&mut self);
fn input_devices(&self) -> Vec<String>;
fn output_devices(&self) -> Vec<String>;
fn create_device(
&mut self,
input_device_name: &str,
output_device_name: &str,
) -> Option<Box<dyn AudioIODevice>>;
}
impl AudioIODeviceType for *mut juce::AudioIODeviceType {
fn name(&self) -> String {
if self.is_null() {
return String::default();
}
let this = unsafe { &*self.cast_const() };
juce::get_type_name(this)
}
fn scan_for_devices(&mut self) {
if let Some(this) = unsafe { self.as_mut().map(|ptr| Pin::new_unchecked(ptr)) } {
this.scan_for_devices();
}
}
fn input_devices(&self) -> Vec<String> {
if self.is_null() {
return vec![];
}
let this = unsafe { &*self.cast_const() };
juce::get_input_device_names(this)
}
fn output_devices(&self) -> Vec<String> {
if self.is_null() {
return vec![];
}
let this = unsafe { &*self.cast_const() };
juce::get_output_device_names(this)
}
fn create_device(
&mut self,
input_device_name: &str,
output_device_name: &str,
) -> Option<Box<dyn AudioIODevice>> {
unsafe { self.as_mut().map(|ptr| Pin::new_unchecked(ptr)) }
.map(|this| juce::new_device(this, input_device_name, output_device_name))
.filter(|device| !device.is_null())
.map(|device| Box::new(device) as _)
}
}
pub trait AudioIODevice {
fn name(&self) -> &str;
fn type_name(&self) -> &str;
fn sample_rate(&mut self) -> f64;
fn buffer_size(&mut self) -> usize;
fn available_sample_rates(&mut self) -> Vec<f64>;
fn available_buffer_sizes(&mut self) -> Vec<usize>;
fn open(&mut self, sample_rate: f64, buffer_size: usize) -> Result<()>;
fn close(&mut self);
fn input_channels(&self) -> i32;
fn output_channels(&self) -> i32;
}
impl AudioIODevice for *mut juce::AudioIODevice {
fn name(&self) -> &str {
unsafe { self.as_ref() }
.map(juce::get_device_name)
.unwrap_or_default()
}
fn type_name(&self) -> &str {
unsafe { self.as_ref() }
.map(juce::get_device_type_name)
.unwrap_or_default()
}
fn sample_rate(&mut self) -> f64 {
unsafe { self.as_mut().map(|this| Pin::new_unchecked(this)) }
.map(|this| this.get_current_sample_rate())
.unwrap_or_default()
}
fn buffer_size(&mut self) -> usize {
unsafe { self.as_mut().map(|this| Pin::new_unchecked(this)) }
.map(|this| this.get_current_buffer_size_samples() as usize)
.unwrap_or_default()
}
fn available_sample_rates(&mut self) -> Vec<f64> {
unsafe { self.as_mut().map(|this| Pin::new_unchecked(this)) }
.map(juce::get_available_sample_rates)
.unwrap_or_default()
}
fn available_buffer_sizes(&mut self) -> Vec<usize> {
unsafe { self.as_mut().map(|this| Pin::new_unchecked(this)) }
.map(juce::get_available_buffer_sizes)
.unwrap_or_default()
}
fn open(&mut self, sample_rate: f64, buffer_size: usize) -> Result<()> {
if let Some(this) = unsafe { self.as_mut().map(|this| Pin::new_unchecked(this)) } {
juce::open(this, sample_rate, buffer_size)?;
}
Ok(())
}
fn close(&mut self) {
if let Some(this) = unsafe { self.as_mut().map(|this| Pin::new_unchecked(this)) } {
this.close();
}
}
fn input_channels(&self) -> i32 {
unsafe { self.as_ref() }
.map(juce::count_active_input_channels)
.unwrap_or_default()
}
fn output_channels(&self) -> i32 {
unsafe { self.as_ref() }
.map(juce::count_active_output_channels)
.unwrap_or_default()
}
}
impl AudioIODevice for Pin<&mut juce::AudioIODevice> {
fn name(&self) -> &str {
juce::get_device_name(self)
}
fn type_name(&self) -> &str {
juce::get_device_type_name(self)
}
fn sample_rate(&mut self) -> f64 {
juce::AudioIODevice::get_current_sample_rate(self.as_mut())
}
fn buffer_size(&mut self) -> usize {
juce::AudioIODevice::get_current_buffer_size_samples(self.as_mut()) as usize
}
fn available_sample_rates(&mut self) -> Vec<f64> {
juce::get_available_sample_rates(self.as_mut())
}
fn available_buffer_sizes(&mut self) -> Vec<usize> {
juce::get_available_buffer_sizes(self.as_mut())
}
fn open(&mut self, sample_rate: f64, buffer_size: usize) -> Result<()> {
juce::open(self.as_mut(), sample_rate, buffer_size)
}
fn close(&mut self) {
juce::AudioIODevice::close(self.as_mut());
}
fn input_channels(&self) -> i32 {
juce::count_active_input_channels(self)
}
fn output_channels(&self) -> i32 {
juce::count_active_output_channels(self)
}
}
impl AudioIODevice for cxx::UniquePtr<juce::AudioIODevice> {
fn name(&self) -> &str {
self.as_ref().map(juce::get_device_name).unwrap_or_default()
}
fn type_name(&self) -> &str {
self.as_ref()
.map(juce::get_device_type_name)
.unwrap_or_default()
}
fn sample_rate(&mut self) -> f64 {
self.as_mut()
.map(|this| this.get_current_sample_rate())
.unwrap_or_default()
}
fn buffer_size(&mut self) -> usize {
self.as_mut()
.map(|this| this.get_current_buffer_size_samples() as usize)
.unwrap_or_default()
}
fn available_sample_rates(&mut self) -> Vec<f64> {
self.as_mut()
.map(juce::get_available_sample_rates)
.unwrap_or_default()
}
fn available_buffer_sizes(&mut self) -> Vec<usize> {
self.as_mut()
.map(juce::get_available_buffer_sizes)
.unwrap_or_default()
}
fn open(&mut self, sample_rate: f64, buffer_size: usize) -> Result<()> {
if let Some(this) = self.as_mut() {
juce::open(this, sample_rate, buffer_size)?;
}
Ok(())
}
fn close(&mut self) {
if let Some(this) = self.as_mut() {
this.close();
}
}
fn input_channels(&self) -> i32 {
self.as_ref()
.map(juce::count_active_input_channels)
.unwrap_or_default()
}
fn output_channels(&self) -> i32 {
self.as_ref()
.map(juce::count_active_output_channels)
.unwrap_or_default()
}
}
pub(crate) mod ffi {
use super::*;
pub mod audio_io_device_callback {
use super::*;
pub fn about_to_start(
mut self_: Pin<&mut BoxedAudioIODeviceCallback>,
mut device: Pin<&mut juce::AudioIODevice>,
) {
self_.about_to_start(&mut device.as_mut());
}
pub fn process_block(
mut self_: Pin<&mut BoxedAudioIODeviceCallback>,
input: &juce::AudioSampleBuffer,
output: Pin<&mut juce::AudioSampleBuffer>,
) {
let input = InputAudioSampleBuffer::new(input);
let mut output = OutputAudioSampleBuffer::new(output);
self_.process_block(&input, &mut output);
}
pub fn stopped(mut self_: Pin<&mut BoxedAudioIODeviceCallback>) {
self_.stopped()
}
}
pub mod audio_io_device_type {
use {super::*, std::ptr::null_mut};
pub fn name(self_: &BoxedAudioIODeviceType) -> String {
self_.name()
}
pub fn scan_for_devices(mut self_: Pin<&mut BoxedAudioIODeviceType>) {
self_.scan_for_devices()
}
pub fn get_device_names(self_: &BoxedAudioIODeviceType, input: bool) -> Vec<String> {
if input {
self_.input_devices()
} else {
self_.output_devices()
}
}
pub fn create_device(
mut self_: Pin<&mut BoxedAudioIODeviceType>,
input_name: &str,
output_name: &str,
) -> *mut BoxedAudioIODevice {
let device = self_.as_mut().create_device(input_name, output_name);
device
.map(|device| Box::into_raw(Box::new(device)))
.unwrap_or(null_mut())
}
pub fn destroy_device(device: *mut BoxedAudioIODevice) {
if device.is_null() {
return;
}
unsafe { Box::from_raw(device) };
}
}
pub mod audio_io_device {
use super::*;
pub fn device_name(self_: &BoxedAudioIODevice) -> String {
self_.name().to_string()
}
pub fn device_type_name(self_: &BoxedAudioIODevice) -> String {
self_.type_name().to_string()
}
pub fn device_sample_rate(mut self_: Pin<&mut BoxedAudioIODevice>) -> f64 {
self_.sample_rate()
}
pub fn device_buffer_size(mut self_: Pin<&mut BoxedAudioIODevice>) -> usize {
self_.buffer_size()
}
pub fn device_available_sample_rates(mut self_: Pin<&mut BoxedAudioIODevice>) -> Vec<f64> {
self_.available_sample_rates()
}
pub fn device_available_buffer_sizes(
mut self_: Pin<&mut BoxedAudioIODevice>,
) -> Vec<usize> {
self_.available_buffer_sizes()
}
pub fn device_open(
mut self_: Pin<&mut BoxedAudioIODevice>,
sample_rate: f64,
buffer_size: usize,
) -> String {
match self_.open(sample_rate, buffer_size) {
Ok(()) => String::default(),
Err(error) => error.to_string(),
}
}
pub fn device_close(mut self_: Pin<&mut BoxedAudioIODevice>) {
self_.close()
}
}
}
pub struct SystemAudioVolume;
impl SystemAudioVolume {
pub fn get_gain() -> f32 {
juce::get_gain()
}
pub fn set_gain(gain: f32) {
juce::set_gain(gain.max(0.0).min(1.0))
}
pub fn is_muted() -> bool {
juce::is_muted()
}
pub fn mute() {
juce::set_muted(true);
}
pub fn unmute() {
juce::set_muted(false);
}
}