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
//! **phonic** is a cross-platform audio playback and DSP library for Rust. It provides a flexible,
//! low-latency audio engine and DSP tools for desktop and web applications.
//!
//! ### Overview
//!
//! - **[`Player`]** is the central component that manages audio playback. It takes an
//! output device instance, plays sounds and manages DSP effects.
//!
//! - **[`OutputDevice`]** represents the audio backend stream. phonic provides implementations
//! for different platforms, such as `cpal` for native applications and `web-audio` for WebAssembly.
//!
//! - **[`Source`]** produces audio signals. You can use the built-in [`FileSource`] for playing
//! back one-shot audio files, [`SynthSource`] for generating synthesized one-shot tones, or create
//! your own custom source implementation. Files can be played preloaded from RAM or streamed
//! on-the-fly.
//!
//! - **[`Generator`]** is a source that plays sounds driven by note and parameter events.
//! Use e.g. a [`Sampler`](crate::generators::Sampler) to play back sample files with
//! optional AHDSR envelopes, or [`FunDspGenerator`](crate::generators::FunDspGenerator)
//! to create a custom synth via [fundsp](https://github.com/SamiPerttu/fundsp), or create your
//! own custom generator.
//!
//! - **[`Effect`]** applies DSP effects to audio signals in a mixer and describes its automatable
//! properties via [`Parameter`]s. Phonic comes with a basic set of [`effects`], but you can
//! create your own custom ones too.
//! Effects are applied within mixers. By default, the player includes one main mixer that routes
//! all sources through it. For more complex audio routing, create additional mixers using
//! [`Player::add_mixer`] and route specific sources to them.
//!
//!
//! ### Getting Started
//!
//! Here's a basic example of how to play audio files with DSP effects.
//!
//! ```rust,no_run
//! use std::time::Duration;
//!
//! use phonic::{
//! DefaultOutputDevice, Player, FilePlaybackOptions, Error,
//! effects::{ChorusEffect, ReverbEffect, CompressorEffect},
//! generators::Sampler, GeneratorPlaybackOptions,
//! };
//!
//! fn main() -> Result<(), Error> {
//! // Create a player with the default audio output device.
//! let mut player = Player::new(DefaultOutputDevice::open()?, None);
//!
//! // Store some constants for event scheduling.
//! let now = player.output_sample_frame_position();
//! let samples_per_sec = player.output_sample_rate() as u64;
//!
//! // Add a new sub-mixer with a chorus and reverb effect to the main mixer.
//! let sub_mixer = {
//! let new_mixer = player.add_mixer(None)?;
//! player.add_effect(ChorusEffect::default(), new_mixer.id())?;
//! player.add_effect(ReverbEffect::default(), new_mixer.id())?;
//! new_mixer
//! };
//!
//! // Add a limiter to the main mixer. All sounds, including the sub mixer's output,
//! // will be routed through this effect now.
//! let limiter = player.add_effect(CompressorEffect::new_limiter(), None)?;
//!
//! // Change effect parameters via the returned handles.
//! // Schedule a parameter change 3 seconds from now (sample-accurate).
//! limiter.set_parameter(
//! CompressorEffect::MAKEUP_GAIN.value_update(3.0),
//! now + 3 * samples_per_sec
//! );
//!
//! // Play a file and get a handle to control it.
//! let file = player.play_file("path/to/your/file.wav",
//! FilePlaybackOptions::default().target_mixer(sub_mixer.id())
//! )?;
//!
//! // Control playback via the returned handles.
//! // Schedule a stop 2 seconds from now (sample-accurate)
//! file.stop(now + 2 * samples_per_sec)?;
//!
//! // Play another file on the main mixer with scheduled start time.
//! let some_other_file = player.play_file("path/to/your/some_other_file.wav",
//! FilePlaybackOptions::default().start_at_time(now + 2 * samples_per_sec)
//! )?;
//!
//! // Create a sampler generator to play a sample.
//! // We configure it to play on the sub-mixer.
//! let generator = player.play_generator(
//! Sampler::from_file(
//! "path/to/instrument_sample.wav",
//! GeneratorPlaybackOptions::default().target_mixer(sub_mixer.id()),
//! player.output_channel_count(),
//! player.output_sample_rate(),
//! )?,
//! None
//! )?;
//!
//! // Trigger a note on the generator. The `generator` handle is `Send + Sync`, so you
//! // can also pass it to other threads (e.g. a MIDI thread) to trigger events from there.
//! generator.note_on(60, Some(1.0), None, None)?;
//!
//! // The player's audio output stream runs on a separate thread. Keep the
//! // main thread running here, until all files finished playing.
//! while file.is_playing() || some_other_file.is_playing() {
//! std::thread::sleep(Duration::from_millis(100));
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! For more advanced usage, such as monitoring playback, sequencing playback, using generator
//! and creating more complex mixer graphs, please see the examples in the `README.md` and the
//! `/examples` directory of the repository.
// enable feature config when building for docs.rs
// enable experimental ASM features for emscripten js! macros
// -------------------------------------------------------------------------------------------------
// private mods (partly re-exported)
// public, flat re-exports (common types and traits)
pub use Error;
pub use DefaultOutputDevice;
pub use OutputDevice;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
// -------------------------------------------------------------------------------------------------
// public, modularized re-exports (common trait impls)
// -------------------------------------------------------------------------------------------------
// public mods
// -------------------------------------------------------------------------------------------------
// public re-exports
/// Create unique [`Parameter`] ids.
pub use four_cc;
/// Create custom Generator impls via [generators::FunDspGenerator].
pub use fundsp;