| 1 | class PCMProcessor extends AudioWorkletProcessor { |
| 2 | constructor() { |
| 3 | super(); |
| 4 | this.buffer = new Float32Array(24000 * 30); // Pre-allocate buffer for ~30 seconds at 24kHz |
| 5 | this.writeIndex = 0; |
| 6 | this.readIndex = 0; |
| 7 | this.pendingBytes = new Uint8Array(0); // Buffer for incomplete samples |
| 8 | this.volume = 1.0; // Default volume (1.0 = 100%, 0.5 = 50%, etc.) |
| 9 | this.port.onmessage = (event) => { |
| 10 | if (event.data.pcmData) { |
| 11 | // Combine any pending bytes with new data |
| 12 | const newData = new Uint8Array(event.data.pcmData); |
| 13 | const combined = new Uint8Array(this.pendingBytes.length + newData.length); |
| 14 | combined.set(this.pendingBytes); |
| 15 | combined.set(newData, this.pendingBytes.length); |
| 16 | |
| 17 | // Calculate how many complete 16-bit samples we have |
| 18 | const completeSamples = Math.floor(combined.length / 2); |
| 19 | const bytesToProcess = completeSamples * 2; |
| 20 | |
| 21 | if (completeSamples > 0) { |
| 22 | // Process complete samples |
| 23 | const int16Array = new Int16Array(combined.buffer.slice(0, bytesToProcess)); |
| 24 | |
| 25 | // Write directly to circular buffer |
| 26 | for (let i = 0; i < int16Array.length; i++) { |
| 27 | // Expand buffer if needed |
| 28 | if (this.writeIndex >= this.buffer.length) { |
| 29 | const newBuffer = new Float32Array(this.buffer.length * 2); |
| 30 | // Copy existing data maintaining order |
| 31 | let sourceIndex = this.readIndex; |
| 32 | let targetIndex = 0; |
| 33 | while (sourceIndex !== this.writeIndex) { |
| 34 | newBuffer[targetIndex++] = this.buffer[sourceIndex]; |
| 35 | sourceIndex = (sourceIndex + 1) % this.buffer.length; |
| 36 | } |
| 37 | this.buffer = newBuffer; |
| 38 | this.readIndex = 0; |
| 39 | this.writeIndex = targetIndex; |
| 40 | } |
| 41 | |
| 42 | this.buffer[this.writeIndex] = int16Array[i] / 32768.0; // Convert 16-bit to float |
| 43 | this.writeIndex = (this.writeIndex + 1) % this.buffer.length; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // Store any remaining incomplete bytes |
| 48 | if (combined.length > bytesToProcess) { |
| 49 | this.pendingBytes = combined.slice(bytesToProcess); |
| 50 | } else { |
| 51 | this.pendingBytes = new Uint8Array(0); |
| 52 | } |
| 53 | } else if (event.data.volume !== undefined) { |
| 54 | // Set volume (0.0 to 1.0, can go higher for amplification) |
| 55 | this.volume = Math.max(0, event.data.volume); |
| 56 | } |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | process(inputs, outputs, parameters) { |
| 61 | const output = outputs[0]; |
| 62 | if (output.length > 0 && this.readIndex !== this.writeIndex) { |
| 63 | const channelData = output[0]; |
| 64 | for (let i = 0; i < channelData.length && this.readIndex !== this.writeIndex; i++) { |
| 65 | channelData[i] = this.buffer[this.readIndex] * this.volume; |
| 66 | this.readIndex = (this.readIndex + 1) % this.buffer.length; |
| 67 | } |
| 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | registerProcessor('pcm-processor', PCMProcessor); |