-
Notifications
You must be signed in to change notification settings - Fork 515
Expand file tree
/
Copy pathcustom.rs
More file actions
375 lines (321 loc) · 11.5 KB
/
Copy pathcustom.rs
File metadata and controls
375 lines (321 loc) · 11.5 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
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
use std::{
fmt,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
time::{Duration, Instant},
};
use cpal::{
traits::{DeviceTrait, HostTrait, StreamTrait},
ChannelCount, Data, Device, DeviceDescription, DeviceDescriptionBuilder, DeviceId, Error,
ErrorKind, FrameCount, FromSample, InputCallbackInfo, OutputCallbackInfo,
OutputStreamTimestamp, Sample, SampleFormat, Stream, StreamConfig, StreamInstant,
SupportedBufferSize, SupportedStreamConfig, SupportedStreamConfigRange,
};
#[allow(dead_code)]
#[derive(Clone)] // Clone, Send+Sync are required
struct MyHost;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
struct MyDevice;
// Needs to be Send+Sync
struct MyStream {
controls: Arc<StreamControls>,
// The instant the audio thread was started; shared with now() so that
// callback timestamps and now() are on the same time base.
start: Instant,
// Option is needed since joining a thread takes ownership,
// and we want to do that on drop (gives us &mut self, not self)
handle: Option<std::thread::JoinHandle<()>>,
}
struct StreamControls {
exit: AtomicBool,
pause: AtomicBool,
}
const CHANNEL_COUNT: ChannelCount = 2;
const BUFFER_SIZE: FrameCount = 4096;
impl HostTrait for MyHost {
type Device = MyDevice;
type Devices = std::iter::Once<MyDevice>;
fn is_available() -> bool {
true
}
fn devices(&self) -> Result<Self::Devices, Error> {
Ok(std::iter::once(MyDevice))
}
fn default_input_device(&self) -> Option<Self::Device> {
None
}
fn default_output_device(&self) -> Option<Self::Device> {
Some(MyDevice)
}
}
impl DeviceTrait for MyDevice {
type SupportedInputConfigs = std::iter::Empty<SupportedStreamConfigRange>;
type SupportedOutputConfigs = std::iter::Once<SupportedStreamConfigRange>;
type Stream = MyStream;
fn description(&self) -> Result<DeviceDescription, Error> {
Ok(DeviceDescriptionBuilder::new("Custom Device").build())
}
fn id(&self) -> Result<DeviceId, Error> {
Err(Error::new(ErrorKind::UnsupportedOperation))
}
fn supported_input_configs(&self) -> Result<Self::SupportedInputConfigs, Error> {
Ok(std::iter::empty())
}
fn supported_output_configs(&self) -> Result<Self::SupportedOutputConfigs, Error> {
Ok(std::iter::once(SupportedStreamConfigRange::new(
CHANNEL_COUNT,
44100,
44100,
SupportedBufferSize::Range {
min: BUFFER_SIZE,
max: BUFFER_SIZE,
},
SampleFormat::F32,
)))
}
fn default_input_config(&self) -> Result<SupportedStreamConfig, Error> {
Err(Error::new(ErrorKind::UnsupportedConfig))
}
fn default_output_config(&self) -> Result<SupportedStreamConfig, Error> {
Ok(SupportedStreamConfig::new(
CHANNEL_COUNT,
44100,
SupportedBufferSize::Range {
min: BUFFER_SIZE,
max: BUFFER_SIZE,
},
SampleFormat::I16,
))
}
fn build_input_stream_raw<D, E>(
&self,
_: StreamConfig,
_: SampleFormat,
_: D,
_: E,
_: Option<Duration>,
) -> Result<Self::Stream, Error>
where
D: FnMut(&Data, &InputCallbackInfo) + Send + 'static,
E: FnMut(Error) + Send + 'static,
{
Err(Error::new(ErrorKind::UnsupportedConfig))
}
// this is the meat of a custom device impl.
// you're expected to repeatedly call `data_callback` and provide it with a buffer of samples,
// as well as a stream timestamp.
// a proper impl would also check the stream config and sample format, as well as handle errors
fn build_output_stream_raw<D, E>(
&self,
_: StreamConfig,
_: SampleFormat,
mut data_callback: D,
_: E,
_: Option<Duration>,
) -> Result<Self::Stream, Error>
where
D: FnMut(&mut Data, &OutputCallbackInfo) + Send + 'static,
E: FnMut(Error) + Send + 'static,
{
let controls = Arc::new(StreamControls {
exit: AtomicBool::new(false),
pause: AtomicBool::new(true), // streams are expected to start out paused by default
});
let start = Instant::now();
let thread_controls = controls.clone();
let handle = std::thread::spawn(move || {
let mut buffer = [0.0_f32; BUFFER_SIZE as usize * CHANNEL_COUNT as usize];
while !thread_controls.exit.load(Ordering::Relaxed) {
std::thread::sleep(std::time::Duration::from_secs_f32(
buffer.len() as f32 / 44100.0,
));
// continue if paused
if thread_controls.pause.load(Ordering::Relaxed) {
continue;
}
// data is cpal's way of having a type erased buffer.
// you're expected to provide a raw pointer, the amount of samples, and the sample format of the buffer
let mut data = unsafe {
Data::from_parts(buffer.as_mut_ptr().cast(), buffer.len(), SampleFormat::F32)
};
let duration = Instant::now().duration_since(start);
let secs = duration.as_nanos() / 1_000_000_000;
let subsec_nanos = duration.as_nanos() - secs * 1_000_000_000;
let stream_instant = StreamInstant::new(secs as _, subsec_nanos as _);
let timestamp = OutputStreamTimestamp {
callback: stream_instant,
playback: stream_instant,
};
data_callback(&mut data, &OutputCallbackInfo::new(timestamp));
let avg = buffer.iter().sum::<f32>() / buffer.len() as f32;
println!("avg: {avg}");
}
});
Ok(MyStream {
controls,
start,
handle: Some(handle),
})
}
}
impl fmt::Display for MyDevice {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let desc = self.description().map_err(|_| fmt::Error)?;
f.write_str(desc.name())
}
}
impl StreamTrait for MyStream {
fn play(&self) -> Result<(), Error> {
self.controls.pause.store(false, Ordering::Relaxed);
Ok(())
}
fn pause(&self) -> Result<(), Error> {
self.controls.pause.store(true, Ordering::Relaxed);
Ok(())
}
fn now(&self) -> StreamInstant {
let elapsed = self.start.elapsed();
StreamInstant::new(elapsed.as_secs(), elapsed.subsec_nanos())
}
fn buffer_size(&self) -> Result<FrameCount, Error> {
Ok(BUFFER_SIZE)
}
}
// streams are expected to stop when dropped
impl Drop for MyStream {
fn drop(&mut self) {
self.controls.exit.store(true, Ordering::Relaxed);
let _ = self.handle.take().unwrap().join();
}
}
#[cfg(feature = "custom")]
fn main() {
let custom_host = cpal::platform::CustomHost::from_host(MyHost);
// alternatively, use cpal::platform::CustomDevice and skip enumerating devices
let host = cpal::Host::from(custom_host); // this host can be passed to rodio or any other crate that uses cpal
let device = host.default_output_device().unwrap();
let config = device.default_output_config().unwrap();
let stream = make_stream(&device, config.into()).unwrap();
stream.play().unwrap();
std::thread::sleep(std::time::Duration::from_millis(4000));
}
#[cfg(not(feature = "custom"))]
fn main() {
panic!("please run with -F custom to try this example")
}
// rest of this example is mostly based off of synth_tones.rs
pub enum Waveform {
Sine,
Square,
Saw,
Triangle,
}
pub struct Oscillator {
pub sample_rate: f32,
pub waveform: Waveform,
pub current_sample_index: f32,
pub frequency_hz: f32,
}
impl Oscillator {
fn advance_sample(&mut self) {
self.current_sample_index = (self.current_sample_index + 1.0) % self.sample_rate;
}
fn set_waveform(&mut self, waveform: Waveform) {
self.waveform = waveform;
}
fn calculate_sine_output_from_freq(&self, freq: f32) -> f32 {
let two_pi = 2.0 * std::f32::consts::PI;
(self.current_sample_index * freq * two_pi / self.sample_rate).sin()
}
fn is_multiple_of_freq_above_nyquist(&self, multiple: f32) -> bool {
self.frequency_hz * multiple > self.sample_rate / 2.0
}
fn sine_wave(&mut self) -> f32 {
self.advance_sample();
self.calculate_sine_output_from_freq(self.frequency_hz)
}
fn generative_waveform(&mut self, harmonic_index_increment: i32, gain_exponent: f32) -> f32 {
self.advance_sample();
let mut output = 0.0;
let mut i = 1;
while !self.is_multiple_of_freq_above_nyquist(i as f32) {
let gain = 1.0 / (i as f32).powf(gain_exponent);
output += gain * self.calculate_sine_output_from_freq(self.frequency_hz * i as f32);
i += harmonic_index_increment;
}
output
}
fn square_wave(&mut self) -> f32 {
self.generative_waveform(2, 1.0)
}
fn saw_wave(&mut self) -> f32 {
self.generative_waveform(1, 1.0)
}
fn triangle_wave(&mut self) -> f32 {
self.generative_waveform(2, 2.0)
}
fn tick(&mut self) -> f32 {
match self.waveform {
Waveform::Sine => self.sine_wave(),
Waveform::Square => self.square_wave(),
Waveform::Saw => self.saw_wave(),
Waveform::Triangle => self.triangle_wave(),
}
}
}
pub fn make_stream(device: &Device, config: StreamConfig) -> Result<Stream, anyhow::Error> {
let num_channels = config.channels as usize;
let mut oscillator = Oscillator {
waveform: Waveform::Sine,
sample_rate: config.sample_rate as f32,
current_sample_index: 0.0,
frequency_hz: 440.0,
};
let err_fn = |err: Error| match err.kind() {
ErrorKind::DeviceChanged | ErrorKind::Xrun | ErrorKind::RealtimeDenied => {
eprintln!("{err}")
}
_ => eprintln!("Stream error: {err}"),
};
let time_at_start = std::time::Instant::now();
println!("Time at start: {time_at_start:?}");
let stream = device.build_output_stream(
config,
move |output: &mut [f32], _: &OutputCallbackInfo| {
// for 0-1s play sine, 1-2s play square, 2-3s play saw, 3-4s play triangle_wave
let time_since_start = Instant::now().duration_since(time_at_start).as_secs_f32();
if time_since_start < 1.0 {
oscillator.set_waveform(Waveform::Sine);
} else if time_since_start < 2.0 {
oscillator.set_waveform(Waveform::Triangle);
} else if time_since_start < 3.0 {
oscillator.set_waveform(Waveform::Square);
} else if time_since_start < 4.0 {
oscillator.set_waveform(Waveform::Saw);
} else {
oscillator.set_waveform(Waveform::Sine);
}
process_frame(output, &mut oscillator, num_channels)
},
err_fn,
None,
)?;
Ok(stream)
}
fn process_frame<SampleType>(
output: &mut [SampleType],
oscillator: &mut Oscillator,
num_channels: usize,
) where
SampleType: Sample + FromSample<f32>,
{
for frame in output.chunks_mut(num_channels) {
let value: SampleType = SampleType::from_sample(oscillator.tick());
// copy the same value to all channels
for sample in frame.iter_mut() {
*sample = value;
}
}
}