diff --git a/app.js b/app.js index ebbee56..54139ff 100644 --- a/app.js +++ b/app.js @@ -86,32 +86,38 @@ 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); - source.connect(processor); - processor.connect(audioContext.destination); + // Create AudioWorkletNode + const broadcasterNode = new AudioWorkletNode(audioContext, 'broadcaster-processor'); + source.connect(broadcasterNode); - processor.onaudioprocess = (event) => { - const audioData = event.inputBuffer.getChannelData(0); - const buffer = b4a.from(new Float32Array(audioData).buffer); - - // Send audio data to all connections + // Handle audio data + broadcasterNode.port.onmessage = (event) => { + const buffer = event.data; for (const conn of conns) { conn.write(buffer); } }; + broadcasterNode.connect(audioContext.destination); // Optional monitoring + isBroadcasting = true; - console.log("Broadcasting started."); + console.log("Broadcasting started with AudioWorklet."); } catch (err) { - console.error("Error accessing microphone:", err); + console.error("Error accessing microphone or setting up broadcast:", err); } } + // Function to stop broadcasting and clean up resources function stopBroadcast() { if (!isBroadcasting) return; @@ -260,4 +266,4 @@ async function joinStation() { if (modalInstance) { modalInstance.hide(); } -} +} \ No newline at end of file diff --git a/broadcaster-processor.js b/broadcaster-processor.js new file mode 100644 index 0000000..7b7bdfb --- /dev/null +++ b/broadcaster-processor.js @@ -0,0 +1,19 @@ +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 3f8c900..dbef9ac 100644 --- a/index.html +++ b/index.html @@ -24,6 +24,18 @@ } +
@@ -31,6 +43,7 @@

pearCast

+