oidos_generate.rs 12.7 KB
Newer Older
Aske Simon Christensen's avatar
Aske Simon Christensen committed
1
2
3
4

#![allow(dead_code)]

use std::{f32, f64};
Aske Simon Christensen's avatar
Aske Simon Christensen committed
5
use std::mem::transmute;
Aske Simon Christensen's avatar
Aske Simon Christensen committed
6
7
8
9
10
11
use std::ops::{Index};
#[cfg(test)] use std::collections::HashMap;

use generate::{SoundGenerator, SoundParameters};


12
const TOTAL_SEMITONES: f32 = 120f32;
Aske Simon Christensen's avatar
Aske Simon Christensen committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const NOISESIZE: usize = 64;

const NAMES: &'static [&'static str] = &[
	"seed",
	"modes",
	"fat",
	"width",
	"overtones",
	"sharpness",
	"harmonicity",
	"decaylow",
	"decayhigh",
	"filterlow",
	"fslopelow",
	"fsweeplow",
	"filterhigh",
	"fslopehigh",
	"fsweephigh",
	"gain",
	"attack",
Aske Simon Christensen's avatar
Aske Simon Christensen committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
	"release",
	"-",
	"q_decaydiff",
	"q_decaylow",
	"q_harmonicity",
	"q_sharpness",
	"q_width",
	"q_f_low",
	"q_fs_low",
	"q_fsw_low",
	"q_f_high",
	"q_fs_high",
	"q_fsw_high",
	"q_gain",
	"q_attack",
	"q_release"
Aske Simon Christensen's avatar
Aske Simon Christensen committed
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
];


pub struct OidosRandomData {
	data: Vec<u32>
}

impl Default for OidosRandomData {
	fn default() -> OidosRandomData {
		let mut data = Vec::with_capacity(NOISESIZE*NOISESIZE*NOISESIZE);
		let mut randomstate: [u32; 4] = [ 0x6F15AAF2, 0x4E89D208, 0x9548B49A, 0x9C4FD335 ];
		for _ in 0..NOISESIZE*NOISESIZE*NOISESIZE {
			let mut r = 0u32;
			for s in 0..3 {
				let mut rs = randomstate[s];
				rs = rs.rotate_right(rs).wrapping_add(randomstate[s+1]);
				randomstate[s] = rs;
				r = r ^ rs;
			}
			data.push(r);
		}

		OidosRandomData {
			data : data
		}
	}
}

#[test]
fn test_random_data() {
	let random = OidosRandomData::default();
	assert_eq!(random.data.len(), NOISESIZE*NOISESIZE*NOISESIZE);
	assert_eq!(*random.data.first().unwrap(), 0xCAADAA7B);
	assert_eq!(*random.data.last().unwrap(),  0xB08A4BA7);
}


Aske Simon Christensen's avatar
Aske Simon Christensen committed
86
87
88
89
90
91
92
93
94
95
96
97
fn quantize(value: f32, level: f32) -> f32 {
	let bit = 1 << ((level * 31.0).floor() as i32);
	let mask = !bit + 1;
	let add = bit >> 1;
	let mut bits = unsafe { transmute::<f32, u32>(value) };
	bits = (bits + add) & mask;
	if bits == 0x80000000 {
		bits = 0x00000000;
	}
	unsafe { transmute::<u32, f32>(bits) }
}

Aske Simon Christensen's avatar
Aske Simon Christensen committed
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
#[derive(Clone, PartialEq)]
pub struct OidosSoundParameters {
	modes: u8,
	fat: u8,
	seed: u8,
	overtones: u8,

	decaylow: f32,
	decaydiff: f32,
	harmonicity: f32,
	sharpness: f32,
	width: f32,

	f_low: f32,
	f_slopelow: f32,
	f_sweeplow: f32,
	f_high: f32,
	f_slopehigh: f32,
	f_sweephigh: f32,

	gain: f32,

	base_freq: f32
}

impl SoundParameters for OidosSoundParameters {
	fn names() -> &'static [&'static str] {
		&NAMES
	}

	fn default_value(name: &str) -> f32 {
Aske Simon Christensen's avatar
Aske Simon Christensen committed
129
130
131
132
		if name.starts_with("q_") {
			return 0.0;
		}

Aske Simon Christensen's avatar
Aske Simon Christensen committed
133
134
135
136
		match name {
			"harmonicity" | "decaylow" | "decayhigh" => 1.0,
			"filterhigh" => 0.8,
			"fsweeplow" | "fsweephigh" => 0.5,
137
			"-" => 0.0,
Aske Simon Christensen's avatar
Aske Simon Christensen committed
138
139
140
141
			_ => 0.2
		}
	}

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
	fn display<P: Index<&'static str, Output = f32>>(&self, name: &'static str, p: &P, sample_rate: f32) -> (String, String) {
		let decaylow = -4096.0 / ((self.decaylow).log2() * sample_rate);
		let decayhigh = -4096.0 / ((self.decaylow + self.decaydiff).log2() * sample_rate);

		let pname = match name {
			"q_decaydiff" => "decaydiff",
			"q_decaylow" => "decaylow",
			"q_harmonicity" => "harmonicity",
			"q_sharpness" => "sharpness",
			"q_width" => "width",
			"q_f_low" => "filterlow",
			"q_fs_low" => "fslopelow",
			"q_fsw_low" => "fsweeplow",
			"q_f_high" => "filterhigh",
			"q_fs_high" => "fslopehigh",
			"q_fsw_high" => "fsweephigh",
			"q_gain" => "gain",
			"q_attack" => "attack",
			"q_release" => "release",
			n => n
		};

		let value = match pname {
			"seed" => format!("{}", self.seed),
			"modes" => format!("{}", self.modes),
			"fat" => format!("{}", self.fat),
			"width" => format!("{:.3}", self.width),
			"overtones" => format!("{:.0}", self.overtones),
			"sharpness" => format!("{:+.1}", self.sharpness * 20.0 * 2f32.log10()),
			"harmonicity" => format!("{:.2}", self.harmonicity),
			"decaylow" => format!("{:.0}", 1000.0 * decaylow),
			"decayhigh" => format!("{:.0}", 1000.0 * decayhigh),
			"decaydiff" => format!("{:+.0}", 1000.0 * (decayhigh - decaylow)),
			"filterlow" => format!("{:+.0}", self.f_low),
			"fslopelow" => format!("{:.1}", 1.0 / self.f_slopelow),
			"fsweeplow" => format!("{:+.1}", self.f_sweeplow * sample_rate),
			"filterhigh" => format!("{:+.0}", self.f_high),
			"fslopehigh" => format!("{:.1}", 1.0 / self.f_slopehigh),
			"fsweephigh" => format!("{:+.1}", self.f_sweephigh * sample_rate),
			"gain" => format!("{:.2}", self.gain),
			"attack" => format!("{:.1}", 1000.0 / (OidosSoundParameters::attack(p, sample_rate) * sample_rate)),
			"release" => format!("{:.2}", 1.0 / (OidosSoundParameters::release(p, sample_rate) * sample_rate)),
			_ => "-".to_string()
		};
		let label = match pname {
			"sharpness" => "dB/oct",
			"width" | "overtones" | "filterlow" | "fslopelow" | "filterhigh" | "fslopehigh" => "ST",
			"fsweeplow" | "fsweephigh" => "ST/s",
			"decaylow" | "decayhigh" | "decaydiff" => "ms half",
			"attack" => "ms",
			"release" => "s",
			_ => ""
		}.to_string();

		(value, label)
Aske Simon Christensen's avatar
Aske Simon Christensen committed
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
	fn influence(name: &'static str) -> Vec<&'static str> {
		match name {
			"width"         => vec!["q_width"],
			"sharpness"     => vec!["q_sharpness"],
			"harmonicity"   => vec!["q_harmonicity"],
			"decaylow"      => vec!["q_decaydiff", "q_decaylow"],
			"decayhigh"     => vec!["q_decaydiff"],
			"filterlow"     => vec!["q_f_low"],
			"fslopelow"     => vec!["q_fs_low"],
			"fsweeplow"     => vec!["q_fsw_low"],
			"filterhigh"    => vec!["q_f_high"],
			"fslopehigh"    => vec!["q_fs_high"],
			"fsweephigh"    => vec!["q_fsw_high"],
			"gain"          => vec!["q_gain"],
			"attack"        => vec!["q_attack"],
			"release"       => vec!["q_release"],

			"q_decaydiff"   => vec!["decayhigh"],
			"q_decaylow"    => vec!["decaylow", "decayhigh", "q_decaydiff"],
			"q_harmonicity" => vec!["harmonicity"],
			"q_sharpness"   => vec!["sharpness"],
			"q_width"       => vec!["width"],
			"q_f_low"       => vec!["filterlow"],
			"q_fs_low"      => vec!["fslopelow"],
			"q_fsw_low"     => vec!["fsweeplow"],
			"q_f_high"      => vec!["filterhigh"],
			"q_fs_high"     => vec!["fslopehigh"],
			"q_fsw_high"    => vec!["fsweephigh"],
			"q_gain"        => vec!["gain"],
			"q_attack"      => vec!["attack"],
			"q_release"     => vec!["release"],

			_               => vec![]
		}
	}

Aske Simon Christensen's avatar
Aske Simon Christensen committed
235
	fn build<P: Index<&'static str, Output = f32>>(p: &P, sample_rate: f32) -> OidosSoundParameters {
Aske Simon Christensen's avatar
Aske Simon Christensen committed
236
		let mut params = OidosSoundParameters {
Aske Simon Christensen's avatar
Aske Simon Christensen committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
			modes:       (p["modes"]     * 100.0 + 0.5).floor().max(1.0) as u8,
			fat:         (p["fat"]       * 100.0 + 0.5).floor().max(1.0) as u8,
			seed:        (p["seed"]      * 100.0 + 0.5).floor() as u8,
			overtones:   (p["overtones"] * 100.0 + 0.5).floor() as u8,

			decaylow:    p["decaylow"],
			decaydiff:   p["decayhigh"] - p["decaylow"],
			harmonicity: p["harmonicity"] * 2.0 - 1.0,
			sharpness:   p["sharpness"] * 5.0 - 4.0,
			width:       p["width"].powi(5) * 100.0,

			f_low:       (p["filterlow"] * 2.0 - 1.0)    * TOTAL_SEMITONES,
			f_slopelow:  (1.0 - p["fslopelow"]).powi(3),
			f_sweeplow:  (p["fsweeplow"] - 0.5).powi(3)  * TOTAL_SEMITONES * 100.0 / sample_rate,
			f_high:      (p["filterhigh"] * 2.0 - 1.0)   * TOTAL_SEMITONES,
			f_slopehigh: (1.0 - p["fslopehigh"]).powi(3),
			f_sweephigh: (p["fsweephigh"] - 0.5).powi(3) * TOTAL_SEMITONES * 100.0 / sample_rate,

255
			gain:        4096f32.powf(p["gain"] - 0.25),
Aske Simon Christensen's avatar
Aske Simon Christensen committed
256
257

			base_freq:   440.0 * 2f32.powf(-57.0 / 12.0) / sample_rate * 2.0 * f32::consts::PI
Aske Simon Christensen's avatar
Aske Simon Christensen committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
		};

		params.decaylow = quantize(params.decaylow, p["q_decaylow"]);
		params.decaydiff = quantize(params.decaydiff, p["q_decaydiff"]);
		params.harmonicity = quantize(params.harmonicity, p["q_harmonicity"]);
		params.sharpness = quantize(params.sharpness, p["q_sharpness"]);
		params.width = quantize(params.width, p["q_width"]);

		params.f_low = quantize(params.f_low, p["q_f_low"]);
		params.f_slopelow = quantize(params.f_slopelow, p["q_fs_low"]);
		params.f_sweeplow = quantize(params.f_sweeplow, p["q_fsw_low"]);
		params.f_high = quantize(params.f_high, p["q_f_high"]);
		params.f_slopehigh = quantize(params.f_slopehigh, p["q_fs_high"]);
		params.f_sweephigh = quantize(params.f_sweephigh, p["q_fsw_high"]);

		params.gain = quantize(params.gain, p["q_gain"]);

		params
Aske Simon Christensen's avatar
Aske Simon Christensen committed
276
277
278
	}

	fn attack<P: Index<&'static str, Output = f32>>(p: &P, sample_rate: f32) -> f32 {
Aske Simon Christensen's avatar
Aske Simon Christensen committed
279
280
		let attack = p["attack"];
		quantize(if attack == 0.0 {
Aske Simon Christensen's avatar
Aske Simon Christensen committed
281
282
			2.0
		} else {
Aske Simon Christensen's avatar
Aske Simon Christensen committed
283
284
			1.0 / (attack * attack * sample_rate)
		}, p["q_attack"])
Aske Simon Christensen's avatar
Aske Simon Christensen committed
285
286
287
	}

	fn release<P: Index<&'static str, Output = f32>>(p: &P, sample_rate: f32) -> f32 {
Aske Simon Christensen's avatar
Aske Simon Christensen committed
288
289
		let release = p["release"];
		quantize(if release == 0.0 {
Aske Simon Christensen's avatar
Aske Simon Christensen committed
290
291
			2.0
		} else {
Aske Simon Christensen's avatar
Aske Simon Christensen committed
292
293
			1.0 / (release * sample_rate)
		}, p["q_release"])
Aske Simon Christensen's avatar
Aske Simon Christensen committed
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
	}

}

#[test]
fn test_oidos_sound_parameters() {
	let names = OidosSoundParameters::names();
	let mut map = HashMap::new();
	for name in names {
		map.insert(*name, OidosSoundParameters::default_value(name));
	}
	let param = OidosSoundParameters::build(&map, 44100.0);
	assert_eq!(param.base_freq, 0.00232970791933f32);
}


pub struct OidosSoundGenerator {
	n_partials:  usize,

	state_re:    Vec<f64>,
	state_im:    Vec<f64>,
	step_re:     Vec<f64>,
	step_im:     Vec<f64>,
	filter_low:  Vec<f64>,
	filter_high: Vec<f64>,

	f_add_low:   f64,
	f_add_high:  f64,

	gain:        f64
}

impl SoundGenerator for OidosSoundGenerator {
	type Parameters = OidosSoundParameters;
	type Output = f32;
	type Global = OidosRandomData;

