-
Notifications
You must be signed in to change notification settings - Fork 9
/
midi.rs
206 lines (192 loc) · 5.86 KB
/
midi.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::midi;
use synthrs::sample;
use synthrs::synthesizer::{make_samples_from_midi, make_samples_from_midi_file, quantize_samples};
use synthrs::wave;
use synthrs::writer::write_wav_file;
fn main() {
// `make_samples_from_midi_file` is a convenience function that parses and synthesises
// a MIDI file given a file path
// Set `use_envelope` to decide whether to use a basic attack/decay envelope when generating samples
// The envelope will slowly fade each note out over time
write_wav_file(
"out/octave.wav",
44_100,
&quantize_samples::<i16>(
&make_samples_from_midi_file(
wave::square_wave,
44_100,
true,
"examples/assets/octave.mid",
)
.unwrap(),
),
)
.expect("failed");
// Pass in any generator to `make_samples_from_midi_file`!
write_wav_file(
"out/octave_bell.wav",
44_100,
&quantize_samples::<i16>(
&make_samples_from_midi_file(
|frequency: f64| wave::bell(frequency, 0.003, 0.5),
44_100,
false,
"examples/assets/octave.mid",
)
.unwrap(),
),
)
.expect("failed");
// Use a sample to generate music!
// This is a YAMAHA SY35 sample grabbed from http://legowelt.org/samples/
let (piano_sample, piano_sample_len) =
sample::samples_from_wave_file("examples/assets/piano110hz.wav").unwrap();
// Give the samples, length of samples, the sample's frequency, and sample's "sample rate (usually 44100Hz)"
let piano_sampler =
|frequency: f64| wave::sampler(frequency, &piano_sample, piano_sample_len, 110.0, 44_100);
write_wav_file(
"out/octave_piano_sampler.wav",
44_100,
&quantize_samples::<i16>(
&make_samples_from_midi_file(
piano_sampler,
44_100,
false,
"examples/assets/octave.mid",
)
.unwrap(),
),
)
.expect("failed");
// This is a YAMAHA SY35 sample grabbed from http://legowelt.org/samples/
let (clarinet_sample, clarinet_sample_len) =
sample::samples_from_wave_file("examples/assets/clarinet262.wav").unwrap();
let clarinet_sampler = |frequency: f64| {
wave::sampler(
frequency,
&clarinet_sample,
clarinet_sample_len,
262.0,
44_100,
)
};
write_wav_file(
"out/octave_clarinet_sampler.wav",
44_100,
&quantize_samples::<i16>(
&make_samples_from_midi_file(
clarinet_sampler,
44_100,
false,
"examples/assets/octave.mid",
)
.unwrap(),
),
)
.expect("failed");
write_wav_file(
"out/octave_bell.wav",
44_100,
&quantize_samples::<i16>(
&make_samples_from_midi_file(
|frequency: f64| wave::bell(frequency, 0.003, 0.5),
44_100,
false,
"examples/assets/octave.mid",
)
.unwrap(),
),
)
.expect("failed");
// Manually parse and synthesise MIDI files
// The `make_samples_from_midi` function works on an already-parsed MIDI file
// `read_midi` does the file reading and parsing
let song = midi::read_midi_file("examples/assets/octave.mid").unwrap();
write_wav_file(
"out/octave_no_envelope.wav",
44_100,
&quantize_samples::<i16>(
&make_samples_from_midi(wave::square_wave, 44_100, false, song).unwrap(),
),
)
.expect("failed");
// Seikilos: the oldest known surviving musical composition
// https://en.wikipedia.org/wiki/Seikilos_epitaph
write_wav_file(
"out/seikilos.wav",
44_100,
&quantize_samples::<i16>(
&make_samples_from_midi_file(
|frequency: f64| {
wave::karplus_strong(wave::sawtooth_wave(frequency), 0.01, 1.0, 0.9, 44_100.0)
},
44_100,
true,
"examples/assets/seikilos.mid",
)
.unwrap(),
),
)
.expect("failed");
// Johann Strauss II - The Blue Danube
write_wav_file(
"out/danube.wav",
44_100,
&quantize_samples::<i16>(
&make_samples_from_midi_file(
wave::sine_wave,
44_100,
true,
"examples/assets/danube.mid",
)
.unwrap(),
),
)
.expect("failed");
// Grieg - In the Hall of the Mountain King
write_wav_file(
"out/mountainking.wav",
44_100,
&quantize_samples::<i16>(
&make_samples_from_midi_file(
wave::square_wave,
44_100,
true,
"examples/assets/mountainking.mid",
)
.unwrap(),
),
)
.expect("failed");
// Satie - Gymnopédies No. 1 using a piano sample
write_wav_file(
"out/gymnopedie_sampler.wav",
44_100,
&quantize_samples::<i16>(
&make_samples_from_midi_file(
piano_sampler,
44_100,
false,
"examples/assets/gymnopedie1.mid",
)
.unwrap(),
),
)
.expect("failed");
// Christian Sinding - Rustle of Spring (Frühlingsrauschen)
write_wav_file(
"out/rustle.wav",
44_100,
&quantize_samples::<i16>(
&make_samples_from_midi_file(
wave::square_wave,
44_100,
true,
"examples/assets/rustle.mid",
)
.unwrap(),
),
)
.expect("failed");
}