synth.rs 7.21 KB
Newer Older
Aske Simon Christensen's avatar
Aske Simon Christensen committed
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289

use std::collections::{HashMap, VecDeque};
use std::marker::PhantomData;

use vst2::api::Supported;
use vst2::buffer::AudioBuffer;
use vst2::event::Event;
use vst2::plugin::{CanDo, Category, Info, Plugin};

use cache::SoundCache;
use generate::{Sample, SoundGenerator, SoundParameters};


#[allow(dead_code)]
pub enum MidiCommand {
	NoteOn      { channel: u8, key: u8, velocity: u8 },
	NoteOff     { channel: u8, key: u8, velocity: u8 },
	AllNotesOff { channel: u8,          velocity: u8 },
	AllSoundOff { channel: u8,          velocity: u8 },
	Unknown
}

impl MidiCommand {
	fn fromData(data: &[u8; 3]) -> MidiCommand {
		match data[0] & 0xF0 {
			0x80 => MidiCommand::NoteOff { channel: data[0] & 0x0F, key: data[1], velocity: data[2] },
			0x90 => MidiCommand::NoteOn  { channel: data[0] & 0x0F, key: data[1], velocity: data[2] },
			0xB0 => match data[1] {
				120 => MidiCommand::AllSoundOff { channel: data[0] & 0x0F, velocity: data[2] },
				123 => MidiCommand::AllNotesOff { channel: data[0] & 0x0F, velocity: data[2] },
				_   => MidiCommand::Unknown
			},
			_    => MidiCommand::Unknown
		}
	}
}

pub struct MidiEvent {
	time: usize,
	command: MidiCommand,
}

struct Note {
	time: usize,
	tone: u8,
	velocity: u8,
	attack: f32,
	release: f32,

	release_time: Option<usize>
}

impl Note {
	fn new(tone: u8, velocity: u8, attack: f32, release: f32) -> Note {
		Note {
			time: 0,
			tone: tone,
			velocity: velocity,
			attack: attack,
			release: release,

			release_time: None
		}
	}

	fn produce_sample<G: SoundGenerator>(&mut self, cache: &mut Vec<SoundCache<G>>, param: &G::Parameters, global: &G::Global) -> Sample {
		let sample = cache[self.tone as usize].get_sample(self.time, param, global);
		self.time += 1;

		let amp = self.attack_amp().min(self.release_amp()) * (self.velocity as f32 / 127.0);
		sample * amp
	}

	fn attack_amp(&self) -> f32 {
		(self.time as f32 * self.attack).min(1.0)
	}

	fn release_amp(&self) -> f32 {
		match self.release_time {
			None => 1.0,
			Some(t) => (1.0 - (self.time - t) as f32 * self.release).max(0.0)
		}
	}

	fn release(&mut self, _velocity: u8) {
		self.release_time = Some(self.time);
	}

	fn is_released(&mut self) -> bool {
		self.release_time.is_some()
	}

	fn is_alive(&mut self) -> bool {
		self.release_amp() > 0.0
	}
}


pub trait SynthInfo {
	fn get_info() -> Info;
}

pub struct SynthPlugin<G: SoundGenerator, S: SynthInfo> {
	sample_rate: f32,
	time: usize,
	notes: Vec<Note>,
	events: VecDeque<MidiEvent>,
	cache: Vec<SoundCache<G>>,

	sound_params: G::Parameters,
	param_names: Vec<&'static str>,
	param_values: Vec<f32>,
	param_map: HashMap<&'static str, f32>,

	global: G::Global,

	phantom: PhantomData<S>
}

impl<G: SoundGenerator, S: SynthInfo> Default for SynthPlugin<G, S> {
	fn default() -> Self {
		let param_names = G::Parameters::names().to_vec();
		let mut param_values = Vec::with_capacity(param_names.len());
		let mut param_map = HashMap::new();
		for s in &param_names {
			let value = G::Parameters::default_value(s);
			param_values.push(value);
			param_map.insert(*s, value);
		}

		let sample_rate = 44100.0;

		SynthPlugin {
			sample_rate: sample_rate,
			time: 0,
			notes: Vec::new(),
			events: VecDeque::new(),
			cache: (0..128).map(|tone| SoundCache::new(tone)).collect(),

			sound_params: G::Parameters::build(&param_map, sample_rate),
			param_names: param_names,
			param_values: param_values,
			param_map: param_map,

			global: G::Global::default(),

			phantom: PhantomData
		}
	}
}

