@ain1084/audio-worklet-stream 0.1.8 → 1.0.1

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 (57) hide show
  1. package/README.md +23 -40
  2. package/dist/events.d.ts +6 -6
  3. package/dist/events.js +8 -8
  4. package/dist/events.js.map +1 -1
  5. package/dist/frame-buffer/buffer-config.d.ts +67 -0
  6. package/dist/frame-buffer/buffer-config.js +64 -0
  7. package/dist/frame-buffer/buffer-config.js.map +1 -0
  8. package/dist/frame-buffer/buffer-factory.d.ts +1 -2
  9. package/dist/frame-buffer/buffer-factory.js +6 -3
  10. package/dist/frame-buffer/buffer-factory.js.map +1 -1
  11. package/dist/frame-buffer/buffer-reader.d.ts +30 -10
  12. package/dist/frame-buffer/buffer-reader.js +38 -16
  13. package/dist/frame-buffer/buffer-reader.js.map +1 -1
  14. package/dist/frame-buffer/buffer-utils.js +2 -2
  15. package/dist/frame-buffer/buffer-utils.js.map +1 -1
  16. package/dist/frame-buffer/buffer-writer.d.ts +32 -12
  17. package/dist/frame-buffer/buffer-writer.js +40 -18
  18. package/dist/frame-buffer/buffer-writer.js.map +1 -1
  19. package/dist/frame-buffer/buffer.d.ts +33 -22
  20. package/dist/frame-buffer/buffer.js +50 -40
  21. package/dist/frame-buffer/buffer.js.map +1 -1
  22. package/dist/output-message.d.ts +9 -9
  23. package/dist/output-stream-node.d.ts +13 -19
  24. package/dist/output-stream-node.js +20 -26
  25. package/dist/output-stream-node.js.map +1 -1
  26. package/dist/output-stream-processor.js +25 -18
  27. package/dist/output-stream-processor.js.map +1 -1
  28. package/dist/stream-node-factory.d.ts +10 -0
  29. package/dist/stream-node-factory.js +27 -12
  30. package/dist/stream-node-factory.js.map +1 -1
  31. package/dist/write-strategy/manual.d.ts +1 -1
  32. package/dist/write-strategy/manual.js +1 -1
  33. package/dist/write-strategy/manual.js.map +1 -1
  34. package/dist/write-strategy/timed.d.ts +1 -1
  35. package/dist/write-strategy/timed.js +1 -1
  36. package/dist/write-strategy/timed.js.map +1 -1
  37. package/dist/write-strategy/worker/message.d.ts +1 -1
  38. package/dist/write-strategy/worker/strategy.d.ts +1 -1
  39. package/dist/write-strategy/worker/worker.js +1 -1
  40. package/dist/write-strategy/worker/worker.js.map +1 -1
  41. package/package.json +9 -9
  42. package/src/events.ts +4 -4
  43. package/src/frame-buffer/buffer-config.ts +129 -0
  44. package/src/frame-buffer/buffer-reader.ts +38 -16
  45. package/src/frame-buffer/buffer-writer.ts +40 -18
  46. package/src/frame-buffer/buffer.ts +51 -40
  47. package/src/output-message.ts +9 -9
  48. package/src/output-stream-node.ts +23 -29
  49. package/src/output-stream-processor.ts +25 -18
  50. package/src/stream-node-factory.ts +33 -15
  51. package/src/write-strategy/manual.ts +1 -1
  52. package/src/write-strategy/timed.ts +1 -1
  53. package/src/write-strategy/worker/message.ts +1 -1
  54. package/src/write-strategy/worker/strategy.ts +1 -1
  55. package/src/write-strategy/worker/worker.ts +1 -1
  56. package/src/frame-buffer/buffer-factory.ts +0 -115
  57. package/src/frame-buffer/buffer-utils.ts +0 -48
