@ain1084/audio-worklet-stream 0.1.8 → 1.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/README.md +12 -27
- package/dist/events.d.ts +6 -6
- package/dist/events.js +8 -8
- package/dist/events.js.map +1 -1
- package/dist/frame-buffer/buffer-config.d.ts +67 -0
- package/dist/frame-buffer/buffer-config.js +64 -0
- package/dist/frame-buffer/buffer-config.js.map +1 -0
- package/dist/frame-buffer/buffer-factory.d.ts +1 -2
- package/dist/frame-buffer/buffer-factory.js +6 -3
- package/dist/frame-buffer/buffer-factory.js.map +1 -1
- package/dist/frame-buffer/buffer-reader.d.ts +29 -10
- package/dist/frame-buffer/buffer-reader.js +37 -16
- package/dist/frame-buffer/buffer-reader.js.map +1 -1
- package/dist/frame-buffer/buffer-utils.js +2 -2
- package/dist/frame-buffer/buffer-utils.js.map +1 -1
- package/dist/frame-buffer/buffer-writer.d.ts +31 -12
- package/dist/frame-buffer/buffer-writer.js +39 -18
- package/dist/frame-buffer/buffer-writer.js.map +1 -1
- package/dist/frame-buffer/buffer.d.ts +33 -22
- package/dist/frame-buffer/buffer.js +50 -40
- package/dist/frame-buffer/buffer.js.map +1 -1
- package/dist/output-message.d.ts +9 -9
- package/dist/output-stream-node.d.ts +13 -19
- package/dist/output-stream-node.js +20 -26
- package/dist/output-stream-node.js.map +1 -1
- package/dist/output-stream-processor.js +25 -18
- package/dist/output-stream-processor.js.map +1 -1
- package/dist/stream-node-factory.d.ts +10 -0
- package/dist/stream-node-factory.js +27 -12
- package/dist/stream-node-factory.js.map +1 -1
- package/dist/write-strategy/manual.d.ts +1 -1
- package/dist/write-strategy/manual.js +1 -1
- package/dist/write-strategy/manual.js.map +1 -1
- package/dist/write-strategy/timed.d.ts +1 -1
- package/dist/write-strategy/timed.js +1 -1
- package/dist/write-strategy/timed.js.map +1 -1
- package/dist/write-strategy/worker/message.d.ts +1 -1
- package/dist/write-strategy/worker/strategy.d.ts +1 -1
- package/dist/write-strategy/worker/worker.js +1 -1
- package/dist/write-strategy/worker/worker.js.map +1 -1
- package/package.json +9 -9
- package/src/events.ts +4 -4
- package/src/frame-buffer/buffer-config.ts +129 -0
- package/src/frame-buffer/buffer-reader.ts +37 -16
- package/src/frame-buffer/buffer-writer.ts +39 -18
- package/src/frame-buffer/buffer.ts +51 -40
- package/src/output-message.ts +9 -9
- package/src/output-stream-node.ts +23 -29
- package/src/output-stream-processor.ts +25 -18
- package/src/stream-node-factory.ts +33 -15
- package/src/write-strategy/manual.ts +1 -1
- package/src/write-strategy/timed.ts +1 -1
- package/src/write-strategy/worker/message.ts +1 -1
- package/src/write-strategy/worker/strategy.ts +1 -1
- package/src/write-strategy/worker/worker.ts +1 -1
- package/src/frame-buffer/buffer-factory.ts +0 -115
- package/src/frame-buffer/buffer-utils.ts +0 -48
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import processor from './output-stream-processor?worker&url';
|
|
2
2
|
import { ManualBufferWriteStrategy } from './write-strategy/manual';
|
|
3
|
-
import {
|
|
3
|
+
import { createFillerFrameBufferConfig, createFrameBufferConfig } from './frame-buffer/buffer-config';
|
|
4
4
|
import { TimedBufferWriteStrategy } from './write-strategy/timed';
|
|
5
5
|
import { WorkerBufferWriteStrategy } from './write-strategy/worker/strategy';
|
|
6
6
|
/**
|
|
@@ -34,7 +34,7 @@ export class StreamNodeFactory {
|
|
|
34
34
|
context.audioWorklet.addModule(processor),
|
|
35
35
|
import('./output-stream-node'),
|
|
36
36
|
]);
|
|
37
|
-
return new StreamNodeFactory(context, loadResults[1].
|
|
37
|
+
return new StreamNodeFactory(context, loadResults[1].OutputStreamNode);
|
|
38
38
|
}
|
|
39
39
|
catch (error) {
|
|
40
40
|
throw new Error('Failed to load modules: ' + error);
|
|
@@ -52,9 +52,9 @@ export class StreamNodeFactory {
|
|
|
52
52
|
if (params.frameBufferSize <= 0 || !Number.isInteger(params.frameBufferSize)) {
|
|
53
53
|
throw new Error('Invalid frameBufferSize: must be a positive integer.');
|
|
54
54
|
}
|
|
55
|
-
const
|
|
56
|
-
const strategy = new ManualBufferWriteStrategy(
|
|
57
|
-
return [await this._outputStreamNodeFactory.create(this.audioContext,
|
|
55
|
+
const config = createFrameBufferConfig({ ...params });
|
|
56
|
+
const strategy = new ManualBufferWriteStrategy(config);
|
|
57
|
+
return [await this._outputStreamNodeFactory.create(this.audioContext, config, strategy), strategy.writer];
|
|
58
58
|
}
|
|
59
59
|
/**
|
|
60
60
|
* Creates an OutputStreamNode with timed buffer writing strategy.
|
|
@@ -63,13 +63,13 @@ export class StreamNodeFactory {
|
|
|
63
63
|
* @returns A promise that resolves to an OutputStreamNode instance.
|
|
64
64
|
*/
|
|
65
65
|
async createTimedBufferNode(frameFiller, params) {
|
|
66
|
-
StreamNodeFactory.validateTimedBufferNodeParams(params);
|
|
67
66
|
if (typeof frameFiller !== 'object' || frameFiller === null) {
|
|
68
67
|
throw new Error('Invalid frameFiller: must be an object.');
|
|
69
68
|
}
|
|
70
|
-
|
|
71
|
-
const
|
|
72
|
-
|
|
69
|
+
StreamNodeFactory.validateTimedBufferNodeParams(params);
|
|
70
|
+
const paramsWithSampleRate = StreamNodeFactory.applySampleRateToParams(params, this._audioContext.sampleRate);
|
|
71
|
+
const config = createFillerFrameBufferConfig(paramsWithSampleRate);
|
|
72
|
+
return this._outputStreamNodeFactory.create(this.audioContext, config, new TimedBufferWriteStrategy(config, frameFiller));
|
|
73
73
|
}
|
|
74
74
|
/**
|
|
75
75
|
* Creates an OutputStreamNode with worker buffer writing strategy.
|
|
@@ -79,9 +79,9 @@ export class StreamNodeFactory {
|
|
|
79
79
|
*/
|
|
80
80
|
async createWorkerBufferNode(worker, params) {
|
|
81
81
|
StreamNodeFactory.validateTimedBufferNodeParams(params);
|
|
82
|
-
const
|
|
83
|
-
const
|
|
84
|
-
return this._outputStreamNodeFactory.create(this._audioContext, config,
|
|
82
|
+
const paramsWithSampleRate = StreamNodeFactory.applySampleRateToParams(params, this._audioContext.sampleRate);
|
|
83
|
+
const config = createFillerFrameBufferConfig(paramsWithSampleRate);
|
|
84
|
+
return this._outputStreamNodeFactory.create(this._audioContext, config, new WorkerBufferWriteStrategy(config, worker, params.fillerParams));
|
|
85
85
|
}
|
|
86
86
|
/**
|
|
87
87
|
* Validates the parameters for timed buffer node creation.
|
|
@@ -102,6 +102,21 @@ export class StreamNodeFactory {
|
|
|
102
102
|
throw new Error('Invalid frameBufferChunks: must be a positive integer.');
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* Applies a default sample rate to the given parameters if the sampleRate is undefined.
|
|
107
|
+
* This function creates a new TimedBufferNodeParams object with the specified sampleRate,
|
|
108
|
+
* while keeping other properties unchanged.
|
|
109
|
+
*
|
|
110
|
+
* @param params - The original parameters for the TimedBufferNode.
|
|
111
|
+
* @param defaultSampleRate - The default sample rate to use if params.sampleRate is undefined.
|
|
112
|
+
* @returns A new TimedBufferNodeParams object with the sample rate applied.
|
|
113
|
+
*/
|
|
114
|
+
static applySampleRateToParams(params, defaultSampleRate) {
|
|
115
|
+
return {
|
|
116
|
+
...params,
|
|
117
|
+
sampleRate: params.sampleRate ?? defaultSampleRate,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
105
120
|
/**
|
|
106
121
|
* Get the AudioContext associated with this factory.
|
|
107
122
|
* @returns The AudioContext instance.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-node-factory.js","sourceRoot":"","sources":["../src/stream-node-factory.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,sCAAsC,CAAA;AAC5D,OAAO,EAAE,yBAAyB,EAAE,MAAM,yBAAyB,CAAA;AACnE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"stream-node-factory.js","sourceRoot":"","sources":["../src/stream-node-factory.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,sCAAsC,CAAA;AAC5D,OAAO,EAAE,yBAAyB,EAAE,MAAM,yBA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|
|
2
|
-
import { FrameBufferConfig } from '../frame-buffer/buffer-
|
|
2
|
+
import { FrameBufferConfig } from '../frame-buffer/buffer-config';
|
|
3
3
|
import { FrameBufferWriter } from '../frame-buffer/buffer-writer';
|
|
4
4
|
/**
|
|
5
5
|
* ManualBufferWriteStrategy class
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createFrameBufferWriter } from '../frame-buffer/buffer-
|
|
1
|
+
import { createFrameBufferWriter } from '../frame-buffer/buffer-config';
|
|
2
2
|
/**
|
|
3
3
|
* ManualBufferWriteStrategy class
|
|
4
4
|
* Implements the BufferWriteStrategy interface for manually writing to the buffer.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"manual.js","sourceRoot":"","sources":["../../src/write-strategy/manual.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uBAAuB,EAAqB,MAAM,
|
|
1
|
+
{"version":3,"file":"manual.js","sourceRoot":"","sources":["../../src/write-strategy/manual.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uBAAuB,EAAqB,MAAM,+BAA+B,CAAA;AAG1F;;;IAGI;AACJ,MAAM,OAAO,yBAAyB;IACnB,OAAO,CAAmB;IAE3C;;;OAGG;IACH,YAAY,MAAyB;QACnC,IAAI,CAAC,OAAO,GAAG,uBAAuB,CAAC,MAAM,CAAC,CAAA;IAChD,CAAC;IAED;;;OAGG;IACH,IAAW,MAAM;QACf,OAAO,IAAI,CAAC,OAAO,CAAA;IACrB,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,MAAM,EAAC,4BAA4B;QACvC,OAAO,IAAI,CAAA;IACb,CAAC;IAED;;;;OAIG;IACH,OAAO,EAAC,4BAA4B;QAClC,OAAO,IAAI,CAAA;IACb,CAAC;IAED;;OAEG;IACH,SAAS,KAAU,CAAC;CACrB"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BufferWriteStrategy } from './strategy';
|
|
2
|
-
import { FillerFrameBufferConfig } from '../frame-buffer/buffer-
|
|
2
|
+
import { FillerFrameBufferConfig } from '../frame-buffer/buffer-config';
|
|
3
3
|
import { FrameBufferFiller } from '../frame-buffer/buffer-filler';
|
|
4
4
|
import { OutputStreamNode } from '../output-stream-node';
|
|
5
5
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timed.js","sourceRoot":"","sources":["../../src/write-strategy/timed.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uBAAuB,EAA2B,MAAM,
|
|
1
|
+
{"version":3,"file":"timed.js","sourceRoot":"","sources":["../../src/write-strategy/timed.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uBAAuB,EAA2B,MAAM,+BAA+B,CAAA;AAKhG;;;GAGG;AACH,MAAM,WAAW;IACP,QAAQ,GAAW,CAAC,CAAA;IAE5B;;;;;;OAMG;IACH,YAAY,IAAsB,EAAE,MAAyB,EAAE,MAAyB,EAAE,YAAoB;QAC5G,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,WAAW,CAAC,GAAG,EAAE;YACtC,IAAI,CAAC,IAAI,CAAC,QAAQ,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;gBACpC,OAAM;YACR,CAAC;YACD,IAAI,CAAC;gBACH,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;oBACzB,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,CAAA;gBAC/B,CAAC;YACH,CAAC;YACD,MAAM,CAAC;gBACL,IAAI,CAAC,OAAO,EAAE,CAAA;gBACd,IAAI,CAAC,IAAI,EAAE,CAAA;YACb,CAAC;QACH,CAAC,EAAE,YAAY,CAAC,CAAA;IAClB,CAAC;IAED;;OAEG;IACI,OAAO;QACZ,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAClB,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;YAC5B,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAA;QACnB,CAAC;IAC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
+
import type { FillerFrameBufferConfig } from '../../frame-buffer/buffer-config';
|
|
3
3
|
import type { OutputStreamNode } from '../../output-stream-node';
|
|
4
4
|
/**
|
|
5
5
|
* WorkerBufferWriteStrategy class
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker.js","sourceRoot":"","sources":["../../../src/write-strategy/worker/worker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAA2B,MAAM,
|
|
1
|
+
{"version":3,"file":"worker.js","sourceRoot":"","sources":["../../../src/write-strategy/worker/worker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAA2B,MAAM,kCAAkC,CAAA;AAKnG;;;GAGG;AACH,MAAM,OAAO;IACM,kBAAkB,CAAmB;IACrC,mBAAmB,CAAS;IAC5B,kBAAkB,CAAmB;IACrC,aAAa,CAAQ;IAC9B,QAAQ,GAAW,CAAC,CAAA;IAE5B;;;;OAIG;IACH,YAAY,MAA+B,EAAE,iBAAoC;QAC/E,IAAI,CAAC,kBAAkB,GAAG,uBAAuB,CAAC,MAAM,CAAC,CAAA;QACzD,IAAI,CAAC,kBAAkB,GAAG,iBAAiB,CAAA;QAC3C,IAAI,CAAC,aAAa,GAAG,MAAM,CAAC,YAAY,CAAA;QACxC,IAAI,CAAC,mBAAmB,GAAG,IAAI,CAAC,UAAU,EAAE,CAAA;IAC9C,CAAC;IAED;;OAEG;IACI,KAAK;QACV,IAAI,CAAC,IAAI,CAAC,mBAAmB,EAAE,CAAC;YAC9B,IAAI,CAAC,UAAU,EAAE,CAAA;YACjB,OAAM;QACR,CAAC;QACD,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,EAAE;YACpC,IAAI,CAAC,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,IAAI,CAAC,kBAAkB,CAAC,EAAE,CAAC;gBAC3D,IAAI,CAAC,UAAU,EAAE,CAAA;gBACjB,IAAI,CAAC,aAAa,CAAC,IAAI,CA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5
|
"module": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -41,14 +41,14 @@
|
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@stylistic/eslint-plugin": "^2.9.0",
|
|
44
|
-
"@types/audioworklet": "^0.0.
|
|
45
|
-
"@types/node": "^22.7.
|
|
46
|
-
"@typescript-eslint/parser": "^8.
|
|
44
|
+
"@types/audioworklet": "^0.0.61",
|
|
45
|
+
"@types/node": "^22.7.6",
|
|
46
|
+
"@typescript-eslint/parser": "^8.9.0",
|
|
47
47
|
"eslint": "^9.12.0",
|
|
48
|
-
"globals": "^15.
|
|
49
|
-
"typedoc": "^0.26.
|
|
50
|
-
"typescript": "^5.6.
|
|
51
|
-
"typescript-eslint": "^8.
|
|
52
|
-
"vite": "^5.4.
|
|
48
|
+
"globals": "^15.11.0",
|
|
49
|
+
"typedoc": "^0.26.10",
|
|
50
|
+
"typescript": "^5.6.3",
|
|
51
|
+
"typescript-eslint": "^8.9.0",
|
|
52
|
+
"vite": "^5.4.9"
|
|
53
53
|
}
|
|
54
54
|
}
|
package/src/events.ts
CHANGED
|
@@ -8,9 +8,9 @@ export class StopEvent extends Event {
|
|
|
8
8
|
public static readonly type = STOP_EVENT_TYPE
|
|
9
9
|
/**
|
|
10
10
|
* Creates an instance of StopEvent.
|
|
11
|
-
* @param
|
|
11
|
+
* @param stoppedAtFrame - The position in the audio stream where the stop occurred.
|
|
12
12
|
*/
|
|
13
|
-
public constructor(readonly
|
|
13
|
+
public constructor(readonly stoppedAtFrame: bigint) {
|
|
14
14
|
super(StopEvent.type)
|
|
15
15
|
}
|
|
16
16
|
}
|
|
@@ -24,9 +24,9 @@ export class UnderrunEvent extends Event {
|
|
|
24
24
|
public static readonly type = UNDERRUN_EVENT_TYPE
|
|
25
25
|
/**
|
|
26
26
|
* Creates an instance of UnderrunEvent.
|
|
27
|
-
* @param
|
|
27
|
+
* @param frameCount - The number of frames that were not processed due to the underrun.
|
|
28
28
|
*/
|
|
29
|
-
public constructor(readonly
|
|
29
|
+
public constructor(readonly frameCount: number) {
|
|
30
30
|
super(UnderrunEvent.type)
|
|
31
31
|
}
|
|
32
32
|
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { FrameBuffer } from './buffer'
|
|
2
|
+
import { FrameBufferWriter } from './buffer-writer'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The default number of chunks in the frame buffer when
|
|
6
|
+
* FillerFrameBufferParams.frameBufferChunks is not provided.
|
|
7
|
+
* A frame buffer sufficient to hold fillInterval * frameBufferChunks of playback time is allocated.
|
|
8
|
+
*/
|
|
9
|
+
const DEFAULT_FILL_INTERVAL_MS = 20
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The default number of chunks in the frame buffer when
|
|
13
|
+
* FillerFrameBufferParams.frameBufferChunks is not provided.
|
|
14
|
+
* The total frame buffer size will be fillInterval * frameBufferChunks.
|
|
15
|
+
*/
|
|
16
|
+
const DEFAULT_FRAME_BUFFER_CHUNKS = 5
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The unit for rounding up the frame buffer size.
|
|
20
|
+
* This value is aligned with the audio data block size used by the AudioWorkletProcessor.
|
|
21
|
+
* Reference: https://developer.mozilla.org/en-US/docs/Web/API/AudioWorkletProcessor/process
|
|
22
|
+
*/
|
|
23
|
+
const AUDIO_WORKLET_BLOCK_SIZE = 128
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Parameters for creating a FrameBuffer.
|
|
27
|
+
* @property frameBufferSize - The size of the frame buffer.
|
|
28
|
+
* @property channelCount - The number of audio channels.
|
|
29
|
+
*/
|
|
30
|
+
export type FrameBufferParams = Readonly<{
|
|
31
|
+
frameBufferSize: number
|
|
32
|
+
channelCount: number
|
|
33
|
+
}>
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Parameters for creating a FillerFrameBuffer.
|
|
37
|
+
* @property channelCount - The number of audio channels.
|
|
38
|
+
* @property fillInterval - The interval in milliseconds for filling the buffer.
|
|
39
|
+
* @property sampleRate - The sample rate of the audio context.
|
|
40
|
+
* @property frameBufferChunks - The number of chunks in the frame buffer.
|
|
41
|
+
*/
|
|
42
|
+
export type FillerFrameBufferParams = Readonly<{
|
|
43
|
+
channelCount: number
|
|
44
|
+
fillInterval?: number
|
|
45
|
+
sampleRate?: number
|
|
46
|
+
frameBufferChunks?: number
|
|
47
|
+
}>
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Configuration for a FrameBuffer.
|
|
51
|
+
* This configuration is returned by the createFrameBufferConfig function.
|
|
52
|
+
* @property sampleBuffer - The shared buffer for audio data frames.
|
|
53
|
+
* @property samplesPerFrame - The number of samples per frame.
|
|
54
|
+
* @property usedFramesInBuffer - The usage count of the frames in the buffer.
|
|
55
|
+
* @property totalReadFrames - The total frames read from the buffer.
|
|
56
|
+
* @property totalWriteFrames - The total frames written to the buffer.
|
|
57
|
+
*/
|
|
58
|
+
export type FrameBufferConfig = Readonly<{
|
|
59
|
+
sampleBuffer: Float32Array
|
|
60
|
+
samplesPerFrame: number
|
|
61
|
+
usedFramesInBuffer: Uint32Array
|
|
62
|
+
totalReadFrames: BigUint64Array
|
|
63
|
+
totalWriteFrames: BigUint64Array
|
|
64
|
+
}>
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Configuration for a FillerFrameBuffer.
|
|
68
|
+
* This configuration is returned by the createFillerFrameBufferConfig function.
|
|
69
|
+
* @property sampleRate - The sample rate of the audio context.
|
|
70
|
+
* @property fillInterval - The interval in milliseconds for filling the buffer.
|
|
71
|
+
*/
|
|
72
|
+
export type FillerFrameBufferConfig = FrameBufferConfig & Readonly<{
|
|
73
|
+
sampleRate: number
|
|
74
|
+
fillInterval: number
|
|
75
|
+
}>
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Creates a FrameBufferWriter instance.
|
|
79
|
+
* @param config - The configuration for the FrameBuffer.
|
|
80
|
+
* @returns A new instance of FrameBufferWriter.
|
|
81
|
+
*/
|
|
82
|
+
export const createFrameBufferWriter = (config: FrameBufferConfig): FrameBufferWriter => {
|
|
83
|
+
return new FrameBufferWriter(
|
|
84
|
+
new FrameBuffer(config.sampleBuffer, config.samplesPerFrame),
|
|
85
|
+
config.usedFramesInBuffer, config.totalWriteFrames,
|
|
86
|
+
)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Creates a FrameBufferConfig instance.
|
|
91
|
+
* @param params - The parameters for the FrameBuffer.
|
|
92
|
+
* @returns A new instance of FrameBufferConfig.
|
|
93
|
+
*/
|
|
94
|
+
export const createFrameBufferConfig = (params: FrameBufferParams): FrameBufferConfig => {
|
|
95
|
+
return {
|
|
96
|
+
sampleBuffer: new Float32Array(
|
|
97
|
+
new SharedArrayBuffer(params.frameBufferSize * params.channelCount * Float32Array.BYTES_PER_ELEMENT),
|
|
98
|
+
),
|
|
99
|
+
samplesPerFrame: params.channelCount,
|
|
100
|
+
usedFramesInBuffer: new Uint32Array(new SharedArrayBuffer(Uint32Array.BYTES_PER_ELEMENT)),
|
|
101
|
+
totalReadFrames: new BigUint64Array(new SharedArrayBuffer(BigUint64Array.BYTES_PER_ELEMENT)),
|
|
102
|
+
totalWriteFrames: new BigUint64Array(new SharedArrayBuffer(BigUint64Array.BYTES_PER_ELEMENT)),
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Creates a FillerFrameBufferConfig instance.
|
|
108
|
+
* @param params - The parameters for the FillerFrameBuffer.
|
|
109
|
+
* @returns A new instance of FillerFrameBufferConfig.
|
|
110
|
+
*/
|
|
111
|
+
export const createFillerFrameBufferConfig = (params: FillerFrameBufferParams): FillerFrameBufferConfig => {
|
|
112
|
+
const sampleRate = params.sampleRate
|
|
113
|
+
// Check if 'sampleRate' is a positive integer
|
|
114
|
+
if (sampleRate === undefined || (!Number.isInteger(sampleRate) || sampleRate <= 0)) {
|
|
115
|
+
throw new Error('Invalid sampleRate: must be a positive integer.')
|
|
116
|
+
}
|
|
117
|
+
const intervalMillisecond = params.fillInterval ?? DEFAULT_FILL_INTERVAL_MS
|
|
118
|
+
const frameBufferSize = Math.floor(
|
|
119
|
+
sampleRate * intervalMillisecond / 1000 + (AUDIO_WORKLET_BLOCK_SIZE - 1),
|
|
120
|
+
) & ~(AUDIO_WORKLET_BLOCK_SIZE - 1)
|
|
121
|
+
const frameBufferChunkCount = params.frameBufferChunks ?? DEFAULT_FRAME_BUFFER_CHUNKS
|
|
122
|
+
const config = createFrameBufferConfig(
|
|
123
|
+
{ frameBufferSize: frameBufferSize * frameBufferChunkCount, channelCount: params.channelCount })
|
|
124
|
+
return {
|
|
125
|
+
...config,
|
|
126
|
+
sampleRate,
|
|
127
|
+
fillInterval: intervalMillisecond,
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { enumFrames, type FrameCallback } from './buffer-utils'
|
|
2
1
|
import { FrameBuffer } from './buffer'
|
|
3
2
|
|
|
4
3
|
/**
|
|
@@ -8,19 +7,19 @@ import { FrameBuffer } from './buffer'
|
|
|
8
7
|
*/
|
|
9
8
|
export class FrameBufferReader {
|
|
10
9
|
private readonly _frameBuffer: FrameBuffer
|
|
11
|
-
private readonly
|
|
10
|
+
private readonly _usedFramesInBuffer: Uint32Array
|
|
12
11
|
private readonly _totalFrames: BigUint64Array
|
|
13
12
|
private _index: number = 0
|
|
14
13
|
|
|
15
14
|
/**
|
|
16
15
|
* Creates an instance of FrameBufferReader.
|
|
17
16
|
* @param frameBuffer - The shared buffer to read from.
|
|
18
|
-
* @param
|
|
17
|
+
* @param usedFramesInBuffer - The Uint32Array tracking the usage of the buffer.
|
|
19
18
|
* @param totalFrames - The BigUint64Array tracking the total frames read from the buffer.
|
|
20
19
|
*/
|
|
21
|
-
constructor(frameBuffer: FrameBuffer,
|
|
20
|
+
constructor(frameBuffer: FrameBuffer, usedFramesInBuffer: Uint32Array, totalFrames: BigUint64Array) {
|
|
22
21
|
this._frameBuffer = frameBuffer
|
|
23
|
-
this.
|
|
22
|
+
this._usedFramesInBuffer = usedFramesInBuffer
|
|
24
23
|
this._totalFrames = totalFrames
|
|
25
24
|
}
|
|
26
25
|
|
|
@@ -28,29 +27,51 @@ export class FrameBufferReader {
|
|
|
28
27
|
* Get the number of available frames in the buffer.
|
|
29
28
|
* @returns The number of available frames in the buffer.
|
|
30
29
|
*/
|
|
31
|
-
public get
|
|
32
|
-
return Atomics.load(this.
|
|
30
|
+
public get availableFrames(): number {
|
|
31
|
+
return Atomics.load(this._usedFramesInBuffer, 0)
|
|
33
32
|
}
|
|
34
33
|
|
|
35
34
|
/**
|
|
36
35
|
* Get the total number of frames read from the buffer.
|
|
36
|
+
*
|
|
37
37
|
* @returns The total number of frames read.
|
|
38
38
|
*/
|
|
39
39
|
public get totalFrames(): bigint {
|
|
40
|
+
// This class is not used concurrently by multiple threads,
|
|
41
|
+
// so `Atomics` is not necessary when reading `totalFrames`.
|
|
40
42
|
return this._totalFrames[0]
|
|
41
43
|
}
|
|
42
44
|
|
|
43
45
|
/**
|
|
44
|
-
* Reads audio frame data from the buffer
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
46
|
+
* Reads audio frame data from the buffer using the provided callback.
|
|
47
|
+
* This method handles one or more readable segments within the ring buffer
|
|
48
|
+
* and invokes the callback for each segment.
|
|
49
|
+
*
|
|
50
|
+
* @param processFrameSegment - The callback function invoked for each readable segment
|
|
51
|
+
* of the ring buffer. It receives:
|
|
52
|
+
* 1. `buffer`: A Float32Array representing the readable segment of the buffer.
|
|
53
|
+
* 2. `offset`: The cumulative number of frames processed so far, used as the starting index
|
|
54
|
+
* for the current segment relative to the entire data.
|
|
55
|
+
* The callback must return the number of frames it successfully processed.
|
|
56
|
+
* If the callback processes fewer frames than available in the current segment,
|
|
57
|
+
* processing will stop early.
|
|
58
|
+
*
|
|
59
|
+
* @returns The total number of frames processed across all segments.
|
|
60
|
+
* Note: The return value is in frames, not in samples.
|
|
61
|
+
*
|
|
62
|
+
* @throws RangeError - If the processFrameSegment callback returns a processed length greater than the available frames in the current segment.
|
|
63
|
+
*
|
|
64
|
+
* @remarks The buffer is an array of samples, but it is always provided in frame-sized segments.
|
|
65
|
+
* Each frame consists of multiple samples (e.g., for stereo, a frame contains a sample for the left channel
|
|
66
|
+
* and one for the right channel). You must access and process the buffer in frame-sized chunks,
|
|
67
|
+
* based on the structure of the frames.
|
|
68
|
+
*
|
|
48
69
|
*/
|
|
49
|
-
public read(
|
|
50
|
-
const result =
|
|
70
|
+
public read(processFrameSegment: (buffer: Float32Array, offset: number) => number): number {
|
|
71
|
+
const result = this._frameBuffer.enumFrameSegments(this._index, this.availableFrames, processFrameSegment)
|
|
51
72
|
this._index = result.nextIndex
|
|
52
|
-
Atomics.sub(this.
|
|
53
|
-
Atomics.add(this._totalFrames, 0, BigInt(result.
|
|
54
|
-
return result.
|
|
73
|
+
Atomics.sub(this._usedFramesInBuffer, 0, result.totalProcessedFrames)
|
|
74
|
+
Atomics.add(this._totalFrames, 0, BigInt(result.totalProcessedFrames))
|
|
75
|
+
return result.totalProcessedFrames
|
|
55
76
|
}
|
|
56
77
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { enumFrames, type FrameCallback } from './buffer-utils'
|
|
2
1
|
import { FrameBuffer } from './buffer'
|
|
3
2
|
|
|
4
3
|
/**
|
|
@@ -8,49 +7,71 @@ import { FrameBuffer } from './buffer'
|
|
|
8
7
|
*/
|
|
9
8
|
export class FrameBufferWriter {
|
|
10
9
|
private readonly _frameBuffer: FrameBuffer
|
|
11
|
-
private readonly
|
|
10
|
+
private readonly _usedFramesInBuffer: Uint32Array
|
|
12
11
|
private readonly _totalFrames: BigUint64Array
|
|
13
12
|
private _index: number = 0
|
|
14
13
|
|
|
15
14
|
/**
|
|
16
15
|
* Creates an instance of FrameBufferWriter.
|
|
17
16
|
* @param frameBuffer - The shared buffer to write to.
|
|
18
|
-
* @param
|
|
17
|
+
* @param usedFramesInBuffer - The Uint32Array tracking the usage of the buffer.
|
|
19
18
|
* @param totalFrames - The BigUint64Array tracking the total frames written to the buffer.
|
|
20
19
|
*/
|
|
21
|
-
constructor(frameBuffer: FrameBuffer,
|
|
20
|
+
constructor(frameBuffer: FrameBuffer, usedFramesInBuffer: Uint32Array, totalFrames: BigUint64Array) {
|
|
22
21
|
this._frameBuffer = frameBuffer
|
|
23
|
-
this.
|
|
22
|
+
this._usedFramesInBuffer = usedFramesInBuffer
|
|
24
23
|
this._totalFrames = totalFrames
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
/**
|
|
28
|
-
* Get the number of available
|
|
29
|
-
*
|
|
27
|
+
* Get the number of available frames in the buffer.
|
|
28
|
+
* This represents the number of frames that can be written before the buffer is full.
|
|
29
|
+
* @returns The number of available frames in the buffer.
|
|
30
30
|
*/
|
|
31
|
-
public get
|
|
32
|
-
return this._frameBuffer.
|
|
31
|
+
public get availableFrames(): number {
|
|
32
|
+
return this._frameBuffer.frameCount - Atomics.load(this._usedFramesInBuffer, 0)
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
36
|
* Get the total number of frames written to the buffer.
|
|
37
|
+
*
|
|
37
38
|
* @returns The total number of frames written.
|
|
38
39
|
*/
|
|
39
40
|
public get totalFrames(): bigint {
|
|
41
|
+
// This class is not used concurrently by multiple threads,
|
|
42
|
+
// so `Atomics` is not necessary when reading `totalFrames`.
|
|
40
43
|
return this._totalFrames[0]
|
|
41
44
|
}
|
|
42
45
|
|
|
43
46
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
47
|
+
* Writes audio frame data into the buffer using the provided callback.
|
|
48
|
+
* This method handles one or more writable segments within the ring buffer
|
|
49
|
+
* and invokes the callback for each segment.
|
|
50
|
+
*
|
|
51
|
+
* @param processFrameSegment - The callback function invoked for each writable segment
|
|
52
|
+
* of the ring buffer. It receives:
|
|
53
|
+
* 1. `buffer`: A Float32Array representing the writable segment of the buffer.
|
|
54
|
+
* 2. `offset`: The cumulative number of frames processed so far, used as the starting index
|
|
55
|
+
* for the current segment relative to the entire data.
|
|
56
|
+
* The callback must return the number of frames it successfully wrote.
|
|
57
|
+
* If the callback writes fewer frames than available in the current segment,
|
|
58
|
+
* processing will stop early.
|
|
59
|
+
*
|
|
60
|
+
* @returns The total number of frames written across all segments.
|
|
61
|
+
* Note: The return value is in frames, not in samples.
|
|
62
|
+
*
|
|
63
|
+
* @throws RangeError - If the processFrameSegment callback returns a written length greater than the available space in the current segment.
|
|
64
|
+
*
|
|
65
|
+
* @remarks The buffer is an array of samples, but it is always provided in frame-sized segments.
|
|
66
|
+
* Each frame consists of multiple samples (e.g., for stereo, a frame contains a sample for the left channel
|
|
67
|
+
* and one for the right channel). You must access and process the buffer in frame-sized chunks,
|
|
68
|
+
* based on the structure of the frames.
|
|
48
69
|
*/
|
|
49
|
-
public write(
|
|
50
|
-
const result =
|
|
70
|
+
public write(processFrameSegment: (buffer: Float32Array, offset: number) => number): number {
|
|
71
|
+
const result = this._frameBuffer.enumFrameSegments(this._index, this.availableFrames, processFrameSegment)
|
|
51
72
|
this._index = result.nextIndex
|
|
52
|
-
Atomics.add(this.
|
|
53
|
-
Atomics.add(this._totalFrames, 0, BigInt(result.
|
|
54
|
-
return result.
|
|
73
|
+
Atomics.add(this._usedFramesInBuffer, 0, result.totalProcessedFrames)
|
|
74
|
+
Atomics.add(this._totalFrames, 0, BigInt(result.totalProcessedFrames))
|
|
75
|
+
return result.totalProcessedFrames
|
|
55
76
|
}
|
|
56
77
|
}
|