interface microphone component with rust wasm
This commit is contained in:
parent
37cb3d25b6
commit
fe5a5aae93
7 changed files with 1275 additions and 25 deletions
|
|
@ -8,20 +8,20 @@
|
|||
import { defineComponent } from 'vue';
|
||||
export default defineComponent({
|
||||
name: 'Microphone',
|
||||
setup() {
|
||||
async setup() {
|
||||
const bufferLen = 4096;
|
||||
const numChannels = 1;
|
||||
window.AudioContext = window.AudioContext || window.webkitAudioContext;
|
||||
const audioContext = new AudioContext();
|
||||
|
||||
let result;
|
||||
let cWasm;
|
||||
const getCwasm = async () => {
|
||||
await import('../../../crate/pkg').then(async module => {
|
||||
result = await module.add(1, 2);
|
||||
console.log(result);
|
||||
cWasm = module;
|
||||
});
|
||||
}
|
||||
getCwasm();
|
||||
await getCwasm();
|
||||
|
||||
function handleStream(stream) {
|
||||
const source = audioContext.createMediaStreamSource(stream);
|
||||
|
|
@ -36,12 +36,12 @@
|
|||
numChannels
|
||||
);
|
||||
|
||||
node.onaudioprocess = (e) => {
|
||||
const inputBuffer = new Float32Array(bufferLen);
|
||||
|
||||
e.inputBuffer.copyFromChannel(inputBuffer, 0);
|
||||
|
||||
// const int16 = new Int16Array(inputBuffer.buffer);
|
||||
node.onaudioprocess = async e => {
|
||||
const inputBuff = new Float32Array(bufferLen);
|
||||
// get data from audio event and copy into inputBuff
|
||||
e.inputBuffer.copyFromChannel(inputBuff, 0);
|
||||
const int16 = new Int16Array(inputBuff.buffer);
|
||||
result = await cWasm.foo(int16);
|
||||
|
||||
// TODO add handle audio stream to main process?
|
||||
// window.postMessage(
|
||||
|
|
@ -56,16 +56,14 @@
|
|||
node.connect(context.destination);
|
||||
}
|
||||
|
||||
navigator.mediaDevices
|
||||
.getUserMedia({ audio: true })
|
||||
.then((stream) => {
|
||||
handleStream(stream)
|
||||
})
|
||||
.catch((error) => {
|
||||
throw error;
|
||||
});
|
||||
|
||||
navigator.mediaDevices
|
||||
.getUserMedia({ audio: true })
|
||||
.then(s => {
|
||||
handleStream(s);
|
||||
}).catch(error => {
|
||||
throw error;
|
||||
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue