1 /************************************************************************
2 ************************************************************************
3 FAUST Architecture File
4 Copyright (C) 2021 GRAME, Centre National de Creation Musicale
5 ---------------------------------------------------------------------
6
7 This is sample code. This file is provided as an example of minimal
8 FAUST architecture file. Redistribution and use in source and binary
9 forms, with or without modification, in part or in full are permitted.
10 In particular you can create a derived work of this FAUST architecture
11 and distribute that work under terms of your choice.
12
13 This sample code is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 ************************************************************************
17 ************************************************************************/
18
19 #![allow(unused_parens)]
20 #![allow(non_snake_case)]
21 #![allow(non_camel_case_types)]
22 #![allow(dead_code)]
23 #![allow(unused_variables)]
24 #![allow(unused_mut)]
25 #![allow(non_upper_case_globals)]
26
27 //! Faust CPAL architecture file
28 extern crate anyhow;
29 extern crate cpal;
30 use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
31 use std::io;
32 extern crate libm;
33
34 type F32 = f32;
35 type F64 = f64;
36
37 #[derive(Copy, Clone)]
38 pub struct ParamIndex(pub i32);
39
40 pub struct Soundfile<'a,T> {
41 fBuffers: &'a&'a T,
42 fLength: &'a i32,
43 fSR: &'a i32,
44 fOffset: &'a i32,
45 fChannels: i32
46 }
47
48 pub trait FaustDsp {
49 type T;
50
new() -> Self where Self: Sized51 fn new() -> Self where Self: Sized;
metadata(&self, m: &mut dyn Meta)52 fn metadata(&self, m: &mut dyn Meta);
get_sample_rate(&self) -> i3253 fn get_sample_rate(&self) -> i32;
get_num_inputs(&self) -> i3254 fn get_num_inputs(&self) -> i32;
get_num_outputs(&self) -> i3255 fn get_num_outputs(&self) -> i32;
class_init(sample_rate: i32) where Self: Sized56 fn class_init(sample_rate: i32) where Self: Sized;
instance_reset_params(&mut self)57 fn instance_reset_params(&mut self);
instance_clear(&mut self)58 fn instance_clear(&mut self);
instance_constants(&mut self, sample_rate: i32)59 fn instance_constants(&mut self, sample_rate: i32);
instance_init(&mut self, sample_rate: i32)60 fn instance_init(&mut self, sample_rate: i32);
init(&mut self, sample_rate: i32)61 fn init(&mut self, sample_rate: i32);
build_user_interface(&self, ui_interface: &mut dyn UI<Self::T>)62 fn build_user_interface(&self, ui_interface: &mut dyn UI<Self::T>);
build_user_interface_static(ui_interface: &mut dyn UI<Self::T>) where Self: Sized63 fn build_user_interface_static(ui_interface: &mut dyn UI<Self::T>) where Self: Sized;
get_param(&self, param: ParamIndex) -> Option<Self::T>64 fn get_param(&self, param: ParamIndex) -> Option<Self::T>;
set_param(&mut self, param: ParamIndex, value: Self::T)65 fn set_param(&mut self, param: ParamIndex, value: Self::T);
compute(&mut self, count: i32, inputs: &[&[Self::T]], outputs: &mut[&mut[Self::T]])66 fn compute(&mut self, count: i32, inputs: &[&[Self::T]], outputs: &mut[&mut[Self::T]]);
67 }
68
69 pub trait Meta {
70 // -- metadata declarations
declare(&mut self, key: &str, value: &str)71 fn declare(&mut self, key: &str, value: &str);
72 }
73
74 pub trait UI<T> {
75 // -- widget's layouts
open_tab_box(&mut self, label: &str)76 fn open_tab_box(&mut self, label: &str);
open_horizontal_box(&mut self, label: &str)77 fn open_horizontal_box(&mut self, label: &str);
open_vertical_box(&mut self, label: &str)78 fn open_vertical_box(&mut self, label: &str);
close_box(&mut self)79 fn close_box(&mut self);
80
81 // -- active widgets
add_button(&mut self, label: &str, param: ParamIndex)82 fn add_button(&mut self, label: &str, param: ParamIndex);
add_check_button(&mut self, label: &str, param: ParamIndex)83 fn add_check_button(&mut self, label: &str, param: ParamIndex);
add_vertical_slider(&mut self, label: &str, param: ParamIndex, init: T, min: T, max: T, step: T)84 fn add_vertical_slider(&mut self, label: &str, param: ParamIndex, init: T, min: T, max: T, step: T);
add_horizontal_slider(&mut self, label: &str, param: ParamIndex, init: T, min: T, max: T, step: T)85 fn add_horizontal_slider(&mut self, label: &str, param: ParamIndex, init: T, min: T, max: T, step: T);
add_num_entry(&mut self, label: &str, param: ParamIndex, init: T, min: T, max: T, step: T)86 fn add_num_entry(&mut self, label: &str, param: ParamIndex, init: T, min: T, max: T, step: T);
87
88 // -- passive widgets
add_horizontal_bargraph(&mut self, label: &str, param: ParamIndex, min: T, max: T)89 fn add_horizontal_bargraph(&mut self, label: &str, param: ParamIndex, min: T, max: T);
add_vertical_bargraph(&mut self, label: &str, param: ParamIndex, min: T, max: T)90 fn add_vertical_bargraph(&mut self, label: &str, param: ParamIndex, min: T, max: T);
91
92 // -- metadata declarations
declare(&mut self, param: Option<ParamIndex>, key: &str, value: &str)93 fn declare(&mut self, param: Option<ParamIndex>, key: &str, value: &str);
94 }
95
96 <<includeIntrinsic>>
97 <<includeclass>>
98
main() -> Result<(), anyhow::Error>99 fn main() -> Result<(), anyhow::Error> {
100
101 // Allocation DSP on the heap
102 let mut dsp = Box::new(mydsp::new());
103
104 println!("get_num_inputs: {}", dsp.get_num_inputs());
105 println!("get_num_outputs: {}", dsp.get_num_outputs());
106
107 //println!("Faust Rust code running with CPAL: sample-rate = {} buffer-size = {}", client.sample_rate(), client.buffer_size());
108
109 println!("Supported hosts:\n {:?}", cpal::ALL_HOSTS);
110 let available_hosts = cpal::available_hosts();
111 println!("Available hosts:\n {:?}", available_hosts);
112
113 for host_id in available_hosts {
114 println!("{}", host_id.name());
115 let host = cpal::host_from_id(host_id)?;
116
117 let default_in = host.default_input_device().map(|e| e.name().unwrap());
118 let default_out = host.default_output_device().map(|e| e.name().unwrap());
119 println!(" Default Input Device:\n {:?}", default_in);
120 println!(" Default Output Device:\n {:?}", default_out);
121
122 /*
123 let devices = host.devices()?;
124 println!(" Devices: ");
125 for (device_index, device) in devices.enumerate() {
126 println!(" {}. \"{}\"", device_index + 1, device.name()?);
127
128 // Input configs
129 if let Ok(conf) = device.default_input_config() {
130 println!(" Default input stream config:\n {:?}", conf);
131 }
132 let input_configs = match device.supported_input_configs() {
133 Ok(f) => f.collect(),
134 Err(e) => {
135 println!(" Error getting supported input configs: {:?}", e);
136 Vec::new()
137 }
138 };
139 if !input_configs.is_empty() {
140 println!(" All supported input stream configs:");
141 for (config_index, config) in input_configs.into_iter().enumerate() {
142 println!(
143 " {}.{}. {:?}",
144 device_index + 1,
145 config_index + 1,
146 config
147 );
148 }
149 }
150
151 // Output configs
152 if let Ok(conf) = device.default_output_config() {
153 println!(" Default output stream config:\n {:?}", conf);
154 }
155 let output_configs = match device.supported_output_configs() {
156 Ok(f) => f.collect(),
157 Err(e) => {
158 println!(" Error getting supported output configs: {:?}", e);
159 Vec::new()
160 }
161 };
162 if !output_configs.is_empty() {
163 println!(" All supported output stream configs:");
164 for (config_index, config) in output_configs.into_iter().enumerate() {
165 println!(
166 " {}.{}. {:?}",
167 device_index + 1,
168 config_index + 1,
169 config
170 );
171 }
172 }
173 }
174 */
175 }
176
177 Ok(())
178 }
179