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
|
// Copyright (C) 2024 Tomasz Kramkowski <tomasz@kramkow.ski>
// SPDX-License-Identifier: GPL-3.0-or-later
use std::{
env,
f64::consts,
io::{self, Write},
process,
};
type Float = f64;
const RATE: f64 = 8000.0;
#[derive(Debug, Copy, Clone)]
struct Beep {
frequency: Float,
volume: Float,
}
impl Beep {
fn new(frequency: Float, volume: Float) -> Self {
Self { frequency, volume }
}
fn sample_at(&self, time: Float) -> Float {
Float::sin(time * consts::TAU * self.frequency) * self.volume
}
}
#[derive(Copy, Clone)]
struct Beat<'a> {
beep: Beep,
start: Float,
duration: Float,
interval: Float,
mask: &'a dyn Fn(Float) -> bool,
}
impl<'a> Beat<'a> {
fn new(
beep: Beep,
start: Float,
duration: Float,
interval: Option<Float>,
mask: Option<&'a dyn Fn(Float) -> bool>,
) -> Self {
Self {
beep,
start,
duration,
interval: interval.unwrap_or(Float::INFINITY),
mask: mask.unwrap_or(&|_| true),
}
}
fn rel_time(&self, time: Float) -> Float {
(time - self.start) % self.interval
}
fn plays_now(&self, time: Float) -> bool {
(self.mask)(time) && time > self.start && self.rel_time(time) < self.duration
}
fn sample_at(&self, time: Float) -> Float {
self.beep.sample_at(self.rel_time(time))
}
}
fn sample_at(beats: &[Beat<'_>], time: Float) -> Float {
let mut sample = 0.0;
for beat in beats {
if beat.plays_now(time) {
sample += beat.sample_at(time)
}
}
sample
}
fn main() {
let mut args = env::args();
// This is bugged, argv[0] is empty if you exec with an empty or no argv
let argv0 = args.next().expect("No argv0");
let (Some(duration), None) = (args.next(), args.next()) else {
eprintln!("Usage: {argv0} duration");
process::exit(1);
};
let duration: Float = duration.parse().expect("Invalid duration");
let mut stdout = io::stdout();
let endstop = |s| s < duration - 1.0;
let beats = [
Beat::new(Beep::new(500.0, 0.5), 0.0, 0.1, Some(30.0), Some(&endstop)),
Beat::new(Beep::new(500.0, 0.5), 14.9, 0.1, Some(30.0), Some(&endstop)),
Beat::new(Beep::new(600.0, 0.5), 15.0, 0.1, Some(30.0), Some(&endstop)),
Beat::new(Beep::new(600.0, 0.5), -0.1, 0.1, Some(30.0), Some(&endstop)),
Beat::new(
Beep::new(300.0, 0.3),
1.45,
0.1,
Some(3.0),
Some(&|s| (s / 15.0) as u64 % 2 == 0),
),
Beat::new(
Beep::new(1000.0, 0.4),
299.95,
0.05,
Some(300.0),
Some(&endstop),
),
Beat::new(
Beep::new(1000.0, 0.4),
300.05,
0.05,
Some(300.0),
Some(&endstop),
),
Beat::new(Beep::new(400.0, 0.5), duration + 0.0, 0.4, None, None),
Beat::new(Beep::new(500.0, 0.5), duration + 0.45, 0.4, None, None),
Beat::new(Beep::new(600.0, 0.5), duration + 0.9, 0.4, None, None),
];
let start = -0.1;
let end = duration + 1.4;
for sample_index in 0..((RATE * (end - start)) as u64) {
let time = start + sample_index as Float / RATE;
let sample = sample_at(&beats, time);
stdout
.write_all(&[((sample + 1.0) / 2.0 * 256.0).floor().clamp(0.0, 255.0) as u8])
.expect("Could not write sample");
}
}
|