devalang 0.1.9

Write music with code. Devalang is a domain-specific language (DSL) for sound designers and music hackers. Compose, automate, and control sound — in plain text.
Documentation
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;
use std::sync::mpsc;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};

use anyhow::{Context, Result, bail};
use rodio::{Decoder, OutputStream, OutputStreamHandle, Sink};
use tokio::time::sleep;

use crate::engine::audio::settings::{AudioBitDepth, AudioChannels, AudioFormat, ResampleQuality};
use crate::tools::logger::Logger;

#[derive(Clone)]
pub struct LivePlaybackEngine {
    inner: Arc<LivePlaybackInner>,
}

struct LivePlaybackInner {
    logger: Arc<Logger>,
    _stream: OutputStream,
    handle: OutputStreamHandle,
}

impl LivePlaybackEngine {
    pub fn new(logger: Arc<Logger>) -> Result<Self> {
        let (stream, handle) =
            OutputStream::try_default().context("failed to access default audio output stream")?;
        Ok(Self {
            inner: Arc::new(LivePlaybackInner {
                logger,
                _stream: stream,
                handle,
            }),
        })
    }

    pub fn logger(&self) -> &Logger {
        &self.inner.logger
    }

    fn handle(&self) -> &OutputStreamHandle {
        &self.inner.handle
    }

    fn create_sink(&self, source: &LiveAudioSource) -> Result<Sink> {
        create_sink_with_handle(self.handle(), source)
    }

    pub async fn play_once(&self, source: LiveAudioSource, volume: f32) -> Result<()> {
        let volume_display = if volume == 0.0 {
            " [MUTED]".to_string()
        } else if volume < 1.0 {
            format!(" [volume: {:.0}%]", volume * 100.0)
        } else {
            String::new()
        };

        self.logger().action(format!(
            "Playing {} ({:?}, {}-bit, {} ch, {}, {} Hz, length {}){}",
            source.path.display(),
            source.format,
            source.bit_depth.bits(),
            source.channels.count(),
            source.resample_quality,
            source.sample_rate,
            format_duration_short(source.length),
            volume_display
        ));
        let sink = Arc::new(self.create_sink(&source)?);
        sink.set_volume(volume);

        // Attempt to load scheduled print events sidecar (module.printlog) for this audio
        let mut scheduled_logs: Vec<(f32, String)> = Vec::new();
        if let Some(stem) = source.path.file_stem().and_then(|s| s.to_str()) {
            let log_path = source.path.with_file_name(format!("{}.printlog", stem));
            if let Ok(contents) = std::fs::read_to_string(&log_path) {
                for line in contents.lines() {
                    if let Some((t, msg)) = line.split_once('\t') {
                        if let Ok(secs) = t.parse::<f32>() {
                            scheduled_logs.push((secs, msg.to_string()));
                        }
                    }
                }
                scheduled_logs
                    .sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));
            }
        }

        let sink_clone = Arc::clone(&sink);
        let wait_handle = std::thread::spawn(move || {
            sink_clone.sleep_until_end();
        });

        // Track playback start time so we can schedule print events
        let start_instant = std::time::Instant::now();
        let mut next_log_idx: usize = 0;

        // Poll loop: while playback is ongoing emit scheduled prints
        let poll_interval = std::time::Duration::from_millis(25);
        loop {
            if wait_handle.is_finished() {
                let _ = wait_handle.join();
                break;
            }

            if !scheduled_logs.is_empty() {
                let elapsed = start_instant.elapsed().as_secs_f32();
                while next_log_idx < scheduled_logs.len()
                    && scheduled_logs[next_log_idx].0 <= elapsed
                {
                    let msg = &scheduled_logs[next_log_idx].1;
                    self.logger().print(msg.clone());
                    next_log_idx += 1;
                }
            }

            std::thread::sleep(poll_interval);
        }

        sink.stop();
        self.logger().success("Playback completed.");
        Ok(())
    }

    pub async fn start_live_session(
        &self,
        source: LiveAudioSource,
        options: LivePlaybackOptions,
        background_event_rx: Option<
            std::sync::Arc<
                std::sync::Mutex<
                    std::sync::mpsc::Receiver<crate::engine::audio::events::AudioEventList>,
                >,
            >,
        >,
    ) -> Result<LivePlaybackSession> {
        let volume = options.volume();
        let volume_display = if volume == 0.0 {
            " [MUTED]".to_string()
        } else if volume < 1.0 {
            format!(" [volume: {:.0}%]", volume * 100.0)
        } else {
            String::new()
        };

        self.logger().action(format!(
            "Starting live session from {} ({:?}, {}-bit, {} ch, {}, {} Hz, loop {}){}",
            source.path.display(),
            source.format,
            source.bit_depth.bits(),
            source.channels.count(),
            source.resample_quality,
            source.sample_rate,
            format_duration_short(source.length),
            volume_display
        ));
        let (tx, rx) = mpsc::channel();
        let last_update = Arc::new(Mutex::new(Instant::now()));
        let logger = Arc::clone(&self.inner.logger);
        let handle_clone = self.handle().clone();
        let options_clone = options.clone();
        let source_clone = source.clone();
        let last_update_for_thread = Arc::clone(&last_update);
        let handle = thread::spawn(move || {
            run_loop(
                logger,
                handle_clone,
                source_clone,
                options_clone,
                rx,
                last_update_for_thread,
            )
        });

        Ok(LivePlaybackSession::new(
            self.clone(),
            tx,
            handle,
            last_update,
            options,
            background_event_rx,
        ))
    }
}

fn create_sink_with_handle(handle: &OutputStreamHandle, source: &LiveAudioSource) -> Result<Sink> {
    let file = File::open(&source.path)
        .with_context(|| format!("unable to open audio file: {}", source.path.display()))?;
    let reader = BufReader::new(file);
    let decoder = Decoder::new(reader)
        .with_context(|| format!("failed to decode audio file: {}", source.path.display()))?;
    let sink = Sink::try_new(handle).context("failed to create audio sink")?;
    sink.append(decoder);
    sink.set_volume(1.0);
    Ok(sink)
}

fn run_loop(
    logger: Arc<Logger>,
    handle: OutputStreamHandle,
    initial: LiveAudioSource,
    options: LivePlaybackOptions,
    rx: mpsc::Receiver<PlaybackCommand>,
    last_update: Arc<Mutex<Instant>>,
) -> Result<()> {
    let mut current = initial;
    let mut pending: Option<LiveAudioSource> = None;
    let poll_interval = options.poll_interval().max(Duration::from_millis(25));

    loop {
        logger.watch(format!(
            "Looping {} (~{})",
            current.path.display(),
            format_duration_short(current.length)
        ));
        if let Ok(mut guard) = last_update.lock() {
            *guard = Instant::now();
        }

        let sink = match create_sink_with_handle(&handle, &current) {
            Ok(sink) => {
                sink.set_volume(options.volume());
                Arc::new(sink)
            }
            Err(err) => {
                logger.error(format!("Failed to prepare live buffer: {err}"));
                match rx.recv() {
                    Ok(PlaybackCommand::Queue(next)) => {
                        pending = Some(next);
                        continue;
                    }
                    Ok(PlaybackCommand::Stop) | Err(_) => break,
                }
            }
        };

        // Attempt to load scheduled print events sidecar (module.printlog) for this audio
        let mut scheduled_logs: Vec<(f32, String)> = Vec::new();
        if let Some(stem) = current.path.file_stem().and_then(|s| s.to_str()) {
            let log_path = current.path.with_file_name(format!("{}.printlog", stem));
            if let Ok(contents) = std::fs::read_to_string(&log_path) {
                for line in contents.lines() {
                    if let Some((t, msg)) = line.split_once('\t') {
                        if let Ok(secs) = t.parse::<f32>() {
                            scheduled_logs.push((secs, msg.to_string()));
                        }
                    }
                }
                scheduled_logs
                    .sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));
            }
        }

        let sink_clone = Arc::clone(&sink);
        let wait_handle = thread::spawn(move || {
            sink_clone.sleep_until_end();
        });

        // Track playback start time so we can schedule print events
        let start_instant = Instant::now();
        let mut next_log_idx: usize = 0;

        let mut stop_requested = false;

        loop {
            if wait_handle.is_finished() {
                let _ = wait_handle.join();
                break;
            }
            // Emit scheduled prints at the correct playback time
            if !scheduled_logs.is_empty() {
                let elapsed = start_instant.elapsed().as_secs_f32();
                while next_log_idx < scheduled_logs.len()
                    && scheduled_logs[next_log_idx].0 <= elapsed
                {
                    let msg = &scheduled_logs[next_log_idx].1;
                    // Emit using the engine logger so print messages use the [PRINT] format
                    logger.print(msg.clone());
                    next_log_idx += 1;
                }
            }

            match rx.recv_timeout(poll_interval) {
                Ok(PlaybackCommand::Queue(next)) => {
                    pending = Some(next);
                }
                Ok(PlaybackCommand::Stop) => {
                    stop_requested = true;
                    sink.stop();
                    let _ = wait_handle.join();
                    break;
                }
                Err(mpsc::RecvTimeoutError::Timeout) => {
                    continue;
                }
                Err(mpsc::RecvTimeoutError::Disconnected) => {
                    stop_requested = true;
                    sink.stop();
                    let _ = wait_handle.join();
                    break;
                }
            }
        }

        if stop_requested {
            break;
        }

        while let Ok(cmd) = rx.try_recv() {
            match cmd {
                PlaybackCommand::Queue(next) => pending = Some(next),
                PlaybackCommand::Stop => {
                    stop_requested = true;
                    break;
                }
            }
        }

        if stop_requested {
            break;
        }

        if let Some(next) = pending.take() {
            logger.success(format!(
                "Next build ready -> {} (~{}). Switching after current loop.",
                next.path.display(),
                format_duration_short(next.length)
            ));
            current = next;
        } else {
            logger.info("Replaying current loop (no pending build).");
        }
    }

    logger.info("Live playback loop stopped.");
    Ok(())
}

