Created
July 5, 2018 19:47
-
-
Save alexcrichton/3afce9c35ddc24fd14857dd91a168920 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/rust-wasm-bindgen/dist/index.html b/rust-wasm-bindgen/dist/index.html | |
index 4db5b19..bb5fe0f 100644 | |
--- a/rust-wasm-bindgen/dist/index.html | |
+++ b/rust-wasm-bindgen/dist/index.html | |
@@ -14,11 +14,11 @@ | |
<script src="drift_meter.js"></script> | |
<script src="colormap.js"></script> | |
<script> | |
- const {process_audio} = wasm_bindgen; | |
+ const { Context } = wasm_bindgen; | |
- let drift_meter; | |
- wasm_bindgen('./drift_meter_bg.wasm').then(function (d) { | |
- drift_meter = d; | |
+ let cx = null; | |
+ wasm_bindgen('./drift_meter_bg.wasm').then(function () { | |
+ cx = Context.new(); | |
}); | |
function start() { | |
@@ -45,7 +45,11 @@ | |
let scriptNode = audioCtx.createScriptProcessor(2048, 1, 1); | |
scriptNode.onaudioprocess = function (audioProcessingEvent) { | |
const inputBuffer = audioProcessingEvent.inputBuffer; | |
- const chroma = process_audio(inputBuffer.getChannelData(0)); | |
+ cx.process_audio(inputBuffer.getChannelData(0)); | |
+ const ptr = cx.result_ptr(); | |
+ const len = cx.result_len(); | |
+ const chroma = new Float32Array(wasm_bindgen.wasm.memory.buffer) | |
+ .subarray(ptr / 4, ptr / 4 + len); | |
const barHeight = HEIGHT / chroma.length; | |
canvasCtx.drawImage(canvasElement, -SPEED, 0); | |
diff --git a/rust-wasm-bindgen/src/lib.rs b/rust-wasm-bindgen/src/lib.rs | |
index 0342463..a2d89c1 100644 | |
--- a/rust-wasm-bindgen/src/lib.rs | |
+++ b/rust-wasm-bindgen/src/lib.rs | |
@@ -12,12 +12,23 @@ use wasm_bindgen::prelude::*; | |
mod filterbank; | |
#[wasm_bindgen] | |
-pub fn process_audio(buf: Vec<f32>) -> Vec<f32> { | |
- // Create FFT processor | |
- let mut fft: RFft1D<f32> = RFft1D::<f32>::new(2048); | |
+pub struct Context { | |
+ fft: RFft1D<f32>, | |
+ result: [f32; 12], | |
+} | |
+ | |
+#[wasm_bindgen] | |
+impl Context { | |
+ pub fn new() -> Context { | |
+ Context { | |
+ fft: RFft1D::new(2048), | |
+ result: [0.0; 12], | |
+ } | |
+ } | |
+ pub fn process_audio(&mut self, buf: &[f32]) { | |
// Do FFT | |
- let bins = fft.forward(&buf); | |
+ let bins = self.fft.forward(buf); | |
// Square FFT coeffs | |
let bins = bins.iter().map(|x| x.norm_sqr()).collect(); | |
@@ -48,12 +59,18 @@ pub fn process_audio(buf: Vec<f32>) -> Vec<f32> { | |
let p = 0.5 * (a - c) / (a - (2. * b) + c); | |
- let mut hpcp: Vec<f32> = Vec::with_capacity(12); | |
for i in 0..12 { | |
- hpcp.push(reshaped_chroma[[i, b_index]] - (0.25 * (reshaped_chroma[[i, a_index]] - reshaped_chroma[[i, c_index]]) * p)); | |
+ self.result[i] = reshaped_chroma[[i, b_index]] - (0.25 * (reshaped_chroma[[i, a_index]] - reshaped_chroma[[i, c_index]]) * p); | |
+ } | |
+ } | |
+ | |
+ pub fn result_ptr(&self) -> *const f32 { | |
+ self.result.as_ptr() | |
} | |
- hpcp | |
+ pub fn result_len(&self) -> usize { | |
+ self.result.len() | |
+ } | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment