@ain1084/audio-worklet-stream 0.1.2
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/LICENSE +222 -0
- package/README.md +243 -0
- package/dist/constants.d.ts +12 -0
- package/dist/constants.js +15 -0
- package/dist/constants.js.map +1 -0
- package/dist/events.d.ts +37 -0
- package/dist/events.js +35 -0
- package/dist/events.js.map +1 -0
- package/dist/frame-buffer/buffer-factory.d.ts +77 -0
- package/dist/frame-buffer/buffer-factory.js +52 -0
- package/dist/frame-buffer/buffer-factory.js.map +1 -0
- package/dist/frame-buffer/buffer-filler.d.ts +13 -0
- package/dist/frame-buffer/buffer-filler.js +2 -0
- package/dist/frame-buffer/buffer-filler.js.map +1 -0
- package/dist/frame-buffer/buffer-reader.d.ts +37 -0
- package/dist/frame-buffer/buffer-reader.js +51 -0
- package/dist/frame-buffer/buffer-reader.js.map +1 -0
- package/dist/frame-buffer/buffer-utils.d.ts +34 -0
- package/dist/frame-buffer/buffer-utils.js +34 -0
- package/dist/frame-buffer/buffer-utils.js.map +1 -0
- package/dist/frame-buffer/buffer-writer.d.ts +37 -0
- package/dist/frame-buffer/buffer-writer.js +51 -0
- package/dist/frame-buffer/buffer-writer.js.map +1 -0
- package/dist/frame-buffer/buffer.d.ts +40 -0
- package/dist/frame-buffer/buffer.js +65 -0
- package/dist/frame-buffer/buffer.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/output-message.d.ts +42 -0
- package/dist/output-message.js +2 -0
- package/dist/output-message.js.map +1 -0
- package/dist/output-stream-node.d.ts +93 -0
- package/dist/output-stream-node.js +166 -0
- package/dist/output-stream-node.js.map +1 -0
- package/dist/output-stream-processor.d.ts +13 -0
- package/dist/output-stream-processor.js +99 -0
- package/dist/output-stream-processor.js.map +1 -0
- package/dist/stream-node-factory.d.ts +87 -0
- package/dist/stream-node-factory.js +113 -0
- package/dist/stream-node-factory.js.map +1 -0
- package/dist/write-strategy/manual.d.ts +36 -0
- package/dist/write-strategy/manual.js +43 -0
- package/dist/write-strategy/manual.js.map +1 -0
- package/dist/write-strategy/strategy.d.ts +23 -0
- package/dist/write-strategy/strategy.js +2 -0
- package/dist/write-strategy/strategy.js.map +1 -0
- package/dist/write-strategy/timed.d.ts +36 -0
- package/dist/write-strategy/timed.js +92 -0
- package/dist/write-strategy/timed.js.map +1 -0
- package/dist/write-strategy/worker/message.d.ts +54 -0
- package/dist/write-strategy/worker/message.js +2 -0
- package/dist/write-strategy/worker/message.js.map +1 -0
- package/dist/write-strategy/worker/strategy.d.ts +34 -0
- package/dist/write-strategy/worker/strategy.js +125 -0
- package/dist/write-strategy/worker/strategy.js.map +1 -0
- package/dist/write-strategy/worker/worker.d.ts +35 -0
- package/dist/write-strategy/worker/worker.js +135 -0
- package/dist/write-strategy/worker/worker.js.map +1 -0
- package/package.json +54 -0
- package/src/constants.ts +18 -0
- package/src/events.ts +43 -0
- package/src/frame-buffer/buffer-factory.ts +115 -0
- package/src/frame-buffer/buffer-filler.ts +14 -0
- package/src/frame-buffer/buffer-reader.ts +56 -0
- package/src/frame-buffer/buffer-utils.ts +48 -0
- package/src/frame-buffer/buffer-writer.ts +56 -0
- package/src/frame-buffer/buffer.ts +68 -0
- package/src/index.ts +9 -0
- package/src/output-message.ts +37 -0
- package/src/output-stream-node.ts +197 -0
- package/src/output-stream-processor.ts +124 -0
- package/src/stream-node-factory.ts +161 -0
- package/src/write-strategy/manual.ts +50 -0
- package/src/write-strategy/strategy.ts +26 -0
- package/src/write-strategy/timed.ts +103 -0
- package/src/write-strategy/worker/message.ts +48 -0
- package/src/write-strategy/worker/strategy.ts +154 -0
- package/src/write-strategy/worker/worker.ts +149 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { createFrameBufferWriter } from '../frame-buffer/buffer-factory';
|
|
2
|
+
/**
|
|
3
|
+
* PlayContext class
|
|
4
|
+
* Manages the buffer filling process using a timer.
|
|
5
|
+
*/
|
|
6
|
+
class PlayContext {
|
|
7
|
+
_timerId = 0;
|
|
8
|
+
/**
|
|
9
|
+
* Creates an instance of PlayContext.
|
|
10
|
+
* @param node - The OutputStreamNode instance.
|
|
11
|
+
* @param writer - The FrameBufferWriter instance.
|
|
12
|
+
* @param filler - The FrameBufferFiller instance.
|
|
13
|
+
* @param fillInterval - The interval in milliseconds for filling the buffer.
|
|
14
|
+
*/
|
|
15
|
+
constructor(node, writer, filler, fillInterval) {
|
|
16
|
+
this._timerId = window.setInterval(() => {
|
|
17
|
+
if (!this._timerId || !node.isStart) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
if (!filler.fill(writer)) {
|
|
22
|
+
node.stop(writer.totalFrames);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
this.cleanup();
|
|
27
|
+
node.stop();
|
|
28
|
+
}
|
|
29
|
+
}, fillInterval);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Cleans up the timer.
|
|
33
|
+
*/
|
|
34
|
+
cleanup() {
|
|
35
|
+
if (this._timerId) {
|
|
36
|
+
clearInterval(this._timerId);
|
|
37
|
+
this._timerId = 0;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* TimedBufferWriteStrategy class
|
|
43
|
+
* Implements the BufferWriteStrategy interface using a timer to manage buffer filling.
|
|
44
|
+
*/
|
|
45
|
+
export class TimedBufferWriteStrategy {
|
|
46
|
+
_writer;
|
|
47
|
+
_filler;
|
|
48
|
+
_interval;
|
|
49
|
+
_isContinuePlayback;
|
|
50
|
+
_context = null;
|
|
51
|
+
/**
|
|
52
|
+
* Creates an instance of TimedBufferWriteStrategy.
|
|
53
|
+
* @param config - The configuration for the filler frame buffer.
|
|
54
|
+
* @param filler - The FrameBufferFiller instance.
|
|
55
|
+
*/
|
|
56
|
+
constructor(config, filler) {
|
|
57
|
+
this._writer = createFrameBufferWriter(config);
|
|
58
|
+
this._filler = filler;
|
|
59
|
+
this._interval = config.fillInterval;
|
|
60
|
+
this._isContinuePlayback = this._filler.fill(this._writer);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Initializes the strategy.
|
|
64
|
+
* @returns A promise that resolves to true when initialization is complete.
|
|
65
|
+
*/
|
|
66
|
+
async onInit( /* node: OutputStreamNode */) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Starts the strategy.
|
|
71
|
+
* @param node - The OutputStreamNode instance.
|
|
72
|
+
* @returns A boolean indicating whether the strategy started successfully.
|
|
73
|
+
*/
|
|
74
|
+
onStart(node) {
|
|
75
|
+
if (this._context) {
|
|
76
|
+
throw new Error('Invalid state: context is not null.');
|
|
77
|
+
}
|
|
78
|
+
if (!this._isContinuePlayback) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
this._context = new PlayContext(node, this._writer, this._filler, this._interval);
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Stops the strategy.
|
|
86
|
+
*/
|
|
87
|
+
onStopped() {
|
|
88
|
+
this._context?.cleanup();
|
|
89
|
+
this._context = null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=timed.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timed.js","sourceRoot":"","sources":["../../src/write-strategy/timed.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uBAAuB,EAA2B,MAAM,gCAAgC,CAAA;AAKjG;;;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,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AE,CAAC;YAClB,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAA;QACxD,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,mBAAmB,EAAE,CAAC;YAC9B,OAAO,KAAK,CAAA;QACd,CAAC;QACD,IAAI,CAAC,QAAQ,GAAG,IAAI,WAAW,CAAC,IAAI,EAAE,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,CAAA;QACjF,OAAO,IAAI,CAAA;IACb,CAAC;IAED;;OAEG;IACH,SAAS;QACP,IAAI,CAAC,QAAQ,EAAE,OAAO,EAAE,CAAA;QACxB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAA;IACtB,CAAC;CACF"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { FillerFrameBufferConfig } from '../../frame-buffer/buffer-factory';
|
|
2
|
+
/**
|
|
3
|
+
* Message sent from the main thread to the worker.
|
|
4
|
+
*
|
|
5
|
+
* @typeParam FillerParams - The type of the parameters for the FrameBufferFiller.
|
|
6
|
+
*
|
|
7
|
+
* For 'init' messages:
|
|
8
|
+
* @property config - The configuration for the filler frame buffer.
|
|
9
|
+
* @property fillerParams - The parameters for the FrameBufferFiller.
|
|
10
|
+
*
|
|
11
|
+
* For 'start' messages:
|
|
12
|
+
*
|
|
13
|
+
* For 'stop' messages:
|
|
14
|
+
*
|
|
15
|
+
* Example:
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const message: MessageToWorker<YourFillerParams> = {
|
|
18
|
+
* type: 'init',
|
|
19
|
+
* config: yourConfig,
|
|
20
|
+
* fillerParams: yourFillerParams,
|
|
21
|
+
* };
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export type MessageToWorker<FillerParams> = {
|
|
25
|
+
type: 'init';
|
|
26
|
+
config: FillerFrameBufferConfig;
|
|
27
|
+
fillerParams: FillerParams;
|
|
28
|
+
} | {
|
|
29
|
+
type: 'start';
|
|
30
|
+
} | {
|
|
31
|
+
type: 'stop';
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Message sent from the worker to the main thread.
|
|
35
|
+
*
|
|
36
|
+
* For 'init-done' messages:
|
|
37
|
+
*
|
|
38
|
+
* For 'stop' messages:
|
|
39
|
+
* @property stopFrames - The position in frames to stop at.
|
|
40
|
+
*
|
|
41
|
+
* Example:
|
|
42
|
+
* ```typescript
|
|
43
|
+
* const message: MessageToStrategy = {
|
|
44
|
+
* type: 'stop',
|
|
45
|
+
* stopFrames: 1000n,
|
|
46
|
+
* };
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export type MessageToStrategy = {
|
|
50
|
+
type: 'init-done';
|
|
51
|
+
} | {
|
|
52
|
+
type: 'stop';
|
|
53
|
+
stopFrames: bigint;
|
|
54
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message.js","sourceRoot":"","sources":["../../../src/write-strategy/worker/message.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { BufferWriteStrategy } from '../strategy';
|
|
2
|
+
import type { FillerFrameBufferConfig } from '../../frame-buffer/buffer-factory';
|
|
3
|
+
import type { OutputStreamNode } from '../../output-stream-node';
|
|
4
|
+
/**
|
|
5
|
+
* WorkerBufferWriteStrategy class
|
|
6
|
+
* Implements the BufferWriteStrategy interface using a web worker to manage buffer filling.
|
|
7
|
+
*/
|
|
8
|
+
export declare class WorkerBufferWriteStrategy<FillerParam> implements BufferWriteStrategy {
|
|
9
|
+
private readonly _createPlayContext;
|
|
10
|
+
private _context;
|
|
11
|
+
/**
|
|
12
|
+
* Creates an instance of WorkerBufferWriteStrategy.
|
|
13
|
+
* @param config - The configuration for the filler frame buffer.
|
|
14
|
+
* @param workerConstructor - The constructor for the Worker.
|
|
15
|
+
* @param fillerParam - The parameters for the FrameBufferFiller.
|
|
16
|
+
*/
|
|
17
|
+
constructor(config: FillerFrameBufferConfig, workerConstructor: new () => Worker, fillerParam: FillerParam);
|
|
18
|
+
/**
|
|
19
|
+
* Initializes the strategy.
|
|
20
|
+
* @param node - The OutputStreamNode instance.
|
|
21
|
+
* @returns A promise that resolves to true when initialization is complete.
|
|
22
|
+
*/
|
|
23
|
+
onInit(node: OutputStreamNode): Promise<boolean>;
|
|
24
|
+
/**
|
|
25
|
+
* Starts the strategy.
|
|
26
|
+
* @param node - The OutputStreamNode instance.
|
|
27
|
+
* @returns A boolean indicating whether the strategy started successfully.
|
|
28
|
+
*/
|
|
29
|
+
onStart(): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Stops the strategy.
|
|
32
|
+
*/
|
|
33
|
+
onStopped(): void;
|
|
34
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PlayContext class
|
|
3
|
+
* Manages the buffer filling process within a web worker.
|
|
4
|
+
*/
|
|
5
|
+
class Context {
|
|
6
|
+
_node;
|
|
7
|
+
_config;
|
|
8
|
+
_fillerParam;
|
|
9
|
+
_worker;
|
|
10
|
+
/**
|
|
11
|
+
* Creates an instance of PlayContext.
|
|
12
|
+
* @param params - The parameters for the PlayContext.
|
|
13
|
+
*/
|
|
14
|
+
constructor(params) {
|
|
15
|
+
this._node = params.node;
|
|
16
|
+
this._config = params.config;
|
|
17
|
+
this._worker = new params.workerConstructor();
|
|
18
|
+
this._fillerParam = params.fillerParam;
|
|
19
|
+
this._worker.onmessage = this.handleWorkerMessage.bind(this);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Creates and initializes an instance of PlayContext.
|
|
23
|
+
* @param params - The parameters for the PlayContext.
|
|
24
|
+
* @returns A promise that resolves to an instance of PlayContext.
|
|
25
|
+
*/
|
|
26
|
+
static async create(params) {
|
|
27
|
+
const instance = new Context(params);
|
|
28
|
+
await instance.init();
|
|
29
|
+
return instance;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Handles messages from the worker.
|
|
33
|
+
* @param ev - The message event from the worker.
|
|
34
|
+
*/
|
|
35
|
+
handleWorkerMessage(ev) {
|
|
36
|
+
if (ev.data.type === 'stop') {
|
|
37
|
+
this._node.stop(ev.data.stopFrames);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Initializes the PlayContext by sending the 'init' message to the worker.
|
|
42
|
+
* @returns A promise that resolves when initialization is complete.
|
|
43
|
+
*/
|
|
44
|
+
init() {
|
|
45
|
+
return new Promise((resolve) => {
|
|
46
|
+
const message = {
|
|
47
|
+
type: 'init',
|
|
48
|
+
config: this._config,
|
|
49
|
+
fillerParams: this._fillerParam,
|
|
50
|
+
};
|
|
51
|
+
this._worker.postMessage(message);
|
|
52
|
+
const listener = (ev) => {
|
|
53
|
+
if (ev.data.type === 'init-done') {
|
|
54
|
+
this._worker.removeEventListener('message', listener);
|
|
55
|
+
resolve();
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
this._worker.addEventListener('message', listener);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Starts the buffer filling process by sending the 'start' message to the worker.
|
|
63
|
+
*/
|
|
64
|
+
start() {
|
|
65
|
+
const message = {
|
|
66
|
+
type: 'start',
|
|
67
|
+
};
|
|
68
|
+
this._worker.postMessage(message);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Stops the worker and terminates it.
|
|
72
|
+
*/
|
|
73
|
+
stopped() {
|
|
74
|
+
this._worker.onmessage = null;
|
|
75
|
+
this._worker.terminate();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* WorkerBufferWriteStrategy class
|
|
80
|
+
* Implements the BufferWriteStrategy interface using a web worker to manage buffer filling.
|
|
81
|
+
*/
|
|
82
|
+
export class WorkerBufferWriteStrategy {
|
|
83
|
+
_createPlayContext;
|
|
84
|
+
_context = null;
|
|
85
|
+
/**
|
|
86
|
+
* Creates an instance of WorkerBufferWriteStrategy.
|
|
87
|
+
* @param config - The configuration for the filler frame buffer.
|
|
88
|
+
* @param workerConstructor - The constructor for the Worker.
|
|
89
|
+
* @param fillerParam - The parameters for the FrameBufferFiller.
|
|
90
|
+
*/
|
|
91
|
+
constructor(config, workerConstructor, fillerParam) {
|
|
92
|
+
this._createPlayContext = (node) => Context.create({ node, config, workerConstructor, fillerParam });
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Initializes the strategy.
|
|
96
|
+
* @param node - The OutputStreamNode instance.
|
|
97
|
+
* @returns A promise that resolves to true when initialization is complete.
|
|
98
|
+
*/
|
|
99
|
+
async onInit(node) {
|
|
100
|
+
this._context = await this._createPlayContext(node);
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Starts the strategy.
|
|
105
|
+
* @param node - The OutputStreamNode instance.
|
|
106
|
+
* @returns A boolean indicating whether the strategy started successfully.
|
|
107
|
+
*/
|
|
108
|
+
onStart( /* node: OutputStreamNode */) {
|
|
109
|
+
if (!this._context) {
|
|
110
|
+
throw new Error('Invalid state: context is null.');
|
|
111
|
+
}
|
|
112
|
+
this._context.start();
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Stops the strategy.
|
|
117
|
+
*/
|
|
118
|
+
onStopped() {
|
|
119
|
+
if (!this._context) {
|
|
120
|
+
throw new Error('Invalid state: context is null.');
|
|
121
|
+
}
|
|
122
|
+
this._context.stopped();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=strategy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strategy.js","sourceRoot":"","sources":["../../../src/write-strategy/worker/strategy.ts"],"names":[],"mappings":"AAmBA;;;GAGG;AACH,MAAM,OAAO;IACM,KAAK,CAAkB;IACvB,OAAO,CAAyB;IAChC,YAAY,CAAc;IAC1B,OAAO,CAAQ;IAEhC;;;OAGG;IACH,YAAoB,MAAy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AC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACnB,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAA;QACpD,CAAC;QACD,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAA;IACzB,CAAC;CACF"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { FrameBufferFiller } from '../../frame-buffer/buffer-filler';
|
|
2
|
+
/**
|
|
3
|
+
* BufferFillWorker class
|
|
4
|
+
* Manages the communication between the main thread and the worker.
|
|
5
|
+
*/
|
|
6
|
+
export declare class BufferFillWorker<FillerParams> {
|
|
7
|
+
private _context;
|
|
8
|
+
private _frameBufferFillerGenerator;
|
|
9
|
+
private _init?;
|
|
10
|
+
/**
|
|
11
|
+
* Creates an instance of BufferFillWorker.
|
|
12
|
+
* @param generator - A generator function to create the FrameBufferFiller instance.
|
|
13
|
+
* @param init - An optional initialization function.
|
|
14
|
+
*/
|
|
15
|
+
constructor(generator: new (params: FillerParams) => FrameBufferFiller, init?: () => Promise<void>);
|
|
16
|
+
/**
|
|
17
|
+
* Handles incoming messages from the main thread.
|
|
18
|
+
* @param event - The message event from the main thread.
|
|
19
|
+
*/
|
|
20
|
+
private handleMessage;
|
|
21
|
+
/**
|
|
22
|
+
* Initializes the worker context.
|
|
23
|
+
* @param config - The configuration for the filler frame buffer.
|
|
24
|
+
* @param params - The parameters for the FrameBufferFiller.
|
|
25
|
+
*/
|
|
26
|
+
private init;
|
|
27
|
+
/**
|
|
28
|
+
* Starts the buffer filling process.
|
|
29
|
+
*/
|
|
30
|
+
private start;
|
|
31
|
+
/**
|
|
32
|
+
* Stops the buffer filling process.
|
|
33
|
+
*/
|
|
34
|
+
private stop;
|
|
35
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { createFrameBufferWriter } from '../../frame-buffer/buffer-factory';
|
|
2
|
+
/**
|
|
3
|
+
* Context class
|
|
4
|
+
* Manages the buffer filling process within a web worker.
|
|
5
|
+
*/
|
|
6
|
+
class Context {
|
|
7
|
+
_frameBufferWriter;
|
|
8
|
+
_isContinuePlayback;
|
|
9
|
+
_frameBufferFiller;
|
|
10
|
+
_fillInterval;
|
|
11
|
+
_timerId = 0;
|
|
12
|
+
/**
|
|
13
|
+
* Creates an instance ofContext.
|
|
14
|
+
* @param config - The configuration for the filler frame buffer.
|
|
15
|
+
* @param frameBufferFiller - The FrameBufferFiller instance.
|
|
16
|
+
*/
|
|
17
|
+
constructor(config, frameBufferFiller) {
|
|
18
|
+
this._frameBufferWriter = createFrameBufferWriter(config);
|
|
19
|
+
this._frameBufferFiller = frameBufferFiller;
|
|
20
|
+
this._fillInterval = config.fillInterval;
|
|
21
|
+
this._isContinuePlayback = this.fillBuffer();
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Starts the buffer filling process.
|
|
25
|
+
*/
|
|
26
|
+
start() {
|
|
27
|
+
if (!this._isContinuePlayback) {
|
|
28
|
+
this.stopFrames();
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
this._timerId = self.setInterval(() => {
|
|
32
|
+
if (!this._frameBufferFiller.fill(this._frameBufferWriter)) {
|
|
33
|
+
this.stopFrames();
|
|
34
|
+
self.clearInterval(this._timerId);
|
|
35
|
+
this._timerId = 0;
|
|
36
|
+
}
|
|
37
|
+
}, this._fillInterval);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Stops the buffer filling process.
|
|
41
|
+
*/
|
|
42
|
+
stopFrames() {
|
|
43
|
+
const message = {
|
|
44
|
+
type: 'stop',
|
|
45
|
+
stopFrames: this._frameBufferWriter.totalFrames,
|
|
46
|
+
};
|
|
47
|
+
self.postMessage(message);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Stops the buffer filling process and clears the interval timer.
|
|
51
|
+
*/
|
|
52
|
+
stop() {
|
|
53
|
+
if (this._timerId) {
|
|
54
|
+
self.clearInterval(this._timerId);
|
|
55
|
+
this._timerId = 0;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Fill the buffer with audio frames.
|
|
60
|
+
* @returns A boolean indicating whether to continue playback.
|
|
61
|
+
*/
|
|
62
|
+
fillBuffer() {
|
|
63
|
+
return this._frameBufferFiller.fill(this._frameBufferWriter);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* BufferFillWorker class
|
|
68
|
+
* Manages the communication between the main thread and the worker.
|
|
69
|
+
*/
|
|
70
|
+
export class BufferFillWorker {
|
|
71
|
+
_context = null;
|
|
72
|
+
_frameBufferFillerGenerator;
|
|
73
|
+
_init;
|
|
74
|
+
/**
|
|
75
|
+
* Creates an instance of BufferFillWorker.
|
|
76
|
+
* @param generator - A generator function to create the FrameBufferFiller instance.
|
|
77
|
+
* @param init - An optional initialization function.
|
|
78
|
+
*/
|
|
79
|
+
constructor(generator, init) {
|
|
80
|
+
this._frameBufferFillerGenerator = generator;
|
|
81
|
+
this._init = init;
|
|
82
|
+
self.onmessage = this.handleMessage.bind(this);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Handles incoming messages from the main thread.
|
|
86
|
+
* @param event - The message event from the main thread.
|
|
87
|
+
*/
|
|
88
|
+
async handleMessage(event) {
|
|
89
|
+
switch (event.data.type) {
|
|
90
|
+
case 'init':
|
|
91
|
+
this.init(event.data.config, event.data.fillerParams);
|
|
92
|
+
break;
|
|
93
|
+
case 'start':
|
|
94
|
+
this.start();
|
|
95
|
+
break;
|
|
96
|
+
case 'stop':
|
|
97
|
+
this.stop();
|
|
98
|
+
break;
|
|
99
|
+
default:
|
|
100
|
+
throw new Error(`Unknown message type: ${event.data}`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Initializes the worker context.
|
|
105
|
+
* @param config - The configuration for the filler frame buffer.
|
|
106
|
+
* @param params - The parameters for the FrameBufferFiller.
|
|
107
|
+
*/
|
|
108
|
+
async init(config, params) {
|
|
109
|
+
if (this._context) {
|
|
110
|
+
throw new Error('Error: Context is already created.');
|
|
111
|
+
}
|
|
112
|
+
await this._init?.();
|
|
113
|
+
this._context = new Context(config, new this._frameBufferFillerGenerator(params));
|
|
114
|
+
self.postMessage({ type: 'init-done' });
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Starts the buffer filling process.
|
|
118
|
+
*/
|
|
119
|
+
start() {
|
|
120
|
+
if (!this._context) {
|
|
121
|
+
throw new Error('Error: Context is not created.');
|
|
122
|
+
}
|
|
123
|
+
this._context.start();
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Stops the buffer filling process.
|
|
127
|
+
*/
|
|
128
|
+
stop() {
|
|
129
|
+
if (!this._context) {
|
|
130
|
+
throw new Error('Error: Context is not created.');
|
|
131
|
+
}
|
|
132
|
+
this._context.stop();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.js","sourceRoot":"","sources":["../../../src/write-strategy/worker/worker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAA2B,MAAM,mCAAmC,CAAA;AAKpG;;;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,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AAI,CAAC,QAAQ,EAAE,CAAC;YACnB,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAA;QACnD,CAAC;QACD,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAA;IACtB,CAAC;CACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ain1084/audio-worklet-stream",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"module": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"src",
|
|
10
|
+
"LICENSE",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"description": "A library for handling audio output stream processing using AudioWorklet in the browser. It provides functionalities for real-time audio output processing with customizable buffer management, ideal for developers working on web-based audio applications.",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"audio",
|
|
16
|
+
"stream",
|
|
17
|
+
"worklet",
|
|
18
|
+
"processor",
|
|
19
|
+
"real-time",
|
|
20
|
+
"web-audio",
|
|
21
|
+
"audio processing",
|
|
22
|
+
"ring buffer",
|
|
23
|
+
"audio context"
|
|
24
|
+
],
|
|
25
|
+
"author": "Seiji Ainoguchi <seiji.ainoguchi@gmail.com>",
|
|
26
|
+
"license": "(MIT OR Apache-2.0)",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "https://github.com/ain1084/audio-worklet-stream.git"
|
|
30
|
+
},
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/ain1084/audio-worklet-stream/issues"
|
|
33
|
+
},
|
|
34
|
+
"homepage": "https://github.com/ain1084/audio-worklet-stream#readme",
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc",
|
|
37
|
+
"watch": "tsc --watch",
|
|
38
|
+
"lint": "eslint .",
|
|
39
|
+
"prepublishOnly": "npm run build",
|
|
40
|
+
"docs": "typedoc"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@stylistic/eslint-plugin": "^2.3.0",
|
|
44
|
+
"@types/audioworklet": "^0.0.57",
|
|
45
|
+
"@types/node": "^20.14.10",
|
|
46
|
+
"@typescript-eslint/parser": "^7.16.0",
|
|
47
|
+
"eslint": "^8.57.0",
|
|
48
|
+
"globals": "^15.8.0",
|
|
49
|
+
"typedoc": "^0.26.4",
|
|
50
|
+
"typescript": "^5.5.3",
|
|
51
|
+
"typescript-eslint": "^7.16.0",
|
|
52
|
+
"vite": "^5.3.3"
|
|
53
|
+
}
|
|
54
|
+
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Constants file
|
|
2
|
+
|
|
3
|
+
const PREFIX = '@ain1084/audio-worklet-stream'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The name of the output stream processor, used as the name of the AudioWorkletProcessor.
|
|
7
|
+
*/
|
|
8
|
+
export const PROCESSOR_NAME = `${PREFIX}/output-stream-processor`
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The event type for the stop event.
|
|
12
|
+
*/
|
|
13
|
+
export const STOP_EVENT_TYPE = `${PREFIX}/stop`
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The event type for the underrun event.
|
|
17
|
+
*/
|
|
18
|
+
export const UNDERRUN_EVENT_TYPE = `${PREFIX}/underrun`
|
package/src/events.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { STOP_EVENT_TYPE, UNDERRUN_EVENT_TYPE } from './constants'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Represents a custom event indicating that the audio processing has stopped.
|
|
5
|
+
* This event is dispatched when the audio processing stops.
|
|
6
|
+
*/
|
|
7
|
+
export class StopEvent extends Event {
|
|
8
|
+
public static readonly type = STOP_EVENT_TYPE
|
|
9
|
+
/**
|
|
10
|
+
* Creates an instance of StopEvent.
|
|
11
|
+
* @param frames - The position in the audio stream where the stop occurred.
|
|
12
|
+
*/
|
|
13
|
+
public constructor(readonly frames: bigint) {
|
|
14
|
+
super(StopEvent.type)
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Represents a custom event indicating that an underrun occurred in the audio processing.
|
|
20
|
+
* UnderrunEvent is triggered not at the moment the underrun occurs, but when the underrun is resolved
|
|
21
|
+
* or just before the node stops.
|
|
22
|
+
*/
|
|
23
|
+
export class UnderrunEvent extends Event {
|
|
24
|
+
public static readonly type = UNDERRUN_EVENT_TYPE
|
|
25
|
+
/**
|
|
26
|
+
* Creates an instance of UnderrunEvent.
|
|
27
|
+
* @param frames - The number of frames that were not processed due to the underrun.
|
|
28
|
+
*/
|
|
29
|
+
public constructor(readonly frames: number) {
|
|
30
|
+
super(UnderrunEvent.type)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Extends the AudioWorkletNodeEventMap interface to include the custom events.
|
|
36
|
+
* This allows the custom events to be used in the context of AudioWorkletNode.
|
|
37
|
+
*/
|
|
38
|
+
declare global {
|
|
39
|
+
interface AudioWorkletNodeEventMap {
|
|
40
|
+
[StopEvent.type]: StopEvent
|
|
41
|
+
[UnderrunEvent.type]: UnderrunEvent
|
|
42
|
+
}
|
|
43
|
+
}
|