@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,149 @@
|
|
|
1
|
+
import { createFrameBufferWriter, FillerFrameBufferConfig } from '../../frame-buffer/buffer-factory'
|
|
2
|
+
import type { FrameBufferFiller } from '../../frame-buffer/buffer-filler'
|
|
3
|
+
import type { FrameBufferWriter } from '../../frame-buffer/buffer-writer'
|
|
4
|
+
import { MessageToStrategy, MessageToWorker } from './message'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Context class
|
|
8
|
+
* Manages the buffer filling process within a web worker.
|
|
9
|
+
*/
|
|
10
|
+
class Context {
|
|
11
|
+
private readonly _frameBufferWriter: FrameBufferWriter
|
|
12
|
+
private readonly _isContinuePlayback: boolean
|
|
13
|
+
private readonly _frameBufferFiller: FrameBufferFiller
|
|
14
|
+
private readonly _fillInterval: number
|
|
15
|
+
private _timerId: number = 0
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Creates an instance ofContext.
|
|
19
|
+
* @param config - The configuration for the filler frame buffer.
|
|
20
|
+
* @param frameBufferFiller - The FrameBufferFiller instance.
|
|
21
|
+
*/
|
|
22
|
+
constructor(config: FillerFrameBufferConfig, frameBufferFiller: FrameBufferFiller) {
|
|
23
|
+
this._frameBufferWriter = createFrameBufferWriter(config)
|
|
24
|
+
this._frameBufferFiller = frameBufferFiller
|
|
25
|
+
this._fillInterval = config.fillInterval
|
|
26
|
+
this._isContinuePlayback = this.fillBuffer()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Starts the buffer filling process.
|
|
31
|
+
*/
|
|
32
|
+
public start() {
|
|
33
|
+
if (!this._isContinuePlayback) {
|
|
34
|
+
this.stopFrames()
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
this._timerId = self.setInterval(() => {
|
|
38
|
+
if (!this._frameBufferFiller.fill(this._frameBufferWriter)) {
|
|
39
|
+
this.stopFrames()
|
|
40
|
+
self.clearInterval(this._timerId)
|
|
41
|
+
this._timerId = 0
|
|
42
|
+
}
|
|
43
|
+
}, this._fillInterval)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Stops the buffer filling process.
|
|
48
|
+
*/
|
|
49
|
+
public stopFrames() {
|
|
50
|
+
const message: MessageToStrategy = {
|
|
51
|
+
type: 'stop',
|
|
52
|
+
stopFrames: this._frameBufferWriter.totalFrames,
|
|
53
|
+
}
|
|
54
|
+
self.postMessage(message)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Stops the buffer filling process and clears the interval timer.
|
|
59
|
+
*/
|
|
60
|
+
public stop() {
|
|
61
|
+
if (this._timerId) {
|
|
62
|
+
self.clearInterval(this._timerId)
|
|
63
|
+
this._timerId = 0
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Fill the buffer with audio frames.
|
|
69
|
+
* @returns A boolean indicating whether to continue playback.
|
|
70
|
+
*/
|
|
71
|
+
private fillBuffer(): boolean {
|
|
72
|
+
return this._frameBufferFiller.fill(this._frameBufferWriter)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* BufferFillWorker class
|
|
78
|
+
* Manages the communication between the main thread and the worker.
|
|
79
|
+
*/
|
|
80
|
+
export class BufferFillWorker<FillerParams> {
|
|
81
|
+
private _context: Context | null = null
|
|
82
|
+
private _frameBufferFillerGenerator: new (params: FillerParams) => FrameBufferFiller
|
|
83
|
+
private _init?: () => Promise<void>
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Creates an instance of BufferFillWorker.
|
|
87
|
+
* @param generator - A generator function to create the FrameBufferFiller instance.
|
|
88
|
+
* @param init - An optional initialization function.
|
|
89
|
+
*/
|
|
90
|
+
constructor(generator: new (params: FillerParams) => FrameBufferFiller, init?: () => Promise<void>) {
|
|
91
|
+
this._frameBufferFillerGenerator = generator
|
|
92
|
+
this._init = init
|
|
93
|
+
self.onmessage = this.handleMessage.bind(this)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Handles incoming messages from the main thread.
|
|
98
|
+
* @param event - The message event from the main thread.
|
|
99
|
+
*/
|
|
100
|
+
private async handleMessage(event: MessageEvent<MessageToWorker<FillerParams>>): Promise<void> {
|
|
101
|
+
switch (event.data.type) {
|
|
102
|
+
case 'init':
|
|
103
|
+
this.init(event.data.config, event.data.fillerParams)
|
|
104
|
+
break
|
|
105
|
+
case 'start':
|
|
106
|
+
this.start()
|
|
107
|
+
break
|
|
108
|
+
case 'stop':
|
|
109
|
+
this.stop()
|
|
110
|
+
break
|
|
111
|
+
default:
|
|
112
|
+
throw new Error(`Unknown message type: ${event.data}`)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Initializes the worker context.
|
|
118
|
+
* @param config - The configuration for the filler frame buffer.
|
|
119
|
+
* @param params - The parameters for the FrameBufferFiller.
|
|
120
|
+
*/
|
|
121
|
+
private async init(config: FillerFrameBufferConfig, params: FillerParams) {
|
|
122
|
+
if (this._context) {
|
|
123
|
+
throw new Error('Error: Context is already created.')
|
|
124
|
+
}
|
|
125
|
+
await this._init?.()
|
|
126
|
+
this._context = new Context(config, new this._frameBufferFillerGenerator(params))
|
|
127
|
+
self.postMessage({ type: 'init-done' } as MessageToStrategy)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Starts the buffer filling process.
|
|
132
|
+
*/
|
|
133
|
+
private start(): void {
|
|
134
|
+
if (!this._context) {
|
|
135
|
+
throw new Error('Error: Context is not created.')
|
|
136
|
+
}
|
|
137
|
+
this._context.start()
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Stops the buffer filling process.
|
|
142
|
+
*/
|
|
143
|
+
private stop(): void {
|
|
144
|
+
if (!this._context) {
|
|
145
|
+
throw new Error('Error: Context is not created.')
|
|
146
|
+
}
|
|
147
|
+
this._context.stop()
|
|
148
|
+
}
|
|
149
|
+
}
|