()
| 9 | |
| 10 | class MoshiProcessor extends AudioWorkletProcessor { |
| 11 | constructor() { |
| 12 | super(); |
| 13 | console.log("Moshi processor lives", currentFrame, sampleRate); |
| 14 | console.log(currentTime); |
| 15 | |
| 16 | // Buffer length definitions |
| 17 | let frameSize = asSamples(80); |
| 18 | // initialBufferSamples: we wait to have at least that many samples before starting to play |
| 19 | this.initialBufferSamples = 1 * frameSize; |
| 20 | // once we have enough samples, we further wait that long before starting to play. |
| 21 | // This allows to have buffer lengths that are not a multiple of frameSize. |
| 22 | this.partialBufferSamples = asSamples(10); |
| 23 | // If the buffer length goes over that many, we will drop the oldest packets until |
| 24 | // we reach back initialBufferSamples + partialBufferSamples. |
| 25 | this.maxBufferSamples = asSamples(10); |
| 26 | // increments |
| 27 | this.partialBufferIncrement = asSamples(5); |
| 28 | this.maxPartialWithIncrements = asSamples(80); |
| 29 | this.maxBufferSamplesIncrement = asSamples(5); |
| 30 | this.maxMaxBufferWithIncrements = asSamples(80); |
| 31 | |
| 32 | // State and metrics |
| 33 | this.initState(); |
| 34 | |
| 35 | this.port.onmessage = (event) => { |
| 36 | if (event.data.type == "reset") { |
| 37 | console.log("Reset audio processor state."); |
| 38 | this.initState(); |
| 39 | return; |
| 40 | } |
| 41 | let frame = event.data.frame; |
| 42 | this.frames.push(frame); |
| 43 | if (this.currentSamples() >= this.initialBufferSamples && !this.started) { |
| 44 | this.start(); |
| 45 | } |
| 46 | if (this.pidx < 20) { |
| 47 | console.log(this.timestamp(), "Got packet", this.pidx++, asMs(this.currentSamples()), asMs(frame.length)) |
| 48 | |
| 49 | } |
| 50 | if (this.currentSamples() >= this.totalMaxBufferSamples()) { |
| 51 | console.log(this.timestamp(), "Dropping packets", asMs(this.currentSamples()), asMs(this.totalMaxBufferSamples())); |
| 52 | let target = this.initialBufferSamples + this.partialBufferSamples |
| 53 | while (this.currentSamples() > (this.initialBufferSamples + this.partialBufferSamples)) { |
| 54 | let first = this.frames[0]; |
| 55 | let to_remove = this.currentSamples() - target; |
| 56 | to_remove = Math.min(first.length - this.offsetInFirstBuffer, to_remove); |
| 57 | this.offsetInFirstBuffer += to_remove; |
| 58 | this.timeInStream += to_remove / sampleRate; |
| 59 | if (this.offsetInFirstBuffer == first.length) { |
| 60 | this.frames.shift(); |
| 61 | this.offsetInFirstBuffer = 0; |
| 62 | } |
| 63 | } |
| 64 | console.log(this.timestamp(), "Packet dropped", asMs(this.currentSamples())); |
| 65 | this.maxBufferSamples += this.maxBufferSamplesIncrement; |
| 66 | this.maxBufferSamples = Math.min(this.maxMaxBufferWithIncrements, this.maxBufferSamples); |
| 67 | console.log("Increased maxBuffer to", asMs(this.maxBufferSamples)); |
| 68 | } |
nothing calls this directly
no test coverage detected