	fn new(param: &OidosSoundParameters, tone: u8, time: usize, random: &OidosRandomData) -> OidosSoundGenerator {
		let n_partials = param.modes as usize * param.fat as usize;
333
		let n_partials_in_array = (n_partials + 3) & !3;
Aske Simon Christensen's avatar
Aske Simon Christensen committed
334
335
336
		let mut gen = OidosSoundGenerator {
			n_partials:   n_partials,

337
338
339
340
341
342
			state_re:     Vec::with_capacity(n_partials_in_array),
			state_im:     Vec::with_capacity(n_partials_in_array),
			step_re:      Vec::with_capacity(n_partials_in_array),
			step_im:      Vec::with_capacity(n_partials_in_array),
			filter_low:   Vec::with_capacity(n_partials_in_array),
			filter_high:  Vec::with_capacity(n_partials_in_array),
Aske Simon Christensen's avatar
Aske Simon Christensen committed
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390

			f_add_low:    (-param.f_sweeplow * param.f_slopelow) as f64,
			f_add_high:   (param.f_sweephigh * param.f_slopehigh) as f64,

			gain:         param.gain as f64
		};

		let f_lowlimit = param.f_low as f64 + tone as f64;
		let f_highlimit = param.f_high as f64 + tone as f64;

		for m in 0..param.modes as usize {
			let mut random_index = m * 256 + param.seed as usize;
			let mut getrandom = || {
				let r = random.data[random_index];
				random_index += 1;
				r as i32 as f64 / 0x80000000u32 as f64
			};

			let subtone = getrandom().abs();
			let reltone = subtone * param.overtones as f64;
			let decay = param.decaylow as f64 + subtone * param.decaydiff as f64;
			let ampmul = decay.powf(1.0 / 4096.0);

			let relfreq = 2f64.powf(reltone / 12.0);
			let relfreq_ot = (relfreq + 0.5).floor();
			let relfreq_h = relfreq + (relfreq_ot - relfreq) * param.harmonicity as f64;
			let reltone = relfreq_h.log2() * 12.0;
			let mtone = tone as f64 + reltone;
			let mamp = getrandom() * 2f64.powf(reltone * param.sharpness as f64 / 12.0);

			for _ in 0..param.fat as usize {
				let ptone = mtone + getrandom() * param.width as f64;
				let phase = param.base_freq as f64 * 2f64.powf(ptone / 12.0);
				gen.step_re.push(ampmul * phase.cos());
				gen.step_im.push(ampmul * phase.sin());

				let angle = getrandom() * f64::consts::PI + phase * time as f64;
				let amp = mamp * ampmul.powi(time as i32);
				gen.state_re.push(amp * angle.cos());
				gen.state_im.push(amp * angle.sin());

				let f_startlow = 1.0 - (f_lowlimit - ptone) * param.f_slopelow as f64;
				let f_starthigh = 1.0 - (ptone - f_highlimit) * param.f_slopehigh as f64;
				gen.filter_low.push(f_startlow + gen.f_add_low * time as f64);
				gen.filter_high.push(f_starthigh + gen.f_add_high * time as f64);
			}
		}

391
392
393
394
395
396
397
398
399
		for _ in n_partials..n_partials_in_array {
			gen.state_re.push(0.0);
			gen.state_im.push(0.0);
			gen.step_re.push(0.0);
			gen.step_im.push(0.0);
			gen.filter_low.push(0.0);
			gen.filter_high.push(0.0);
		}

Aske Simon Christensen's avatar
Aske Simon Christensen committed
400
401
402
403
		gen
	}

	fn produce_sample(&mut self) -> f32 {
404
405
406
407
408
409
410
		let s = unsafe {
			additive_core(self.state_re.as_mut_ptr(), self.state_im.as_mut_ptr(),
		                  self.step_re.as_ptr(), self.step_im.as_ptr(),
		                  self.filter_low.as_mut_ptr(), self.filter_high.as_mut_ptr(),
		                  self.f_add_low, self.f_add_high, self.n_partials)
		};
		(s * (self.gain / (self.n_partials as f64 + (self.gain - 1.0) * s * s)).sqrt()) as f32
Aske Simon Christensen's avatar
Aske Simon Christensen committed
411
412
413
	}
}

414
415
416
extern "cdecl" {
	fn additive_core(state_re: *mut f64, state_im: *mut f64, step_re: *const f64, step_im: *const f64,
	                 filter_low: *mut f64, filter_high: *mut f64, f_add_low: f64, f_add_high: f64, n: usize) -> f64;
Aske Simon Christensen's avatar
Aske Simon Christensen committed
417
}