@ain1084/audio-worklet-stream 1.0.3 → 1.0.5

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.
Files changed (48) hide show
  1. package/README.md +30 -3
  2. package/dist/frame-buffer/buffer-config.d.ts +0 -7
  3. package/dist/frame-buffer/buffer-config.js +7 -14
  4. package/dist/frame-buffer/buffer-config.js.map +1 -1
  5. package/dist/frame-buffer/buffer-reader.d.ts +8 -5
  6. package/dist/frame-buffer/buffer-reader.js +11 -7
  7. package/dist/frame-buffer/buffer-reader.js.map +1 -1
  8. package/dist/frame-buffer/buffer-writer.d.ts +8 -5
  9. package/dist/frame-buffer/buffer-writer.js +11 -7
  10. package/dist/frame-buffer/buffer-writer.js.map +1 -1
  11. package/dist/index.d.ts +1 -1
  12. package/dist/index.js.map +1 -1
  13. package/dist/output-stream-node.d.ts +3 -2
  14. package/dist/output-stream-node.js +5 -8
  15. package/dist/output-stream-node.js.map +1 -1
  16. package/dist/output-stream-processor.d.ts +1 -13
  17. package/dist/output-stream-processor.js +1 -5
  18. package/dist/output-stream-processor.js.map +1 -1
  19. package/dist/stream-node-factory.d.ts +23 -14
  20. package/dist/stream-node-factory.js +3 -3
  21. package/dist/stream-node-factory.js.map +1 -1
  22. package/dist/write-strategy/manual-strategy.d.ts +36 -0
  23. package/dist/write-strategy/manual-strategy.js +43 -0
  24. package/dist/write-strategy/manual-strategy.js.map +1 -0
  25. package/dist/write-strategy/timed-strategy.d.ts +36 -0
  26. package/dist/write-strategy/timed-strategy.js +92 -0
  27. package/dist/write-strategy/timed-strategy.js.map +1 -0
  28. package/dist/write-strategy/worker/worker-strategy.d.ts +34 -0
  29. package/dist/write-strategy/worker/worker-strategy.js +125 -0
  30. package/dist/write-strategy/worker/worker-strategy.js.map +1 -0
  31. package/dist/write-strategy/worker/worker.d.ts +4 -2
  32. package/dist/write-strategy/worker/worker.js +6 -4
  33. package/dist/write-strategy/worker/worker.js.map +1 -1
  34. package/dist/write-strategy/worker-strategy.d.ts +34 -0
  35. package/dist/write-strategy/worker-strategy.js +125 -0
  36. package/dist/write-strategy/worker-strategy.js.map +1 -0
  37. package/package.json +11 -9
  38. package/src/frame-buffer/buffer-config.ts +7 -20
  39. package/src/frame-buffer/buffer-reader.ts +11 -7
  40. package/src/frame-buffer/buffer-writer.ts +11 -7
  41. package/src/index.ts +1 -0
  42. package/src/output-stream-node.ts +5 -9
  43. package/src/output-stream-processor.ts +2 -23
  44. package/src/stream-node-factory.ts +26 -17
  45. package/src/write-strategy/{manual.ts → manual-strategy.ts} +3 -3
  46. package/src/write-strategy/{timed.ts → timed-strategy.ts} +5 -5
  47. package/src/write-strategy/worker/worker.ts +7 -5
  48. package/src/write-strategy/{worker/strategy.ts → worker-strategy.ts} +4 -4
@@ -1,5 +1,4 @@
1
1
  import type { MessageToProcessor, MessageToAudioNode } from './output-message'
2
- import type { OutputStreamProcessorOptions } from './output-stream-processor'
3
2
  import { PROCESSOR_NAME } from './constants'
4
3
  import { StopEvent, UnderrunEvent } from './events'
5
4
  import { BufferWriteStrategy } from './write-strategy/strategy'
