1use std::path::PathBuf;
13use std::sync::Arc;
14use std::time::{Duration, Instant};
15
16pub const RATE: u32 = 44_100;
18pub const SLOTS: usize = 256;
19pub const CHANNELS: usize = 16;
20pub const FIRST_APC_CHANNEL: usize = 2;
23pub const BASE_DB: f32 = -12.0;
25const MAX_FRAMES: usize = RATE as usize * 600;
27
28pub type Pcm = Arc<[i16]>;
30
31#[derive(Clone)]
35pub struct Sound(Arc<SoundData>);
36
37struct SoundData {
38 frames: usize,
39 pcm: std::sync::OnceLock<Pcm>,
40}
41
42impl Sound {
43 pub fn ready(pcm: Pcm) -> Sound {
44 let data = SoundData { frames: pcm.len() / 2, pcm: std::sync::OnceLock::new() };
45 let _ = data.pcm.set(pcm);
46 Sound(Arc::new(data))
47 }
48
49 fn decoded_later(frames: usize, decode: impl FnOnce() -> Option<Vec<i16>> + Send + 'static) -> Sound {
52 let sound = Sound(Arc::new(SoundData { frames, pcm: std::sync::OnceLock::new() }));
53 let fill = sound.0.clone();
54 let spawned = std::thread::Builder::new().name("decode".into()).spawn(move || {
55 let _ = fill.pcm.set(decode().unwrap_or_default().into());
56 });
57 if let Err(e) = spawned {
58 log::warn!("decoding a sound: {e}");
59 let _ = sound.0.pcm.set(Vec::new().into());
60 }
61 sound
62 }
63
64 pub fn frames(&self) -> usize {
66 self.pcm().map_or(self.0.frames, |p| p.len() / 2)
67 }
68
69 pub fn pcm(&self) -> Option<&Pcm> {
71 self.0.pcm.get()
72 }
73
74 pub fn wait(&self) -> &Pcm {
76 loop {
77 if let Some(p) = self.pcm() {
78 return p;
79 }
80 std::thread::sleep(Duration::from_millis(5));
81 }
82 }
83}
84
85impl PartialEq for Sound {
86 fn eq(&self, other: &Sound) -> bool {
87 Arc::ptr_eq(&self.0, &other.0)
88 }
89}
90
91impl std::fmt::Debug for Sound {
92 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
93 write!(f, "Sound({} frames{})", self.0.frames, if self.pcm().is_some() { "" } else { ", decoding" })
94 }
95}
96
97#[derive(Clone, Debug, PartialEq)]
99pub enum AudioOp {
100 Queue {
104 channel: usize,
105 sound: Sound,
106 fade_in: usize,
107 fade_out: usize,
108 crossfade: bool,
109 looped: bool,
110 volume: (f32, f32),
111 },
112 Flush { channel: usize, fade_out: usize },
114 Volume { channel: usize, left: Option<f32>, right: Option<f32>, ramp: usize },
116}
117
118#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
120enum Busy {
121 #[default]
122 Idle,
123 Until(Instant),
124 Looping,
125}
126
127pub struct Audio {
128 slots: Vec<Option<Sound>>,
129 busy: [Busy; CHANNELS],
130 notify: [bool; CHANNELS],
132}
133
134impl Default for Audio {
135 fn default() -> Audio {
136 Audio { slots: vec![None; SLOTS], busy: [Busy::Idle; CHANNELS], notify: [false; CHANNELS] }
137 }
138}
139
140fn duration(s: &str, hz: Option<f64>) -> usize {
143 let digits = s.bytes().take_while(u8::is_ascii_digit).count();
144 let Ok(n) = s[..digits].parse::<u64>() else { return 0 };
145 match &s[digits..] {
146 "" | "ms" => (n * RATE as u64 / 1000) as usize,
147 "f" => n as usize,
148 "p" => hz.filter(|&f| f > 0.0).map_or(0, |f| (n as f64 * RATE as f64 / f + 0.5) as usize),
149 _ => 0,
150 }
151}
152
153fn volume(s: &str) -> f32 {
155 let lower = s.to_ascii_lowercase();
156 if let Some(db) = lower.strip_suffix("db") {
157 return db.trim().parse().unwrap_or(0.0);
158 }
159 match s.trim().parse::<f32>() {
160 Ok(v) if v <= 0.0 => -60.0,
161 Ok(v) => 20.0 * (v.min(100.0) / 100.0).log10(),
162 Err(_) => 0.0,
163 }
164}
165
166fn field<'a>(fields: &[&'a str], key: &str) -> Option<&'a str> {
168 fields.iter().find_map(|f| f.strip_prefix(key)?.strip_prefix('='))
169}
170
171fn number(fields: &[&str], key: &str) -> Option<usize> {
172 field(fields, key)?.trim().parse().ok()
173}
174
175impl Audio {
176 pub fn apc(&mut self, fields: &[&str], cache: Option<&PathBuf>, ops: &mut Vec<AudioOp>) -> Option<Instant> {
179 let (&verb, rest) = fields.split_first()?;
180 let slot = number(rest, "S").filter(|&s| s < SLOTS);
181 let channel = number(rest, "C").filter(|&c| c < CHANNELS);
182 match verb {
183 "Load" | "LoadBlob" => {
184 let data = rest.iter().rfind(|f| !f.starts_with("S="))?;
186 let bytes = if verb == "Load" {
187 let name = crate::syncterm::safe_name(data)?;
188 std::fs::read(cache?.join(name)).ok()?
189 } else {
190 crate::syncterm::decode_b64(data)?
191 };
192 match decode(&bytes) {
193 Some(sound) => self.slots[slot?] = Some(sound),
194 None => log::info!("audio {verb}: can't decode it"),
195 }
196 }
197 "Synth" => {
198 let shape = field(rest, "W").and_then(Shape::parse)?;
199 let hz: f64 = field(rest, "F").and_then(|f| f.trim().parse().ok()).unwrap_or(0.0);
200 let frames = duration(field(rest, "T")?, Some(hz)).min(MAX_FRAMES);
201 if frames > 0 {
202 self.slots[slot?] = Some(Sound::ready(synth(shape, hz, frames).into()));
203 }
204 }
205 "Copy" => {
206 let to = number(rest, "D").filter(|&d| d < SLOTS)?;
207 self.slots[to] = self.slots[slot?].clone();
208 }
209 "Queue" => {
210 let channel = channel.filter(|&c| c >= FIRST_APC_CHANNEL)?;
211 let sound = self.slots[slot?].take().filter(|s| s.frames() > 0)?;
212 let both = field(rest, "V").map(volume).unwrap_or(0.0);
213 let left = field(rest, "VL").map(volume).unwrap_or(both);
214 let right = field(rest, "VR").map(volume).unwrap_or(both);
215 let fade_in = field(rest, "I").map_or(0, |d| duration(d, None));
216 let crossfade = rest.contains(&"X");
217 let looped = rest.contains(&"L");
218 let now = Instant::now();
219 let length = Duration::from_secs_f64(sound.frames() as f64 / RATE as f64);
220 self.busy[channel] = match (looped, self.busy[channel]) {
221 (true, _) => Busy::Looping,
222 (false, Busy::Until(t)) if !crossfade && t > now => Busy::Until(t + length),
223 _ => Busy::Until(now + length),
224 };
225 ops.push(AudioOp::Queue {
226 channel,
227 sound,
228 fade_in,
229 fade_out: field(rest, "O").map_or(0, |d| duration(d, None)),
230 crossfade,
231 looped,
232 volume: (left, right),
233 });
234 }
235 "Flush" => {
236 let channel = channel?;
237 let fade_out = field(rest, "O").map_or(0, |d| duration(d, None));
238 self.busy[channel] = match fade_out {
239 0 => Busy::Idle,
240 f => Busy::Until(Instant::now() + Duration::from_secs_f64(f as f64 / RATE as f64)),
241 };
242 ops.push(AudioOp::Flush { channel, fade_out });
243 }
244 "Volume" => {
245 let channel = channel?;
246 let both = field(rest, "V").map(volume);
247 let left = field(rest, "VL").map(volume).or(both);
248 let right = field(rest, "VR").map(volume).or(both);
249 if left.is_some() || right.is_some() {
250 let ramp = field(rest, "T").map_or(0, |d| duration(d, None));
251 ops.push(AudioOp::Volume { channel, left, right, ramp });
252 }
253 }
254 "Wait" => {
255 if let Busy::Until(t) = self.busy[channel?] {
256 return Some(t);
257 }
258 }
259 "Update" => self.notify[channel?] = true,
260 _ => log::debug!("unhandled audio APC {verb}"),
261 }
262 None
263 }
264
265 pub fn music(&mut self, length: Duration) {
267 let now = Instant::now();
268 self.busy[0] = match self.busy[0] {
269 Busy::Until(t) if t > now => Busy::Until(t + length),
270 _ => Busy::Until(now + length),
271 };
272 }
273
274 fn playing(&self, channel: usize, now: Instant) -> bool {
275 match self.busy[channel] {
276 Busy::Idle => false,
277 Busy::Until(t) => t > now,
278 Busy::Looping => true,
279 }
280 }
281
282 pub fn report(&self, channel: Option<usize>) -> String {
284 let now = Instant::now();
285 match channel {
286 Some(c) => format!("\x1b[=7;{c};{}n", (c < CHANNELS && self.playing(c, now)) as u8),
287 None => {
288 let pairs: String =
289 (0..CHANNELS).filter(|&c| self.playing(c, now)).map(|c| format!(";{c};1")).collect();
290 format!("\x1b[=7{pairs}n")
291 }
292 }
293 }
294
295 pub fn poll(&mut self, now: Instant, replies: &mut Vec<u8>) {
298 for c in 0..CHANNELS {
299 if self.notify[c] && !self.playing(c, now) {
300 self.notify[c] = false;
301 replies.extend_from_slice(format!("\x1b[=7;{c};0n").as_bytes());
302 }
303 }
304 }
305
306 pub fn next_due(&self) -> Option<Instant> {
308 (0..CHANNELS)
309 .filter(|&c| self.notify[c])
310 .filter_map(|c| match self.busy[c] {
311 Busy::Until(t) => Some(t),
312 _ => None,
313 })
314 .min()
315 }
316}
317
318pub fn format_supported(pm: u32, ps: u32) -> bool {
321 const WAV: u32 = 1;
322 const AIFF: u32 = 2;
323 const VOC: u32 = 8;
324 const WAVEX: u32 = 19;
325 const FLAC: u32 = 23;
326 const OGG: u32 = 32;
327 const VORBIS: u32 = 96;
328 const OPUS: u32 = 100;
329 const MPEG: u32 = 35;
330 match pm {
331 WAV | WAVEX => matches!(ps, 2..=7),
332 AIFF => matches!(ps, 1..=4 | 6),
333 VOC => matches!(ps, 2 | 5),
334 FLAC => matches!(ps, 1..=3),
335 OGG => matches!(ps, VORBIS | OPUS),
336 MPEG => matches!(ps, 128..=130),
338 _ => false,
339 }
340}
341
342pub fn parse_id(s: &str) -> u32 {
344 let s = s.trim();
345 match s.strip_prefix("0x").or_else(|| s.strip_prefix("0X")) {
346 Some(h) => u32::from_str_radix(h, 16).unwrap_or(0),
347 None => s.parse().unwrap_or(0),
348 }
349}
350
351#[derive(Clone, Copy, Debug, PartialEq, Eq)]
353enum Shape {
354 Sine,
355 Saw,
356 Square,
357 SineHarm,
358 SineSaw,
359 SineSawChord,
360 SineSawHarm,
361 Silence,
362}
363
364impl Shape {
365 fn parse(s: &str) -> Option<Shape> {
366 Some(match s.trim() {
367 "SIN" => Shape::Sine,
368 "SAW" => Shape::Saw,
369 "SQ" => Shape::Square,
370 "SINE_HARM" => Shape::SineHarm,
371 "SINE_SAW" => Shape::SineSaw,
372 "SINE_SAW_CHORD" => Shape::SineSawChord,
373 "SINE_SAW_HARM" => Shape::SineSawHarm,
374 "SILENCE" => Shape::Silence,
375 _ => return None,
376 })
377 }
378}
379
380fn synth(shape: Shape, hz: f64, frames: usize) -> Vec<i16> {
385 if hz <= 0.0 || shape == Shape::Silence {
386 return vec![0; frames * 2];
387 }
388 use std::f64::consts::{PI, TAU};
389 let inc = TAU * hz / RATE as f64;
390 let u8c = |v: f64| (v as i64) as u8;
392 let mut mono: Vec<i16> = (0..frames)
393 .map(|i| {
394 let i = i as f64;
395 let pos = (inc * i) % TAU;
396 let noisy = |u: u8| (((u as i32) - 128) << 8) as i16;
397 match shape {
398 Shape::Sine => (pos.sin() * 32767.0) as i16,
399 Shape::Saw => (32767.0 - (pos / TAU) * 65535.0) as i16,
400 Shape::Square => {
401 if pos < PI {
402 32767
403 } else {
404 -32768
405 }
406 }
407 Shape::SineHarm => noisy(u8c((inc * 3.0 * i).sin() * 16.0)),
408 Shape::SineSaw => noisy(u8c(((pos.sin() * 127.0 + 128.0) + ((TAU - pos) * 40.5)) / 2.0)),
409 Shape::SineSawChord | Shape::SineSawHarm => {
410 let (a, b) = if shape == Shape::SineSawChord { (0.5, 1.0 / 3.0) } else { (2.0, 3.0) };
411 let mut u = u8c(((pos.sin() * 64.0 + 128.0) + ((TAU - pos) * 6.2)) / 2.0);
412 u = u8c(u as f64 + (inc * a * i).sin() * 24.0);
413 u = u8c(u as f64 + (inc * b * i).sin() * 16.0);
414 noisy(u)
415 }
416 Shape::Silence => 0,
417 }
418 })
419 .collect();
420 soften(&mut mono, hz);
421 mono.iter().flat_map(|&v| [v, v]).collect()
422}
423
424fn soften(mono: &mut [i16], hz: f64) {
427 let n = mono.len();
428 if n < 4 {
429 return;
430 }
431 let mid = n / 2;
432 let end_high = mono[n - 1] > 0;
433 if mono[n - 1] != 0 {
434 let mut i = n - 1;
435 while i > mid {
436 if end_high && mono[i] < 0 || !end_high && mono[i] > 0 {
437 break;
438 }
439 mono[i] = 0;
440 i -= 1;
441 }
442 }
443 let crossings_wanted = (hz / 100.0) as i32;
444 if crossings_wanted == 0 {
445 return;
446 }
447 let mut crossings = 0;
448 for i in 0..n - 1 {
449 if ((mono[i] < 0 && mono[i + 1] >= 0) || (mono[i] > 0 && mono[i + 1] <= 0)) && i > 2 {
450 crossings += 1;
451 if crossings >= crossings_wanted {
452 break;
453 }
454 }
455 let d = crossings_wanted - crossings;
456 mono[i] = (mono[i] as i32 / (d * d)) as i16;
457 }
458 crossings = 0;
459 for i in (1..n).rev() {
460 if ((mono[i] < 0 && mono[i - 1] >= 0) || (mono[i] > 0 && mono[i - 1] <= 0)) && i > 2 {
461 crossings += 1;
462 if crossings >= crossings_wanted {
463 break;
464 }
465 }
466 let d = crossings_wanted - crossings;
467 mono[i] = (mono[i] as i32 / (d * d)) as i16;
468 }
469}
470
471pub fn decode(data: &[u8]) -> Option<Sound> {
474 let (rate, channels, samples) = if data.starts_with(b"RIFF") && data.get(8..12) == Some(b"WAVE") {
475 decode_wav(data)?
476 } else if data.starts_with(b"FORM") && matches!(data.get(8..12), Some(b"AIFF" | b"AIFC")) {
477 decode_aiff(data)?
478 } else if data.starts_with(b"Creative Voice File\x1a") {
479 decode_voc(data)?
480 } else if data.starts_with(b"OggS") || data.starts_with(b"fLaC") || nanomp3::detect(data) {
481 let (frames, decode) = later(data)?;
482 let frames = frames.min(MAX_FRAMES);
483 return Some(Sound::decoded_later(frames, move || {
484 let (rate, channels, samples) = decode()?;
485 Some(to_stereo(rate, channels, &samples))
486 }));
487 } else {
488 return None;
489 };
490 Some(Sound::ready(to_stereo(rate, channels, &samples).into()))
491}
492
493type Decoded = (u32, usize, Vec<f32>);
495
496#[allow(clippy::type_complexity)]
499fn later(data: &[u8]) -> Option<(usize, Box<dyn FnOnce() -> Option<Decoded> + Send>)> {
500 let at_rate = |n: u64, rate: u32| (n as f64 * RATE as f64 / rate.max(1) as f64) as usize;
501 if data.starts_with(b"fLaC") {
502 let reader = claxon::FlacReader::new(std::io::Cursor::new(data.to_vec())).ok()?;
503 let info = reader.streaminfo();
504 let frames = at_rate(info.samples.unwrap_or(0), info.sample_rate);
505 return Some((frames, Box::new(move || decode_flac(reader))));
506 }
507 if !data.starts_with(b"OggS") {
508 let reader = nanomp3::SliceReader::<f32>::new(data);
510 let rate = reader.sample_rate();
511 let frames = at_rate(reader.total_samples().unwrap_or(0), rate);
512 if rate == 0 || frames == 0 {
513 return None;
514 }
515 let owned = data.to_vec();
516 return Some((frames, Box::new(move || decode_mp3(&owned))));
517 }
518 let mut reader = ogg::PacketReader::new(std::io::Cursor::new(data));
521 let head = reader.read_packet().ok()??.data;
522 let mut last = 0;
523 while let Ok(Some(p)) = reader.read_packet() {
524 last = p.absgp_page();
525 }
526 let owned = data.to_vec();
527 if head.starts_with(b"OpusHead") {
528 let pre_skip = u16le(&head, 10)? as u64;
529 Some((at_rate(last.saturating_sub(pre_skip), 48_000), Box::new(move || decode_opus(&owned))))
530 } else if head.starts_with(b"\x01vorbis") {
531 let rate = u32le(&head, 12)?;
532 Some((at_rate(last, rate), Box::new(move || decode_vorbis(owned))))
533 } else {
534 None
535 }
536}
537
538fn decode_mp3(data: &[u8]) -> Option<Decoded> {
539 let mut reader = nanomp3::SliceReader::<f32>::new(data);
540 let channels = reader.channels().map_or(2, |c| c as usize);
541 let rate = reader.sample_rate();
542 let mut samples = Vec::new();
543 let mut buf = vec![0f32; 4608];
544 while let Ok(n @ 1..) = reader.read(&mut buf) {
545 samples.extend_from_slice(&buf[..n]);
546 if samples.len() > MAX_FRAMES * channels {
547 break;
548 }
549 }
550 Some((rate, channels, samples))
551}
552
553fn decode_flac(mut reader: claxon::FlacReader<std::io::Cursor<Vec<u8>>>) -> Option<Decoded> {
554 let info = reader.streaminfo();
555 let scale = 1.0 / (1u64 << (info.bits_per_sample.clamp(1, 32) - 1)) as f32;
556 let mut samples = Vec::new();
557 for s in reader.samples() {
558 samples.push(s.ok()? as f32 * scale);
559 if samples.len() > MAX_FRAMES * 2 * info.channels as usize / 2 {
560 break;
561 }
562 }
563 Some((info.sample_rate, info.channels as usize, samples))
564}
565
566fn decode_vorbis(data: Vec<u8>) -> Option<Decoded> {
567 let mut reader = lewton::inside_ogg::OggStreamReader::new(std::io::Cursor::new(data)).ok()?;
568 let (rate, channels) = (reader.ident_hdr.audio_sample_rate, reader.ident_hdr.audio_channels as usize);
569 let mut samples = Vec::new();
570 while let Ok(Some(packet)) = reader.read_dec_packet_itl() {
571 samples.extend(packet.iter().map(|&s| s as f32 / 32768.0));
572 if samples.len() > MAX_FRAMES * channels.max(1) {
573 break;
574 }
575 }
576 Some((rate, channels, samples))
577}
578
579fn to_stereo(rate: u32, channels: usize, samples: &[f32]) -> Vec<i16> {
582 let channels = channels.max(1);
583 let frames = samples.len() / channels;
584 let frame = |i: usize| -> (f32, f32) {
585 let f = &samples[i.min(frames - 1) * channels..][..channels];
586 (f[0], if channels > 1 { f[1] } else { f[0] })
587 };
588 if frames == 0 || rate == 0 {
589 return Vec::new();
590 }
591 let out_frames = ((frames as u64 * RATE as u64 / rate as u64) as usize).min(MAX_FRAMES);
592 let step = rate as f64 / RATE as f64;
593 let mut out = Vec::with_capacity(out_frames * 2);
594 for i in 0..out_frames {
595 let pos = i as f64 * step;
596 let (j, t) = (pos as usize, (pos.fract()) as f32);
597 let ((l0, r0), (l1, r1)) = (frame(j), frame(j + 1));
598 let s = |a: f32, b: f32| ((a + (b - a) * t) * 32768.0).round().clamp(-32768.0, 32767.0) as i16;
599 out.push(s(l0, l1));
600 out.push(s(r0, r1));
601 }
602 out
603}
604
605fn u16le(d: &[u8], at: usize) -> Option<u16> {
606 Some(u16::from_le_bytes(d.get(at..at + 2)?.try_into().ok()?))
607}
608
609fn u32le(d: &[u8], at: usize) -> Option<u32> {
610 Some(u32::from_le_bytes(d.get(at..at + 4)?.try_into().ok()?))
611}
612
613fn u16be(d: &[u8], at: usize) -> Option<u16> {
614 Some(u16::from_be_bytes(d.get(at..at + 2)?.try_into().ok()?))
615}
616
617fn u32be(d: &[u8], at: usize) -> Option<u32> {
618 Some(u32::from_be_bytes(d.get(at..at + 4)?.try_into().ok()?))
619}
620
621fn pcm_samples(data: &[u8], bits: u16, float: bool, big_endian: bool, unsigned8: bool) -> Option<Vec<f32>> {
624 let bytes = bits as usize / 8;
625 if bytes == 0 || bytes > 8 {
626 return None;
627 }
628 let limit = MAX_FRAMES * 2 * bytes;
629 let data = &data[..data.len().min(limit) / bytes * bytes];
630 Some(
631 data.chunks_exact(bytes)
632 .map(|c| {
633 let mut b = [0u8; 8];
634 if big_endian {
635 for (i, &v) in c.iter().rev().enumerate() {
636 b[i] = v;
637 }
638 } else {
639 b[..bytes].copy_from_slice(c);
640 }
641 match (float, bytes) {
642 (true, 4) => f32::from_le_bytes([b[0], b[1], b[2], b[3]]),
643 (true, 8) => f64::from_le_bytes(b) as f32,
644 (_, 1) if unsigned8 => (b[0] as f32 - 128.0) / 128.0,
645 (_, 1) => b[0] as i8 as f32 / 128.0,
646 (_, 2) => i16::from_le_bytes([b[0], b[1]]) as f32 / 32768.0,
647 (_, 3) => (i32::from_le_bytes([0, b[0], b[1], b[2]]) >> 8) as f32 / 8_388_608.0,
648 (_, 4) => i32::from_le_bytes([b[0], b[1], b[2], b[3]]) as f32 / 2_147_483_648.0,
649 _ => 0.0,
650 }
651 })
652 .collect(),
653 )
654}
655
656fn decode_wav(d: &[u8]) -> Option<(u32, usize, Vec<f32>)> {
657 let (mut at, mut fmt, mut data) = (12, None, None);
658 while at + 8 <= d.len() {
659 let size = u32le(d, at + 4)? as usize;
660 let body = d.get(at + 8..(at + 8 + size).min(d.len()))?;
661 match &d[at..at + 4] {
662 b"fmt " => fmt = Some(body),
663 b"data" => data = Some(body),
664 _ => {}
665 }
666 at += 8 + size + size % 2;
667 }
668 let (fmt, data) = (fmt?, data?);
669 let mut tag = u16le(fmt, 0)?;
670 let channels = u16le(fmt, 2)? as usize;
671 let rate = u32le(fmt, 4)?;
672 let bits = u16le(fmt, 14)?;
673 if tag == 0xFFFE {
674 tag = u16le(fmt, 24)?;
676 }
677 let float = match tag {
678 1 => false,
679 3 => true,
680 _ => return None,
681 };
682 Some((rate, channels, pcm_samples(data, bits, float, false, true)?))
683}
684
685fn extended(b: &[u8]) -> Option<f64> {
687 let exp = (u16be(b, 0)? & 0x7FFF) as i32;
688 let mant = u64::from_be_bytes(b.get(2..10)?.try_into().ok()?);
689 Some(mant as f64 * 2f64.powi(exp - 16383 - 63))
690}
691
692fn decode_aiff(d: &[u8]) -> Option<(u32, usize, Vec<f32>)> {
693 let aifc = &d[8..12] == b"AIFC";
694 let (mut at, mut comm, mut ssnd) = (12, None, None);
695 while at + 8 <= d.len() {
696 let size = u32be(d, at + 4)? as usize;
697 let body = d.get(at + 8..(at + 8 + size).min(d.len()))?;
698 match &d[at..at + 4] {
699 b"COMM" => comm = Some(body),
700 b"SSND" => ssnd = Some(body),
701 _ => {}
702 }
703 at += 8 + size + size % 2;
704 }
705 let (comm, ssnd) = (comm?, ssnd?);
706 let channels = u16be(comm, 0)? as usize;
707 let bits = u16be(comm, 6)?;
708 let rate = extended(comm.get(8..18)?)? as u32;
709 let (float, big) = match comm.get(18..22).filter(|_| aifc) {
710 None | Some(b"NONE") | Some(b"twos") => (false, true),
711 Some(b"sowt") => (false, false),
712 Some(b"fl32") | Some(b"FL32") => (true, true),
713 _ => return None,
714 };
715 let offset = u32be(ssnd, 0)? as usize;
716 let data = ssnd.get(8 + offset..)?;
717 let bits = if float { 32 } else { bits.div_ceil(8) * 8 };
718 Some((rate, channels, pcm_samples(data, bits, float, big, false)?))
719}
720
721fn decode_voc(d: &[u8]) -> Option<(u32, usize, Vec<f32>)> {
722 let mut at = u16le(d, 20)? as usize;
723 let (mut rate, mut channels, mut bits) = (0u32, 1usize, 8u16);
724 let mut samples = Vec::new();
725 while let Some(&kind) = d.get(at) {
726 if kind == 0 {
727 break;
728 }
729 let size = u32le(d, at)? as usize >> 8;
730 let body = d.get(at + 4..(at + 4 + size).min(d.len()))?;
731 at += 4 + size;
732 let pcm = match kind {
733 1 => {
735 if rate == 0 {
736 rate = 1_000_000 / (256 - *body.first()? as u32);
737 }
738 (*body.get(1)? == 0).then(|| body.get(2..)).flatten()
739 }
740 2 => Some(body),
742 9 => {
744 rate = u32le(body, 0)?;
745 bits = *body.get(4)? as u16;
746 channels = *body.get(5)? as usize;
747 matches!(u16le(body, 6)?, 0 | 4).then(|| body.get(12..)).flatten()
748 }
749 _ => None,
750 };
751 if let Some(p) = pcm {
752 samples.extend(pcm_samples(p, bits, false, false, true)?);
753 if samples.len() > MAX_FRAMES * 2 {
754 break;
755 }
756 }
757 }
758 (rate > 0).then_some((rate, channels, samples))
759}
760
761fn decode_opus(d: &[u8]) -> Option<(u32, usize, Vec<f32>)> {
763 let mut reader = ogg::PacketReader::new(std::io::Cursor::new(d));
764 let head = reader.read_packet().ok()??;
765 if !head.data.starts_with(b"OpusHead") || head.data.get(18).copied().unwrap_or(0) != 0 {
766 return None;
768 }
769 let pre_skip = u16le(&head.data, 10)? as usize;
770 let gain = i16::from_le_bytes(head.data.get(16..18)?.try_into().ok()?);
771 let gain = 10f32.powf(gain as f32 / 256.0 / 20.0);
772 let mut decoder = rusty_opus::OpusDecoder::new(48_000, 2).ok()?;
773 let mut buf = vec![0f32; 5760 * 2];
774 let mut samples = Vec::new();
775 let mut skipped = 0;
776 while let Ok(Some(packet)) = reader.read_packet() {
777 if packet.data.starts_with(b"OpusTags") {
778 continue;
779 }
780 let Ok(n) = decoder.decode(&packet.data, 5760, &mut buf) else { continue };
781 let mut frames = &buf[..n * 2];
782 if skipped < pre_skip {
783 let skip = (pre_skip - skipped).min(n);
784 skipped += skip;
785 frames = &frames[skip * 2..];
786 }
787 samples.extend(frames.iter().map(|s| s * gain));
788 if samples.len() > MAX_FRAMES * 2 {
789 break;
790 }
791 }
792 Some((48_000, 2, samples))
793}
794
795#[cfg(test)]
796mod tests {
797 use super::*;
798
799 fn wav(rate: u32, channels: u16, bits: u16, data: &[u8]) -> Vec<u8> {
800 let mut f = b"RIFF\0\0\0\0WAVEfmt ".to_vec();
801 f.extend(16u32.to_le_bytes());
802 f.extend(1u16.to_le_bytes());
803 f.extend(channels.to_le_bytes());
804 f.extend(rate.to_le_bytes());
805 f.extend((rate * channels as u32 * bits as u32 / 8).to_le_bytes());
806 f.extend((channels * bits / 8).to_le_bytes());
807 f.extend(bits.to_le_bytes());
808 f.extend(b"data");
809 f.extend((data.len() as u32).to_le_bytes());
810 f.extend(data);
811 f
812 }
813
814 #[test]
815 fn durations_and_volumes() {
816 assert_eq!(duration("500", None), 22050);
817 assert_eq!(duration("500ms", None), 22050);
818 assert_eq!(duration("100f", None), 100);
819 assert_eq!(duration("2p", Some(441.0)), 200);
820 assert_eq!(duration("2p", None), 0);
821 assert_eq!(duration("x", None), 0);
822 assert_eq!(volume("-6.5dB"), -6.5);
823 assert_eq!(volume("100"), 0.0);
824 assert_eq!(volume("0"), -60.0);
825 assert!((volume("50") + 6.02).abs() < 0.01);
826 }
827
828 #[test]
829 fn files_decode_to_stereo_at_44k() {
830 let pcm = decode(&wav(22050, 1, 8, &[128, 255, 0, 128])).unwrap().wait().clone();
832 assert_eq!(pcm.len(), 8 * 2);
833 assert_eq!(pcm[0], 0);
834 assert_eq!(pcm[2], pcm[3], "mono goes to both sides");
835 assert!((16000..16500).contains(&pcm[2]), "{}", pcm[2]);
837 assert!(pcm[4] > 32000);
838 let pcm = decode(&wav(44100, 2, 16, &[0, 0x40, 0, 0xC0])).unwrap().wait().clone();
840 assert_eq!(&pcm[..], &[16384, -16384]);
841 let mut voc = b"Creative Voice File\x1a\x1a\x00\x0a\x01\x29\x11".to_vec();
843 voc.extend([1, 6, 0, 0, 233, 0, 128, 255, 128, 0, 0]);
844 let pcm = decode(&voc).unwrap().wait().clone();
845 assert!(pcm.len() >= 8 && pcm.iter().any(|&s| s > 30000));
846 let mut comm = 1u16.to_be_bytes().to_vec();
848 comm.extend(2u32.to_be_bytes());
849 comm.extend(16u16.to_be_bytes());
850 comm.extend([0x40, 0x0E, 0xAC, 0x44, 0, 0, 0, 0, 0, 0]);
851 let mut aiff = b"FORM\0\0\0\0AIFFCOMM".to_vec();
852 aiff.extend((comm.len() as u32).to_be_bytes());
853 aiff.extend(&comm);
854 aiff.extend(b"SSND");
855 aiff.extend(12u32.to_be_bytes());
856 aiff.extend([0u8; 8]);
857 aiff.extend([0x40, 0x00, 0xC0, 0x00]);
858 assert_eq!(&decode(&aiff).unwrap().wait()[..], &[16384, 16384, -16384, -16384]);
859 assert!(decode(b"not a sound").is_none());
860 }
861
862 #[test]
863 fn slots_channels_and_reports() {
864 let mut a = Audio::default();
865 let mut ops = Vec::new();
866 let apc = |a: &mut Audio, ops: &mut Vec<AudioOp>, s: &str| {
867 let fields: Vec<&str> = s.split(';').collect();
868 a.apc(&fields, None, ops)
869 };
870 apc(&mut a, &mut ops, "Synth;S=3;W=SQ;F=441;T=10p");
871 apc(&mut a, &mut ops, "Copy;S=3;D=4");
872 apc(&mut a, &mut ops, "Queue;C=1;S=3");
874 assert!(ops.is_empty());
875 apc(&mut a, &mut ops, "Queue;C=2;S=3;V=50;I=5;L");
876 let Some(AudioOp::Queue { channel: 2, sound, looped: true, fade_in: 220, volume, .. }) = ops.first() else {
877 panic!("{ops:?}")
878 };
879 assert_eq!(sound.frames(), 1000);
880 assert!((volume.0 + 6.02).abs() < 0.01);
881 apc(&mut a, &mut ops, "Queue;C=3;S=3");
883 assert_eq!(ops.len(), 1);
884 apc(&mut a, &mut ops, "Queue;C=3;S=4");
885 assert_eq!(a.report(None), "\x1b[=7;2;1;3;1n");
886 assert_eq!(a.report(Some(5)), "\x1b[=7;5;0n");
887 let until = apc(&mut a, &mut ops, "Wait;C=3").expect("a hold");
889 assert!(until > Instant::now() && until < Instant::now() + Duration::from_millis(100));
890 apc(&mut a, &mut ops, "Update;C=3");
892 let mut replies = Vec::new();
893 a.poll(Instant::now(), &mut replies);
894 assert!(replies.is_empty());
895 a.poll(Instant::now() + Duration::from_secs(1), &mut replies);
896 assert_eq!(replies, b"\x1b[=7;3;0n");
897 apc(&mut a, &mut ops, "Flush;C=2");
898 assert_eq!(a.report(Some(2)), "\x1b[=7;2;0n");
899 assert_eq!(ops.last(), Some(&AudioOp::Flush { channel: 2, fade_out: 0 }));
900 }
901
902 #[test]
903 fn tones_match_synctermss_shapes() {
904 let sq = synth(Shape::Square, 441.0, 1000);
905 assert_eq!(sq[425 * 2], 32767);
907 assert_eq!(sq[475 * 2], -32768);
908 assert!(sq[0].abs() < 32767 / 4);
909 assert_eq!(synth(Shape::Silence, 441.0, 10), vec![0; 20]);
910 let noisy = synth(Shape::SineSawHarm, 441.0, 1000);
911 assert!(noisy.iter().any(|&v| v != 0));
912 assert!(
913 format_supported(32, 100)
914 && format_supported(1, 2)
915 && format_supported(23, 2)
916 && format_supported(32, 96)
917 && format_supported(35, 130)
918 && !format_supported(23, 96)
919 );
920 assert_eq!(parse_id("0x20"), 32);
921 }
922
923 #[test]
925 #[ignore]
926 fn sound_file_decodes() {
927 let data = std::fs::read(std::env::var("BADTERM_SOUND").expect("set BADTERM_SOUND")).unwrap();
928 let t = Instant::now();
929 let sound = decode(&data).expect("decodes");
930 let frames = sound.frames();
931 let pcm = sound.wait().clone();
932 eprintln!("length from the headers {frames}, decoded {}", pcm.len() / 2);
933 let secs = pcm.len() as f64 / 2.0 / RATE as f64;
934 let peak = pcm.iter().map(|s| s.unsigned_abs()).max().unwrap_or(0);
935 let rms = (pcm.iter().map(|&s| (s as f64).powi(2)).sum::<f64>() / pcm.len() as f64).sqrt();
936 let zc = pcm.chunks(2).collect::<Vec<_>>().windows(2).filter(|w| (w[0][0] < 0) != (w[1][0] < 0)).count();
938 eprintln!(
939 "{secs:.2} s, peak {peak}, rms {rms:.0}, {:.0} crossings/s, decoded in {:?}",
940 zc as f64 / secs,
941 t.elapsed()
942 );
943 if let Ok(out) = std::env::var("BADTERM_WAV_OUT") {
944 let bytes: Vec<u8> = pcm.iter().flat_map(|s| s.to_le_bytes()).collect();
945 let mut f = b"RIFF\0\0\0\0WAVEfmt ".to_vec();
946 f.extend(16u32.to_le_bytes());
947 f.extend([1, 0, 2, 0]);
948 f.extend(RATE.to_le_bytes());
949 f.extend((RATE * 4).to_le_bytes());
950 f.extend([4, 0, 16, 0]);
951 f.extend(b"data");
952 f.extend((bytes.len() as u32).to_le_bytes());
953 f.extend(bytes);
954 std::fs::write(out, f).unwrap();
955 }
956 }
957}