-
Notifications
You must be signed in to change notification settings - Fork 9
/
simple.rs
206 lines (187 loc) · 6.24 KB
/
simple.rs
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
extern crate synthrs;
use synthrs::synthesizer::{make_samples, peak_normalize, quantize_samples, SamplesIter};
use synthrs::wave::{
bell, karplus_strong, noise, organ, rising_linear, sawtooth_wave, sine_wave, square_wave,
tangent_wave, triangle_wave,
};
use synthrs::writer::{write_pcm_file, write_wav_file};
fn main() {
// This creates a sine wave for 1.0s at 44_100Hz
// 0. `sine_wave` create a 440Hz sine function
// 1. `make_samples` creates a `Vec` of samples from the `sine_wave` function
// from 0.0 to 1.0 seconds at a 44_100Hz sample rate
// 2. `quantize_samples::<i16>` quantizes the floating-point samples as a signed 16-bit int
// 3. `write_pcm` writes the samples to a PCM file
write_pcm_file(
"out/sine.pcm",
&quantize_samples::<i16>(&make_samples(1.0, 44_100, sine_wave(440.0))),
)
.expect("failed");
// Write to a WAV file
write_wav_file(
"out/sine.wav",
44_100,
&quantize_samples::<i16>(&make_samples(1.0, 44_100, sine_wave(440.0))),
)
.expect("failed");
// `make_samples` takes in an Fn closure of type `|t: f64| -> f64`, where `t` = seconds
write_wav_file(
"out/sine_closure.wav",
44_100,
&quantize_samples::<i16>(&make_samples(1.0, 44_100, |t| {
(t * 440.0 * 2.0 * std::f64::consts::PI).sin()
})),
)
.expect("failed to write to file");
// `quantize_samples` takes in an interator, which the `make_samples` function returns
let sine_iter = SamplesIter::new(44_100, Box::new(sine_wave(440.0)));
write_wav_file(
"out/sine_iter.wav",
44_100,
&quantize_samples::<i16>(sine_iter.take(44_100).collect::<Vec<f64>>().as_slice()),
)
.expect("failed");
write_wav_file(
"out/square.wav",
44_100,
&quantize_samples::<i16>(&make_samples(1.0, 44_100, square_wave(440.0))),
)
.expect("failed");
write_wav_file(
"out/sawtooth.wav",
44_100,
&quantize_samples::<i16>(&make_samples(1.0, 44_100, sawtooth_wave(440.0))),
)
.expect("failed");
write_wav_file(
"out/triangle.wav",
44_100,
&quantize_samples::<i16>(&make_samples(1.0, 44_100, triangle_wave(440.0))),
)
.expect("failed");
write_wav_file(
"out/tangent.wav",
44_100,
&quantize_samples::<i16>(&make_samples(1.0, 44_100, tangent_wave(440.0))),
)
.expect("failed");
write_wav_file(
"out/noise.wav",
44_100,
&quantize_samples::<i16>(&make_samples(1.0, 44_100, noise())),
)
.expect("failed");
write_wav_file(
"out/organ.wav",
44_100,
&quantize_samples::<i16>(&make_samples(1.0, 44_100, organ(440.0))),
)
.expect("failed");
// Custom function for tone generation, t is in seconds
write_wav_file(
"out/wolftone.wav",
44_100,
&quantize_samples::<i16>(&make_samples(1.0, 44_100, |t: f64| -> f64 {
(square_wave(1000.0)(t) + square_wave(1020.0)(t)) / 2.0
})),
)
.expect("failed");
write_wav_file(
"out/rising.wav",
44_100,
&quantize_samples::<i16>(&make_samples(1.0, 44_100, |t: f64| -> f64 {
let (min_f, max_f) = (1000.0, 8000.0);
let max_t = 1.0; // Duration of clip in seconds
let range = max_f - min_f;
let f = max_f - (max_t - t) * range;
sine_wave(f)(t)
})),
)
.expect("failed");
write_wav_file(
"out/rising_wub.wav",
44_100,
&quantize_samples::<i16>(&make_samples(
3.0,
44_100,
rising_linear(440.0, 1760.0, 0.1),
)),
)
.expect("failed");
write_wav_file(
"out/bell.wav",
44_100,
&quantize_samples::<i16>(&make_samples(10.0, 44_100, |t: f64| -> f64 {
bell(200.0, 0.003, 0.5)(t)
})),
)
.expect("failed");
// Karplus-Strong introduces decay to the waveform
write_wav_file(
"out/karplus_strong.wav",
44_100,
&quantize_samples::<i16>(&peak_normalize(&make_samples(
5.0,
44_100,
|t: f64| -> f64 { karplus_strong(sawtooth_wave(440.0), 0.01, 1.0, 0.9, 44_100.0)(t) },
))),
)
.expect("failed");
// Using an "organ" we get a nice-sounding chime
write_wav_file(
"out/karplus_strong_organ.wav",
44_100,
&quantize_samples::<i16>(&peak_normalize(&make_samples(
5.0,
44_100,
|t: f64| -> f64 { karplus_strong(organ(440.0), 0.01, 1.0, 0.9, 44_100.0)(t) },
))),
)
.expect("failed");
write_wav_file(
"out/racecar.wav",
44_100,
&quantize_samples::<i16>(&make_samples(15.0, 44_100, |t: f64| -> f64 {
let mut out = 0.0;
if t < 14.0 {
out += sawtooth_wave(40.63 * (t / 2.0))(t);
} // Engine
if t < 1.0 {
out += sawtooth_wave(30.0)(t) * 10.0;
} // Engine start
if t < 14.0 && t.tan() > 0.0 {
out += square_wave(t.fract() * 130.8125)(t);
} // Gear
if t < 14.0 && t.tan() < 0.0 {
out += square_wave(t.sin() * 261.625)(t);
} // Gear
if t < 14.0 && t.tan() > 0.866 {
out += square_wave(t.fract() * 523.25)(t);
} // Gear
if t > 14.0 {
out += tangent_wave(100.0)(t)
} // Tree
(out / 4.0).min(1.0)
})),
)
.expect("failed");
write_wav_file(
"out/shepard.wav",
44_100,
&quantize_samples::<i16>(&make_samples(30.0, 44_100, |t: f64| -> f64 {
let length = 10.0;
let t_mod = t % length;
let progress = t_mod / length;
let tone_a2 = sine_wave(110.0 / (1.0 + progress));
let tone_a3 = sine_wave(220.0 / (1.0 + progress));
let tone_a4 = sine_wave(440.0 / (1.0 + progress));
let tone_a5 = sine_wave(880.0 / (1.0 + progress));
(tone_a2(t_mod) * (1.0 - progress)
+ tone_a5(t_mod) * progress
+ tone_a3(t_mod)
+ tone_a4(t_mod))
/ 4.0
})),
)
.expect("failed");
}