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
#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
use std::error::Error;
#[cfg(all(target_os = "unknown", target_arch = "wasm32"))]
use wasm_bindgen::prelude::wasm_bindgen;
mod aaudio;
mod alsa;
mod coreaudio;
mod directsound;
mod pulse;
mod web;
#[doc(hidden)]
pub mod prelude {
pub use super::{run_output_device, OutputDevice, OutputDeviceParameters};
}
/// Parameters of an output device.
#[derive(Copy, Clone)]
pub struct OutputDeviceParameters {
/// Sample rate of your audio data. Typical values are: 11025 Hz, 22050 Hz, 44100 Hz (default), 48000 Hz,
/// 96000 Hz.
pub sample_rate: usize,
/// Desired amount of audio channels. Must be at least one. Typical values: 1 - mono, 2 - stereo, etc.
/// The data provided by the call back is _interleaved_, which means that if you have two channels then
/// the sample layout will be like so: `LRLRLR..`, where `L` - a sample of left channel, and `R` a sample
/// of right channel.
pub channels_count: usize,
/// Amount of samples per each channel. Allows you to tweak audio latency, the more the value the more
/// latency will be and vice versa. Keep in mind, that your data callback must be able to render the
/// samples while previous portion of data is being played, otherwise you'll get a glitchy audio.
///
/// If you need to get a specific length in **seconds**, then you need to use sampling rate to calculate
/// the required amount of samples per channel: `channel_sample_count = sample_rate * time_in_seconds`.
///
/// The crate guarantees, that the intermediate buffer size will match the requested value.
pub channel_sample_count: usize,
}
trait BaseAudioOutputDevice: Send + 'static {}
impl BaseAudioOutputDevice for () {}
trait AudioOutputDevice: BaseAudioOutputDevice {
fn new<C>(params: OutputDeviceParameters, data_callback: C) -> Result<Self, Box<dyn Error>>
where
C: FnMut(&mut [f32]) + Send + 'static,
Self: Sized;
}
/// An opaque "handle" to platform-dependent audio output device.
#[cfg_attr(all(target_os = "unknown", target_arch = "wasm32"), wasm_bindgen)]
pub struct OutputDevice {
device: Option<Box<dyn BaseAudioOutputDevice>>,
}
impl OutputDevice {
fn new<D: BaseAudioOutputDevice>(device: D) -> Self {
Self {
device: Some(Box::new(device)),
}
}
}
#[cfg_attr(all(target_os = "unknown", target_arch = "wasm32"), wasm_bindgen)]
impl OutputDevice {
/// Closes the output device and release all system resources occupied by it. Any calls of this
/// method after the device was closed does nothing.
pub fn close(&mut self) {
self.device.take();
}
}
/// Creates a new output device that uses default audio output device of your operating system to play the
/// samples produced by the specified `data_callback`. The callback will be called periodically to generate
/// another portion of samples.
///
/// ## Examples
///
/// The following examples plays a 440 Hz sine wave for 5 seconds.
///
/// ```rust,no_run
/// # use tinyaudio::prelude::*;
/// let params = OutputDeviceParameters {
/// channels_count: 2,
/// sample_rate: 44100,
/// channel_sample_count: 4410,
/// };
///
/// let _device = run_output_device(params, {
/// let mut clock = 0f32;
/// move |data| {
/// for samples in data.chunks_mut(params.channels_count) {
/// clock = (clock + 1.0) % params.sample_rate as f32;
/// let value =
/// (clock * 440.0 * 2.0 * std::f32::consts::PI / params.sample_rate as f32).sin();
/// for sample in samples {
/// *sample = value;
/// }
/// }
/// }
/// })
/// .unwrap();
///
/// std::thread::sleep(std::time::Duration::from_secs(5));
/// ```
#[allow(clippy::needless_return)]
pub fn run_output_device<C>(
params: OutputDeviceParameters,
data_callback: C,
) -> Result<OutputDevice, Box<dyn Error>>
where
C: FnMut(&mut [f32]) + Send + 'static,
{
#[cfg(target_os = "windows")]
{
return Ok(OutputDevice::new(directsound::DirectSoundDevice::new(
params,
data_callback,
)?));
}
#[cfg(target_os = "android")]
{
return Ok(OutputDevice::new(aaudio::AAudioOutputDevice::new(
params,
data_callback,
)?));
}
#[cfg(target_os = "linux")]
{
#[cfg(feature = "alsa")]
{
return Ok(OutputDevice::new(alsa::AlsaSoundDevice::new(
params,
data_callback,
)?));
}
#[cfg(all(feature = "pulse", not(feature = "alsa")))]
{
return Ok(OutputDevice::new(pulse::PulseSoundDevice::new(
params,
data_callback,
)?));
}
#[cfg(all(not(feature = "alsa"), not(feature = "pulse")))]
{
compile_error!("Select \"alsa\" or \"pulse\" feature to use an audio device on Linux")
}
}
#[cfg(all(target_os = "unknown", target_arch = "wasm32"))]
{
return Ok(OutputDevice::new(web::WebAudioDevice::new(
params,
data_callback,
)?));
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
{
return Ok(OutputDevice::new(coreaudio::CoreaudioSoundDevice::new(
params,
data_callback,
)?));
}
#[cfg(not(any(
target_os = "windows",
target_os = "linux",
target_os = "android",
target_os = "macos",
target_os = "ios",
all(target_os = "unknown", target_arch = "wasm32")
)))]
{
Err("Platform is not supported".to_string().into())
}
}