@@ -1,115 +0,0 @@
1
- import { FrameBuffer } from './buffer'
2
- import { FrameBufferWriter } from './buffer-writer'
3
-
4
- /**
5
- * Parameters for creating a FrameBuffer.
6
- * @property frameBufferSize - The size of the frame buffer.
7
- * @property channelCount - The number of audio channels.
8
- */
9
- export type FrameBufferParams = Readonly<{
10
- frameBufferSize: number
11
- channelCount: number
12
- }>
13
-
14
- /**
15
- * Parameters for creating a FillerFrameBuffer.
16
- * @property channelCount - The number of audio channels.
17
- * @property fillInterval - The interval in milliseconds for filling the buffer.
18
- * @property sampleRate - The sample rate of the audio context.
19
- * @property frameBufferChunks - The number of chunks in the frame buffer.
20
- */
21
- export type FillerFrameBufferParams = Readonly<{
22
- channelCount: number
23
- fillInterval?: number
24
- sampleRate?: number
25
- frameBufferChunks?: number
26
- }>
27
-
28
- /**
29
- * Configuration for a FrameBuffer.
30
- * This configuration is returned by the createFrameBufferConfig function.
31
- * @property sampleBuffer - The shared buffer for audio data frames.
32
- * @property samplesPerFrame - The number of samples per frame.
33
- * @property usedFramesInBuffer - The usage count of the frames in the buffer.
34
- * @property totalReadFrames - The total frames read from the buffer.
35
- * @property totalWriteFrames - The total frames written to the buffer.
36
- */
37
- export type FrameBufferConfig = Readonly<{
38
- sampleBuffer: Float32Array
39
- samplesPerFrame: number
40
- usedFramesInBuffer: Uint32Array
41
- totalReadFrames: BigUint64Array
42
- totalWriteFrames: BigUint64Array
43
- }>
44
-
45
- /**
46
- * Configuration for a FillerFrameBuffer.
47
- * This configuration is returned by the createFillerFrameBufferConfig function.
48
- * @property sampleRate - The sample rate of the audio context.
49
- * @property fillInterval - The interval in milliseconds for filling the buffer.
50
- */
51
- export type FillerFrameBufferConfig = FrameBufferConfig & Readonly<{
52
- sampleRate: number
53
- fillInterval: number
54
- }>
55
-
56
- /**
57
- * Creates a FrameBufferWriter instance.
58
- * @param config - The configuration for the FrameBuffer.
59
- * @returns A new instance of FrameBufferWriter.
60
- */
61
- export const createFrameBufferWriter = (config: FrameBufferConfig): FrameBufferWriter => {
62
- return new FrameBufferWriter(
63
- new FrameBuffer(config.sampleBuffer, config.samplesPerFrame),
64
- config.usedFramesInBuffer, config.totalWriteFrames,
65
- )
66
- }
67
-
68
- /**
69
- * FrameBufferFactory class
70
- * Provides static methods to create frame buffer configurations and writers.
71
- */
72
- export class FrameBufferFactory {
73
- public static readonly DEFAULT_FILL_INTERVAL_MS = 20
74
- public static readonly DEFAULT_FRAME_BUFFER_CHUNKS = 5
75
- public static readonly PROCESS_UNIT = 128
76
-
77
- /**
78
- * Creates a FrameBufferConfig instance.
79
- * @param params - The parameters for the FrameBuffer.
80
- * @returns A new instance of FrameBufferConfig.
81
- */
82
- public static createFrameBufferConfig(params: FrameBufferParams): FrameBufferConfig {
83
- return {
84
- sampleBuffer: new Float32Array(
85
- new SharedArrayBuffer(params.frameBufferSize * params.channelCount * Float32Array.BYTES_PER_ELEMENT),
86
- ),
87
- samplesPerFrame: params.channelCount,
88
- usedFramesInBuffer: new Uint32Array(new SharedArrayBuffer(Uint32Array.BYTES_PER_ELEMENT)),
89
- totalReadFrames: new BigUint64Array(new SharedArrayBuffer(BigUint64Array.BYTES_PER_ELEMENT)),
90
- totalWriteFrames: new BigUint64Array(new SharedArrayBuffer(BigUint64Array.BYTES_PER_ELEMENT)),
91
- }
92
- }
93
-
94
- /**
95
- * Creates a FillerFrameBufferConfig instance.
96
- * @param defaultSampleRate - The sample rate of the audio context.
97
- * @param params - The parameters for the FillerFrameBuffer.
98
- * @returns A new instance of FillerFrameBufferConfig.
99
- */
100
- public static createFillerFrameBufferConfig(defaultSampleRate: number, params: FillerFrameBufferParams): FillerFrameBufferConfig {
101
- const sampleRate = params.sampleRate ?? defaultSampleRate
102
- const intervalMillisecond = params.fillInterval ?? FrameBufferFactory.DEFAULT_FILL_INTERVAL_MS
103
- const frameBufferSize = Math.floor(
104
- sampleRate * intervalMillisecond / 1000 + (FrameBufferFactory.PROCESS_UNIT - 1),
105
- ) & ~(FrameBufferFactory.PROCESS_UNIT - 1)
106
- const frameBufferChunkCount = params.frameBufferChunks ?? FrameBufferFactory.DEFAULT_FRAME_BUFFER_CHUNKS
107
- const config = FrameBufferFactory.createFrameBufferConfig(
108
- { frameBufferSize: frameBufferSize * frameBufferChunkCount, channelCount: params.channelCount })
109
- return {
110
- ...config,
111
- sampleRate,
112
- fillInterval: intervalMillisecond,
113
- }
114
- }
115
- }
@@ -1,48 +0,0 @@
1
- import type { FrameBuffer } from './buffer'
2
-
3
- /**
4
- * Type definition for the callback function used in enumFrames.
5
- * The callback processes each section of the frame buffer.
6
- * @param frame - An object containing:
7
- * - buffer: The FrameBuffer instance.
8
- * - index: The starting index in the buffer.
9
- * - frames: The number of frames in the section.
10
- * @param offset - The offset in the buffer from the start of processing.
11
- * @returns The number of frames processed.
12
- */
13
- export type FrameCallback = (frame: { buffer: FrameBuffer, index: number, frames: number }, offset: number) => number
14
-
15
- /**
16
- * Processes sections of a Float32Array buffer using a callback function.
17
- * This function is intended for internal use only.
18
- *
19
- * @param buffer - The FrameBuffer to process. This buffer is expected to be shared.
20
- * @param startIndex - The starting index in the buffer from where processing should begin.
21
- * @param availableFrames - The total number of frames available to process in the buffer.
22
- * @param frameCallback - The callback function to process each section of the buffer.
23
- * It should return the number of frames processed.
24
- * @returns An object containing:
25
- * - frames: The number of frames successfully processed.
26
- * - nextIndex: The index in the buffer for the next processing cycle.
27
- * @throws RangeError - If the frameCallback returns a processed length greater than the part length.
28
- */
29
- export const enumFrames = (buffer: FrameBuffer, startIndex: number, availableFrames: number, frameCallback: FrameCallback):
30
- { frames: number, nextIndex: number } => {
31
- let totalFrames = 0
32
- while (totalFrames < availableFrames) {
33
- // Determine the length of the current section to process
34
- const sectionFrames = Math.min(buffer.length - startIndex, availableFrames - totalFrames)
35
- // Process the current section using the frameCallback function
36
- const processedFrames = frameCallback({ buffer, index: startIndex, frames: sectionFrames }, totalFrames)
37
- // Ensure the processed length does not exceed the section length
38
- if (processedFrames > sectionFrames) {
39
- throw new RangeError(`Processed frames (${processedFrames}) exceeds section frames (${sectionFrames})`)
40
- }
41
- totalFrames += processedFrames
42
- startIndex = (startIndex + processedFrames) % buffer.length
43
- if (processedFrames < sectionFrames) {
44
- break
45
- }
46
- }
47
- return { frames: totalFrames, nextIndex: startIndex }
48
- }