diff --git a/app.js b/app.js index 54139ff..ebbee56 100644 --- a/app.js +++ b/app.js @@ -86,38 +86,32 @@ async function startBroadcast() { try { audioContext = new (window.AudioContext || window.webkitAudioContext)(); - - // Load and register the audio worklet processor - await audioContext.audioWorklet.addModule('broadcaster-processor.js'); - micStream = await navigator.mediaDevices.getUserMedia({ audio: { deviceId: currentDeviceId ? { exact: currentDeviceId } : undefined }, }); - const source = audioContext.createMediaStreamSource(micStream); + const processor = audioContext.createScriptProcessor(4096, 1, 1); - // Create AudioWorkletNode - const broadcasterNode = new AudioWorkletNode(audioContext, 'broadcaster-processor'); - source.connect(broadcasterNode); + source.connect(processor); + processor.connect(audioContext.destination); - // Handle audio data - broadcasterNode.port.onmessage = (event) => { - const buffer = event.data; + processor.onaudioprocess = (event) => { + const audioData = event.inputBuffer.getChannelData(0); + const buffer = b4a.from(new Float32Array(audioData).buffer); + + // Send audio data to all connections for (const conn of conns) { conn.write(buffer); } }; - broadcasterNode.connect(audioContext.destination); // Optional monitoring - isBroadcasting = true; - console.log("Broadcasting started with AudioWorklet."); + console.log("Broadcasting started."); } catch (err) { - console.error("Error accessing microphone or setting up broadcast:", err); + console.error("Error accessing microphone:", err); } } - // Function to stop broadcasting and clean up resources function stopBroadcast() { if (!isBroadcasting) return; @@ -266,4 +260,4 @@ async function joinStation() { if (modalInstance) { modalInstance.hide(); } -} \ No newline at end of file +} diff --git a/broadcaster-processor.js b/broadcaster-processor.js deleted file mode 100644 index 7b7bdfb..0000000 --- a/broadcaster-processor.js +++ /dev/null @@ -1,19 +0,0 @@ -class BroadcasterProcessor extends AudioWorkletProcessor { - process(inputs, outputs) { - const input = inputs[0]; - const output = outputs[0]; - - if (input && output) { - for (let channel = 0; channel < input.length; ++channel) { - const inputChannel = input[channel]; - const outputChannel = output[channel]; - for (let i = 0; i < inputChannel.length; ++i) { - outputChannel[i] = inputChannel[i]; - } - } - } - return true; // Keep the processor alive - } -} - -registerProcessor('broadcaster-processor', BroadcasterProcessor); diff --git a/index.html b/index.html index dbef9ac..3f8c900 100644 --- a/index.html +++ b/index.html @@ -24,18 +24,6 @@ } -