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
//! Simulated real-time data input for sonification.
//!
//! Provides several data source generators (sine, random LCG, ramp, custom)
//! and a `DataProcessor` that converts generated samples to sonification events.
use crate::scale_mapper_v2::{DataSonificationEvent, ScaleMapper, map_series};
use std::sync::mpsc;
// ---------------------------------------------------------------------------
// DataSource
// ---------------------------------------------------------------------------
/// Describes the source of generated samples.
#[derive(Debug, Clone)]
pub enum DataSource {
/// Sinusoidal wave.
Sine { freq: f64, amplitude: f64 },
/// Pseudo-random noise via a linear congruential generator.
Random { seed: u64 },
/// Linear ramp from `start` to `end` over `steps` samples, then wraps.
Ramp { start: f64, end: f64, steps: usize },
/// Replay a fixed sequence, cycling when exhausted.
Custom(Vec<f64>),
}
// ---------------------------------------------------------------------------
// LiveInputConfig
// ---------------------------------------------------------------------------
/// Configuration for a [`LiveInputGenerator`].
#[derive(Debug, Clone)]
pub struct LiveInputConfig {
pub source: DataSource,
pub sample_rate_hz: f64,
pub buffer_size: usize,
}
// ---------------------------------------------------------------------------
// LiveInputGenerator
// ---------------------------------------------------------------------------
/// Generates a stream of `f64` samples from a [`DataSource`].
pub struct LiveInputGenerator {
pub config: LiveInputConfig,
/// Current position within the sequence (for Ramp / Custom).
pub position: usize,
/// LCG state for `DataSource::Random`.
pub lcg_state: u64,
}
// LCG constants (Knuth)
const LCG_A: u64 = 6_364_136_223_846_793_005;
const LCG_C: u64 = 1_442_695_040_888_963_407;
impl LiveInputGenerator {
pub fn new(config: LiveInputConfig) -> Self {
let lcg_state = match &config.source {
DataSource::Random { seed } => *seed,
_ => 0,
};
Self {
config,
position: 0,
lcg_state,
}
}
/// Advance the LCG and return a value in `[-1.0, 1.0]`.
fn lcg_next(&mut self) -> f64 {
self.lcg_state = self.lcg_state.wrapping_mul(LCG_A).wrapping_add(LCG_C);
// Map to [-1.0, 1.0]
let unsigned = (self.lcg_state >> 33) as f64; // 31-bit value
unsigned / (0x7FFF_FFFFu64 as f64) - 1.0
}
/// Return the next generated sample.
pub fn next_sample(&mut self) -> f64 {
match &self.config.source.clone() {
DataSource::Sine { freq, amplitude } => {
let t = self.position as f64 / self.config.sample_rate_hz;
self.position = self.position.wrapping_add(1);
amplitude * (2.0 * std::f64::consts::PI * freq * t).sin()
}
DataSource::Random { .. } => self.lcg_next(),
DataSource::Ramp { start, end, steps } => {
let steps = (*steps).max(2);
let pos = self.position % steps;
self.position += 1;
start + (end - start) * (pos as f64 / (steps - 1) as f64)
}
DataSource::Custom(data) => {
if data.is_empty() {
return 0.0;
}
let v = data[self.position % data.len()];
self.position += 1;
v
}
}
}
/// Fill `buf` with generated samples.
pub fn fill_buffer(&mut self, buf: &mut [f64]) {
for slot in buf.iter_mut() {
*slot = self.next_sample();
}
}
/// Send `num_buffers` buffers of samples through a synchronous `mpsc` channel.
pub fn stream_to_channel(
&mut self,
tx: &mpsc::Sender<Vec<f64>>,
num_buffers: usize,
) {
let size = self.config.buffer_size;
for _ in 0..num_buffers {
let mut buf = vec![0.0f64; size];
self.fill_buffer(&mut buf);
if tx.send(buf).is_err() {
break;
}
}
}
}
// ---------------------------------------------------------------------------
// DataProcessor
// ---------------------------------------------------------------------------
/// Combines a [`LiveInputGenerator`] with a [`ScaleMapper`] to produce
/// sonification events from generated data.
pub struct DataProcessor {
pub generator: LiveInputGenerator,
pub mapper: ScaleMapper,
pub output: Vec<DataSonificationEvent>,
}
impl DataProcessor {
pub fn new(generator: LiveInputGenerator, mapper: ScaleMapper) -> Self {
Self {
generator,
mapper,
output: Vec::new(),
}
}
/// Convert a pre-collected buffer of samples to events.
pub fn process_buffer(&mut self, buf: &[f64]) -> Vec<DataSonificationEvent> {
map_series(buf, &self.mapper)
}
/// Generate `num_samples` samples and process them all at once.
pub fn run_sync(&mut self, num_samples: usize) -> Vec<DataSonificationEvent> {
let mut buf = vec![0.0f64; num_samples];
self.generator.fill_buffer(&mut buf);
let events = self.process_buffer(&buf);
self.output.extend(events.clone());
events
}
}
// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
use crate::scale_mapper_v2::MusicalScale;
fn sine_gen(freq: f64, amplitude: f64) -> LiveInputGenerator {
LiveInputGenerator::new(LiveInputConfig {
source: DataSource::Sine { freq, amplitude },
sample_rate_hz: 44100.0,
buffer_size: 256,
})
}
#[test]
fn sine_values_within_amplitude() {
let amp = 2.5;
let mut gen = sine_gen(440.0, amp);
for _ in 0..1000 {
let v = gen.next_sample();
assert!(
v >= -amp - 1e-9 && v <= amp + 1e-9,
"sine sample {} out of range [-{}, {}]",
v, amp, amp
);
}
}
#[test]
fn ramp_is_monotone_increasing() {
let mut gen = LiveInputGenerator::new(LiveInputConfig {
source: DataSource::Ramp { start: 0.0, end: 10.0, steps: 100 },
sample_rate_hz: 44100.0,
buffer_size: 100,
});
let mut prev = gen.next_sample();
for _ in 1..100 {
let cur = gen.next_sample();
assert!(cur >= prev, "ramp not monotone: {} < {}", cur, prev);
prev = cur;
}
}
#[test]
fn buffer_fill_correct_length() {
let mut gen = sine_gen(220.0, 1.0);
let mut buf = vec![0.0f64; 512];
gen.fill_buffer(&mut buf);
assert_eq!(buf.len(), 512);
// All values should be finite.
assert!(buf.iter().all(|v| v.is_finite()));
}
#[test]
fn random_source_varies() {
let mut gen = LiveInputGenerator::new(LiveInputConfig {
source: DataSource::Random { seed: 42 },
sample_rate_hz: 44100.0,
buffer_size: 64,
});
let a = gen.next_sample();
let b = gen.next_sample();
// Two consecutive LCG samples should differ.
assert_ne!(a, b);
}
#[test]
fn run_sync_produces_events() {
let gen = LiveInputGenerator::new(LiveInputConfig {
source: DataSource::Ramp { start: 0.0, end: 1.0, steps: 10 },
sample_rate_hz: 44100.0,
buffer_size: 10,
});
let mapper = ScaleMapper::new(MusicalScale::Major, 60, 2);
let mut proc = DataProcessor::new(gen, mapper);
let events = proc.run_sync(10);
assert_eq!(events.len(), 10);
}
}