File size: 2,863 Bytes
27b879b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/** @module worklet */

// Called with `audioWorker.addModule('worklet.js')`
// Defines the following global variables:
// - sampleRate
// - registerProcessor

/**
 * The `Processor` class is an AudioWorkletProcessor that resamples the input audio to a target sample rate.
 */
class Processor extends AudioWorkletProcessor {
    /**
     * @param {object} options - The options object.
     * @param {object} options.processorOptions - The processor options object.
     * @param {number} options.processorOptions.targetSampleRate - The target sample rate.
     */
    constructor(options) {
        super(options);
        this.targetSampleRate = options.processorOptions.targetSampleRate;
        this.inputBuffer = new Float32Array(this.inputFrameSize);
        this.inputBufferSize = 0;
        this.outputBuffer = new Float32Array(this.targetFrameSize);
    }

    /**
     * The size of the input frame.
     * @type {number}
     */
    get inputFrameSize() {
        return Math.round(sampleRate / 50);
    }

    /**
     * The size of the target frame.
     * @type {number}
     */
    get targetFrameSize() {
        return Math.round(this.targetSampleRate / 50);
    }

    /**
     * Flushes the input buffer to the output buffer, resampling the audio.
     * Then sends the output buffer to the main thread using the port.
     */
    async flush() {
        const ratio = sampleRate / this.targetSampleRate;
        this.outputBuffer.fill(0);
        for (let i = 0; i < this.targetFrameSize; i++) {
            const index = i * ratio;
            const left = Math.floor(index);
            const right = Math.min(left + 1, this.targetFrameSize - 1);
            const weight = index - left;
            this.outputBuffer[i] = this.inputBuffer[left] * (1 - weight) + this.inputBuffer[right] * weight;
        }
        await this.port.postMessage(this.outputBuffer);
    }

    /**
     * Pushes audio to the input buffer.
     * @param {Float32Array} inputArray - The input audio.
     */
    pushAudio(inputArray) {
        const inputLength = inputArray.length;
        const remainingLength = this.inputFrameSize - this.inputBufferSize;
        if (inputLength < remainingLength) {
            this.inputBuffer.set(inputArray, this.inputBufferSize);
            this.inputBufferSize += inputLength;
            return;
        }
        this.inputBuffer.set(inputArray.subarray(0, remainingLength), this.inputBufferSize);
        this.flush();
        this.inputBufferSize = 0;
        this.pushAudio(inputArray.subarray(remainingLength));
    }

    /**
     * Processes the input audio (the main worklet loop).
     */
    process(inputArray, outputArray, parameters) {
        this.pushAudio(inputArray[0][0]);
        return true;
    }
}

// Registers the processor with the name "hey-buddy".
registerProcessor("hey-buddy", Processor);