@@ -41,13 +40,9 @@ export class OutputStreamNode extends AudioWorkletNode {
41
40
  bufferConfig: FrameBufferConfig,
42
41
  strategy: BufferWriteStrategy,
43
42
  ) {
44
- const processorOptions: OutputStreamProcessorOptions = {
45
- ...bufferConfig,
46
- totalFrames: bufferConfig.totalReadFrames,
47
- }
48
43
  super(baseAudioContext, PROCESSOR_NAME, {
49
44
  outputChannelCount: [bufferConfig.samplesPerFrame],
50
- processorOptions,
45
+ processorOptions: bufferConfig,
51
46
  })
52
47
  this._strategy = strategy
53
48
  this._totalWriteFrames = bufferConfig.totalWriteFrames
@@ -56,14 +51,15 @@ export class OutputStreamNode extends AudioWorkletNode {
56
51
  }
57
52
 
58
53
  /**
54
+ * @internal
59
55
  * Creates an instance of OutputStreamNode.
60
56
  * @param audioContext - The audio context to use.
61
- * @param info - The configuration for the buffer.
57
+ * @param config - The configuration for the buffer.
62
58
  * @param strategy - The strategy for writing to the buffer.
63
59
  * @returns A promise that resolves to an instance of OutputStreamNode.
64
60
  */
65
- public static async create(audioContext: BaseAudioContext, info: FrameBufferConfig, strategy: BufferWriteStrategy): Promise<OutputStreamNode> {
66
- const node = new OutputStreamNode(audioContext, info, strategy)
61
+ public static async create(audioContext: BaseAudioContext, config: FrameBufferConfig, strategy: BufferWriteStrategy): Promise<OutputStreamNode> {
62
+ const node = new OutputStreamNode(audioContext, config, strategy)
67
63
  if (!(await strategy.onInit(node))) {
68
64
  throw new Error('Failed to onInit.')
69
65
  }
@@ -1,28 +1,7 @@
1
1
  import { FrameBufferReader } from './frame-buffer/buffer-reader'
2
2
  import { PROCESSOR_NAME } from './constants'
3
3
  import type { MessageToAudioNode, MessageToProcessor } from './output-message'
4
- import { FrameBuffer } from './frame-buffer/buffer'
5
-
6
- /**
7
- * Options for the OutputStreamProcessor
8
- * @property sampleBuffer - The shared buffer for audio data frames.
9
- * @property samplesPerFrame - The number of samples per frame.
10
- * @property usedFramesInBuffer - The usage count of the frames in the buffer.
11
- * @property totalFrames - The current position in the buffer, in frames.
12
- */
13
- export type OutputStreamProcessorOptions = Readonly<{
14
- sampleBuffer: Float32Array
15
- samplesPerFrame: number
16
- usedFramesInBuffer: Uint32Array
17
- totalFrames: BigUint64Array
18
- }>
19
-
20
- const createFrameBufferReader = (options: OutputStreamProcessorOptions) => {
21
- return new FrameBufferReader(
22
- new FrameBuffer(options.sampleBuffer, options.samplesPerFrame),
23
- options.usedFramesInBuffer, options.totalFrames,
24
- )
25
- }
4
+ import type { FrameBufferConfig } from './frame-buffer/buffer-config'
26
5
 
27
6
  /**
28
7
  * OutputStreamProcessor class
@@ -41,7 +20,7 @@ class OutputStreamProcessor extends AudioWorkletProcessor {
41
20
  */
42
21
  constructor(options: AudioWorkletNodeOptions) {
43
22
  super()
44
- this._frameReader = createFrameBufferReader(options.processorOptions as OutputStreamProcessorOptions)
23
+ this._frameReader = new FrameBufferReader(options.processorOptions as FrameBufferConfig)
45
24
  this.port.onmessage = this.handleMessage.bind(this)
46
25
  }
47
26
 
@@ -1,43 +1,52 @@
1
1
  import processor from './output-stream-processor?worker&url'
2
- import { ManualBufferWriteStrategy } from './write-strategy/manual'
2
+ import { ManualBufferWriteStrategy } from './write-strategy/manual-strategy'
3
3
  import { createFillerFrameBufferConfig, createFrameBufferConfig } from './frame-buffer/buffer-config'
4
4
  import type { OutputStreamNode } from './output-stream-node'
5
- import { TimedBufferWriteStrategy } from './write-strategy/timed'
5
+ import { TimedBufferWriteStrategy } from './write-strategy/timed-strategy'
6
6
  import type { FrameBufferFiller } from './frame-buffer/buffer-filler'
7
7
  import { FrameBufferWriter } from './frame-buffer/buffer-writer'
8
- import { WorkerBufferWriteStrategy } from './write-strategy/worker/strategy'
8
+ import { WorkerBufferWriteStrategy } from './write-strategy/worker-strategy'
9
9
 
10
10
  /**
11
11
  * Parameters for creating a manual buffer node.
12
+ * Used in the {@link StreamNodeFactory.createManualBufferNode} function.
12
13
  */
13
- export type ManualBufferNodeParams = Readonly<{
14
+ export type ManualBufferNodeParams = {
14
15
  /** The number of audio channels. */
15
- channelCount: number
16
+ readonly channelCount: number
16
17
  /** The size of the frame buffer in frames. */
17
- frameBufferSize: number
18
- }>
18
+ readonly frameBufferSize: number
19
+ }
19
20
 
20
21
  /**
21
22
  * Parameters for creating a timed buffer node.
23
+ * Used in the {@link StreamNodeFactory.createTimedBufferNode} function.
22
24
  */
23
- export type TimedBufferNodeParams = Readonly<{
25
+ export type TimedBufferNodeParams = {
24
26
  /** The number of audio channels. */
25
- channelCount: number
27
+ readonly channelCount: number
26
28
  /** Optional. The number of chunks in the frame buffer. */
27
- frameBufferChunks?: number
29
+ readonly frameBufferChunks?: number
28
30
  /** Optional. The interval in milliseconds for filling the buffer. */
29
- fillInterval?: number
31
+ readonly fillInterval?: number
30
32
  /** Optional. The sample rate of the audio context. */
31
- sampleRate?: number
32
- }>
33
+ readonly sampleRate?: number
34
+ }
33
35
 
34
36
  /**
35
37
  * Parameters for creating a worker buffer node.
38
+ * Used in the {@link StreamNodeFactory.createWorkerBufferNode} function.
39
+ *
40
+ * @template FillerParams - The parameters used by the FrameBufferFiller in the worker.
36
41
  */
37
- export type WorkerBufferNodeParams<T> = TimedBufferNodeParams & Readonly<{
38
- /** Parameters specific to the filler used in the worker. */
39
- fillerParams: T
40
- }>
42
+ export type WorkerBufferNodeParams<FillerParams> = TimedBufferNodeParams & {
43
+ /**
44
+ * Parameters passed to the constructor when the Worker instantiates the FrameBufferFiller implementation class.
45
+ * Note: The values passed as FillerParams must be serializable (e.g., primitives, arrays, objects).
46
+ * Non-serializable values such as functions or DOM elements cannot be passed.
47
+ */
48
+ readonly fillerParams: FillerParams
49
+ }
41
50
 
42
51
  /**
43
52
  * StreamNodeFactory class
@@ -1,5 +1,5 @@
1
- import { BufferWriteStrategy } from './strategy'
2
- import { createFrameBufferWriter, FrameBufferConfig } from '../frame-buffer/buffer-config'
1
+ import type { BufferWriteStrategy } from './strategy'
2
+ import type { FrameBufferConfig } from '../frame-buffer/buffer-config'
3
3
  import { FrameBufferWriter } from '../frame-buffer/buffer-writer'
4
4
 
5
5
  /**
@@ -14,7 +14,7 @@ export class ManualBufferWriteStrategy implements BufferWriteStrategy {
14
14
  * @param config - The configuration for the frame buffer.
15
15
  */
16
16
  constructor(config: FrameBufferConfig) {
17
- this._writer = createFrameBufferWriter(config)
17
+ this._writer = new FrameBufferWriter(config)
18
18
  }
19
19
 
20
20
  /**
@@ -1,8 +1,8 @@
1
- import { BufferWriteStrategy } from './strategy'
2
- import { createFrameBufferWriter, FillerFrameBufferConfig } from '../frame-buffer/buffer-config'
1
+ import type { BufferWriteStrategy } from './strategy'
2
+ import type { FillerFrameBufferConfig } from '../frame-buffer/buffer-config'
3
3
  import { FrameBufferWriter } from '../frame-buffer/buffer-writer'
4
- import { FrameBufferFiller } from '../frame-buffer/buffer-filler'
5
- import { OutputStreamNode } from '../output-stream-node'
4
+ import type { FrameBufferFiller } from '../frame-buffer/buffer-filler'
5
+ import type { OutputStreamNode } from '../output-stream-node'
6
6
 
7
7
  /**
8
8
  * PlayContext class
@@ -63,7 +63,7 @@ export class TimedBufferWriteStrategy implements BufferWriteStrategy {
63
63
  * @param filler - The FrameBufferFiller instance.
64
64
  */
65
65
  constructor(config: FillerFrameBufferConfig, filler: FrameBufferFiller) {
66
- this._writer = createFrameBufferWriter(config)
66
+ this._writer = new FrameBufferWriter(config)
67
67
  this._filler = filler
68
68
  this._interval = config.fillInterval
69
69
  this._isContinuePlayback = this._filler.fill(this._writer)
@@ -1,6 +1,6 @@
1
- import { createFrameBufferWriter, FillerFrameBufferConfig } from '../../frame-buffer/buffer-config'
1
+ import type { FillerFrameBufferConfig } from '../../frame-buffer/buffer-config'
2
2
  import type { FrameBufferFiller } from '../../frame-buffer/buffer-filler'
3
- import type { FrameBufferWriter } from '../../frame-buffer/buffer-writer'
3
+ import { FrameBufferWriter } from '../../frame-buffer/buffer-writer'
4
4
  import { MessageToStrategy, MessageToWorker } from './message'
5
5
 
6
6
  /**
@@ -20,7 +20,7 @@ class Context {
20
20
  * @param frameBufferFiller - The FrameBufferFiller instance.
21
21
  */
22
22
  constructor(config: FillerFrameBufferConfig, frameBufferFiller: FrameBufferFiller) {
23
- this._frameBufferWriter = createFrameBufferWriter(config)
23
+ this._frameBufferWriter = new FrameBufferWriter(config)
24
24
  this._frameBufferFiller = frameBufferFiller
25
25
  this._fillInterval = config.fillInterval
26
26
  this._isContinuePlayback = this.fillBuffer()
@@ -76,7 +76,9 @@ class Context {
76
76
  /**
77
77
  * BufferFillWorker class
78
78
  * Manages the communication between the main thread and the worker.
79
- */
79
+ *
80
+ * @template FillerParams - The parameters used by the FrameBufferFiller in the worker.
81
+ */
80
82
  export class BufferFillWorker<FillerParams> {
81
83
  private _context: Context | null = null
82
84
  private _frameBufferFillerGenerator: new (params: FillerParams) => FrameBufferFiller
@@ -84,7 +86,7 @@ export class BufferFillWorker<FillerParams> {
84
86
 
85
87
  /**
86
88
  * Creates an instance of BufferFillWorker.
87
- * @param generator - A generator function to create the FrameBufferFiller instance.
89
+ * @param generator - A constructor function to create an instance of the FrameBufferFiller class.
88
90
  * @param init - An optional initialization function.
89
91
  */
90
92
  constructor(generator: new (params: FillerParams) => FrameBufferFiller, init?: () => Promise<void>) {
@@ -1,7 +1,7 @@
1
- import type { BufferWriteStrategy } from '../strategy'
2
- import type { FillerFrameBufferConfig } from '../../frame-buffer/buffer-config'
3
- import type { MessageToStrategy, MessageToWorker } from './message'
4
- import type { OutputStreamNode } from '../../output-stream-node'
1
+ import type { BufferWriteStrategy } from './strategy'
2
+ import type { FillerFrameBufferConfig } from '../frame-buffer/buffer-config'
3
+ import type { MessageToStrategy, MessageToWorker } from './worker/message'
4
+ import type { OutputStreamNode } from '../output-stream-node'
5
5
 
6
6
  /**
7
7
  * Parameters for creating a PlayContext.