impl<G: SoundGenerator, S: SynthInfo> Plugin for SynthPlugin<G, S> {
	fn get_info(&self) -> Info {
		Info {
			presets: 0,
			parameters: self.param_names.len() as i32,
			inputs: 0,
			outputs: 2,
			category: Category::Synth,
			f64_precision: false,

			.. S::get_info()
		}
	}

	fn can_do(&self, can_do: CanDo) -> Supported {
		match can_do {
			CanDo::ReceiveMidiEvent => Supported::Yes,
			CanDo::Offline          => Supported::Yes,
			_                       => Supported::No
		}
	}

	fn process_events(&mut self, events: Vec<Event>) {
		for e in events.iter() {
			match *e {
				Event::Midi { delta_frames, ref data, .. } => {
					self.events.push_back(MidiEvent {
						time: self.time + (delta_frames as usize),
						command: MidiCommand::fromData(data)
					});
				}
				_ => {}
			}
		}
	}

	fn process(&mut self, buffer: AudioBuffer<f32>) {
		let mut outputs = buffer.split().1;
		for i in 0..outputs[0].len() {
			while !self.events.is_empty() && self.events.front().unwrap().time == self.time {
				let event = self.events.pop_front().unwrap();
				self.handle_event(event);
			}
			let sample = self.produce_sample();
			outputs[0][i] = sample.left;
			outputs[1][i] = sample.right;
			self.time += 1;
		}
	}

	fn set_sample_rate(&mut self, rate: f32) {
		self.sample_rate = rate;
		self.build_sound_params();
	}

	fn get_parameter_name(&self, index: i32) -> String {
		self.param_names[index as usize].to_string()
	}

	fn get_parameter_text(&self, index: i32) -> String {
		self.sound_params.display(self.param_names[index as usize], &self.param_map).0
	}

	fn get_parameter_label(&self, index: i32) -> String {
		self.sound_params.display(self.param_names[index as usize], &self.param_map).1
	}

	fn get_parameter(&self, index: i32) -> f32 {
		self.param_values[index as usize]
	}

	fn set_parameter(&mut self, index: i32, value: f32) {
		let old_value = self.param_values[index as usize];
		self.param_values[index as usize] = value;

		let name = self.param_names[index as usize];
		self.param_map.insert(name, value);

		if value != old_value {
			self.build_sound_params();
		}
	}
}

impl<G: SoundGenerator, S: SynthInfo> SynthPlugin<G, S> {
	fn handle_event(&mut self, event: MidiEvent) {
		match event.command {
			MidiCommand::NoteOn { key, velocity, .. } => {
				let attack = G::Parameters::attack(&self.param_map, self.sample_rate);
				let release = G::Parameters::release(&self.param_map, self.sample_rate);
				let note = Note::new(key, velocity, attack, release);
				self.notes.push(note);
			},
			MidiCommand::NoteOff { key, velocity, .. } => {
				for note in &mut self.notes {
					if note.tone == key && !note.is_released() {
						note.release(velocity);
						break;
					}
				}
			},
			MidiCommand::AllNotesOff { velocity, .. } => {
				for note in &mut self.notes {
					if !note.is_released() {
						note.release(velocity);
					}
				}
			},
			MidiCommand::AllSoundOff { .. } => {
				self.notes.clear();
			},
			MidiCommand::Unknown => {}
		}
	}

	fn produce_sample(&mut self) -> Sample {
		let mut sample: Sample = Sample::from(0.0);
		for i in (0..self.notes.len()).rev() {
			if self.notes[i].is_alive() {
				sample += self.notes[i].produce_sample(&mut self.cache, &self.sound_params, &self.global);
			} else {
				self.notes.remove(i);
			}
		}
		sample
	}

	fn build_sound_params(&mut self) {
		let new_sound_params = G::Parameters::build(&self.param_map, self.sample_rate);
		if new_sound_params != self.sound_params {
			self.sound_params = new_sound_params;
			for mut c in &mut self.cache {
				c.invalidate();
			}
		}
	}

}