|
| 1 | +import './style.css'; |
| 2 | +import * as tf from '@tensorflow/tfjs'; |
| 3 | +// require('@tensorflow/tfjs-backend-wasm'); |
| 4 | + |
| 5 | + |
| 6 | +interface AppState { |
| 7 | + TFBackend: string, |
| 8 | + microphoneConfig: tf.data.MicrophoneConfig; |
| 9 | + microphoneCaptureIntervalMs: number; |
| 10 | + signalCanvas: HTMLCanvasElement; |
| 11 | + // dBSpecCanvas: HTMLCanvasElement; |
| 12 | + logMelSpecCanvas: HTMLCanvasElement; |
| 13 | + predictionLabel: HTMLElement; |
| 14 | + running: boolean; |
| 15 | + renderIntervalID: number; |
| 16 | + spec2logmel: tf.GraphModel; |
| 17 | + signals2logmel: tf.GraphModel; |
| 18 | + xvector: tf.LayersModel; |
| 19 | + int2label: tf.Tensor1D; |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | +export const state: AppState = { |
| 24 | + TFBackend: "webgl", |
| 25 | + microphoneConfig: { |
| 26 | + fftSize: 1024, |
| 27 | + numFramesPerSpectrogram: 198, |
| 28 | + sampleRateHz: 44100, |
| 29 | + includeSpectrogram: true, |
| 30 | + includeWaveform: true, |
| 31 | + }, |
| 32 | + microphoneCaptureIntervalMs: 200, |
| 33 | + signalCanvas: null, |
| 34 | + // dBSpecCanvas: null, |
| 35 | + logMelSpecCanvas: null, |
| 36 | + predictionLabel: null, |
| 37 | + running: false, |
| 38 | + renderIntervalID: 0, |
| 39 | + spec2logmel: null, |
| 40 | + signals2logmel: null, |
| 41 | + xvector: null, |
| 42 | + int2label: null, |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +function fatalError(error: Error): void { |
| 47 | + console.error(error) |
| 48 | + console.error("cannot recover from error") |
| 49 | + stopApp() |
| 50 | +} |
| 51 | + |
| 52 | +export function stopApp(): void { |
| 53 | + console.info("app stopping") |
| 54 | + state.running = false |
| 55 | + if (state.renderIntervalID > 0) { |
| 56 | + window.clearInterval(state.renderIntervalID) |
| 57 | + state.renderIntervalID = 0 |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +function createElement(id, tag): HTMLElement { |
| 63 | + const e: HTMLElement = document.createElement(tag) |
| 64 | + e.id = id |
| 65 | + document.body.appendChild(e) |
| 66 | + return e |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +function spectrogramToCanvas(spec: tf.Tensor3D, canvas: HTMLCanvasElement): void { |
| 71 | + // Scale all values between 0 and 1 |
| 72 | + const min: tf.Tensor3D = spec.min([0], true) |
| 73 | + const max: tf.Tensor3D = spec.max([0], true) |
| 74 | + let image: tf.Tensor3D = tf.divNoNan(spec.sub(min), max.sub(min)) |
| 75 | + image = image.transpose([1, 0, 2]).reverse(0) as tf.Tensor3D |
| 76 | + |
| 77 | + // Render to canvas |
| 78 | + tf.browser.toPixels(image, canvas).catch(fatalError) |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +let spec2logmelInput = { |
| 83 | + spec: tf.zeros([1, 1, 1]), |
| 84 | + sample_rate: tf.scalar(16000, "int32"), |
| 85 | + num_mel_bins: tf.scalar(40, "int32"), |
| 86 | +} |
| 87 | + |
| 88 | +let signals2logmelInput = { |
| 89 | + signals: tf.zeros([1, 1]), |
| 90 | + sample_rate: tf.scalar(state.microphoneConfig.sampleRateHz, "int32"), |
| 91 | + num_mel_bins: tf.scalar(40, "int32"), |
| 92 | +} |
| 93 | + |
| 94 | +function updatePredictionLabel(predictedIndexes: Int32Array): void { |
| 95 | + const labels: string[] = Array.from(predictedIndexes, i => state.int2label[i]) |
| 96 | + state.predictionLabel.innerText = "prediction: " + labels.join(", ") |
| 97 | +} |
| 98 | + |
| 99 | +function handleMicrophoneInput(data: any): void { |
| 100 | + if (!state.running) { |
| 101 | + console.warn("app not running, ignoring microphone input data") |
| 102 | + return |
| 103 | + } |
| 104 | + // tf.tidy(() => spectrogramToCanvas(data.spectrogram.clipByValue(-200, 0), state.dBSpecCanvas)) |
| 105 | + |
| 106 | + tf.tidy(() => { |
| 107 | + spec2logmelInput.spec = data.spectrogram.transpose([2, 0, 1]) |
| 108 | + data.spectrogram.dispose() |
| 109 | + const logmel = state.spec2logmel.execute(spec2logmelInput) |
| 110 | + const imgInput = (logmel as tf.Tensor).clipByValue(-1, 1).transpose([1, 2, 0]) as tf.Tensor3D |
| 111 | + spectrogramToCanvas(imgInput, state.logMelSpecCanvas) |
| 112 | + |
| 113 | + const prediction: tf.Tensor1D = state.xvector.predict(logmel) as tf.Tensor1D |
| 114 | + prediction.argMax(1).data().then(updatePredictionLabel) |
| 115 | + }) |
| 116 | + |
| 117 | + // signal2logmelInput.signals = data.waveform.transpose([1, 0]) |
| 118 | + // data.waveform.dispose() |
| 119 | + |
| 120 | + // state.signals2logmel.executeAsync(signal2logmelInput) |
| 121 | + // .then(logmel => { |
| 122 | + // tf.tidy(() => { |
| 123 | + // const imgInput = (logmel as tf.Tensor).clipByValue(-1, 1).transpose([1, 2, 0]) as tf.Tensor3D |
| 124 | + // spectrogramToCanvas(imgInput, state.logMelSpecCanvas) |
| 125 | + // signal2logmelInput.signals.dispose(); |
| 126 | + // (logmel as tf.Tensor).dispose() |
| 127 | + // }) |
| 128 | + // }) |
| 129 | + // .catch(fatalError) |
| 130 | + |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +function startListenLoop(mic: any): void { |
| 135 | + state.renderIntervalID = window.setInterval( |
| 136 | + () => { |
| 137 | + mic.capture() |
| 138 | + .then(micData => handleMicrophoneInput(micData)) |
| 139 | + .catch(fatalError) |
| 140 | + }, |
| 141 | + state.microphoneCaptureIntervalMs) |
| 142 | +} |
| 143 | + |
| 144 | + |
| 145 | +async function main() { |
| 146 | + // state.signalCanvas = createCanvas("signal-canvas") |
| 147 | + // state.dBSpecCanvas = createCanvas("decibel-spectrogram-canvas") |
| 148 | + state.logMelSpecCanvas = createElement("logscale-melspectrogram-canvas", "canvas") as HTMLCanvasElement |
| 149 | + state.predictionLabel = createElement("prediction-label", "h2") |
| 150 | + |
| 151 | + state.microphoneConfig.columnTruncateLength = Math.round( |
| 152 | + (state.microphoneConfig.fftSize / 2 + 1) |
| 153 | + / (state.microphoneConfig.sampleRateHz/16000)) |
| 154 | + |
| 155 | + await tf.setBackend(state.TFBackend) |
| 156 | + console.log("initialized tensorflow.js backend:", tf.getBackend()) |
| 157 | + |
| 158 | + console.log("requesting access to an input device") |
| 159 | + const mic = await tf.data.microphone(state.microphoneConfig) |
| 160 | + console.log("got permission to use input device", (mic as any).stream.id) |
| 161 | + |
| 162 | + const graph1 = await tf.loadGraphModel("./static/tfjs/spec2logmel/model.json") |
| 163 | + console.log("tf graph1 loaded") |
| 164 | + state.spec2logmel = graph1 |
| 165 | + |
| 166 | + // const graph2 = await tf.loadGraphModel("./static/tfjs/signals2logmel/model.json") |
| 167 | + // console.log("tf graph2 loaded") |
| 168 | + // state.signals2logmel = graph2 |
| 169 | + |
| 170 | + const graph3 = await tf.loadLayersModel("./static/tfjs/xvector_mv/model.json") |
| 171 | + console.log("tf graph3 loaded") |
| 172 | + state.xvector = graph3 |
| 173 | + state.xvector.summary() |
| 174 | + |
| 175 | + const int2label = await tf.util.fetch("./static/tfjs/xvector_mv/int2label.json") |
| 176 | + state.int2label = await int2label.json() |
| 177 | + |
| 178 | + console.log("starting app") |
| 179 | + state.running = true |
| 180 | + startListenLoop(mic) |
| 181 | + |
| 182 | +} |
| 183 | + |
| 184 | + |
| 185 | +document.addEventListener("DOMContentLoaded", () => main().catch(fatalError)) |
0 commit comments