DCSOlympus/frontend/react/src/radio/microphonehandler.ts
2024-09-03 20:19:11 +02:00

35 lines
956 B
TypeScript

import { SRSRadioSetting } from "../interfaces";
import { AudioPacket } from "./audiopacket";
import { CapturePipeline } from "./capturepipeline";
export class MicrophoneHandler {
#socket: WebSocket;
#setting: SRSRadioSetting;
constructor(socket, setting) {
this.#socket = socket;
this.#setting = setting;
console.log("Starting microphone handler");
const pipeline = new CapturePipeline();
navigator.mediaDevices.enumerateDevices()
.then(function(devices) {
devices.forEach(function(device) {
console.log(device.kind + ": " + device.label +
" id = " + device.deviceId);
});
})
pipeline.connect().then(() => {
pipeline.onencoded = (data) => {
let buffer = new ArrayBuffer(data.byteLength);
data.copyTo(buffer);
let packet = new AudioPacket(new Uint8Array(buffer), this.#setting);
this.#socket.send(packet.getArray());
}
})
}
}