@ain1084/audio-worklet-stream 1.0.5 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/filler-frame-buffer-context.d.ts +30 -0
- package/dist/{frame-buffer/buffer-config.js → filler-frame-buffer-context.js} +5 -21
- package/dist/filler-frame-buffer-context.js.map +1 -0
- package/dist/{frame-buffer/buffer-filler.d.ts → frame-buffer-filler.d.ts} +2 -2
- package/dist/frame-buffer-filler.js +2 -0
- package/dist/frame-buffer-filler.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/output-stream-node.d.ts +4 -4
- package/dist/output-stream-node.js +9 -9
- package/dist/output-stream-node.js.map +1 -1
- package/dist/output-stream-processor.js +4 -4
- package/dist/output-stream-processor.js.map +1 -1
- package/dist/stream-node-factory.d.ts +4 -4
- package/dist/stream-node-factory.js +9 -8
- package/dist/stream-node-factory.js.map +1 -1
- package/dist/write-strategy/manual-strategy.d.ts +3 -4
- package/dist/write-strategy/manual-strategy.js +4 -4
- package/dist/write-strategy/manual-strategy.js.map +1 -1
- package/dist/write-strategy/timed-strategy.d.ts +4 -4
- package/dist/write-strategy/timed-strategy.js +5 -5
- package/dist/write-strategy/timed-strategy.js.map +1 -1
- package/dist/write-strategy/worker/message.d.ts +3 -3
- package/dist/write-strategy/worker/worker.d.ts +2 -2
- package/dist/write-strategy/worker/worker.js +8 -8
- package/dist/write-strategy/worker/worker.js.map +1 -1
- package/dist/write-strategy/worker-strategy.d.ts +3 -3
- package/dist/write-strategy/worker-strategy.js +6 -6
- package/dist/write-strategy/worker-strategy.js.map +1 -1
- package/package.json +10 -10
- package/src/{frame-buffer/buffer-config.ts → filler-frame-buffer-context.ts} +6 -50
- package/src/{frame-buffer/buffer-filler.ts → frame-buffer-filler.ts} +2 -2
- package/src/index.ts +2 -2
- package/src/output-stream-node.ts +10 -10
- package/src/output-stream-processor.ts +5 -6
- package/src/stream-node-factory.ts +12 -12
- package/src/write-strategy/manual-strategy.ts +4 -5
- package/src/write-strategy/timed-strategy.ts +7 -7
- package/src/write-strategy/worker/message.ts +3 -3
- package/src/write-strategy/worker/worker.ts +10 -10
- package/src/write-strategy/worker-strategy.ts +9 -9
- package/dist/frame-buffer/buffer-config.d.ts +0 -60
- package/dist/frame-buffer/buffer-config.js.map +0 -1
- package/dist/frame-buffer/buffer-factory.d.ts +0 -76
- package/dist/frame-buffer/buffer-factory.js +0 -55
- package/dist/frame-buffer/buffer-factory.js.map +0 -1
- package/dist/frame-buffer/buffer-filler.js +0 -2
- package/dist/frame-buffer/buffer-filler.js.map +0 -1
- package/dist/frame-buffer/buffer-reader.d.ts +0 -60
- package/dist/frame-buffer/buffer-reader.js +0 -77
- package/dist/frame-buffer/buffer-reader.js.map +0 -1
- package/dist/frame-buffer/buffer-utils.d.ts +0 -34
- package/dist/frame-buffer/buffer-utils.js +0 -34
- package/dist/frame-buffer/buffer-utils.js.map +0 -1
- package/dist/frame-buffer/buffer-writer.d.ts +0 -60
- package/dist/frame-buffer/buffer-writer.js +0 -77
- package/dist/frame-buffer/buffer-writer.js.map +0 -1
- package/dist/frame-buffer/buffer.d.ts +0 -51
- package/dist/frame-buffer/buffer.js +0 -75
- package/dist/frame-buffer/buffer.js.map +0 -1
- package/dist/write-strategy/manual.d.ts +0 -36
- package/dist/write-strategy/manual.js +0 -43
- package/dist/write-strategy/manual.js.map +0 -1
- package/dist/write-strategy/timed.d.ts +0 -36
- package/dist/write-strategy/timed.js +0 -92
- package/dist/write-strategy/timed.js.map +0 -1
- package/dist/write-strategy/worker/strategy.d.ts +0 -34
- package/dist/write-strategy/worker/strategy.js +0 -125
- package/dist/write-strategy/worker/strategy.js.map +0 -1
- package/dist/write-strategy/worker/worker-strategy.d.ts +0 -34
- package/dist/write-strategy/worker/worker-strategy.js +0 -125
- package/dist/write-strategy/worker/worker-strategy.js.map +0 -1
- package/src/frame-buffer/buffer-reader.ts +0 -82
- package/src/frame-buffer/buffer-writer.ts +0 -82
- package/src/frame-buffer/buffer.ts +0 -79
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { FrameBuffer } from './buffer'
|
|
2
|
-
import { FrameBufferConfig } from './buffer-config'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* FrameBufferWriter class
|
|
6
|
-
* This class writes audio frame data to a shared Float32Array buffer.
|
|
7
|
-
* The buffer usage is tracked using a Uint32Array.
|
|
8
|
-
*/
|
|
9
|
-
export class FrameBufferWriter {
|
|
10
|
-
private readonly _frameBuffer: FrameBuffer
|
|
11
|
-
private readonly _usedFramesInBuffer: Uint32Array
|
|
12
|
-
private readonly _totalFrames: BigUint64Array
|
|
13
|
-
private _index: number = 0
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @internal
|
|
17
|
-
* Creates an instance of FrameBufferWriter.
|
|
18
|
-
* @param config - The configuration object containing:
|
|
19
|
-
* - `sampleBuffer`: The shared buffer to write audio data frames.
|
|
20
|
-
* - `samplesPerFrame`: The number of samples per frame.
|
|
21
|
-
* - `usedFramesInBuffer`: A Uint32Array tracking the usage of frames in the buffer.
|
|
22
|
-
* - `totalWriteFrames`: A BigUint64Array tracking the total frames written to the buffer.
|
|
23
|
-
*/
|
|
24
|
-
constructor(config: FrameBufferConfig) {
|
|
25
|
-
this._frameBuffer = new FrameBuffer(config.sampleBuffer, config.samplesPerFrame)
|
|
26
|
-
this._usedFramesInBuffer = config.usedFramesInBuffer
|
|
27
|
-
this._totalFrames = config.totalWriteFrames
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Get the number of available frames in the buffer.
|
|
32
|
-
* This represents the number of frames that can be written before the buffer is full.
|
|
33
|
-
* @returns The number of available frames in the buffer.
|
|
34
|
-
*/
|
|
35
|
-
public get availableFrames(): number {
|
|
36
|
-
return this._frameBuffer.frameCount - Atomics.load(this._usedFramesInBuffer, 0)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Get the total number of frames written to the buffer.
|
|
41
|
-
*
|
|
42
|
-
* @returns The total number of frames written.
|
|
43
|
-
*/
|
|
44
|
-
public get totalFrames(): bigint {
|
|
45
|
-
// This class is not used concurrently by multiple threads,
|
|
46
|
-
// so `Atomics` is not necessary when reading `totalFrames`.
|
|
47
|
-
return this._totalFrames[0]
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Writes audio frame data into the buffer using the provided callback.
|
|
52
|
-
* This method handles one or more writable segments within the ring buffer
|
|
53
|
-
* and invokes the callback for each segment.
|
|
54
|
-
*
|
|
55
|
-
* @param processFrameSegment - The callback function invoked for each writable segment
|
|
56
|
-
* of the ring buffer. It receives:
|
|
57
|
-
* 1. `buffer`: A Float32Array representing the writable segment of the buffer.
|
|
58
|
-
* 2. `offset`: The cumulative number of frames processed so far, used as the starting index
|
|
59
|
-
* for the current segment relative to the entire data.
|
|
60
|
-
*
|
|
61
|
-
* The callback must return the number of frames it successfully wrote.
|
|
62
|
-
* If the callback writes fewer frames than available in the current segment,
|
|
63
|
-
* processing will stop early.
|
|
64
|
-
*
|
|
65
|
-
* @returns The total number of frames written across all segments.
|
|
66
|
-
* Note: The return value is in frames, not in samples.
|
|
67
|
-
*
|
|
68
|
-
* @throws RangeError - If the processFrameSegment callback returns a written length greater than the available space in the current segment.
|
|
69
|
-
*
|
|
70
|
-
* @remarks The buffer is an array of samples, but it is always provided in frame-sized segments.
|
|
71
|
-
* Each frame consists of multiple samples (e.g., for stereo, a frame contains a sample for the left channel
|
|
72
|
-
* and one for the right channel). You must access and process the buffer in frame-sized chunks,
|
|
73
|
-
* based on the structure of the frames.
|
|
74
|
-
*/
|
|
75
|
-
public write(processFrameSegment: (buffer: Float32Array, offset: number) => number): number {
|
|
76
|
-
const result = this._frameBuffer.enumFrameSegments(this._index, this.availableFrames, processFrameSegment)
|
|
77
|
-
this._index = result.nextIndex
|
|
78
|
-
Atomics.add(this._usedFramesInBuffer, 0, result.totalProcessedFrames)
|
|
79
|
-
Atomics.add(this._totalFrames, 0, BigInt(result.totalProcessedFrames))
|
|
80
|
-
return result.totalProcessedFrames
|
|
81
|
-
}
|
|
82
|
-
}
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* FrameBuffer class
|
|
3
|
-
* This class manages a buffer of audio frames.
|
|
4
|
-
*/
|
|
5
|
-
export class FrameBuffer {
|
|
6
|
-
private readonly _buffer: Float32Array
|
|
7
|
-
private readonly _samplesPerFrame: number
|
|
8
|
-
private readonly _frameCount: number
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Creates an instance of FrameBuffer.
|
|
12
|
-
* @param buffer - The Float32Array buffer to manage.
|
|
13
|
-
* @param samplesPerFrame - The number of samples per frame.
|
|
14
|
-
*/
|
|
15
|
-
public constructor(buffer: Float32Array, samplesPerFrame: number) {
|
|
16
|
-
this._buffer = buffer
|
|
17
|
-
this._samplesPerFrame = samplesPerFrame
|
|
18
|
-
this._frameCount = Math.floor(buffer.length / samplesPerFrame)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Gets the count of the buffer in frames.
|
|
23
|
-
* @returns The count of the buffer in frames.
|
|
24
|
-
*/
|
|
25
|
-
public get frameCount(): number {
|
|
26
|
-
return this._frameCount
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
private getFrames(index: number, count: number): Float32Array {
|
|
30
|
-
return this._buffer.subarray(index * this._samplesPerFrame, (index + count) * this._samplesPerFrame)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Processes sections of a Float32Array buffer using a callback function.
|
|
35
|
-
* This function handles one or more segments within the ring buffer and invokes
|
|
36
|
-
* the provided callback for each segment. It is intended for internal use only.
|
|
37
|
-
*
|
|
38
|
-
* @param startIndex - The starting index in the buffer from where processing should begin.
|
|
39
|
-
* @param availableFrames - The total number of frames available to process in the buffer.
|
|
40
|
-
* @param processFrameSegment - The callback function invoked for each segment
|
|
41
|
-
* of the ring buffer during enumeration. It receives:
|
|
42
|
-
* 1. `buffer`: A Float32Array representing the segment to process. The buffer is an array
|
|
43
|
-
* of samples but is always provided in frame-sized segments.
|
|
44
|
-
* 2. `offset`: The cumulative number of frames processed so far, used as the starting index
|
|
45
|
-
* for the current segment relative to the entire data.
|
|
46
|
-
* The callback must return the number of frames it successfully processed.
|
|
47
|
-
* If the callback processes fewer frames than available in the current segment,
|
|
48
|
-
* processing will stop early.
|
|
49
|
-
*
|
|
50
|
-
* @returns An object containing:
|
|
51
|
-
* - totalProcessedFrames: The number of frames successfully processed.
|
|
52
|
-
* - nextIndex: The index in the buffer for the next processing cycle.
|
|
53
|
-
*
|
|
54
|
-
* @throws RangeError - If the processFrameSegment callback returns a processed length greater than the available section length.
|
|
55
|
-
*
|
|
56
|
-
* @remarks The buffer is always provided in frame-sized segments, meaning that the buffer contains complete frames.
|
|
57
|
-
* You must process the buffer in frame-sized chunks based on the structure of the frames.
|
|
58
|
-
*/
|
|
59
|
-
public enumFrameSegments(startIndex: number, availableFrames: number,
|
|
60
|
-
processFrameSegment: (buffer: Float32Array, offset: number) => number): { totalProcessedFrames: number, nextIndex: number } {
|
|
61
|
-
let totalProcessedFrames = 0
|
|
62
|
-
while (totalProcessedFrames < availableFrames) {
|
|
63
|
-
// Determine the length of the current section to process
|
|
64
|
-
const sectionFrames = Math.min(this.frameCount - startIndex, availableFrames - totalProcessedFrames)
|
|
65
|
-
// Process the current section using the frameCallback function
|
|
66
|
-
const processedFrames = processFrameSegment(this.getFrames(startIndex, sectionFrames), totalProcessedFrames)
|
|
67
|
-
// Ensure the processed length does not exceed the section length
|
|
68
|
-
if (processedFrames > sectionFrames) {
|
|
69
|
-
throw new RangeError(`Processed frames (${processedFrames}) exceeds section frames (${sectionFrames})`)
|
|
70
|
-
}
|
|
71
|
-
totalProcessedFrames += processedFrames
|
|
72
|
-
startIndex = (startIndex + processedFrames) % this.frameCount
|
|
73
|
-
if (processedFrames < sectionFrames) {
|
|
74
|
-
break
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return { totalProcessedFrames, nextIndex: startIndex }
|
|
78
|
-
}
|
|
79
|
-
}
|