node-av 0.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 (175) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/LICENSE.md +22 -0
  3. package/README.md +377 -0
  4. package/binding.gyp +78 -0
  5. package/dist/api/bitstream-filter.d.ts +246 -0
  6. package/dist/api/bitstream-filter.js +369 -0
  7. package/dist/api/bitstream-filter.js.map +1 -0
  8. package/dist/api/decoder.d.ts +257 -0
  9. package/dist/api/decoder.js +424 -0
  10. package/dist/api/decoder.js.map +1 -0
  11. package/dist/api/encoder.d.ts +298 -0
  12. package/dist/api/encoder.js +574 -0
  13. package/dist/api/encoder.js.map +1 -0
  14. package/dist/api/filter.d.ts +457 -0
  15. package/dist/api/filter.js +876 -0
  16. package/dist/api/filter.js.map +1 -0
  17. package/dist/api/hardware.d.ts +318 -0
  18. package/dist/api/hardware.js +558 -0
  19. package/dist/api/hardware.js.map +1 -0
  20. package/dist/api/index.d.ts +12 -0
  21. package/dist/api/index.js +20 -0
  22. package/dist/api/index.js.map +1 -0
  23. package/dist/api/io-stream.d.ts +109 -0
  24. package/dist/api/io-stream.js +124 -0
  25. package/dist/api/io-stream.js.map +1 -0
  26. package/dist/api/media-input.d.ts +295 -0
  27. package/dist/api/media-input.js +456 -0
  28. package/dist/api/media-input.js.map +1 -0
  29. package/dist/api/media-output.d.ts +274 -0
  30. package/dist/api/media-output.js +486 -0
  31. package/dist/api/media-output.js.map +1 -0
  32. package/dist/api/pipeline.d.ts +117 -0
  33. package/dist/api/pipeline.js +836 -0
  34. package/dist/api/pipeline.js.map +1 -0
  35. package/dist/api/types.d.ts +440 -0
  36. package/dist/api/types.js +2 -0
  37. package/dist/api/types.js.map +1 -0
  38. package/dist/api/utilities/audio-sample.d.ts +115 -0
  39. package/dist/api/utilities/audio-sample.js +110 -0
  40. package/dist/api/utilities/audio-sample.js.map +1 -0
  41. package/dist/api/utilities/channel-layout.d.ts +83 -0
  42. package/dist/api/utilities/channel-layout.js +87 -0
  43. package/dist/api/utilities/channel-layout.js.map +1 -0
  44. package/dist/api/utilities/image.d.ts +177 -0
  45. package/dist/api/utilities/image.js +183 -0
  46. package/dist/api/utilities/image.js.map +1 -0
  47. package/dist/api/utilities/index.d.ts +8 -0
  48. package/dist/api/utilities/index.js +17 -0
  49. package/dist/api/utilities/index.js.map +1 -0
  50. package/dist/api/utilities/media-type.d.ts +56 -0
  51. package/dist/api/utilities/media-type.js +60 -0
  52. package/dist/api/utilities/media-type.js.map +1 -0
  53. package/dist/api/utilities/pixel-format.d.ts +94 -0
  54. package/dist/api/utilities/pixel-format.js +102 -0
  55. package/dist/api/utilities/pixel-format.js.map +1 -0
  56. package/dist/api/utilities/sample-format.d.ts +132 -0
  57. package/dist/api/utilities/sample-format.js +144 -0
  58. package/dist/api/utilities/sample-format.js.map +1 -0
  59. package/dist/api/utilities/streaming.d.ts +104 -0
  60. package/dist/api/utilities/streaming.js +137 -0
  61. package/dist/api/utilities/streaming.js.map +1 -0
  62. package/dist/api/utilities/timestamp.d.ts +187 -0
  63. package/dist/api/utilities/timestamp.js +200 -0
  64. package/dist/api/utilities/timestamp.js.map +1 -0
  65. package/dist/api/utils.d.ts +61 -0
  66. package/dist/api/utils.js +330 -0
  67. package/dist/api/utils.js.map +1 -0
  68. package/dist/index.d.ts +2 -0
  69. package/dist/index.js +5 -0
  70. package/dist/index.js.map +1 -0
  71. package/dist/lib/audio-fifo.d.ts +339 -0
  72. package/dist/lib/audio-fifo.js +365 -0
  73. package/dist/lib/audio-fifo.js.map +1 -0
  74. package/dist/lib/binding.d.ts +192 -0
  75. package/dist/lib/binding.js +70 -0
  76. package/dist/lib/binding.js.map +1 -0
  77. package/dist/lib/bitstream-filter-context.d.ts +345 -0
  78. package/dist/lib/bitstream-filter-context.js +407 -0
  79. package/dist/lib/bitstream-filter-context.js.map +1 -0
  80. package/dist/lib/bitstream-filter.d.ts +124 -0
  81. package/dist/lib/bitstream-filter.js +138 -0
  82. package/dist/lib/bitstream-filter.js.map +1 -0
  83. package/dist/lib/channel-layouts.d.ts +51 -0
  84. package/dist/lib/channel-layouts.js +55 -0
  85. package/dist/lib/channel-layouts.js.map +1 -0
  86. package/dist/lib/codec-context.d.ts +763 -0
  87. package/dist/lib/codec-context.js +974 -0
  88. package/dist/lib/codec-context.js.map +1 -0
  89. package/dist/lib/codec-parameters.d.ts +362 -0
  90. package/dist/lib/codec-parameters.js +460 -0
  91. package/dist/lib/codec-parameters.js.map +1 -0
  92. package/dist/lib/codec-parser.d.ts +185 -0
  93. package/dist/lib/codec-parser.js +193 -0
  94. package/dist/lib/codec-parser.js.map +1 -0
  95. package/dist/lib/codec.d.ts +432 -0
  96. package/dist/lib/codec.js +492 -0
  97. package/dist/lib/codec.js.map +1 -0
  98. package/dist/lib/constants.d.ts +2037 -0
  99. package/dist/lib/constants.js +1659 -0
  100. package/dist/lib/constants.js.map +1 -0
  101. package/dist/lib/dictionary.d.ts +371 -0
  102. package/dist/lib/dictionary.js +406 -0
  103. package/dist/lib/dictionary.js.map +1 -0
  104. package/dist/lib/error.d.ts +216 -0
  105. package/dist/lib/error.js +254 -0
  106. package/dist/lib/error.js.map +1 -0
  107. package/dist/lib/filter-context.d.ts +445 -0
  108. package/dist/lib/filter-context.js +505 -0
  109. package/dist/lib/filter-context.js.map +1 -0
  110. package/dist/lib/filter-graph.d.ts +556 -0
  111. package/dist/lib/filter-graph.js +608 -0
  112. package/dist/lib/filter-graph.js.map +1 -0
  113. package/dist/lib/filter-inout.d.ts +205 -0
  114. package/dist/lib/filter-inout.js +264 -0
  115. package/dist/lib/filter-inout.js.map +1 -0
  116. package/dist/lib/filter.d.ts +231 -0
  117. package/dist/lib/filter.js +260 -0
  118. package/dist/lib/filter.js.map +1 -0
  119. package/dist/lib/format-context.d.ts +798 -0
  120. package/dist/lib/format-context.js +845 -0
  121. package/dist/lib/format-context.js.map +1 -0
  122. package/dist/lib/frame.d.ts +784 -0
  123. package/dist/lib/frame.js +933 -0
  124. package/dist/lib/frame.js.map +1 -0
  125. package/dist/lib/hardware-device-context.d.ts +407 -0
  126. package/dist/lib/hardware-device-context.js +429 -0
  127. package/dist/lib/hardware-device-context.js.map +1 -0
  128. package/dist/lib/hardware-frames-context.d.ts +374 -0
  129. package/dist/lib/hardware-frames-context.js +430 -0
  130. package/dist/lib/hardware-frames-context.js.map +1 -0
  131. package/dist/lib/index.d.ts +31 -0
  132. package/dist/lib/index.js +54 -0
  133. package/dist/lib/index.js.map +1 -0
  134. package/dist/lib/input-format.d.ts +216 -0
  135. package/dist/lib/input-format.js +246 -0
  136. package/dist/lib/input-format.js.map +1 -0
  137. package/dist/lib/io-context.d.ts +495 -0
  138. package/dist/lib/io-context.js +550 -0
  139. package/dist/lib/io-context.js.map +1 -0
  140. package/dist/lib/log.d.ts +201 -0
  141. package/dist/lib/log.js +219 -0
  142. package/dist/lib/log.js.map +1 -0
  143. package/dist/lib/native-types.d.ts +719 -0
  144. package/dist/lib/native-types.js +2 -0
  145. package/dist/lib/native-types.js.map +1 -0
  146. package/dist/lib/option.d.ts +589 -0
  147. package/dist/lib/option.js +853 -0
  148. package/dist/lib/option.js.map +1 -0
  149. package/dist/lib/output-format.d.ts +179 -0
  150. package/dist/lib/output-format.js +205 -0
  151. package/dist/lib/output-format.js.map +1 -0
  152. package/dist/lib/packet.d.ts +487 -0
  153. package/dist/lib/packet.js +558 -0
  154. package/dist/lib/packet.js.map +1 -0
  155. package/dist/lib/rational.d.ts +210 -0
  156. package/dist/lib/rational.js +233 -0
  157. package/dist/lib/rational.js.map +1 -0
  158. package/dist/lib/software-resample-context.d.ts +572 -0
  159. package/dist/lib/software-resample-context.js +610 -0
  160. package/dist/lib/software-resample-context.js.map +1 -0
  161. package/dist/lib/software-scale-context.d.ts +290 -0
  162. package/dist/lib/software-scale-context.js +308 -0
  163. package/dist/lib/software-scale-context.js.map +1 -0
  164. package/dist/lib/stream.d.ts +322 -0
  165. package/dist/lib/stream.js +408 -0
  166. package/dist/lib/stream.js.map +1 -0
  167. package/dist/lib/types.d.ts +59 -0
  168. package/dist/lib/types.js +8 -0
  169. package/dist/lib/types.js.map +1 -0
  170. package/dist/lib/utilities.d.ts +346 -0
  171. package/dist/lib/utilities.js +424 -0
  172. package/dist/lib/utilities.js.map +1 -0
  173. package/install/check.js +113 -0
  174. package/install/ffmpeg.js +163 -0
  175. package/package.json +107 -0
@@ -0,0 +1,339 @@
1
+ import type { AVSampleFormat } from './constants.js';
2
+ import type { NativeAudioFifo, NativeWrapper } from './native-types.js';
3
+ /**
4
+ * Audio FIFO buffer for sample management.
5
+ *
6
+ * Provides a first-in-first-out buffer for audio samples.
7
+ * Supports both planar and interleaved audio formats.
8
+ * Automatically handles reallocation when needed.
9
+ *
10
+ * Direct mapping to FFmpeg's AVAudioFifo.
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * import { AudioFifo, FFmpegError } from 'node-av';
15
+ * import { AV_SAMPLE_FMT_FLTP } from 'node-av/constants';
16
+ *
17
+ * // Create audio FIFO for stereo float samples
18
+ * const fifo = new AudioFifo();
19
+ * fifo.alloc(AV_SAMPLE_FMT_FLTP, 2, 4096);
20
+ *
21
+ * // Write samples to FIFO
22
+ * const samplesWritten = await fifo.write(inputBuffers, frameSize);
23
+ * FFmpegError.throwIfError(samplesWritten, 'write');
24
+ *
25
+ * // Read samples from FIFO
26
+ * const samplesRead = await fifo.read(outputBuffers, frameSize);
27
+ * FFmpegError.throwIfError(samplesRead, 'read');
28
+ *
29
+ * // Check available samples
30
+ * console.log(`Samples in FIFO: ${fifo.size}`);
31
+ *
32
+ * // Cleanup
33
+ * fifo.free();
34
+ * ```
35
+ */
36
+ export declare class AudioFifo implements Disposable, NativeWrapper<NativeAudioFifo> {
37
+ private native;
38
+ /**
39
+ * Create a new audio FIFO buffer.
40
+ *
41
+ * The FIFO is uninitialized - you must call alloc() before use.
42
+ * No FFmpeg resources are allocated until alloc() is called.
43
+ *
44
+ * Direct wrapper around AVAudioFifo.
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * import { AudioFifo } from 'node-av';
49
+ * import { AV_SAMPLE_FMT_S16 } from 'node-av/constants';
50
+ *
51
+ * const fifo = new AudioFifo();
52
+ * fifo.alloc(AV_SAMPLE_FMT_S16, 2, 1024);
53
+ * // FIFO is now ready for use
54
+ * ```
55
+ */
56
+ constructor();
57
+ /**
58
+ * Get the current number of samples in the AVAudioFifo.
59
+ *
60
+ * Returns the number of samples currently buffered in the FIFO.
61
+ *
62
+ * Direct mapping to av_audio_fifo_size()
63
+ *
64
+ * @returns Number of samples currently in the FIFO
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * import { FFmpegError } from 'node-av';
69
+ *
70
+ * if (fifo.size >= frameSize) {
71
+ * // Enough samples available for a full frame
72
+ * const ret = await fifo.read(outputBuffer, frameSize);
73
+ * FFmpegError.throwIfError(ret, 'read');
74
+ * }
75
+ * ```
76
+ *
77
+ * @readonly
78
+ */
79
+ get size(): number;
80
+ /**
81
+ * Get the available space in the AVAudioFifo.
82
+ *
83
+ * Returns the number of samples that can be written without reallocation.
84
+ *
85
+ * Direct mapping to av_audio_fifo_space()
86
+ *
87
+ * @returns Number of samples that can be written to the FIFO
88
+ *
89
+ * @example
90
+ * ```typescript
91
+ * import { FFmpegError } from 'node-av';
92
+ *
93
+ * if (fifo.space >= frameSize) {
94
+ * // Enough space for a full frame
95
+ * const ret = await fifo.write(inputBuffer, frameSize);
96
+ * FFmpegError.throwIfError(ret, 'write');
97
+ * }
98
+ * ```
99
+ *
100
+ * @readonly
101
+ */
102
+ get space(): number;
103
+ /**
104
+ * Allocate an AVAudioFifo.
105
+ *
106
+ * Allocates a FIFO buffer for the specified audio format and size.
107
+ * The FIFO will automatically grow if more data is written than allocated.
108
+ *
109
+ * Direct mapping to av_audio_fifo_alloc()
110
+ *
111
+ * @param sampleFmt - Sample format (e.g., AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLTP)
112
+ * @param channels - Number of channels
113
+ * @param nbSamples - Initial allocation size, in samples
114
+ *
115
+ * @throws {Error} Memory allocation failure (ENOMEM)
116
+ *
117
+ * @example
118
+ * ```typescript
119
+ * import { AudioFifo } from 'node-av';
120
+ * import { AV_SAMPLE_FMT_FLTP } from 'node-av/constants';
121
+ *
122
+ * const fifo = new AudioFifo();
123
+ * fifo.alloc(AV_SAMPLE_FMT_FLTP, 2, 4096);
124
+ * // FIFO can now hold up to 4096 stereo float samples
125
+ * ```
126
+ *
127
+ * @see {@link realloc} To resize the FIFO
128
+ */
129
+ alloc(sampleFmt: AVSampleFormat, channels: number, nbSamples: number): void;
130
+ /**
131
+ * Free an AVAudioFifo.
132
+ *
133
+ * Frees the FIFO buffer and all associated resources.
134
+ * The FIFO object becomes invalid after this call.
135
+ *
136
+ * Direct mapping to av_audio_fifo_free()
137
+ *
138
+ * @example
139
+ * ```typescript
140
+ * fifo.free();
141
+ * // FIFO is now invalid and should not be used
142
+ * ```
143
+ */
144
+ free(): void;
145
+ /**
146
+ * Write data to an AVAudioFifo.
147
+ *
148
+ * Writes audio samples to the FIFO buffer.
149
+ * Automatically reallocates the FIFO if more space is needed.
150
+ *
151
+ * Direct mapping to av_audio_fifo_write()
152
+ *
153
+ * @param data - Audio buffer(s) to write from.
154
+ * For planar formats: array of buffers (one per channel).
155
+ * For interleaved formats: single buffer.
156
+ * @param nbSamples - Number of samples to write
157
+ *
158
+ * @returns Number of samples actually written, or negative AVERROR on error:
159
+ * - >=0: Number of samples written
160
+ * - AVERROR(EINVAL): Invalid parameters
161
+ * - AVERROR(ENOMEM): Memory allocation failure
162
+ *
163
+ * @example
164
+ * ```typescript
165
+ * import { FFmpegError } from 'node-av';
166
+ *
167
+ * // Planar format (separate buffers per channel)
168
+ * const leftChannel = Buffer.alloc(frameSize * 4); // float32
169
+ * const rightChannel = Buffer.alloc(frameSize * 4);
170
+ * const written = await fifo.write([leftChannel, rightChannel], frameSize);
171
+ * FFmpegError.throwIfError(written, 'write');
172
+ *
173
+ * // Interleaved format (single buffer)
174
+ * const interleavedBuffer = Buffer.alloc(frameSize * 2 * 2); // stereo s16
175
+ * const written2 = await fifo.write(interleavedBuffer, frameSize);
176
+ * FFmpegError.throwIfError(written2, 'write');
177
+ * ```
178
+ *
179
+ * @see {@link read} To read samples from FIFO
180
+ */
181
+ write(data: Buffer | Buffer[], nbSamples: number): Promise<number>;
182
+ /**
183
+ * Read data from an AVAudioFifo.
184
+ *
185
+ * Reads and removes samples from the FIFO buffer.
186
+ * Reads up to nb_samples or the available amount, whichever is less.
187
+ *
188
+ * Direct mapping to av_audio_fifo_read()
189
+ *
190
+ * @param data - Audio buffer(s) to read into.
191
+ * For planar formats: array of buffers (one per channel).
192
+ * For interleaved formats: single buffer.
193
+ * Buffers must be pre-allocated with sufficient size.
194
+ * @param nbSamples - Number of samples to read
195
+ *
196
+ * @returns Number of samples actually read, or negative AVERROR on error:
197
+ * - >=0: Number of samples read
198
+ * - AVERROR(EINVAL): Invalid parameters
199
+ *
200
+ * @example
201
+ * ```typescript
202
+ * import { FFmpegError } from 'node-av';
203
+ *
204
+ * // Planar format (separate buffers per channel)
205
+ * const leftChannel = Buffer.alloc(frameSize * 4); // float32
206
+ * const rightChannel = Buffer.alloc(frameSize * 4);
207
+ * const read = await fifo.read([leftChannel, rightChannel], frameSize);
208
+ * FFmpegError.throwIfError(read, 'read');
209
+ *
210
+ * // Interleaved format (single buffer)
211
+ * const interleavedBuffer = Buffer.alloc(frameSize * 2 * 2); // stereo s16
212
+ * const read2 = await fifo.read(interleavedBuffer, frameSize);
213
+ * FFmpegError.throwIfError(read2, 'read');
214
+ * ```
215
+ *
216
+ * @see {@link peek} To read without removing
217
+ * @see {@link write} To write samples to FIFO
218
+ */
219
+ read(data: Buffer | Buffer[], nbSamples: number): Promise<number>;
220
+ /**
221
+ * Peek data from an AVAudioFifo.
222
+ *
223
+ * Reads samples from the FIFO without removing them.
224
+ * Useful for inspecting upcoming data.
225
+ *
226
+ * Direct mapping to av_audio_fifo_peek()
227
+ *
228
+ * @param data - Audio buffer(s) to peek into.
229
+ * For planar formats: array of buffers (one per channel).
230
+ * For interleaved formats: single buffer.
231
+ * Buffers must be pre-allocated.
232
+ * @param nbSamples - Number of samples to peek
233
+ *
234
+ * @returns Number of samples actually peeked, or negative AVERROR on error:
235
+ * - >=0: Number of samples peeked
236
+ * - AVERROR(EINVAL): Invalid parameters
237
+ *
238
+ * @example
239
+ * ```typescript
240
+ * import { FFmpegError } from 'node-av';
241
+ *
242
+ * // Peek at next samples without removing them
243
+ * const peekBuffer = Buffer.alloc(frameSize * 4);
244
+ * const peeked = await fifo.peek(peekBuffer, frameSize);
245
+ * FFmpegError.throwIfError(peeked, 'peek');
246
+ * // Samples are still in FIFO after peek
247
+ * ```
248
+ *
249
+ * @see {@link read} To read and remove samples
250
+ */
251
+ peek(data: Buffer | Buffer[], nbSamples: number): Promise<number>;
252
+ /**
253
+ * Remove samples from the FIFO without reading them.
254
+ *
255
+ * Discards the specified number of samples from the FIFO.
256
+ * Useful for skipping unwanted audio data.
257
+ *
258
+ * Direct mapping to av_audio_fifo_drain()
259
+ *
260
+ * @param nbSamples - Number of samples to drain
261
+ *
262
+ * @example
263
+ * ```typescript
264
+ * // Skip next 100 samples
265
+ * fifo.drain(100);
266
+ * ```
267
+ */
268
+ drain(nbSamples: number): void;
269
+ /**
270
+ * Reset the AVAudioFifo buffer.
271
+ *
272
+ * Empties all data in the FIFO without deallocating the buffer.
273
+ * The FIFO remains allocated and ready for use.
274
+ *
275
+ * Direct mapping to av_audio_fifo_reset()
276
+ *
277
+ * @example
278
+ * ```typescript
279
+ * fifo.reset();
280
+ * // FIFO is now empty but still allocated
281
+ * ```
282
+ */
283
+ reset(): void;
284
+ /**
285
+ * Reallocate an AVAudioFifo to a new size.
286
+ *
287
+ * Changes the size of the FIFO buffer.
288
+ * Can be used to grow or shrink the buffer.
289
+ *
290
+ * Direct mapping to av_audio_fifo_realloc()
291
+ *
292
+ * @param nbSamples - New allocation size, in samples
293
+ *
294
+ * @returns 0 on success, negative AVERROR on error:
295
+ * - 0: Success
296
+ * - AVERROR(EINVAL): Invalid parameters
297
+ * - AVERROR(ENOMEM): Memory allocation failure
298
+ *
299
+ * @example
300
+ * ```typescript
301
+ * import { FFmpegError } from 'node-av';
302
+ *
303
+ * // Increase FIFO capacity
304
+ * const ret = fifo.realloc(8192);
305
+ * FFmpegError.throwIfError(ret, 'realloc');
306
+ * ```
307
+ *
308
+ * @see {@link alloc} For initial allocation
309
+ */
310
+ realloc(nbSamples: number): number;
311
+ /**
312
+ * Get the native FFmpeg AVAudioFifo pointer.
313
+ *
314
+ * @internal For use by other wrapper classes
315
+ * @returns The underlying native AudioFifo object
316
+ */
317
+ getNative(): NativeAudioFifo;
318
+ /**
319
+ * Dispose of the audio FIFO buffer.
320
+ *
321
+ * Implements the Disposable interface for automatic cleanup.
322
+ * Equivalent to calling free().
323
+ *
324
+ * @example
325
+ * ```typescript
326
+ * import { AudioFifo } from 'node-av';
327
+ * import { AV_SAMPLE_FMT_S16 } from 'node-av/constants';
328
+ *
329
+ * {
330
+ * using fifo = new AudioFifo();
331
+ * fifo.alloc(AV_SAMPLE_FMT_S16, 2, 1024);
332
+ * // ... use FIFO
333
+ * } // Automatically freed when leaving scope
334
+ * ```
335
+ *
336
+ * @see {@link free} For manual cleanup
337
+ */
338
+ [Symbol.dispose](): void;
339
+ }
@@ -0,0 +1,365 @@
1
+ import { bindings } from './binding.js';
2
+ /**
3
+ * Audio FIFO buffer for sample management.
4
+ *
5
+ * Provides a first-in-first-out buffer for audio samples.
6
+ * Supports both planar and interleaved audio formats.
7
+ * Automatically handles reallocation when needed.
8
+ *
9
+ * Direct mapping to FFmpeg's AVAudioFifo.
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * import { AudioFifo, FFmpegError } from 'node-av';
14
+ * import { AV_SAMPLE_FMT_FLTP } from 'node-av/constants';
15
+ *
16
+ * // Create audio FIFO for stereo float samples
17
+ * const fifo = new AudioFifo();
18
+ * fifo.alloc(AV_SAMPLE_FMT_FLTP, 2, 4096);
19
+ *
20
+ * // Write samples to FIFO
21
+ * const samplesWritten = await fifo.write(inputBuffers, frameSize);
22
+ * FFmpegError.throwIfError(samplesWritten, 'write');
23
+ *
24
+ * // Read samples from FIFO
25
+ * const samplesRead = await fifo.read(outputBuffers, frameSize);
26
+ * FFmpegError.throwIfError(samplesRead, 'read');
27
+ *
28
+ * // Check available samples
29
+ * console.log(`Samples in FIFO: ${fifo.size}`);
30
+ *
31
+ * // Cleanup
32
+ * fifo.free();
33
+ * ```
34
+ */
35
+ export class AudioFifo {
36
+ native;
37
+ /**
38
+ * Create a new audio FIFO buffer.
39
+ *
40
+ * The FIFO is uninitialized - you must call alloc() before use.
41
+ * No FFmpeg resources are allocated until alloc() is called.
42
+ *
43
+ * Direct wrapper around AVAudioFifo.
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * import { AudioFifo } from 'node-av';
48
+ * import { AV_SAMPLE_FMT_S16 } from 'node-av/constants';
49
+ *
50
+ * const fifo = new AudioFifo();
51
+ * fifo.alloc(AV_SAMPLE_FMT_S16, 2, 1024);
52
+ * // FIFO is now ready for use
53
+ * ```
54
+ */
55
+ constructor() {
56
+ this.native = new bindings.AudioFifo();
57
+ }
58
+ /**
59
+ * Get the current number of samples in the AVAudioFifo.
60
+ *
61
+ * Returns the number of samples currently buffered in the FIFO.
62
+ *
63
+ * Direct mapping to av_audio_fifo_size()
64
+ *
65
+ * @returns Number of samples currently in the FIFO
66
+ *
67
+ * @example
68
+ * ```typescript
69
+ * import { FFmpegError } from 'node-av';
70
+ *
71
+ * if (fifo.size >= frameSize) {
72
+ * // Enough samples available for a full frame
73
+ * const ret = await fifo.read(outputBuffer, frameSize);
74
+ * FFmpegError.throwIfError(ret, 'read');
75
+ * }
76
+ * ```
77
+ *
78
+ * @readonly
79
+ */
80
+ get size() {
81
+ return this.native.size;
82
+ }
83
+ /**
84
+ * Get the available space in the AVAudioFifo.
85
+ *
86
+ * Returns the number of samples that can be written without reallocation.
87
+ *
88
+ * Direct mapping to av_audio_fifo_space()
89
+ *
90
+ * @returns Number of samples that can be written to the FIFO
91
+ *
92
+ * @example
93
+ * ```typescript
94
+ * import { FFmpegError } from 'node-av';
95
+ *
96
+ * if (fifo.space >= frameSize) {
97
+ * // Enough space for a full frame
98
+ * const ret = await fifo.write(inputBuffer, frameSize);
99
+ * FFmpegError.throwIfError(ret, 'write');
100
+ * }
101
+ * ```
102
+ *
103
+ * @readonly
104
+ */
105
+ get space() {
106
+ return this.native.space;
107
+ }
108
+ /**
109
+ * Allocate an AVAudioFifo.
110
+ *
111
+ * Allocates a FIFO buffer for the specified audio format and size.
112
+ * The FIFO will automatically grow if more data is written than allocated.
113
+ *
114
+ * Direct mapping to av_audio_fifo_alloc()
115
+ *
116
+ * @param sampleFmt - Sample format (e.g., AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLTP)
117
+ * @param channels - Number of channels
118
+ * @param nbSamples - Initial allocation size, in samples
119
+ *
120
+ * @throws {Error} Memory allocation failure (ENOMEM)
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * import { AudioFifo } from 'node-av';
125
+ * import { AV_SAMPLE_FMT_FLTP } from 'node-av/constants';
126
+ *
127
+ * const fifo = new AudioFifo();
128
+ * fifo.alloc(AV_SAMPLE_FMT_FLTP, 2, 4096);
129
+ * // FIFO can now hold up to 4096 stereo float samples
130
+ * ```
131
+ *
132
+ * @see {@link realloc} To resize the FIFO
133
+ */
134
+ alloc(sampleFmt, channels, nbSamples) {
135
+ this.native.alloc(sampleFmt, channels, nbSamples);
136
+ }
137
+ /**
138
+ * Free an AVAudioFifo.
139
+ *
140
+ * Frees the FIFO buffer and all associated resources.
141
+ * The FIFO object becomes invalid after this call.
142
+ *
143
+ * Direct mapping to av_audio_fifo_free()
144
+ *
145
+ * @example
146
+ * ```typescript
147
+ * fifo.free();
148
+ * // FIFO is now invalid and should not be used
149
+ * ```
150
+ */
151
+ free() {
152
+ this.native.free();
153
+ }
154
+ /**
155
+ * Write data to an AVAudioFifo.
156
+ *
157
+ * Writes audio samples to the FIFO buffer.
158
+ * Automatically reallocates the FIFO if more space is needed.
159
+ *
160
+ * Direct mapping to av_audio_fifo_write()
161
+ *
162
+ * @param data - Audio buffer(s) to write from.
163
+ * For planar formats: array of buffers (one per channel).
164
+ * For interleaved formats: single buffer.
165
+ * @param nbSamples - Number of samples to write
166
+ *
167
+ * @returns Number of samples actually written, or negative AVERROR on error:
168
+ * - >=0: Number of samples written
169
+ * - AVERROR(EINVAL): Invalid parameters
170
+ * - AVERROR(ENOMEM): Memory allocation failure
171
+ *
172
+ * @example
173
+ * ```typescript
174
+ * import { FFmpegError } from 'node-av';
175
+ *
176
+ * // Planar format (separate buffers per channel)
177
+ * const leftChannel = Buffer.alloc(frameSize * 4); // float32
178
+ * const rightChannel = Buffer.alloc(frameSize * 4);
179
+ * const written = await fifo.write([leftChannel, rightChannel], frameSize);
180
+ * FFmpegError.throwIfError(written, 'write');
181
+ *
182
+ * // Interleaved format (single buffer)
183
+ * const interleavedBuffer = Buffer.alloc(frameSize * 2 * 2); // stereo s16
184
+ * const written2 = await fifo.write(interleavedBuffer, frameSize);
185
+ * FFmpegError.throwIfError(written2, 'write');
186
+ * ```
187
+ *
188
+ * @see {@link read} To read samples from FIFO
189
+ */
190
+ async write(data, nbSamples) {
191
+ return this.native.write(data, nbSamples);
192
+ }
193
+ /**
194
+ * Read data from an AVAudioFifo.
195
+ *
196
+ * Reads and removes samples from the FIFO buffer.
197
+ * Reads up to nb_samples or the available amount, whichever is less.
198
+ *
199
+ * Direct mapping to av_audio_fifo_read()
200
+ *
201
+ * @param data - Audio buffer(s) to read into.
202
+ * For planar formats: array of buffers (one per channel).
203
+ * For interleaved formats: single buffer.
204
+ * Buffers must be pre-allocated with sufficient size.
205
+ * @param nbSamples - Number of samples to read
206
+ *
207
+ * @returns Number of samples actually read, or negative AVERROR on error:
208
+ * - >=0: Number of samples read
209
+ * - AVERROR(EINVAL): Invalid parameters
210
+ *
211
+ * @example
212
+ * ```typescript
213
+ * import { FFmpegError } from 'node-av';
214
+ *
215
+ * // Planar format (separate buffers per channel)
216
+ * const leftChannel = Buffer.alloc(frameSize * 4); // float32
217
+ * const rightChannel = Buffer.alloc(frameSize * 4);
218
+ * const read = await fifo.read([leftChannel, rightChannel], frameSize);
219
+ * FFmpegError.throwIfError(read, 'read');
220
+ *
221
+ * // Interleaved format (single buffer)
222
+ * const interleavedBuffer = Buffer.alloc(frameSize * 2 * 2); // stereo s16
223
+ * const read2 = await fifo.read(interleavedBuffer, frameSize);
224
+ * FFmpegError.throwIfError(read2, 'read');
225
+ * ```
226
+ *
227
+ * @see {@link peek} To read without removing
228
+ * @see {@link write} To write samples to FIFO
229
+ */
230
+ async read(data, nbSamples) {
231
+ return this.native.read(data, nbSamples);
232
+ }
233
+ /**
234
+ * Peek data from an AVAudioFifo.
235
+ *
236
+ * Reads samples from the FIFO without removing them.
237
+ * Useful for inspecting upcoming data.
238
+ *
239
+ * Direct mapping to av_audio_fifo_peek()
240
+ *
241
+ * @param data - Audio buffer(s) to peek into.
242
+ * For planar formats: array of buffers (one per channel).
243
+ * For interleaved formats: single buffer.
244
+ * Buffers must be pre-allocated.
245
+ * @param nbSamples - Number of samples to peek
246
+ *
247
+ * @returns Number of samples actually peeked, or negative AVERROR on error:
248
+ * - >=0: Number of samples peeked
249
+ * - AVERROR(EINVAL): Invalid parameters
250
+ *
251
+ * @example
252
+ * ```typescript
253
+ * import { FFmpegError } from 'node-av';
254
+ *
255
+ * // Peek at next samples without removing them
256
+ * const peekBuffer = Buffer.alloc(frameSize * 4);
257
+ * const peeked = await fifo.peek(peekBuffer, frameSize);
258
+ * FFmpegError.throwIfError(peeked, 'peek');
259
+ * // Samples are still in FIFO after peek
260
+ * ```
261
+ *
262
+ * @see {@link read} To read and remove samples
263
+ */
264
+ async peek(data, nbSamples) {
265
+ return this.native.peek(data, nbSamples);
266
+ }
267
+ /**
268
+ * Remove samples from the FIFO without reading them.
269
+ *
270
+ * Discards the specified number of samples from the FIFO.
271
+ * Useful for skipping unwanted audio data.
272
+ *
273
+ * Direct mapping to av_audio_fifo_drain()
274
+ *
275
+ * @param nbSamples - Number of samples to drain
276
+ *
277
+ * @example
278
+ * ```typescript
279
+ * // Skip next 100 samples
280
+ * fifo.drain(100);
281
+ * ```
282
+ */
283
+ drain(nbSamples) {
284
+ this.native.drain(nbSamples);
285
+ }
286
+ /**
287
+ * Reset the AVAudioFifo buffer.
288
+ *
289
+ * Empties all data in the FIFO without deallocating the buffer.
290
+ * The FIFO remains allocated and ready for use.
291
+ *
292
+ * Direct mapping to av_audio_fifo_reset()
293
+ *
294
+ * @example
295
+ * ```typescript
296
+ * fifo.reset();
297
+ * // FIFO is now empty but still allocated
298
+ * ```
299
+ */
300
+ reset() {
301
+ this.native.reset();
302
+ }
303
+ /**
304
+ * Reallocate an AVAudioFifo to a new size.
305
+ *
306
+ * Changes the size of the FIFO buffer.
307
+ * Can be used to grow or shrink the buffer.
308
+ *
309
+ * Direct mapping to av_audio_fifo_realloc()
310
+ *
311
+ * @param nbSamples - New allocation size, in samples
312
+ *
313
+ * @returns 0 on success, negative AVERROR on error:
314
+ * - 0: Success
315
+ * - AVERROR(EINVAL): Invalid parameters
316
+ * - AVERROR(ENOMEM): Memory allocation failure
317
+ *
318
+ * @example
319
+ * ```typescript
320
+ * import { FFmpegError } from 'node-av';
321
+ *
322
+ * // Increase FIFO capacity
323
+ * const ret = fifo.realloc(8192);
324
+ * FFmpegError.throwIfError(ret, 'realloc');
325
+ * ```
326
+ *
327
+ * @see {@link alloc} For initial allocation
328
+ */
329
+ realloc(nbSamples) {
330
+ return this.native.realloc(nbSamples);
331
+ }
332
+ /**
333
+ * Get the native FFmpeg AVAudioFifo pointer.
334
+ *
335
+ * @internal For use by other wrapper classes
336
+ * @returns The underlying native AudioFifo object
337
+ */
338
+ getNative() {
339
+ return this.native;
340
+ }
341
+ /**
342
+ * Dispose of the audio FIFO buffer.
343
+ *
344
+ * Implements the Disposable interface for automatic cleanup.
345
+ * Equivalent to calling free().
346
+ *
347
+ * @example
348
+ * ```typescript
349
+ * import { AudioFifo } from 'node-av';
350
+ * import { AV_SAMPLE_FMT_S16 } from 'node-av/constants';
351
+ *
352
+ * {
353
+ * using fifo = new AudioFifo();
354
+ * fifo.alloc(AV_SAMPLE_FMT_S16, 2, 1024);
355
+ * // ... use FIFO
356
+ * } // Automatically freed when leaving scope
357
+ * ```
358
+ *
359
+ * @see {@link free} For manual cleanup
360
+ */
361
+ [Symbol.dispose]() {
362
+ this.native[Symbol.dispose]();
363
+ }
364
+ }
365
+ //# sourceMappingURL=audio-fifo.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"audio-fifo.js","sourceRoot":"","sources":["../../src/lib/audio-fifo.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAKxC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,OAAO,SAAS;IACZ,MAAM,CAAkB;IAEhC;;;;;;;;;;;;;;;;;OAiBG;IACH;QACE,IAAI,CAAC,MAAM,GAAG,IAAI,QAAQ,CAAC,SAAS,EAAE,CAAC;IACzC,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC;IAC1B,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;IAC3B,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,KAAK,CAAC,SAAyB,EAAE,QAAgB,EAAE,SAAiB;QAClE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC;IACpD,CAAC;IAED;;;;;;;;;;;;;OAaG;IACH,IAAI;QACF,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IACrB,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAmCG;IACH,KAAK,CAAC,KAAK,CAAC,IAAuB,EAAE,SAAiB;QACpD,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;IAC5C,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAoCG;IACH,KAAK,CAAC,IAAI,CAAC,IAAuB,EAAE,SAAiB;QACnD,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;IAC3C,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACH,KAAK,CAAC,IAAI,CAAC,IAAuB,EAAE,SAAiB;QACnD,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;IAC3C,CAAC;IAED;;;;;;;;;;;;;;;OAeG;IACH,KAAK,CAAC,SAAiB;QACrB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IAC/B,CAAC;IAED;;;;;;;;;;;;;OAaG;IACH,KAAK;QACH,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IACtB,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,OAAO,CAAC,SAAiB;QACvB,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACxC,CAAC;IAED;;;;;OAKG;IACH,SAAS;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,CAAC,MAAM,CAAC,OAAO,CAAC;QACd,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;IAChC,CAAC;CACF"}