-
Notifications
You must be signed in to change notification settings - Fork 4
/
lib.rs
833 lines (696 loc) · 24.5 KB
/
lib.rs
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
extern crate bela_sys;
use bela_sys::{BelaContext, BelaInitSettings};
use std::convert::TryInto;
use std::{mem, slice};
use std::{thread, time};
pub mod error;
pub enum DigitalDirection {
INPUT,
OUTPUT,
}
#[repr(C)]
pub enum BelaHw {
NoHw = bela_sys::BelaHw_BelaHw_NoHw as isize,
Bela = bela_sys::BelaHw_BelaHw_Bela as isize,
BelaMini = bela_sys::BelaHw_BelaHw_BelaMini as isize,
Salt = bela_sys::BelaHw_BelaHw_Salt as isize,
CtagFace = bela_sys::BelaHw_BelaHw_CtagFace as isize,
CtagBeast = bela_sys::BelaHw_BelaHw_CtagBeast as isize,
CtagFaceBela = bela_sys::BelaHw_BelaHw_CtagFaceBela as isize,
CtagBeastBela = bela_sys::BelaHw_BelaHw_CtagBeastBela as isize,
}
impl BelaHw {
fn from_i32(v: i32) -> Option<BelaHw> {
match v {
bela_sys::BelaHw_BelaHw_NoHw => Some(BelaHw::NoHw),
bela_sys::BelaHw_BelaHw_Bela => Some(BelaHw::Bela),
bela_sys::BelaHw_BelaHw_BelaMini => Some(BelaHw::BelaMini),
bela_sys::BelaHw_BelaHw_Salt => Some(BelaHw::Salt),
bela_sys::BelaHw_BelaHw_CtagFace => Some(BelaHw::CtagFace),
bela_sys::BelaHw_BelaHw_CtagBeast => Some(BelaHw::CtagBeast),
bela_sys::BelaHw_BelaHw_CtagFaceBela => Some(BelaHw::CtagFaceBela),
bela_sys::BelaHw_BelaHw_CtagBeastBela => Some(BelaHw::CtagBeastBela),
_ => None,
}
}
}
/// The `Bela` struct is essentially built to ensure that the type parameter
/// `<T>` is consistent across all invocations of the setup, render, and cleanup
/// functions. This is because `<T>` is the `UserData` of the original Bela
/// library -- we want to ensure that the `UserData` we are initializing with
/// is the exact same as the one we are attempting to access with each function.
///
/// TODO: Bela needs to also wrap the various setup, render, and cleanup
/// functions and keep them in the same struct.
///
/// Called when audio is initialized.
///
/// ```rust
/// pub type SetupFn = FnOnce(&mut Context, T) -> bool;
/// ```
///
/// Called on every frame.
///
/// ```rust
/// pub type RenderFn = Fn(&mut Context, T);
/// ```
///
/// Called when audio is stopped.
///
/// ```rust
/// pub type CleanupFn = FnOnce(&mut Context, T) -> bool;
/// ```
pub struct Bela<T> {
initialized: bool,
user_data: T,
}
extern "C" fn render_trampoline<'a, T>(
context: *mut BelaContext,
user_data: *mut std::os::raw::c_void,
) where
T: UserData<'a> + 'a,
{
let mut context = Context::new(context);
let user_data = unsafe { &mut *(user_data as *mut T) };
user_data.render_fn(&mut context);
}
extern "C" fn setup_trampoline<'a, T>(
context: *mut BelaContext,
user_data: *mut std::os::raw::c_void,
) -> bool
where
T: UserData<'a> + 'a,
{
let mut context = Context::new(context);
let user_data = unsafe { &mut *(user_data as *mut T) };
user_data.setup_fn(&mut context).is_ok()
}
extern "C" fn cleanup_trampoline<'a, T>(
context: *mut BelaContext,
user_data: *mut std::os::raw::c_void,
) where
T: UserData<'a> + 'a,
{
let mut context = Context::new(context);
let user_data = unsafe { &mut *(user_data as *mut T) };
user_data.cleanup_fn(&mut context);
}
pub struct CreatedTask(bela_sys::AuxiliaryTask);
impl<'a, T: UserData<'a> + 'a> Bela<T> {
pub fn new(user_data: T) -> Self {
Bela {
initialized: false,
user_data,
}
}
pub fn run(&mut self, settings: &mut InitSettings) -> Result<(), error::Error> {
self.init_audio(settings)?;
self.start_audio()?;
while !self.should_stop() {
thread::sleep(time::Duration::new(0, 1000));
}
self.stop_audio();
self.cleanup_audio();
Ok(())
}
pub fn set_render<F: 'a>(&mut self, func: &'a mut F)
where
F: FnMut(&mut Context, T::Data),
for<'r, 's> F: FnMut(&'r mut Context, &'s mut T::Data),
{
self.user_data.set_render_fn(func);
}
pub fn set_setup<F: 'a>(&mut self, func: &'a mut F)
where
F: FnMut(&mut Context, T::Data) -> bool,
for<'r, 's> F: FnMut(&'r mut Context, &'s mut T::Data) -> Result<(), error::Error>,
{
self.user_data.set_setup_fn(Some(func));
}
pub fn set_cleanup<F: 'a>(&mut self, func: &'a mut F)
where
F: FnMut(&mut Context, T::Data),
for<'r, 's> F: FnMut(&'r mut Context, &'s mut T::Data),
{
self.user_data.set_cleanup_fn(Some(func));
}
pub fn init_audio(&mut self, settings: &mut InitSettings) -> Result<(), error::Error> {
settings.settings.setup = Some(setup_trampoline::<T>);
settings.settings.render = Some(render_trampoline::<T>);
settings.settings.cleanup = Some(cleanup_trampoline::<T>);
let out = unsafe {
bela_sys::Bela_initAudio(
settings.settings_ptr(),
&mut self.user_data as *mut _ as *mut _,
)
};
match out {
0 => {
self.initialized = true;
Ok(())
}
_ => Err(error::Error::Init),
}
}
pub fn start_audio(&self) -> Result<(), error::Error> {
if !self.initialized {
return Err(error::Error::Start);
}
let out = unsafe { bela_sys::Bela_startAudio() };
match out {
0 => Ok(()),
_ => Err(error::Error::Start),
}
}
pub fn should_stop(&self) -> bool {
unsafe { bela_sys::Bela_stopRequested() != 0 }
}
/// Create an auxiliary task that runs on a lower-priority thread
/// `name` must be globally unique across all Xenomai processes!
pub fn create_auxiliary_task<Auxiliary>(
task: Box<Auxiliary>,
priority: i32,
name: &std::ffi::CStr,
) -> CreatedTask
where
Auxiliary: FnMut() + Send + 'static,
{
// TODO: Bela API does not currently offer an API to stop and unregister a task,
// so we can only leak the task. Otherwise, we could `Box::into_raw` here, store the
// raw pointer in `CreatedTask` and drop it after unregistering & joining the thread
// using `Box::from_raw`.
let task_ptr = Box::leak(task) as *mut _ as *mut _;
extern "C" fn auxiliary_task_trampoline<Auxiliary>(aux_ptr: *mut std::os::raw::c_void)
where
Auxiliary: FnMut() + Send + 'static,
{
let task_ptr = unsafe { &mut *(aux_ptr as *mut Auxiliary) };
task_ptr();
}
let aux_task = unsafe {
bela_sys::Bela_createAuxiliaryTask(
Some(auxiliary_task_trampoline::<Auxiliary>),
priority,
name.as_ptr(),
task_ptr,
)
};
CreatedTask(aux_task)
}
pub fn schedule_auxiliary_task(task: &CreatedTask) -> Result<(), error::Error> {
let res = unsafe { bela_sys::Bela_scheduleAuxiliaryTask(task.0) };
match res {
0 => Ok(()),
_ => Err(error::Error::Task),
}
}
pub fn stop_audio(&self) {
unsafe {
bela_sys::Bela_stopAudio();
}
}
pub fn cleanup_audio(&self) {
unsafe {
bela_sys::Bela_cleanupAudio();
}
}
}
/// Wraps `BelaContext`
pub struct Context {
context: *mut BelaContext,
}
impl Context {
pub fn new(context: *mut BelaContext) -> Context {
Context { context }
}
pub fn context_mut_ptr(&mut self) -> *mut BelaContext {
let ptr: *mut BelaContext = self.context;
ptr
}
pub fn context_ptr(&self) -> *const BelaContext {
let ptr: *mut BelaContext = self.context;
ptr
}
/// Access the audio output slice
///
/// Mutably borrows self so that (hopefully) we do not have multiple mutable
/// pointers to the audio buffer available simultaneously.
pub fn audio_out(&mut self) -> &mut [f32] {
unsafe {
let context = self.context_mut_ptr();
let n_frames = (*context).audioFrames;
let n_channels = (*context).audioOutChannels;
let audio_out_ptr = (*context).audioOut;
slice::from_raw_parts_mut(audio_out_ptr, (n_frames * n_channels) as usize)
}
}
/// Access the audio input slice
///
/// Immutably borrows self and returns an immutable buffer of audio in data.
pub fn audio_in(&self) -> &[f32] {
unsafe {
let context = self.context_ptr();
let n_frames = (*context).audioFrames;
let n_channels = (*context).audioInChannels;
let audio_in_ptr = (*context).audioIn;
slice::from_raw_parts(audio_in_ptr, (n_frames * n_channels) as usize)
}
}
/// Access the digital input/output slice immutably
pub fn digital(&self) -> &[u32] {
unsafe {
let context = self.context_ptr();
let n_frames = (*context).digitalFrames;
let n_channels = (*context).digitalChannels;
let digital_ptr = (*context).digital;
slice::from_raw_parts(digital_ptr, (n_frames * n_channels) as usize)
}
}
/// Access the digital input/output slice mutably
///
/// Mutably borrows self so that (hopefully) we do not have multiple mutable
/// pointers to the digital buffer available simultaneously.
pub fn digital_mut(&mut self) -> &mut [u32] {
unsafe {
let context = self.context_ptr();
let n_frames = (*context).digitalFrames;
let n_channels = (*context).digitalChannels;
let digital_ptr = (*context).digital;
slice::from_raw_parts_mut(digital_ptr, (n_frames * n_channels) as usize)
}
}
/// Access the analog output slice
///
/// Mutably borrows self so that (hopefully) we do not have multiple mutable
/// pointers to the analog buffer available simultaneously.
pub fn analog_out(&mut self) -> &mut [f32] {
unsafe {
let context = self.context_ptr();
let n_frames = (*context).analogFrames;
let n_channels = (*context).analogOutChannels;
let analog_out_ptr = (*context).analogOut;
slice::from_raw_parts_mut(analog_out_ptr, (n_frames * n_channels) as usize)
}
}
/// Access the analog input slice
pub fn analog_in(&self) -> &[f32] {
unsafe {
let n_frames = (*self.context).analogFrames;
let n_channels = (*self.context).analogInChannels;
let analog_in_ptr = (*self.context).analogIn;
slice::from_raw_parts(analog_in_ptr, (n_frames * n_channels) as usize)
}
}
pub fn audio_frames(&self) -> usize {
unsafe { (*self.context).audioFrames as usize }
}
pub fn audio_in_channels(&self) -> usize {
unsafe { (*self.context).audioInChannels as usize }
}
pub fn audio_out_channels(&self) -> usize {
unsafe { (*self.context).audioOutChannels as usize }
}
pub fn audio_sample_rate(&self) -> f32 {
unsafe { (*self.context).audioSampleRate }
}
pub fn analog_frames(&self) -> usize {
unsafe { (*self.context).analogFrames as usize }
}
pub fn analog_in_channels(&self) -> usize {
unsafe { (*self.context).analogInChannels as usize }
}
pub fn analog_out_channels(&self) -> usize {
unsafe { (*self.context).analogOutChannels as usize }
}
pub fn analog_sample_rate(&self) -> f32 {
unsafe { (*self.context).analogSampleRate }
}
pub fn digital_frames(&self) -> usize {
unsafe { (*self.context).digitalFrames as usize }
}
pub fn digital_channels(&self) -> usize {
unsafe { (*self.context).digitalChannels as usize }
}
pub fn digital_sample_rate(&self) -> f32 {
unsafe { (*self.context).digitalSampleRate }
}
pub fn audio_frames_elapsed(&self) -> usize {
unsafe { (*self.context).audioFramesElapsed as usize }
}
pub fn multiplexer_channels(&self) -> usize {
unsafe { (*self.context).multiplexerChannels as usize }
}
pub fn multiplexer_starting_channels(&self) -> usize {
unsafe { (*self.context).multiplexerStartingChannel as usize }
}
pub fn multiplexer_analog_in(&self) -> &[f32] {
unsafe {
let n_frames = (*self.context).analogFrames;
let n_channels = (*self.context).multiplexerChannels;
let analog_in_ptr = (*self.context).multiplexerAnalogIn;
slice::from_raw_parts(analog_in_ptr, (n_frames * n_channels) as usize)
}
}
pub fn multiplexer_enabled(&self) -> u32 {
unsafe { (*self.context).audioExpanderEnabled }
}
pub fn flags(&self) -> u32 {
unsafe { (*self.context).flags }
}
// Returns the value of a given digital input at the given frame number
pub fn digital_read(&self, frame: usize, channel: usize) -> bool {
let digital = self.digital();
(digital[frame] >> (channel + 16)) & 1 != 0
}
// Sets a given digital output channel to a value for the current frame and all subsequent frames
pub fn digital_write(&mut self, frame: usize, channel: usize, value: bool) {
let digital = self.digital_mut();
for out in &mut digital[frame..] {
if value {
*out |= 1 << (channel + 16)
} else {
*out &= !(1 << (channel + 16));
}
}
}
// Sets a given digital output channel to a value for the current frame only
pub fn digital_write_once(&mut self, frame: usize, channel: usize, value: bool) {
let digital = self.digital_mut();
if value {
digital[frame] |= 1 << (channel + 16);
} else {
digital[frame] &= !(1 << (channel + 16));
}
}
// Sets the direction of a digital pin for the current frame and all subsequent frames
pub fn pin_mode(&mut self, frame: usize, channel: usize, mode: DigitalDirection) {
let digital = self.digital_mut();
for out in &mut digital[frame..] {
match mode {
DigitalDirection::INPUT => {
*out |= 1 << channel;
}
DigitalDirection::OUTPUT => {
*out &= !(1 << channel);
}
}
}
}
// Sets the direction of a digital pin for the current frame only
pub fn pin_mode_once(&mut self, frame: usize, channel: usize, mode: DigitalDirection) {
let digital = self.digital_mut();
match mode {
DigitalDirection::INPUT => {
digital[frame] |= 1 << channel;
}
DigitalDirection::OUTPUT => {
digital[frame] &= !(1 << channel);
}
}
}
}
pub trait UserData<'a> {
type Data;
fn render_fn(&mut self, context: &mut Context);
fn set_render_fn(&mut self, render_fn: &'a mut dyn FnMut(&mut Context, &mut Self::Data));
fn setup_fn(&mut self, context: &mut Context) -> Result<(), error::Error>;
fn set_setup_fn(
&mut self,
setup_fn: Option<
&'a mut dyn FnMut(&mut Context, &mut Self::Data) -> Result<(), error::Error>,
>,
);
fn cleanup_fn(&mut self, context: &mut Context);
fn set_cleanup_fn(
&mut self,
cleanup_fn: Option<&'a mut dyn FnMut(&mut Context, &mut Self::Data)>,
);
}
pub struct AppData<'a, D: 'a> {
pub data: D,
render: &'a mut dyn FnMut(&mut Context, &mut D),
setup: Option<&'a mut dyn FnMut(&mut Context, &mut D) -> Result<(), error::Error>>,
cleanup: Option<&'a mut dyn FnMut(&mut Context, &mut D)>,
}
impl<'a, D> AppData<'a, D> {
pub fn new(
data: D,
render: &'a mut dyn FnMut(&mut Context, &mut D),
setup: Option<&'a mut dyn FnMut(&mut Context, &mut D) -> Result<(), error::Error>>,
cleanup: Option<&'a mut dyn FnMut(&mut Context, &mut D)>,
) -> AppData<'a, D> {
AppData {
data,
render,
setup,
cleanup,
}
}
}
impl<'a, D> UserData<'a> for AppData<'a, D> {
type Data = D;
fn render_fn(&mut self, context: &mut Context) {
let AppData { render, data, .. } = self;
render(context, data)
}
fn set_render_fn(&mut self, callback: &'a mut (dyn FnMut(&mut Context, &mut D) + 'a)) {
self.render = callback;
}
fn setup_fn(&mut self, context: &mut Context) -> Result<(), error::Error> {
let AppData { setup, data, .. } = self;
match setup {
Some(f) => f(context, data),
None => Ok(()),
}
}
fn set_setup_fn(
&mut self,
callback: Option<
&'a mut (dyn FnMut(&mut Context, &mut D) -> Result<(), error::Error> + 'a),
>,
) {
self.setup = callback;
}
fn cleanup_fn(&mut self, context: &mut Context) {
let AppData { cleanup, data, .. } = self;
match cleanup {
Some(f) => f(context, data),
None => (),
};
}
fn set_cleanup_fn(&mut self, callback: Option<&'a mut (dyn FnMut(&mut Context, &mut D) + 'a)>) {
self.cleanup = callback;
}
}
/// Safe wrapper for `BelaInitSettings`, which sets initial parameters for the
/// Bela system.
pub struct InitSettings {
settings: BelaInitSettings,
}
impl InitSettings {
pub fn settings_ptr(&mut self) -> *mut BelaInitSettings {
&mut self.settings
}
/// Get number of analog frames per period (buffer). Number of audio frames
/// depends on relative sample rates of the two. By default, audio is twice
/// the sample rate, so has twice the period size.
pub fn period_size(&self) -> usize {
self.settings.periodSize as usize
}
/// Set number of analog frames per period (buffer). Number of audio frames
/// depends on relative sample rates of the two. By default, audio is twice
/// the sample rate, so has twice the period size.
pub fn set_period_size(&mut self, size: usize) {
self.settings.periodSize = size.try_into().unwrap();
}
/// Get whether to use the analog input and output
pub fn use_analog(&self) -> bool {
self.settings.useAnalog != 0
}
/// Set whether to use the analog input and output
pub fn set_use_analog(&mut self, use_analog: bool) {
self.settings.useAnalog = use_analog as _;
}
/// Get whether to use the digital input and output
pub fn use_digital(&self) -> bool {
self.settings.useDigital != 0
}
/// Set whether to use the digital input and output
pub fn set_use_digital(&mut self, use_digital: bool) {
self.settings.useDigital = use_digital as _;
}
pub fn num_analog_in_channels(&self) -> usize {
self.settings.numAnalogInChannels as usize
}
pub fn set_num_analog_in_channels(&mut self, num: usize) {
self.settings.numAnalogInChannels = num.try_into().unwrap();
}
pub fn num_analog_out_channels(&self) -> usize {
self.settings.numAnalogOutChannels as usize
}
pub fn set_num_analog_out_channels(&mut self, num: usize) {
self.settings.numAnalogOutChannels = num.try_into().unwrap();
}
pub fn num_digital_channels(&self) -> usize {
self.settings.numDigitalChannels as usize
}
pub fn set_num_digital_channels(&mut self, num: usize) {
self.settings.numDigitalChannels = num.try_into().unwrap();
}
pub fn begin_muted(&self) -> bool {
self.settings.beginMuted != 0
}
pub fn set_begin_muted(&mut self, val: bool) {
self.settings.beginMuted = val as _;
}
pub fn dac_level(&self) -> f32 {
self.settings.dacLevel
}
pub fn set_dac_level(&mut self, val: f32) {
self.settings.dacLevel = val;
}
pub fn adc_level(&self) -> f32 {
self.settings.adcLevel
}
pub fn set_adc_level(&mut self, val: f32) {
self.settings.adcLevel = val;
}
pub fn pga_gain(&self) -> [f32; 2] {
self.settings.pgaGain
}
pub fn set_pga_gain(&mut self, val: [f32; 2]) {
self.settings.pgaGain = val;
}
pub fn headphone_level(&self) -> f32 {
self.settings.headphoneLevel
}
pub fn set_headphone_level(&mut self, val: f32) {
self.settings.headphoneLevel = val;
}
pub fn num_mux_channels(&self) -> usize {
self.settings.numMuxChannels as usize
}
pub fn set_num_mux_channels(&mut self, val: usize) {
self.settings.numMuxChannels = val.try_into().unwrap();
}
pub fn audio_expander_inputs(&self) -> usize {
self.settings.audioExpanderInputs as usize
}
pub fn set_audio_expander_inputs(&mut self, val: usize) {
self.settings.audioExpanderInputs = val.try_into().unwrap();
}
pub fn audio_expander_outputs(&self) -> usize {
self.settings.audioExpanderOutputs as usize
}
pub fn set_audio_expander_outputs(&mut self, val: usize) {
self.settings.audioExpanderOutputs = val.try_into().unwrap();
}
pub fn pru_number(&self) -> usize {
self.settings.pruNumber as usize
}
pub fn set_pru_number(&mut self, val: usize) {
self.settings.pruNumber = val.try_into().unwrap();
}
pub fn pru_filename(&self) -> [u8; 256] {
self.settings.pruFilename
}
pub fn set_pru_filename(&mut self, val: [u8; 256]) {
self.settings.pruFilename = val;
}
pub fn detect_underruns(&self) -> bool {
self.settings.detectUnderruns != 0
}
pub fn set_detect_underruns(&mut self, val: bool) {
self.settings.detectUnderruns = val as _;
}
pub fn verbose(&self) -> bool {
self.settings.verbose != 0
}
pub fn set_verbose(&mut self, val: bool) {
self.settings.verbose = val as _;
}
pub fn enable_led(&self) -> bool {
self.settings.enableLED != 0
}
pub fn set_enable_led(&mut self, val: bool) {
self.settings.enableLED = val as _;
}
pub fn stop_button_pin(&self) -> Option<i8> {
match self.settings.stopButtonPin {
0..=127 => Some(self.settings.stopButtonPin as _),
_ => None,
}
}
pub fn set_stop_button_pin(&mut self, val: Option<i8>) {
self.settings.stopButtonPin = match val {
Some(v) if v >= 0 => v as _,
_ => -1,
};
}
pub fn high_performance_mode(&self) -> bool {
self.settings.highPerformanceMode != 0
}
pub fn set_high_performance_mode(&mut self, val: bool) {
self.settings.highPerformanceMode = val as _;
}
pub fn interleave(&self) -> bool {
self.settings.interleave != 0
}
pub fn set_interleave(&mut self, val: bool) {
self.settings.interleave = val as _;
}
pub fn analog_outputs_persist(&self) -> bool {
self.settings.analogOutputsPersist != 0
}
pub fn set_analog_outputs_persist(&mut self, val: bool) {
self.settings.analogOutputsPersist = val as _;
}
pub fn uniform_sample_rate(&self) -> bool {
self.settings.uniformSampleRate != 0
}
pub fn set_uniform_sample_rate(&mut self, val: bool) {
self.settings.uniformSampleRate = val as _;
}
pub fn audio_thread_stack_size(&self) -> usize {
self.settings.audioThreadStackSize as usize
}
pub fn set_audio_thread_stack_size(&mut self, num: usize) {
self.settings.audioThreadStackSize = num.try_into().unwrap();
}
pub fn auxiliary_task_stack_size(&self) -> usize {
self.settings.auxiliaryTaskStackSize as usize
}
pub fn set_auxiliary_task_stack_size(&mut self, num: usize) {
self.settings.auxiliaryTaskStackSize = num.try_into().unwrap();
}
pub fn amp_mute_pin(&self) -> Option<i8> {
match self.settings.ampMutePin {
0..=127 => Some(self.settings.ampMutePin as _),
_ => None,
}
}
pub fn set_amp_mute_pin(&mut self, val: Option<i8>) {
self.settings.ampMutePin = match val {
Some(v) if v >= 0 => v as _,
_ => -1,
};
}
/// Get user selected board to work with (as opposed to detected hardware).
pub fn board(&self) -> BelaHw {
BelaHw::from_i32(self.settings.board).expect("unexpected board type")
}
/// Set user selected board to work with (as opposed to detected hardware).
pub fn set_board(&mut self, board: BelaHw) {
self.settings.board = board as _;
}
}
impl Default for InitSettings {
fn default() -> InitSettings {
let settings = unsafe {
let mut settings = mem::MaybeUninit::<BelaInitSettings>::uninit();
bela_sys::Bela_defaultSettings(settings.as_mut_ptr());
settings.assume_init()
};
InitSettings { settings }
}
}