feat: add web interface and WebSocket server for real-time audio transmission to Discord

This commit is contained in:
baharsah
2026-05-13 00:32:27 +07:00
parent 1a5449f16d
commit 18cf941da0
7 changed files with 405 additions and 2 deletions

47
src/player.ts Normal file
View File

@@ -0,0 +1,47 @@
import {
createAudioPlayer,
createAudioResource,
AudioPlayerStatus,
VoiceConnection,
AudioPlayer,
StreamType
} from "@discordjs/voice";
import { Readable } from "stream";
export class DiscordPlayer {
private player: AudioPlayer;
private connection: VoiceConnection | null = null;
constructor() {
this.player = createAudioPlayer();
this.player.on(AudioPlayerStatus.Playing, () => {
console.log("[player] Audio player is now playing!");
});
this.player.on("error", error => {
console.error(`[player] Error: ${error.message}`);
});
}
public setConnection(connection: VoiceConnection) {
this.connection = connection;
this.connection.subscribe(this.player);
}
public playStream(stream: Readable) {
// We assume the stream is Opus or PCM.
// For MediaRecorder (webm/opus), we might need to parse it.
// But let's start with a simple resource.
const resource = createAudioResource(stream, {
inputType: StreamType.WebmOpus,
});
this.player.play(resource);
}
public stop() {
this.player.stop();
}
}
export const discordPlayer = new DiscordPlayer();