mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 12:02:50 +08:00
feat: add Android gateway preview and refactoring plan
This commit is contained in:
1 parent
9e2b9bf564
commit
aeaeb15965
73 files changed
+4347
-24
No files matched your search
@@ -0,0 +1,56 @@
|
||||
import { createRequire } from "node:module";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
type NativeCodec = {
|
||||
encode(pcm: Buffer): Buffer;
|
||||
decode(data: Buffer): Buffer;
|
||||
};
|
||||
|
||||
type ScriptCodec = {
|
||||
encode(pcm: Buffer, frameSize: number): Buffer | Uint8Array;
|
||||
decode(data: Buffer): Buffer | Uint8Array;
|
||||
delete(): void;
|
||||
};
|
||||
|
||||
type OpusScriptModule = {
|
||||
new (sampleRate: number, channels: number, application: number): ScriptCodec;
|
||||
Application: { AUDIO: number };
|
||||
};
|
||||
|
||||
/** Keep the existing native codec on servers; the Android bundle uses the WASM codec. */
|
||||
export class OpusEncoder {
|
||||
private readonly codec: NativeCodec | ScriptCodec;
|
||||
private readonly channels: number;
|
||||
private readonly mobile: boolean;
|
||||
|
||||
constructor(sampleRate: number, channels: number) {
|
||||
this.channels = channels;
|
||||
this.mobile = process.env.WEBSPEAK_MOBILE === "1";
|
||||
if (this.mobile) {
|
||||
const OpusScript = require("opusscript") as OpusScriptModule;
|
||||
this.codec = new OpusScript(sampleRate, channels, OpusScript.Application.AUDIO);
|
||||
} else {
|
||||
const { OpusEncoder: NativeOpusEncoder } = require("@discordjs/opus") as {
|
||||
OpusEncoder: new (rate: number, channelCount: number) => NativeCodec;
|
||||
};
|
||||
this.codec = new NativeOpusEncoder(sampleRate, channels);
|
||||
}
|
||||
}
|
||||
|
||||
encode(pcm: Buffer): Buffer {
|
||||
const encoded = this.mobile
|
||||
? (this.codec as ScriptCodec).encode(pcm, pcm.length / (2 * this.channels))
|
||||
: (this.codec as NativeCodec).encode(pcm);
|
||||
return Buffer.isBuffer(encoded) ? encoded : Buffer.from(encoded);
|
||||
}
|
||||
|
||||
decode(data: Buffer): Buffer {
|
||||
const decoded = this.codec.decode(data);
|
||||
return Buffer.isBuffer(decoded) ? decoded : Buffer.from(decoded);
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
if (this.mobile) (this.codec as ScriptCodec).delete();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user