more changes
This commit is contained in:
parent
ee63bb0ac1
commit
b08061d080
2 changed files with 33 additions and 8 deletions
|
|
@ -1,18 +1,30 @@
|
|||
import useMitt from "@/modules/mitt";
|
||||
import { useIpc } from '@/modules/ipc';
|
||||
import { showRecIcon, hideRecIcon } from "./animations";
|
||||
// import useMediaRecorder from "./mediarecorder";
|
||||
import { onMounted, onUnmounted, ref, Ref } from "vue";
|
||||
import keyboardNameMap from "../keyBoardMaps/keyboardNameMap";
|
||||
import { renderMessage, clientMessage } from '@/modules/message';
|
||||
import { AudioData } from "@/types";
|
||||
|
||||
|
||||
function arrayBufferToBase64 (buffer: any) {
|
||||
var binary = '';
|
||||
var bytes = new Uint8Array(buffer);
|
||||
var len = bytes.byteLength;
|
||||
for (var i = 0; i < len; i++) {
|
||||
binary += String.fromCharCode( bytes[ i ] );
|
||||
}
|
||||
return window.btoa( binary );
|
||||
}
|
||||
|
||||
|
||||
|
||||
export default function useAudioInputController (typing: Ref) {
|
||||
|
||||
let mediaRecorder: any;
|
||||
let buffer = '';
|
||||
let sampleRate = 44100; // default
|
||||
let channels = 1;
|
||||
|
||||
// For sending messages.
|
||||
const { post, invoke } = useIpc();
|
||||
|
|
@ -39,10 +51,17 @@ export default function useAudioInputController (typing: Ref) {
|
|||
processor.connect(context.destination);
|
||||
|
||||
processor.onaudioprocess = (e: AudioProcessingEvent) => {
|
||||
|
||||
if (recording.value) {
|
||||
const arr = new Uint16Array(e.inputBuffer);
|
||||
buffer += String.fromCharCode.apply(null, new Float32Array(e.inputBuffer));
|
||||
|
||||
const data = e.inputBuffer.getChannelData(0).buffer;
|
||||
|
||||
buffer += arrayBufferToBase64(data);
|
||||
|
||||
sampleRate = e.inputBuffer.sampleRate;
|
||||
|
||||
} else buffer = '';
|
||||
|
||||
};
|
||||
|
||||
});
|
||||
|
|
@ -73,7 +92,6 @@ export default function useAudioInputController (typing: Ref) {
|
|||
|
||||
// get audio from buffer then stop recording
|
||||
console.log("INPT:Stopping record.")
|
||||
const audio = buffer;
|
||||
recording.value = false;
|
||||
hideRecIcon()
|
||||
|
||||
|
|
@ -85,10 +103,17 @@ export default function useAudioInputController (typing: Ref) {
|
|||
"sf"
|
||||
);
|
||||
|
||||
emitter.emit("self-message", message);
|
||||
emitter.emit("self-message", message);
|
||||
|
||||
console.log(`Posting audio: ${audio}`);
|
||||
console.log(typeof audio);
|
||||
// send audio to backend
|
||||
const audio: AudioData = {
|
||||
content: buffer,
|
||||
channels: channels,
|
||||
fs: sampleRate
|
||||
}
|
||||
|
||||
console.log("INPT:Sending audio to backend");
|
||||
post("client-message", clientMessage("", audio, message.uid));
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ export interface RenderMessage {
|
|||
}
|
||||
|
||||
export interface AudioData {
|
||||
hex: string;
|
||||
content: string;
|
||||
channels: number;
|
||||
fs: number;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue