@ain1084/audio-worklet-stream 1.0.5 → 2.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/dist/filler-frame-buffer-context.d.ts +30 -0
- package/dist/{frame-buffer/buffer-config.js → filler-frame-buffer-context.js} +5 -21
- package/dist/filler-frame-buffer-context.js.map +1 -0
- package/dist/{frame-buffer/buffer-filler.d.ts → frame-buffer-filler.d.ts} +2 -2
- package/dist/frame-buffer-filler.js +2 -0
- package/dist/frame-buffer-filler.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/output-stream-node.d.ts +4 -4
- package/dist/output-stream-node.js +9 -9
- package/dist/output-stream-node.js.map +1 -1
- package/dist/output-stream-processor.js +4 -4
- package/dist/output-stream-processor.js.map +1 -1
- package/dist/stream-node-factory.d.ts +4 -4
- package/dist/stream-node-factory.js +9 -8
- package/dist/stream-node-factory.js.map +1 -1
- package/dist/write-strategy/manual-strategy.d.ts +3 -4
- package/dist/write-strategy/manual-strategy.js +4 -4
- package/dist/write-strategy/manual-strategy.js.map +1 -1
- package/dist/write-strategy/timed-strategy.d.ts +4 -4
- package/dist/write-strategy/timed-strategy.js +5 -5
- package/dist/write-strategy/timed-strategy.js.map +1 -1
- package/dist/write-strategy/worker/message.d.ts +3 -3
- package/dist/write-strategy/worker/worker.d.ts +2 -2
- package/dist/write-strategy/worker/worker.js +8 -8
- package/dist/write-strategy/worker/worker.js.map +1 -1
- package/dist/write-strategy/worker-strategy.d.ts +3 -3
- package/dist/write-strategy/worker-strategy.js +6 -6
- package/dist/write-strategy/worker-strategy.js.map +1 -1
- package/package.json +10 -10
- package/src/{frame-buffer/buffer-config.ts → filler-frame-buffer-context.ts} +6 -50
- package/src/{frame-buffer/buffer-filler.ts → frame-buffer-filler.ts} +2 -2
- package/src/index.ts +2 -2
- package/src/output-stream-node.ts +10 -10
- package/src/output-stream-processor.ts +5 -6
- package/src/stream-node-factory.ts +12 -12
- package/src/write-strategy/manual-strategy.ts +4 -5
- package/src/write-strategy/timed-strategy.ts +7 -7
- package/src/write-strategy/worker/message.ts +3 -3
- package/src/write-strategy/worker/worker.ts +10 -10
- package/src/write-strategy/worker-strategy.ts +9 -9
- package/dist/frame-buffer/buffer-config.d.ts +0 -60
- package/dist/frame-buffer/buffer-config.js.map +0 -1
- package/dist/frame-buffer/buffer-factory.d.ts +0 -76
- package/dist/frame-buffer/buffer-factory.js +0 -55
- package/dist/frame-buffer/buffer-factory.js.map +0 -1
- package/dist/frame-buffer/buffer-filler.js +0 -2
- package/dist/frame-buffer/buffer-filler.js.map +0 -1
- package/dist/frame-buffer/buffer-reader.d.ts +0 -60
- package/dist/frame-buffer/buffer-reader.js +0 -77
- package/dist/frame-buffer/buffer-reader.js.map +0 -1
- package/dist/frame-buffer/buffer-utils.d.ts +0 -34
- package/dist/frame-buffer/buffer-utils.js +0 -34
- package/dist/frame-buffer/buffer-utils.js.map +0 -1
- package/dist/frame-buffer/buffer-writer.d.ts +0 -60
- package/dist/frame-buffer/buffer-writer.js +0 -77
- package/dist/frame-buffer/buffer-writer.js.map +0 -1
- package/dist/frame-buffer/buffer.d.ts +0 -51
- package/dist/frame-buffer/buffer.js +0 -75
- package/dist/frame-buffer/buffer.js.map +0 -1
- package/dist/write-strategy/manual.d.ts +0 -36
- package/dist/write-strategy/manual.js +0 -43
- package/dist/write-strategy/manual.js.map +0 -1
- package/dist/write-strategy/timed.d.ts +0 -36
- package/dist/write-strategy/timed.js +0 -92
- package/dist/write-strategy/timed.js.map +0 -1
- package/dist/write-strategy/worker/strategy.d.ts +0 -34
- package/dist/write-strategy/worker/strategy.js +0 -125
- package/dist/write-strategy/worker/strategy.js.map +0 -1
- package/dist/write-strategy/worker/worker-strategy.d.ts +0 -34
- package/dist/write-strategy/worker/worker-strategy.js +0 -125
- package/dist/write-strategy/worker/worker-strategy.js.map +0 -1
- package/src/frame-buffer/buffer-reader.ts +0 -82
- package/src/frame-buffer/buffer-writer.ts +0 -82
- package/src/frame-buffer/buffer.ts +0 -79
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { FrameBufferWriter } from './buffer-writer';
|
|
2
|
-
/**
|
|
3
|
-
* Parameters for creating a FrameBuffer.
|
|
4
|
-
* @property frameBufferSize - The size of the frame buffer.
|
|
5
|
-
* @property channelCount - The number of audio channels.
|
|
6
|
-
*/
|
|
7
|
-
export type FrameBufferParams = Readonly<{
|
|
8
|
-
frameBufferSize: number;
|
|
9
|
-
channelCount: number;
|
|
10
|
-
}>;
|
|
11
|
-
/**
|
|
12
|
-
* Parameters for creating a FillerFrameBuffer.
|
|
13
|
-
* @property channelCount - The number of audio channels.
|
|
14
|
-
* @property fillInterval - The interval in milliseconds for filling the buffer.
|
|
15
|
-
* @property sampleRate - The sample rate of the audio context.
|
|
16
|
-
* @property frameBufferChunks - The number of chunks in the frame buffer.
|
|
17
|
-
*/
|
|
18
|
-
export type FillerFrameBufferParams = Readonly<{
|
|
19
|
-
channelCount: number;
|
|
20
|
-
fillInterval?: number;
|
|
21
|
-
sampleRate?: number;
|
|
22
|
-
frameBufferChunks?: number;
|
|
23
|
-
}>;
|
|
24
|
-
/**
|
|
25
|
-
* Configuration for a FrameBuffer.
|
|
26
|
-
* This configuration is returned by the createFrameBufferConfig function.
|
|
27
|
-
* @property sampleBuffer - The shared buffer for audio data frames.
|
|
28
|
-
* @property samplesPerFrame - The number of samples per frame.
|
|
29
|
-
* @property usedFramesInBuffer - The usage count of the frames in the buffer.
|
|
30
|
-
* @property totalReadFrames - The total frames read from the buffer.
|
|
31
|
-
* @property totalWriteFrames - The total frames written to the buffer.
|
|
32
|
-
*/
|
|
33
|
-
export type FrameBufferConfig = Readonly<{
|
|
34
|
-
sampleBuffer: Float32Array;
|
|
35
|
-
samplesPerFrame: number;
|
|
36
|
-
usedFramesInBuffer: Uint32Array;
|
|
37
|
-
totalReadFrames: BigUint64Array;
|
|
38
|
-
totalWriteFrames: BigUint64Array;
|
|
39
|
-
}>;
|
|
40
|
-
/**
|
|
41
|
-
* Configuration for a FillerFrameBuffer.
|
|
42
|
-
* This configuration is returned by the createFillerFrameBufferConfig function.
|
|
43
|
-
* @property sampleRate - The sample rate of the audio context.
|
|
44
|
-
* @property fillInterval - The interval in milliseconds for filling the buffer.
|
|
45
|
-
*/
|
|
46
|
-
export type FillerFrameBufferConfig = FrameBufferConfig & Readonly<{
|
|
47
|
-
sampleRate: number;
|
|
48
|
-
fillInterval: number;
|
|
49
|
-
}>;
|
|
50
|
-
/**
|
|
51
|
-
* Creates a FrameBufferWriter instance.
|
|
52
|
-
* @param config - The configuration for the FrameBuffer.
|
|
53
|
-
* @returns A new instance of FrameBufferWriter.
|
|
54
|
-
*/
|
|
55
|
-
export declare const createFrameBufferWriter: (config: FrameBufferConfig) => FrameBufferWriter;
|
|
56
|
-
/**
|
|
57
|
-
* FrameBufferFactory class
|
|
58
|
-
* Provides static methods to create frame buffer configurations and writers.
|
|
59
|
-
*/
|
|
60
|
-
export declare class FrameBufferFactory {
|
|
61
|
-
static readonly DEFAULT_FILL_INTERVAL_MS = 20;
|
|
62
|
-
static readonly DEFAULT_FRAME_BUFFER_CHUNKS = 5;
|
|
63
|
-
static readonly PROCESS_UNIT = 128;
|
|
64
|
-
/**
|
|
65
|
-
* Creates a FrameBufferConfig instance.
|
|
66
|
-
* @param params - The parameters for the FrameBuffer.
|
|
67
|
-
* @returns A new instance of FrameBufferConfig.
|
|
68
|
-
*/
|
|
69
|
-
static createFrameBufferConfig(params: FrameBufferParams): FrameBufferConfig;
|
|
70
|
-
/**
|
|
71
|
-
* Creates a FillerFrameBufferConfig instance.
|
|
72
|
-
* @param params - The parameters for the FillerFrameBuffer.
|
|
73
|
-
* @returns A new instance of FillerFrameBufferConfig.
|
|
74
|
-
*/
|
|
75
|
-
static createFillerFrameBufferConfig(params: FillerFrameBufferParams): FillerFrameBufferConfig;
|
|
76
|
-
}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { FrameBuffer } from './buffer';
|
|
2
|
-
import { FrameBufferWriter } from './buffer-writer';
|
|
3
|
-
/**
|
|
4
|
-
* Creates a FrameBufferWriter instance.
|
|
5
|
-
* @param config - The configuration for the FrameBuffer.
|
|
6
|
-
* @returns A new instance of FrameBufferWriter.
|
|
7
|
-
*/
|
|
8
|
-
export const createFrameBufferWriter = (config) => {
|
|
9
|
-
return new FrameBufferWriter(new FrameBuffer(config.sampleBuffer, config.samplesPerFrame), config.usedFramesInBuffer, config.totalWriteFrames);
|
|
10
|
-
};
|
|
11
|
-
/**
|
|
12
|
-
* FrameBufferFactory class
|
|
13
|
-
* Provides static methods to create frame buffer configurations and writers.
|
|
14
|
-
*/
|
|
15
|
-
export class FrameBufferFactory {
|
|
16
|
-
static DEFAULT_FILL_INTERVAL_MS = 20;
|
|
17
|
-
static DEFAULT_FRAME_BUFFER_CHUNKS = 5;
|
|
18
|
-
static PROCESS_UNIT = 128;
|
|
19
|
-
/**
|
|
20
|
-
* Creates a FrameBufferConfig instance.
|
|
21
|
-
* @param params - The parameters for the FrameBuffer.
|
|
22
|
-
* @returns A new instance of FrameBufferConfig.
|
|
23
|
-
*/
|
|
24
|
-
static createFrameBufferConfig(params) {
|
|
25
|
-
return {
|
|
26
|
-
sampleBuffer: new Float32Array(new SharedArrayBuffer(params.frameBufferSize * params.channelCount * Float32Array.BYTES_PER_ELEMENT)),
|
|
27
|
-
samplesPerFrame: params.channelCount,
|
|
28
|
-
usedFramesInBuffer: new Uint32Array(new SharedArrayBuffer(Uint32Array.BYTES_PER_ELEMENT)),
|
|
29
|
-
totalReadFrames: new BigUint64Array(new SharedArrayBuffer(BigUint64Array.BYTES_PER_ELEMENT)),
|
|
30
|
-
totalWriteFrames: new BigUint64Array(new SharedArrayBuffer(BigUint64Array.BYTES_PER_ELEMENT)),
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Creates a FillerFrameBufferConfig instance.
|
|
35
|
-
* @param params - The parameters for the FillerFrameBuffer.
|
|
36
|
-
* @returns A new instance of FillerFrameBufferConfig.
|
|
37
|
-
*/
|
|
38
|
-
static createFillerFrameBufferConfig(params) {
|
|
39
|
-
const sampleRate = params.sampleRate;
|
|
40
|
-
// Check if 'sampleRate' is a positive integer
|
|
41
|
-
if (sampleRate === undefined || (!Number.isInteger(sampleRate) || sampleRate <= 0)) {
|
|
42
|
-
throw new Error('Invalid sampleRate: must be a positive integer.');
|
|
43
|
-
}
|
|
44
|
-
const intervalMillisecond = params.fillInterval ?? FrameBufferFactory.DEFAULT_FILL_INTERVAL_MS;
|
|
45
|
-
const frameBufferSize = Math.floor(sampleRate * intervalMillisecond / 1000 + (FrameBufferFactory.PROCESS_UNIT - 1)) & ~(FrameBufferFactory.PROCESS_UNIT - 1);
|
|
46
|
-
const frameBufferChunkCount = params.frameBufferChunks ?? FrameBufferFactory.DEFAULT_FRAME_BUFFER_CHUNKS;
|
|
47
|
-
const config = FrameBufferFactory.createFrameBufferConfig({ frameBufferSize: frameBufferSize * frameBufferChunkCount, channelCount: params.channelCount });
|
|
48
|
-
return {
|
|
49
|
-
...config,
|
|
50
|
-
sampleRate,
|
|
51
|
-
fillInterval: intervalMillisecond,
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
//# sourceMappingURL=buffer-factory.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"buffer-factory.js","sourceRoot":"","sources":["../../src/frame-buffer/buffer-factory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AACtC,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAA;AAsDnD;;;;GAIG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,MAAyB,EAAqB,EAAE;IACtF,OAAO,IAAI,iBAAiB,CAC1B,IAAI,WAAW,CAAC,MAAM,CAAC,YAAY,EAAE,MAAM,CAAC,eAAe,CAAC,EAC5D,MAAM,CAAC,kBAAkB,E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AG,CAAC,kBAAkB,CAAC,YAAY,GAAG,CAAC,CAAC,CAChF,GAAG,CAAC,CAAC,kBAAkB,CAAC,YAAY,GAAG,CAAC,CAAC,CAAA;QAC1C,MAAM,qBAAqB,GAAG,MAAM,CAAC,iBAAiB,IAAI,kBAAkB,CAAC,2BAA2B,CAAA;QACxG,MAAM,MAAM,GAAG,kBAAkB,CAAC,uBAAuB,CACvD,EAAE,eAAe,EAAE,eAAe,GAAG,qBAAqB,EAAE,YAAY,EAAE,MAAM,CAAC,YAAY,EAAE,CAAC,CAAA;QAClG,OAAO;YACL,GAAG,MAAM;YACT,UAAU;YACV,YAAY,EAAE,mBAAmB;SAClC,CAAA;IACH,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"buffer-filler.js","sourceRoot":"","sources":["../../src/frame-buffer/buffer-filler.ts"],"names":[],"mappings":""}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { FrameBufferConfig } from './buffer-config';
|
|
2
|
-
/**
|
|
3
|
-
* FrameBufferReader class
|
|
4
|
-
* This class reads audio frame data from a shared Float32Array buffer and processes it.
|
|
5
|
-
* The buffer usage is tracked using a Uint32Array.
|
|
6
|
-
*/
|
|
7
|
-
export declare class FrameBufferReader {
|
|
8
|
-
private readonly _frameBuffer;
|
|
9
|
-
private readonly _usedFramesInBuffer;
|
|
10
|
-
private readonly _totalFrames;
|
|
11
|
-
private _index;
|
|
12
|
-
/**
|
|
13
|
-
* @internal
|
|
14
|
-
* Creates an instance of FrameBufferReader.
|
|
15
|
-
* @param config - The configuration object containing:
|
|
16
|
-
* - `sampleBuffer`: The shared buffer to read audio data frames from.
|
|
17
|
-
* - `samplesPerFrame`: The number of samples per frame.
|
|
18
|
-
* - `usedFramesInBuffer`: A Uint32Array tracking the usage of frames in the buffer.
|
|
19
|
-
* - `totalReadFrames`: A BigUint64Array tracking the total frames read from the buffer.
|
|
20
|
-
*/
|
|
21
|
-
constructor(config: FrameBufferConfig);
|
|
22
|
-
/**
|
|
23
|
-
* Get the number of available frames in the buffer.
|
|
24
|
-
* @returns The number of available frames in the buffer.
|
|
25
|
-
*/
|
|
26
|
-
get availableFrames(): number;
|
|
27
|
-
/**
|
|
28
|
-
* Get the total number of frames read from the buffer.
|
|
29
|
-
*
|
|
30
|
-
* @returns The total number of frames read.
|
|
31
|
-
*/
|
|
32
|
-
get totalFrames(): bigint;
|
|
33
|
-
/**
|
|
34
|
-
* Reads audio frame data from the buffer using the provided callback.
|
|
35
|
-
* This method handles one or more readable segments within the ring buffer
|
|
36
|
-
* and invokes the callback for each segment.
|
|
37
|
-
*
|
|
38
|
-
* @param processFrameSegment - The callback function invoked for each readable segment
|
|
39
|
-
* of the ring buffer. It receives:
|
|
40
|
-
* 1. `buffer`: A Float32Array representing the readable segment of the buffer.
|
|
41
|
-
* 2. `offset`: The cumulative number of frames processed so far, used as the starting index
|
|
42
|
-
* for the current segment relative to the entire data.
|
|
43
|
-
*
|
|
44
|
-
* The callback must return the number of frames it successfully processed.
|
|
45
|
-
* If the callback processes fewer frames than available in the current segment,
|
|
46
|
-
* processing will stop early.
|
|
47
|
-
*
|
|
48
|
-
* @returns The total number of frames processed across all segments.
|
|
49
|
-
* Note: The return value is in frames, not in samples.
|
|
50
|
-
*
|
|
51
|
-
* @throws RangeError - If the processFrameSegment callback returns a processed length greater than the available frames in the current segment.
|
|
52
|
-
*
|
|
53
|
-
* @remarks The buffer is an array of samples, but it is always provided in frame-sized segments.
|
|
54
|
-
* Each frame consists of multiple samples (e.g., for stereo, a frame contains a sample for the left channel
|
|
55
|
-
* and one for the right channel). You must access and process the buffer in frame-sized chunks,
|
|
56
|
-
* based on the structure of the frames.
|
|
57
|
-
*
|
|
58
|
-
*/
|
|
59
|
-
read(processFrameSegment: (buffer: Float32Array, offset: number) => number): number;
|
|
60
|
-
}
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { FrameBuffer } from './buffer';
|
|
2
|
-
/**
|
|
3
|
-
* FrameBufferReader class
|
|
4
|
-
* This class reads audio frame data from a shared Float32Array buffer and processes it.
|
|
5
|
-
* The buffer usage is tracked using a Uint32Array.
|
|
6
|
-
*/
|
|
7
|
-
export class FrameBufferReader {
|
|
8
|
-
_frameBuffer;
|
|
9
|
-
_usedFramesInBuffer;
|
|
10
|
-
_totalFrames;
|
|
11
|
-
_index = 0;
|
|
12
|
-
/**
|
|
13
|
-
* @internal
|
|
14
|
-
* Creates an instance of FrameBufferReader.
|
|
15
|
-
* @param config - The configuration object containing:
|
|
16
|
-
* - `sampleBuffer`: The shared buffer to read audio data frames from.
|
|
17
|
-
* - `samplesPerFrame`: The number of samples per frame.
|
|
18
|
-
* - `usedFramesInBuffer`: A Uint32Array tracking the usage of frames in the buffer.
|
|
19
|
-
* - `totalReadFrames`: A BigUint64Array tracking the total frames read from the buffer.
|
|
20
|
-
*/
|
|
21
|
-
constructor(config) {
|
|
22
|
-
this._frameBuffer = new FrameBuffer(config.sampleBuffer, config.samplesPerFrame);
|
|
23
|
-
this._usedFramesInBuffer = config.usedFramesInBuffer;
|
|
24
|
-
this._totalFrames = config.totalReadFrames;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Get the number of available frames in the buffer.
|
|
28
|
-
* @returns The number of available frames in the buffer.
|
|
29
|
-
*/
|
|
30
|
-
get availableFrames() {
|
|
31
|
-
return Atomics.load(this._usedFramesInBuffer, 0);
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Get the total number of frames read from the buffer.
|
|
35
|
-
*
|
|
36
|
-
* @returns The total number of frames read.
|
|
37
|
-
*/
|
|
38
|
-
get totalFrames() {
|
|
39
|
-
// This class is not used concurrently by multiple threads,
|
|
40
|
-
// so `Atomics` is not necessary when reading `totalFrames`.
|
|
41
|
-
return this._totalFrames[0];
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Reads audio frame data from the buffer using the provided callback.
|
|
45
|
-
* This method handles one or more readable segments within the ring buffer
|
|
46
|
-
* and invokes the callback for each segment.
|
|
47
|
-
*
|
|
48
|
-
* @param processFrameSegment - The callback function invoked for each readable segment
|
|
49
|
-
* of the ring buffer. It receives:
|
|
50
|
-
* 1. `buffer`: A Float32Array representing the readable segment of the buffer.
|
|
51
|
-
* 2. `offset`: The cumulative number of frames processed so far, used as the starting index
|
|
52
|
-
* for the current segment relative to the entire data.
|
|
53
|
-
*
|
|
54
|
-
* The callback must return the number of frames it successfully processed.
|
|
55
|
-
* If the callback processes fewer frames than available in the current segment,
|
|
56
|
-
* processing will stop early.
|
|
57
|
-
*
|
|
58
|
-
* @returns The total number of frames processed across all segments.
|
|
59
|
-
* Note: The return value is in frames, not in samples.
|
|
60
|
-
*
|
|
61
|
-
* @throws RangeError - If the processFrameSegment callback returns a processed length greater than the available frames in the current segment.
|
|
62
|
-
*
|
|
63
|
-
* @remarks The buffer is an array of samples, but it is always provided in frame-sized segments.
|
|
64
|
-
* Each frame consists of multiple samples (e.g., for stereo, a frame contains a sample for the left channel
|
|
65
|
-
* and one for the right channel). You must access and process the buffer in frame-sized chunks,
|
|
66
|
-
* based on the structure of the frames.
|
|
67
|
-
*
|
|
68
|
-
*/
|
|
69
|
-
read(processFrameSegment) {
|
|
70
|
-
const result = this._frameBuffer.enumFrameSegments(this._index, this.availableFrames, processFrameSegment);
|
|
71
|
-
this._index = result.nextIndex;
|
|
72
|
-
Atomics.sub(this._usedFramesInBuffer, 0, result.totalProcessedFrames);
|
|
73
|
-
Atomics.add(this._totalFrames, 0, BigInt(result.totalProcessedFrames));
|
|
74
|
-
return result.totalProcessedFrames;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
//# sourceMappingURL=buffer-reader.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"buffer-reader.js","sourceRoot":"","sources":["../../src/frame-buffer/buffer-reader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AAGtC;;;;GAIG;AACH,MAA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
|
3
|
-
* Type definition for the callback function used in enumFrames.
|
|
4
|
-
* The callback processes each section of the frame buffer.
|
|
5
|
-
* @param frame - An object containing:
|
|
6
|
-
* - buffer: The FrameBuffer instance.
|
|
7
|
-
* - index: The starting index in the buffer.
|
|
8
|
-
* - frames: The number of frames in the section.
|
|
9
|
-
* @param offset - The offset in the buffer from the start of processing.
|
|
10
|
-
* @returns The number of frames processed.
|
|
11
|
-
*/
|
|
12
|
-
export type FrameCallback = (frame: {
|
|
13
|
-
buffer: FrameBuffer;
|
|
14
|
-
index: number;
|
|
15
|
-
frames: number;
|
|
16
|
-
}, offset: number) => number;
|
|
17
|
-
/**
|
|
18
|
-
* Processes sections of a Float32Array buffer using a callback function.
|
|
19
|
-
* This function is intended for internal use only.
|
|
20
|
-
*
|
|
21
|
-
* @param buffer - The FrameBuffer to process. This buffer is expected to be shared.
|
|
22
|
-
* @param startIndex - The starting index in the buffer from where processing should begin.
|
|
23
|
-
* @param availableFrames - The total number of frames available to process in the buffer.
|
|
24
|
-
* @param frameCallback - The callback function to process each section of the buffer.
|
|
25
|
-
* It should return the number of frames processed.
|
|
26
|
-
* @returns An object containing:
|
|
27
|
-
* - frames: The number of frames successfully processed.
|
|
28
|
-
* - nextIndex: The index in the buffer for the next processing cycle.
|
|
29
|
-
* @throws RangeError - If the frameCallback returns a processed length greater than the part length.
|
|
30
|
-
*/
|
|
31
|
-
export declare const enumFrames: (buffer: FrameBuffer, startIndex: number, availableFrames: number, frameCallback: FrameCallback) => {
|
|
32
|
-
frames: number;
|
|
33
|
-
nextIndex: number;
|
|
34
|
-
};
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Processes sections of a Float32Array buffer using a callback function.
|
|
3
|
-
* This function is intended for internal use only.
|
|
4
|
-
*
|
|
5
|
-
* @param buffer - The FrameBuffer to process. This buffer is expected to be shared.
|
|
6
|
-
* @param startIndex - The starting index in the buffer from where processing should begin.
|
|
7
|
-
* @param availableFrames - The total number of frames available to process in the buffer.
|
|
8
|
-
* @param frameCallback - The callback function to process each section of the buffer.
|
|
9
|
-
* It should return the number of frames processed.
|
|
10
|
-
* @returns An object containing:
|
|
11
|
-
* - frames: The number of frames successfully processed.
|
|
12
|
-
* - nextIndex: The index in the buffer for the next processing cycle.
|
|
13
|
-
* @throws RangeError - If the frameCallback returns a processed length greater than the part length.
|
|
14
|
-
*/
|
|
15
|
-
export const enumFrames = (buffer, startIndex, availableFrames, frameCallback) => {
|
|
16
|
-
let totalFrames = 0;
|
|
17
|
-
while (totalFrames < availableFrames) {
|
|
18
|
-
// Determine the length of the current section to process
|
|
19
|
-
const sectionFrames = Math.min(buffer.frameCount - startIndex, availableFrames - totalFrames);
|
|
20
|
-
// Process the current section using the frameCallback function
|
|
21
|
-
const processedFrames = frameCallback({ buffer, index: startIndex, frames: sectionFrames }, totalFrames);
|
|
22
|
-
// Ensure the processed length does not exceed the section length
|
|
23
|
-
if (processedFrames > sectionFrames) {
|
|
24
|
-
throw new RangeError(`Processed frames (${processedFrames}) exceeds section frames (${sectionFrames})`);
|
|
25
|
-
}
|
|
26
|
-
totalFrames += processedFrames;
|
|
27
|
-
startIndex = (startIndex + processedFrames) % buffer.frameCount;
|
|
28
|
-
if (processedFrames < sectionFrames) {
|
|
29
|
-
break;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return { frames: totalFrames, nextIndex: startIndex };
|
|
33
|
-
};
|
|
34
|
-
//# sourceMappingURL=buffer-utils.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"buffer-utils.js","sourceRoot":"","sources":["../../src/frame-buffer/buffer-utils.ts"],"names":[],"mappings":"AAcA;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,MAAmB,EAAE,UAAkB,EAAE,eAAuB,EAAE,aAA4B,EACnF,EAAE;IACtC,IAAI,WAAW,GAAG,CAAC,CAAA;IACnB,OAAO,WAAW,GAAG,eAAe,EAAE,CAAC;QACrC,yDAAyD;QACzD,MAAM,aAAa,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,UAAU,GAAG,UAAU,EAAE,eAAe,GAAG,WAAW,CAAC,CAAA;QAC7F,+DAA+D;QAC/D,MAAM,eAAe,GAAG,aAAa,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,aAAa,EAAE,EAAE,WAAW,CAAC,CAAA;QACxG,iEAAiE;QACjE,IAAI,eAAe,GAAG,aAAa,EAAE,CAAC;YACpC,MAAM,IAAI,UAAU,CAAC,qBAAqB,eAAe,6BAA6B,aAAa,GAAG,CAAC,CAAA;QACzG,CAAC;QACD,WAAW,IAAI,eAAe,CAAA;QAC9B,UAAU,GAAG,CAAC,UAAU,GAAG,eAAe,CAAC,GAAG,MAAM,CAAC,UAAU,CAAA;QAC/D,IAAI,eAAe,GAAG,aAAa,EAAE,CAAC;YACpC,MAAK;QACP,CAAC;IACH,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,CAAA;AACvD,CAAC,CAAA"}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { FrameBufferConfig } from './buffer-config';
|
|
2
|
-
/**
|
|
3
|
-
* FrameBufferWriter class
|
|
4
|
-
* This class writes audio frame data to a shared Float32Array buffer.
|
|
5
|
-
* The buffer usage is tracked using a Uint32Array.
|
|
6
|
-
*/
|
|
7
|
-
export declare class FrameBufferWriter {
|
|
8
|
-
private readonly _frameBuffer;
|
|
9
|
-
private readonly _usedFramesInBuffer;
|
|
10
|
-
private readonly _totalFrames;
|
|
11
|
-
private _index;
|
|
12
|
-
/**
|
|
13
|
-
* @internal
|
|
14
|
-
* Creates an instance of FrameBufferWriter.
|
|
15
|
-
* @param config - The configuration object containing:
|
|
16
|
-
* - `sampleBuffer`: The shared buffer to write audio data frames.
|
|
17
|
-
* - `samplesPerFrame`: The number of samples per frame.
|
|
18
|
-
* - `usedFramesInBuffer`: A Uint32Array tracking the usage of frames in the buffer.
|
|
19
|
-
* - `totalWriteFrames`: A BigUint64Array tracking the total frames written to the buffer.
|
|
20
|
-
*/
|
|
21
|
-
constructor(config: FrameBufferConfig);
|
|
22
|
-
/**
|
|
23
|
-
* Get the number of available frames in the buffer.
|
|
24
|
-
* This represents the number of frames that can be written before the buffer is full.
|
|
25
|
-
* @returns The number of available frames in the buffer.
|
|
26
|
-
*/
|
|
27
|
-
get availableFrames(): number;
|
|
28
|
-
/**
|
|
29
|
-
* Get the total number of frames written to the buffer.
|
|
30
|
-
*
|
|
31
|
-
* @returns The total number of frames written.
|
|
32
|
-
*/
|
|
33
|
-
get totalFrames(): bigint;
|
|
34
|
-
/**
|
|
35
|
-
* Writes audio frame data into the buffer using the provided callback.
|
|
36
|
-
* This method handles one or more writable segments within the ring buffer
|
|
37
|
-
* and invokes the callback for each segment.
|
|
38
|
-
*
|
|
39
|
-
* @param processFrameSegment - The callback function invoked for each writable segment
|
|
40
|
-
* of the ring buffer. It receives:
|
|
41
|
-
* 1. `buffer`: A Float32Array representing the writable segment of the buffer.
|
|
42
|
-
* 2. `offset`: The cumulative number of frames processed so far, used as the starting index
|
|
43
|
-
* for the current segment relative to the entire data.
|
|
44
|
-
*
|
|
45
|
-
* The callback must return the number of frames it successfully wrote.
|
|
46
|
-
* If the callback writes fewer frames than available in the current segment,
|
|
47
|
-
* processing will stop early.
|
|
48
|
-
*
|
|
49
|
-
* @returns The total number of frames written across all segments.
|
|
50
|
-
* Note: The return value is in frames, not in samples.
|
|
51
|
-
*
|
|
52
|
-
* @throws RangeError - If the processFrameSegment callback returns a written length greater than the available space in the current segment.
|
|
53
|
-
*
|
|
54
|
-
* @remarks The buffer is an array of samples, but it is always provided in frame-sized segments.
|
|
55
|
-
* Each frame consists of multiple samples (e.g., for stereo, a frame contains a sample for the left channel
|
|
56
|
-
* and one for the right channel). You must access and process the buffer in frame-sized chunks,
|
|
57
|
-
* based on the structure of the frames.
|
|
58
|
-
*/
|
|
59
|
-
write(processFrameSegment: (buffer: Float32Array, offset: number) => number): number;
|
|
60
|
-
}
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { FrameBuffer } from './buffer';
|
|
2
|
-
/**
|
|
3
|
-
* FrameBufferWriter class
|
|
4
|
-
* This class writes audio frame data to a shared Float32Array buffer.
|
|
5
|
-
* The buffer usage is tracked using a Uint32Array.
|
|
6
|
-
*/
|
|
7
|
-
export class FrameBufferWriter {
|
|
8
|
-
_frameBuffer;
|
|
9
|
-
_usedFramesInBuffer;
|
|
10
|
-
_totalFrames;
|
|
11
|
-
_index = 0;
|
|
12
|
-
/**
|
|
13
|
-
* @internal
|
|
14
|
-
* Creates an instance of FrameBufferWriter.
|
|
15
|
-
* @param config - The configuration object containing:
|
|
16
|
-
* - `sampleBuffer`: The shared buffer to write audio data frames.
|
|
17
|
-
* - `samplesPerFrame`: The number of samples per frame.
|
|
18
|
-
* - `usedFramesInBuffer`: A Uint32Array tracking the usage of frames in the buffer.
|
|
19
|
-
* - `totalWriteFrames`: A BigUint64Array tracking the total frames written to the buffer.
|
|
20
|
-
*/
|
|
21
|
-
constructor(config) {
|
|
22
|
-
this._frameBuffer = new FrameBuffer(config.sampleBuffer, config.samplesPerFrame);
|
|
23
|
-
this._usedFramesInBuffer = config.usedFramesInBuffer;
|
|
24
|
-
this._totalFrames = config.totalWriteFrames;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
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
|
-
*/
|
|
31
|
-
get availableFrames() {
|
|
32
|
-
return this._frameBuffer.frameCount - Atomics.load(this._usedFramesInBuffer, 0);
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Get the total number of frames written to the buffer.
|
|
36
|
-
*
|
|
37
|
-
* @returns The total number of frames written.
|
|
38
|
-
*/
|
|
39
|
-
get totalFrames() {
|
|
40
|
-
// This class is not used concurrently by multiple threads,
|
|
41
|
-
// so `Atomics` is not necessary when reading `totalFrames`.
|
|
42
|
-
return this._totalFrames[0];
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Writes audio frame data into the buffer using the provided callback.
|
|
46
|
-
* This method handles one or more writable segments within the ring buffer
|
|
47
|
-
* and invokes the callback for each segment.
|
|
48
|
-
*
|
|
49
|
-
* @param processFrameSegment - The callback function invoked for each writable segment
|
|
50
|
-
* of the ring buffer. It receives:
|
|
51
|
-
* 1. `buffer`: A Float32Array representing the writable segment of the buffer.
|
|
52
|
-
* 2. `offset`: The cumulative number of frames processed so far, used as the starting index
|
|
53
|
-
* for the current segment relative to the entire data.
|
|
54
|
-
*
|
|
55
|
-
* The callback must return the number of frames it successfully wrote.
|
|
56
|
-
* If the callback writes fewer frames than available in the current segment,
|
|
57
|
-
* processing will stop early.
|
|
58
|
-
*
|
|
59
|
-
* @returns The total number of frames written across all segments.
|
|
60
|
-
* Note: The return value is in frames, not in samples.
|
|
61
|
-
*
|
|
62
|
-
* @throws RangeError - If the processFrameSegment callback returns a written length greater than the available space 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
|
-
*/
|
|
69
|
-
write(processFrameSegment) {
|
|
70
|
-
const result = this._frameBuffer.enumFrameSegments(this._index, this.availableFrames, processFrameSegment);
|
|
71
|
-
this._index = result.nextIndex;
|
|
72
|
-
Atomics.add(this._usedFramesInBuffer, 0, result.totalProcessedFrames);
|
|
73
|
-
Atomics.add(this._totalFrames, 0, BigInt(result.totalProcessedFrames));
|
|
74
|
-
return result.totalProcessedFrames;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
//# sourceMapp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AO,CAAC,GAAG,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC,CAAA;QACtE,OAAO,MAAM,CAAC,oBAAoB,CAAA;IACpC,CAAC;CACF"}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* FrameBuffer class
|
|
3
|
-
* This class manages a buffer of audio frames.
|
|
4
|
-
*/
|
|
5
|
-
export declare class FrameBuffer {
|
|
6
|
-
private readonly _buffer;
|
|
7
|
-
private readonly _samplesPerFrame;
|
|
8
|
-
private readonly _frameCount;
|
|
9
|
-
/**
|
|
10
|
-
* Creates an instance of FrameBuffer.
|
|
11
|
-
* @param buffer - The Float32Array buffer to manage.
|
|
12
|
-
* @param samplesPerFrame - The number of samples per frame.
|
|
13
|
-
*/
|
|
14
|
-
constructor(buffer: Float32Array, samplesPerFrame: number);
|
|
15
|
-
/**
|
|
16
|
-
* Gets the count of the buffer in frames.
|
|
17
|
-
* @returns The count of the buffer in frames.
|
|
18
|
-
*/
|
|
19
|
-
get frameCount(): number;
|
|
20
|
-
private getFrames;
|
|
21
|
-
/**
|
|
22
|
-
* Processes sections of a Float32Array buffer using a callback function.
|
|
23
|
-
* This function handles one or more segments within the ring buffer and invokes
|
|
24
|
-
* the provided callback for each segment. It is intended for internal use only.
|
|
25
|
-
*
|
|
26
|
-
* @param startIndex - The starting index in the buffer from where processing should begin.
|
|
27
|
-
* @param availableFrames - The total number of frames available to process in the buffer.
|
|
28
|
-
* @param processFrameSegment - The callback function invoked for each segment
|
|
29
|
-
* of the ring buffer during enumeration. It receives:
|
|
30
|
-
* 1. `buffer`: A Float32Array representing the segment to process. The buffer is an array
|
|
31
|
-
* of samples but is always provided in frame-sized segments.
|
|
32
|
-
* 2. `offset`: The cumulative number of frames processed so far, used as the starting index
|
|
33
|
-
* for the current segment relative to the entire data.
|
|
34
|
-
* The callback must return the number of frames it successfully processed.
|
|
35
|
-
* If the callback processes fewer frames than available in the current segment,
|
|
36
|
-
* processing will stop early.
|
|
37
|
-
*
|
|
38
|
-
* @returns An object containing:
|
|
39
|
-
* - totalProcessedFrames: The number of frames successfully processed.
|
|
40
|
-
* - nextIndex: The index in the buffer for the next processing cycle.
|
|
41
|
-
*
|
|
42
|
-
* @throws RangeError - If the processFrameSegment callback returns a processed length greater than the available section length.
|
|
43
|
-
*
|
|
44
|
-
* @remarks The buffer is always provided in frame-sized segments, meaning that the buffer contains complete frames.
|
|
45
|
-
* You must process the buffer in frame-sized chunks based on the structure of the frames.
|
|
46
|
-
*/
|
|
47
|
-
enumFrameSegments(startIndex: number, availableFrames: number, processFrameSegment: (buffer: Float32Array, offset: number) => number): {
|
|
48
|
-
totalProcessedFrames: number;
|
|
49
|
-
nextIndex: number;
|
|
50
|
-
};
|
|
51
|
-
}
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* FrameBuffer class
|
|
3
|
-
* This class manages a buffer of audio frames.
|
|
4
|
-
*/
|
|
5
|
-
export class FrameBuffer {
|
|
6
|
-
_buffer;
|
|
7
|
-
_samplesPerFrame;
|
|
8
|
-
_frameCount;
|
|
9
|
-
/**
|
|
10
|
-
* Creates an instance of FrameBuffer.
|
|
11
|
-
* @param buffer - The Float32Array buffer to manage.
|
|
12
|
-
* @param samplesPerFrame - The number of samples per frame.
|
|
13
|
-
*/
|
|
14
|
-
constructor(buffer, samplesPerFrame) {
|
|
15
|
-
this._buffer = buffer;
|
|
16
|
-
this._samplesPerFrame = samplesPerFrame;
|
|
17
|
-
this._frameCount = Math.floor(buffer.length / samplesPerFrame);
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Gets the count of the buffer in frames.
|
|
21
|
-
* @returns The count of the buffer in frames.
|
|
22
|
-
*/
|
|
23
|
-
get frameCount() {
|
|
24
|
-
return this._frameCount;
|
|
25
|
-
}
|
|
26
|
-
getFrames(index, count) {
|
|
27
|
-
return this._buffer.subarray(index * this._samplesPerFrame, (index + count) * this._samplesPerFrame);
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Processes sections of a Float32Array buffer using a callback function.
|
|
31
|
-
* This function handles one or more segments within the ring buffer and invokes
|
|
32
|
-
* the provided callback for each segment. It is intended for internal use only.
|
|
33
|
-
*
|
|
34
|
-
* @param startIndex - The starting index in the buffer from where processing should begin.
|
|
35
|
-
* @param availableFrames - The total number of frames available to process in the buffer.
|
|
36
|
-
* @param processFrameSegment - The callback function invoked for each segment
|
|
37
|
-
* of the ring buffer during enumeration. It receives:
|
|
38
|
-
* 1. `buffer`: A Float32Array representing the segment to process. The buffer is an array
|
|
39
|
-
* of samples but is always provided in frame-sized segments.
|
|
40
|
-
* 2. `offset`: The cumulative number of frames processed so far, used as the starting index
|
|
41
|
-
* for the current segment relative to the entire data.
|
|
42
|
-
* The callback must return the number of frames it successfully processed.
|
|
43
|
-
* If the callback processes fewer frames than available in the current segment,
|
|
44
|
-
* processing will stop early.
|
|
45
|
-
*
|
|
46
|
-
* @returns An object containing:
|
|
47
|
-
* - totalProcessedFrames: The number of frames successfully processed.
|
|
48
|
-
* - nextIndex: The index in the buffer for the next processing cycle.
|
|
49
|
-
*
|
|
50
|
-
* @throws RangeError - If the processFrameSegment callback returns a processed length greater than the available section length.
|
|
51
|
-
*
|
|
52
|
-
* @remarks The buffer is always provided in frame-sized segments, meaning that the buffer contains complete frames.
|
|
53
|
-
* You must process the buffer in frame-sized chunks based on the structure of the frames.
|
|
54
|
-
*/
|
|
55
|
-
enumFrameSegments(startIndex, availableFrames, processFrameSegment) {
|
|
56
|
-
let totalProcessedFrames = 0;
|
|
57
|
-
while (totalProcessedFrames < availableFrames) {
|
|
58
|
-
// Determine the length of the current section to process
|
|
59
|
-
const sectionFrames = Math.min(this.frameCount - startIndex, availableFrames - totalProcessedFrames);
|
|
60
|
-
// Process the current section using the frameCallback function
|
|
61
|
-
const processedFrames = processFrameSegment(this.getFrames(startIndex, sectionFrames), totalProcessedFrames);
|
|
62
|
-
// Ensure the processed length does not exceed the section length
|
|
63
|
-
if (processedFrames > sectionFrames) {
|
|
64
|
-
throw new RangeError(`Processed frames (${processedFrames}) exceeds section frames (${sectionFrames})`);
|
|
65
|
-
}
|
|
66
|
-
totalProcessedFrames += processedFrames;
|
|
67
|
-
startIndex = (startIndex + processedFrames) % this.frameCount;
|
|
68
|
-
if (processedFrames < sectionFrames) {
|
|
69
|
-
break;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return { totalProcessedFrames, nextIndex: startIndex };
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
//# sourceMappingURL=buffer.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"buffer.js","sourceRoot":"","sources":["../../src/frame-buffer/buffer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,OAAO,WAAW;IACL,OAAO,CAAc;IACrB,gBAAgB,CAAQ;IACxB,WAAW,CAAQ;IAEpC;;;;OAIG;IACH,YAAmB,MAAoB,EAAE,eAAuB;QAC9D,IAAI,CAAC,OAAO,GAAG,MAAM,CAAA;QACrB,IAAI,CAAC,gBAAgB,GAAG,eAAe,CAAA;QACvC,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,GAAG,eAAe,CAAC,CAAA;IAChE,CAAC;IAED;;;OAGG;IACH,IAAW,UAAU;QACnB,OAAO,IAAI,CAAC,WAAW,CAAA;IACzB,CAAC;IAEO,SAAS,CAAC,KAAa,EAAE,KAAa;QAC5C,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,GAAG,IAAI,CAAC,gBAAgB,EAAE,CAAC,KAAK,GAAG,KAAK,CAAC,GAAG,IAAI,CAAC,gBAAgB,CAAC,CAAA;IACtG,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACI,iBAAiB,CAAC,UAAkB,EAAE,eAAuB,EAClE,mBAAqE;QAC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