add voice.ts todo
This commit is contained in:
@@ -11,7 +11,9 @@
|
||||
"tauri": "tauri",
|
||||
"buildsite": "vite build --outDir dist",
|
||||
"updatePlugin": "tsc public/pluginApi.ts",
|
||||
"runserver": "node server/node/server.cjs"
|
||||
"runserver": "node server/node/server.cjs",
|
||||
"sync": "node electron/sync",
|
||||
"electron": "node electron/dist/electron"
|
||||
},
|
||||
"dependencies": {
|
||||
"@adobe/css-tools": "4.3.2",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { get } from "svelte/store"
|
||||
import { get, writable, type Writable } from "svelte/store"
|
||||
import type { Database, Message } from "./storage/database"
|
||||
import { DataBase } from "./storage/database"
|
||||
import { selectedCharID } from "./stores"
|
||||
@@ -371,4 +371,8 @@ export function getCurrentCharacter(){
|
||||
const db = get(DataBase)
|
||||
const selectedChar = get(selectedCharID)
|
||||
return db.characters[selectedChar]
|
||||
}
|
||||
|
||||
export function toState<T>(t:T):Writable<T>{
|
||||
return writable(t)
|
||||
}
|
||||
47
src/ts/voice.ts
Normal file
47
src/ts/voice.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { sleep } from "./util";
|
||||
|
||||
class voiceDetector{
|
||||
|
||||
private userMedia: MediaStream;
|
||||
private audioContext: AudioContext;
|
||||
private analyser: AnalyserNode;
|
||||
private mediaRecorder: MediaRecorder;
|
||||
private intervalID: number;
|
||||
private audioChunks: {
|
||||
data:Blob,
|
||||
time: number
|
||||
}[];
|
||||
|
||||
|
||||
async init() {
|
||||
this.userMedia = await navigator.mediaDevices.getUserMedia({ audio: true });
|
||||
this.audioContext = new AudioContext();
|
||||
this.mediaRecorder = new MediaRecorder(this.userMedia);
|
||||
this.analyser = this.audioContext.createAnalyser();
|
||||
const source = this.audioContext.createMediaStreamSource(this.userMedia);
|
||||
source.connect(this.analyser);
|
||||
this.audioChunks = [];
|
||||
this.mediaRecorder.ondataavailable = (event) => {
|
||||
this.audioChunks.push({
|
||||
data: event.data,
|
||||
time: Date.now()
|
||||
});
|
||||
};
|
||||
this.mediaRecorder.start();
|
||||
//analyze the audio volume every 100ms
|
||||
let start = Date.now();
|
||||
this.analyser.fftSize = 2048;
|
||||
this.intervalID = setInterval(() => {
|
||||
const dataArray = new Uint8Array(this.analyser.frequencyBinCount);
|
||||
this.analyser.getByteTimeDomainData(dataArray);
|
||||
const volume = dataArray.reduce((acc, val) => acc + val, 0) / dataArray.length;
|
||||
console.log(volume);
|
||||
if (Date.now() - start > 10000) {
|
||||
this.mediaRecorder.stop();
|
||||
clearInterval(this.intervalID);
|
||||
}
|
||||
}, 100) as any as number;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user