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
//! # Klingt
//!
//! A lock-free audio graph library with message-passing parameter control.
//!
//! ## Quick Start
//!
//! The simplest way to play audio is with [`Klingt::default_output`]:
//!
//! ```no_run
//! use klingt::{Klingt, nodes::Sine};
//!
//! // Create engine with default audio device
//! let mut klingt = Klingt::default_output().expect("No audio device");
//!
//! // Add a sine oscillator and connect to output
//! let sine = klingt.add(Sine::new(440.0));
//! klingt.output(&sine);
//!
//! // Main audio loop - call process() repeatedly
//! loop {
//! klingt.process();
//! std::thread::sleep(std::time::Duration::from_micros(500));
//! }
//! ```
//!
//! ## Core Concepts
//!
//! ### Nodes and Handles
//!
//! Audio processing is done by **nodes** that implement [`AudioNode`]. When you add a node
//! to Klingt, you get back a [`Handle`] that lets you:
//! - Connect nodes together with [`Klingt::connect`]
//! - Send parameter updates with [`Handle::send`]
//!
//! ```no_run
//! # use klingt::{Klingt, nodes::{Sine, SineMessage, Gain}};
//! # let mut klingt = Klingt::default_output().unwrap();
//! let mut sine = klingt.add(Sine::new(440.0));
//! let gain = klingt.add(Gain::new(0.5));
//!
//! klingt.connect(&sine, &gain);
//! klingt.output(&gain);
//!
//! // Change frequency at runtime (lock-free!)
//! sine.send(SineMessage::SetFrequency(880.0)).ok();
//! ```
//!
//! ### Automatic Sample Rate Conversion
//!
//! Klingt automatically handles sample rate mismatches. If you add a node that
//! has a different native sample rate (like a pre-decoded audio file), Klingt
//! creates a sub-graph at that rate and resamples to match the output device:
//!
//! ```ignore
//! // Audio file at 48000Hz + device at 44100Hz = automatic resampling
//! let player = SamplePlayer::new(samples, 2, 48000);
//! let handle = klingt.add(player); // Sub-graph created automatically
//! klingt.output(&handle); // Routed through resampler
//! ```
//!
//! ### Message Passing (No Locks!)
//!
//! All parameter updates use lock-free ring buffers. The audio thread never
//! blocks waiting for the main thread. Messages are processed at the start of
//! each audio block (64 samples by default).
//!
//! ## Built-in Nodes
//!
//! See the [`nodes`] module for available nodes:
//!
//! - **Sources**: [`Sine`](nodes::Sine), [`SamplePlayer`](nodes::SamplePlayer)
//! - **Effects**: [`Gain`](nodes::Gain), [`Mixer`](nodes::Mixer), [`SlewLimiter`](nodes::SlewLimiter)
//! - **Sinks**: [`CpalSink`](nodes::CpalSink) (with `cpal_sink` feature)
//!
//! ## Custom Nodes
//!
//! Implement [`AudioNode`] to create your own nodes. Here's a complete example
//! of a square wave oscillator with message-based parameter control:
//!
//! ```
//! use klingt::{AudioNode, ProcessContext};
//! use dasp_graph::{Buffer, Input};
//!
//! // Define messages for runtime parameter control
//! #[derive(Clone, Copy, Debug)]
//! pub enum SquareMessage {
//! SetFrequency(f32),
//! SetPulseWidth(f32), // 0.0 to 1.0, where 0.5 is a standard square
//! SetAmplitude(f32),
//! }
//!
//! pub struct Square {
//! frequency: f32,
//! pulse_width: f32,
//! amplitude: f32,
//! phase: f32,
//! }
//!
//! impl Square {
//! pub fn new(frequency: f32) -> Self {
//! Self {
//! frequency,
//! pulse_width: 0.5,
//! amplitude: 0.25,
//! phase: 0.0,
//! }
//! }
//! }
//!
//! impl AudioNode for Square {
//! type Message = SquareMessage;
//!
//! fn process(
//! &mut self,
//! ctx: &ProcessContext,
//! messages: impl Iterator<Item = SquareMessage>,
//! _inputs: &[Input],
//! outputs: &mut [Buffer],
//! ) {
//! // 1. Handle messages first (parameter updates)
//! for msg in messages {
//! match msg {
//! SquareMessage::SetFrequency(f) => self.frequency = f.max(0.0),
//! SquareMessage::SetPulseWidth(pw) => self.pulse_width = pw.clamp(0.0, 1.0),
//! SquareMessage::SetAmplitude(a) => self.amplitude = a.clamp(0.0, 1.0),
//! }
//! }
//!
//! // 2. Generate audio
//! let phase_inc = self.frequency / ctx.sample_rate as f32;
//!
//! for sample in outputs[0].iter_mut() {
//! // Square wave: high when phase < pulse_width, low otherwise
//! *sample = if self.phase < self.pulse_width {
//! self.amplitude
//! } else {
//! -self.amplitude
//! };
//!
//! // Advance and wrap phase
//! self.phase += phase_inc;
//! if self.phase >= 1.0 {
//! self.phase -= 1.0;
//! }
//! }
//! }
//!
//! fn num_outputs(&self) -> usize { 1 }
//! }
//! ```
//!
//! Then use it like any built-in node:
//!
//! ```ignore
//! let mut square = klingt.add(Square::new(440.0));
//! klingt.output(&square);
//!
//! // Modulate pulse width for PWM synthesis
//! square.send(SquareMessage::SetPulseWidth(0.25)).ok();
//! ```
//!
//! ### Node Types
//!
//! The three types of nodes differ by their input/output counts:
//!
//! | Type | Inputs | Outputs | Examples |
//! |--------|--------|---------|----------|
//! | Source | 0 | 1+ | Oscillators, sample players |
//! | Effect | 1+ | 1+ | Gain, filters, delays |
//! | Sink | 1+ | 0 | Audio output, recorders |
//!
//! Override [`num_inputs`](AudioNode::num_inputs) and [`num_outputs`](AudioNode::num_outputs)
//! to define your node's channel configuration.
//!
//! ## Feature Flags
//!
//! - `cpal_sink` - Enable CPAL audio output (adds [`CpalDevice`] and [`CpalSink`](nodes::CpalSink))
//! - `std` - Enable standard library (enabled by default)
//!
//! ## Design Principles
//!
//! - **Lock-free audio thread**: No allocations, no `Arc`/`Mutex` on the hot path
//! - **Automatic resampling**: Nodes at different sample rates just work
//! - **Fixed block size**: 64 samples per block (from dasp_graph)
extern crate alloc;
pub use ;
pub use ;
pub use CpalDevice;