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
use std::cmp;
use std::sync::Arc;
use crate::midifile::Message;
use crate::midifile::MidiFile;
use crate::synthesizer::Synthesizer;
/// An instance of the MIDI file sequencer.
#[derive(Debug)]
#[non_exhaustive]
pub struct MidiFileSequencer {
synthesizer: Synthesizer,
speed: f64,
midi_file: Option<Arc<MidiFile>>,
play_loop: bool,
block_wrote: usize,
current_time: f64,
msg_index: usize,
loop_index: usize,
}
impl MidiFileSequencer {
/// Initializes a new instance of the sequencer.
///
/// # Arguments
///
/// * `synthesizer` - The synthesizer to be handled by the sequencer.
pub fn new(synthesizer: Synthesizer) -> Self {
Self {
synthesizer,
speed: 1.0,
midi_file: None,
play_loop: false,
block_wrote: 0,
current_time: 0.0,
msg_index: 0,
loop_index: 0,
}
}
/// Plays the MIDI file.
///
/// # Arguments
///
/// * `midi_file` - The MIDI file to be played.
/// * `play_loop` - If `true`, the MIDI file loops after reaching the end.
pub fn play(&mut self, midi_file: &Arc<MidiFile>, play_loop: bool) {
self.midi_file = Some(Arc::clone(midi_file));
self.play_loop = play_loop;
self.block_wrote = self.synthesizer.block_size;
self.current_time = 0.0;
self.msg_index = 0;
self.loop_index = 0;
self.synthesizer.reset()
}
/// Stops playing.
pub fn stop(&mut self) {
self.midi_file = None;
self.synthesizer.reset();
}
/// Renders the waveform.
///
/// # Arguments
///
/// * `left` - The buffer of the left channel to store the rendered waveform.
/// * `right` - The buffer of the right channel to store the rendered waveform.
///
/// # Remarks
///
/// The output buffers for the left and right must be the same length.
pub fn render(&mut self, left: &mut [f32], right: &mut [f32]) {
if left.len() != right.len() {
panic!("The output buffers for the left and right must be the same length.");
}
let left_length = left.len();
let mut wrote: usize = 0;
while wrote < left_length {
if self.block_wrote == self.synthesizer.block_size {
self.process_events();
self.block_wrote = 0;
self.current_time += self.speed * self.synthesizer.block_size as f64
/ self.synthesizer.sample_rate as f64;
}
let src_rem = self.synthesizer.block_size - self.block_wrote;
let dst_rem = left_length - wrote;
let rem = cmp::min(src_rem, dst_rem);
self.synthesizer.render(
&mut left[wrote..wrote + rem],
&mut right[wrote..wrote + rem],
);
self.block_wrote += rem;
wrote += rem;
}
}
fn process_events(&mut self) {
let midi_file = match self.midi_file.as_ref() {
Some(value) => value,
None => return,
};
while self.msg_index < midi_file.messages.len() {
let time = midi_file.times[self.msg_index];
let msg = midi_file.messages[self.msg_index];
if time <= self.current_time {
match msg {
Message::Normal {
status,
data1,
data2,
} => {
let channel = status & 0x0F;
let command = status & 0xF0;
self.synthesizer.process_midi_message(
channel as i32,
command as i32,
data1 as i32,
data2 as i32,
);
}
Message::LoopStart if self.play_loop => self.loop_index = self.msg_index,
Message::LoopEnd if self.play_loop => {
self.current_time = midi_file.times[self.loop_index];
self.msg_index = self.loop_index;
self.synthesizer.note_off_all(false);
}
_ => (),
}
self.msg_index += 1;
} else {
break;
}
}
if self.msg_index == midi_file.messages.len() && self.play_loop {
self.current_time = midi_file.times[self.loop_index];
self.msg_index = self.loop_index;
self.synthesizer.note_off_all(false);
}
}
/// Gets the synthesizer handled by the sequencer.
pub fn get_synthesizer(&self) -> &Synthesizer {
&self.synthesizer
}
/// Gets the currently playing MIDI file.
pub fn get_midi_file(&self) -> Option<&MidiFile> {
match &self.midi_file {
None => None,
Some(value) => Some(value),
}
}
/// Gets the current playback position in seconds.
pub fn get_position(&self) -> f64 {
self.current_time
}
/// Gets a value that indicates whether the current playback position is at the end of the sequence.
///
/// # Remarks
///
/// If the `play` method has not yet been called, this value will be `true`.
/// This value will never be `true` if loop playback is enabled.
pub fn end_of_sequence(&self) -> bool {
match &self.midi_file {
None => true,
Some(value) => self.msg_index == value.messages.len(),
}
}
/// Gets the current playback speed.
///
/// # Remarks
///
/// The default value is 1.
/// The tempo will be multiplied by this value during playback.
pub fn get_speed(&self) -> f64 {
self.speed
}
/// Sets the playback speed.
///
/// # Remarks
///
/// The value must be non-negative.
pub fn set_speed(&mut self, value: f64) {
if value < 0.0 {
panic!("The playback speed must be a non-negative value.");
}
self.speed = value;
}
}