72 lines
1.7 KiB
Vue
72 lines
1.7 KiB
Vue
<template>
|
|
<div>
|
|
Hello from the mic
|
|
</div>
|
|
</template>
|
|
|
|
<script>
|
|
import { defineComponent } from 'vue';
|
|
export default defineComponent({
|
|
name: 'Microphone',
|
|
setup() {
|
|
const bufferLen = 4096;
|
|
const numChannels = 1;
|
|
window.AudioContext = window.AudioContext || window.webkitAudioContext;
|
|
const audioContext = new AudioContext();
|
|
|
|
let result;
|
|
const getCwasm = async () => {
|
|
await import('../../../crate/pkg').then(async module => {
|
|
result = await module.add(1, 2);
|
|
console.log(result);
|
|
});
|
|
}
|
|
getCwasm();
|
|
|
|
function handleStream(stream) {
|
|
const source = audioContext.createMediaStreamSource(stream);
|
|
const context = source.context;
|
|
// NOTE might come in handy for VAD
|
|
// const sampleRate = context.sampleRate;
|
|
|
|
const node = context.createScriptProcessor.call(
|
|
context,
|
|
bufferLen,
|
|
numChannels,
|
|
numChannels
|
|
);
|
|
|
|
node.onaudioprocess = (e) => {
|
|
const inputBuffer = new Float32Array(bufferLen);
|
|
|
|
e.inputBuffer.copyFromChannel(inputBuffer, 0);
|
|
|
|
// const int16 = new Int16Array(inputBuffer.buffer);
|
|
|
|
// TODO add handle audio stream to main process?
|
|
// window.postMessage(
|
|
// {
|
|
// myTypeField: "handle-audio-stream",
|
|
// audioData: int16,
|
|
// },
|
|
// "*"
|
|
// );
|
|
};
|
|
source.connect(node);
|
|
node.connect(context.destination);
|
|
}
|
|
|
|
navigator.mediaDevices
|
|
.getUserMedia({ audio: true })
|
|
.then((stream) => {
|
|
handleStream(stream)
|
|
})
|
|
.catch((error) => {
|
|
throw error;
|
|
});
|
|
|
|
|
|
}
|
|
})
|
|
|
|
</script>
|