-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
119 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
class AudioProcessor extends AudioWorkletProcessor { | ||
constructor(nodeOptions) { | ||
super(); | ||
this.buffer = new CircularBuffer(2048); | ||
this.port.onmessage = event => { | ||
for (const value of event.data) { | ||
this.buffer.write(value); | ||
} | ||
} | ||
} | ||
|
||
process(inputs, outputs, parameters) { | ||
outputs[0][0].set(this.buffer.readSlice(outputs[0][0].length)); | ||
return true; | ||
} | ||
} | ||
|
||
registerProcessor("audio-processor", AudioProcessor); | ||
|
||
class CircularBuffer { | ||
constructor(size) { | ||
this.buffer = new Float32Array(size); | ||
this.writeCursor = Math.floor(size / 2); | ||
this.readCursor = 0; | ||
} | ||
|
||
write(value) { | ||
this.buffer[this.writeCursor] = value; | ||
this.writeCursor = (this.writeCursor + 1) % this.buffer.length; | ||
} | ||
|
||
readSlice(length) { | ||
let end = this.readCursor + length; | ||
let result = null; | ||
if (end < this.buffer.length) { | ||
result = this.buffer.slice(this.readCursor, end); | ||
} else { | ||
end = end - this.buffer.length; | ||
result = new Float32Array([ | ||
...this.buffer.slice(this.readCursor), | ||
...this.buffer.slice(0, end) | ||
]); | ||
} | ||
this.readCursor = end; | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const BUFFER_SIZE = 128; | ||
|
||
let audioProcessorNode = null; | ||
const buffer = new Float32Array(BUFFER_SIZE); | ||
let bufferIndex = 0; | ||
|
||
async function startAudio() { | ||
if (audioProcessorNode) return; | ||
const context = new AudioContext({ sampleRate: 44100 }); | ||
await context.audioWorklet.addModule('./audio-processor.js'); | ||
audioProcessorNode = new AudioWorkletNode(context, 'audio-processor'); | ||
audioProcessorNode.connect(context.destination); | ||
} | ||
|
||
addEventListener("click", startAudio); | ||
addEventListener("keydown", startAudio); | ||
addEventListener("visibilitychange", () => { | ||
if (audioProcessorNode && document.visibilityState !== 'visible') { | ||
audioProcessorNode.port.postMessage(new Float32Array(BUFFER_SIZE)) | ||
} | ||
}); | ||
|
||
export function pushAudioBuffer(byte) { | ||
if (audioProcessorNode == null || document.visibilityState !== 'visible') return; | ||
buffer[bufferIndex] = (byte / 255) - 0.5; | ||
bufferIndex += 1; | ||
if (bufferIndex === BUFFER_SIZE) { | ||
bufferIndex = 0; | ||
audioProcessorNode.port.postMessage(buffer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters