generate.rs 1.63 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

use std::ops::{Add, AddAssign, Index, Mul, MulAssign};

#[derive(Clone, Copy)]
pub struct Sample {
	pub left: f32,
	pub right: f32
}

impl Add<Sample> for Sample {
	type Output = Sample;

	fn add(self, other: Sample) -> Sample {
		Sample {
			left: self.left + other.left,
			right: self.right + other.right
		}
	}
}

impl AddAssign<Sample> for Sample {
	fn add_assign(&mut self, other: Sample) {
		self.left += other.left;
		self.right += other.right;
	}
}

impl Mul<f32> for Sample {
	type Output = Sample;

	fn mul(self, scale: f32) -> Sample {
		Sample {
			left: self.left * scale,
			right: self.right * scale
		}
	}
}

impl MulAssign<f32> for Sample {
	fn mul_assign(&mut self, scale: f32) {
		self.left *= scale;
		self.right *= scale;
	}
}

impl From<f32> for Sample {
	fn from(s: f32) -> Sample {
		Sample {
			left: s,
			right: s
		}
	}
}

pub trait SoundParameters {
	fn names() -> &'static [&'static str];
	fn default_value(name: &str) -> f32;
	fn display<P: Index<&'static str, Output = f32>>(&self, name: &'static str, p: &P) -> (String, String);
	fn build<P: Index<&'static str, Output = f32>>(p: &P, sample_rate: f32) -> Self;
	fn attack<P: Index<&'static str, Output = f32>>(p: &P, sample_rate: f32) -> f32;
	fn release<P: Index<&'static str, Output = f32>>(p: &P, sample_rate: f32) -> f32;
}

pub trait SoundGenerator {
	type Parameters: PartialEq + SoundParameters;
66
	type Output: Default + Copy + Into<Sample>;
Aske Simon Christensen's avatar
Aske Simon Christensen committed
67
68
69
70
71
	type Global: Default;

	fn new(param: &Self::Parameters, tone: u8, time: usize, global: &Self::Global) -> Self;
	fn produce_sample(&mut self) -> Self::Output;
}