fn format_duration_short(duration: Duration) -> String {
    if duration.as_secs() >= 1 {
        format!("{:.2}s", duration.as_secs_f64())
    } else {
        let ms = duration.as_secs_f64() * 1000.0;
        if ms >= 100.0 {
            format!("{:.0}ms", ms)
        } else {
            format!("{:.1}ms", ms)
        }
    }
}

#[derive(Clone)]
pub struct LiveAudioSource {
    pub path: PathBuf,
    pub format: AudioFormat,
    pub bit_depth: AudioBitDepth,
    pub channels: AudioChannels,
    pub sample_rate: u32,
    pub resample_quality: ResampleQuality,
    pub length: Duration,
}

impl LiveAudioSource {
    pub fn with_path(
        path: PathBuf,
        format: AudioFormat,
        bit_depth: AudioBitDepth,
        channels: AudioChannels,
        sample_rate: u32,
        resample_quality: ResampleQuality,
        length: Duration,
    ) -> Self {
        Self {
            path,
            format,
            bit_depth,
            channels,
            sample_rate,
            resample_quality,
            length,
        }
    }
}

#[derive(Clone)]
pub struct LivePlaybackOptions {
    poll_interval: Duration,
    volume: f32,
}

impl LivePlaybackOptions {
    pub fn new(poll_interval: Duration) -> Self {
        Self {
            poll_interval,
            volume: 1.0,
        }
    }

    pub fn with_volume(mut self, volume: f32) -> Self {
        self.volume = volume.clamp(0.0, 1.0);
        self
    }

    pub fn poll_interval(&self) -> Duration {
        self.poll_interval
    }

    pub fn volume(&self) -> f32 {
        self.volume
    }
}

enum PlaybackCommand {
    Queue(LiveAudioSource),
    Stop,
}

pub struct LivePlaybackSession {
    engine: LivePlaybackEngine,
    commands: mpsc::Sender<PlaybackCommand>,
    handle: Option<thread::JoinHandle<Result<()>>>,
    last_update: Arc<Mutex<Instant>>,
    options: LivePlaybackOptions,
    background_event_rx: Option<
        std::sync::Arc<
            std::sync::Mutex<
                std::sync::mpsc::Receiver<crate::engine::audio::events::AudioEventList>,
            >,
        >,
    >,
}

impl LivePlaybackSession {
    fn new(
        engine: LivePlaybackEngine,
        commands: mpsc::Sender<PlaybackCommand>,
        handle: thread::JoinHandle<Result<()>>,
        last_update: Arc<Mutex<Instant>>,
        options: LivePlaybackOptions,
        background_event_rx: Option<
            std::sync::Arc<
                std::sync::Mutex<
                    std::sync::mpsc::Receiver<crate::engine::audio::events::AudioEventList>,
                >,
            >,
        >,
    ) -> Self {
        Self {
            engine,
            commands,
            handle: Some(handle),
            last_update,
            options,
            background_event_rx,
        }
    }

    pub fn queue_source(&self, next: LiveAudioSource) -> Result<()> {
        self.commands
            .send(PlaybackCommand::Queue(next))
            .context("failed to queue next live buffer")
    }

    pub async fn heartbeat(&self) {
        sleep(self.options.poll_interval()).await;
    }

    pub async fn finish(mut self) -> Result<()> {
        let _ = self.commands.send(PlaybackCommand::Stop);
        if let Some(handle) = self.handle.take() {
            match handle.join() {
                Ok(result) => result?,
                Err(err) => bail!("live playback thread panicked: {err:?}"),
            }
        }
        self.engine
            .logger()
            .info("Live session finished; awaiting next command.");
        Ok(())
    }

    #[allow(dead_code)]
    pub fn last_update(&self) -> Instant {
        *self.last_update.lock().expect("last_update poisoned")
    }
}