summaryrefslogtreecommitdiff
path: root/src/model.rs
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2017-12-26 13:04:37 +0200
committerJustin Worthe <justin@worthe-it.co.za>2017-12-26 13:04:37 +0200
commitbbce5d68e2a6007fc8b023bf694ec058373b718c (patch)
tree03c14e541f826ca1d1c380bc26589cd5b6826242 /src/model.rs
parent5e0a70fa9b181637942d3d0d55f6a51c33fefbad (diff)
Moved overall model updating logic out of the GUI layer
This will be useful eventually for sharing a bit more overall application logic with the wasm build.
Diffstat (limited to 'src/model.rs')
-rw-r--r--src/model.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/model.rs b/src/model.rs
new file mode 100644
index 0000000..7fad354
--- /dev/null
+++ b/src/model.rs
@@ -0,0 +1,35 @@
+use transforms;
+
+#[derive(Default)]
+pub struct Model {
+ pub fundamental_frequency: Option<f32>,
+ pub pitch: String,
+ pub error: Option<f32>,
+ pub signal: Vec<f32>,
+ pub correlation: Vec<f32>
+}
+
+impl Model {
+ pub fn new() -> Model {
+ Model::default()
+ }
+
+ pub fn from_signal(signal: &[f32], sample_rate: f32) -> Model {
+ let correlation = transforms::correlation(signal);
+ let fundamental = transforms::find_fundamental_frequency(signal, sample_rate);
+ let pitch = fundamental.map_or(
+ String::new(),
+ transforms::hz_to_pitch
+ );
+
+ let error = fundamental.map(transforms::hz_to_cents_error);
+
+ Model {
+ fundamental_frequency: fundamental,
+ pitch: pitch,
+ error: error,
+ signal: transforms::align_to_rising_edge(signal),
+ correlation: correlation
+ }
+ }
+}