refactor: modularize CRC mocking and implement a persistent Ogg stream for web audio playback

This commit is contained in:
baharsah
2026-05-13 01:12:11 +07:00
parent c911b06b95
commit 44ac346c21
6 changed files with 77 additions and 44 deletions

View File

@@ -94,35 +94,6 @@ export async function startRecording(client: Client, channel: VoiceChannel): Pro
try {
const packetFilter = new PacketFilter(10);
// Mock node-crc to provide pure JS implementation and bypass native build issues
const CRC_TABLE = new Uint32Array(256);
for (let i = 0; i < 256; i++) {
let r = i << 24;
for (let j = 0; j < 8; j++) {
r = (r & 0x80000000) !== 0 ? ((r << 1) ^ 0x04c11db7) : (r << 1);
}
CRC_TABLE[i] = (r >>> 0);
}
const Module = require('module');
const originalRequire = Module.prototype.require;
Module.prototype.require = function (id: string) {
if (id === 'node-crc') {
return {
crc: function (width: number, reflectIn: boolean, poly: number, init: number, refOut: boolean, xorOut: number, unk1: number, unk2: number, buffer: Buffer) {
let crc = 0;
for (let i = 0; i < buffer.length; i++) {
crc = ((crc << 8) >>> 0) ^ CRC_TABLE[((crc >>> 24) ^ buffer[i]) & 0xff];
crc >>>= 0;
}
const result = Buffer.alloc(4);
result.writeUInt32BE(crc, 0);
return result;
}
};
}
return originalRequire.apply(this, arguments);
};
const oggStream = new prism.opus.OggLogicalBitstream({
opusHead: new prism.opus.OpusHead({
@@ -139,10 +110,10 @@ export async function startRecording(client: Client, channel: VoiceChannel): Pro
// Pipe: audioStream -> packetFilter -> oggStream -> out
audioStream.pipe(packetFilter).pipe(oggStream).pipe(out);
// Also forward to web listeners
oggStream.on('data', (chunk) => {
if ((global as any).broadcastToWeb) {
(global as any).broadcastToWeb(chunk);
// Forward raw Opus packets to the web shared Ogg stream
packetFilter.on('data', (chunk) => {
if ((global as any).broadcastOpusToWeb) {
(global as any).broadcastOpusToWeb(chunk);
}
});