@qvac/decoder-audio 0.2.10 → 0.3.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.
package/README.md CHANGED
@@ -1,12 +1,11 @@
1
1
  # qvac-lib-decoder-audio
2
2
 
3
- This decoder addon leverages the GStreamer pipeline for efficient, asynchronous audio decoding. It simplifies processing of input audio, particularly as a preprocessing step for other addons.
3
+ This decoder library leverages FFmpeg for efficient audio decoding. It simplifies processing of input audio, particularly as a preprocessing step for other addons.
4
4
 
5
5
  ## Table of Contents
6
6
 
7
7
  - [Installation](#installation)
8
8
  - [Usage](#usage)
9
- - [Supported Audio Formats](#supported-audio-formats)
10
9
  - [1. Creating the Decoder Instance](#1-creating-the-decoder-instance)
11
10
  - [2. Loading the Decoder](#2-loading-the-decoder)
12
11
  - [3. Decoding Audio](#3-decoding-audio)
@@ -57,34 +56,16 @@ npm install @tetherto/qvac-lib-decoder-audio@latest
57
56
 
58
57
  This library provides a simple workflow for decoding audio streams.
59
58
 
60
- ### Supported Audio Formats
61
-
62
- The library exports two constants that define the supported audio formats:
63
-
64
- ```javascript
65
- const GSTDecoder = require('@tetherto/qvac-lib-decoder-audio')
66
-
67
- // Audio formats that require decoding before processing
68
- console.log(GSTDecoder.FORMATS_NEEDING_DECODE)
69
- // ['.mp3', '.m4a', '.ogg', '.flac', '.aac', '.wav']
70
-
71
- // All supported audio formats (including raw)
72
- console.log(GSTDecoder.SUPPORTED_AUDIO_FORMATS)
73
- // ['.mp3', '.m4a', '.ogg', '.wav', '.flac', '.aac', '.raw']
74
- ```
75
-
76
- These constants can be used to validate input file formats or to determine if a file needs decoding before processing.
77
-
78
59
  ### 1. Creating the Decoder Instance
79
60
 
80
- To get started, import the decoder addon and create an instance:
61
+ To get started, import the decoder and create an instance:
81
62
 
82
63
  ```javascript
83
- const GSTDecoder = require('@tetherto/qvac-lib-decoder-audio')
64
+ const { FFmpegDecoder } = require('@tetherto/qvac-lib-decoder-audio')
84
65
 
85
- const decoder = new GSTDecoder({
66
+ const decoder = new FFmpegDecoder({
86
67
  config: {
87
- audioFormat: 'encoded', // 'encoded' | 's16le' | 'f32le'; default is 'encoded'
68
+ audioFormat: 's16le', // 's16le' | 'f32le'; default is 's16le'
88
69
  sampleRate: 16000 // in Hz; default is 16000
89
70
  }
90
71
  })
@@ -92,20 +73,19 @@ const decoder = new GSTDecoder({
92
73
 
93
74
  The `config` object accepts the following parameters:
94
75
 
95
- * **`audioFormat`**: Specifies the format of the input audio. Supported values:
76
+ * **`audioFormat`**: Specifies the output format of the decoded audio. Supported values:
96
77
 
97
- * `'encoded'`: For encoded audio input. Currently supported formats are OGG, WAV, MP3 and M4A.
98
78
  * `'s16le'`: Signed 16-bit little-endian PCM — a widely used raw format.
99
79
  * `'f32le'`: 32-bit floating-point little-endian PCM — ideal for high-precision audio processing.
100
80
 
101
- Default: `'encoded'`.
81
+ Default: `'s16le'`.
102
82
 
103
- * **`sampleRate`**: Sample rate of the input audio in Hertz (Hz).
83
+ * **`sampleRate`**: Sample rate of the output audio in Hertz (Hz).
104
84
  Default: `16000` (16 kHz), commonly used for speech processing.
105
85
 
106
86
  ### 2. Loading the Decoder
107
87
 
108
- Initializes and activates the decoder addon with the provided or default configuration. This method must be called before decoding any audio input.
88
+ Initializes and activates the decoder with the provided or default configuration. This method must be called before decoding any audio input.
109
89
 
110
90
  ```javascript
111
91
  try {
@@ -156,7 +136,7 @@ try {
156
136
 
157
137
  ## Quickstart Example
158
138
 
159
- The following example demonstrates how to use the decoder addon to decode a sample OGG file into a raw audio file. Follow these steps, to run the example:
139
+ The following example demonstrates how to use the decoder to decode a sample OGG file into a raw audio file. Follow these steps, to run the example:
160
140
 
161
141
  ### 1. Create a new project:
162
142
 
@@ -178,45 +158,38 @@ npm install bare-fs @tetherto/qvac-lib-decoder-audio
178
158
  'use strict'
179
159
 
180
160
  const fs = require('bare-fs')
181
- const GSTDecoder = require('@tetherto/qvac-lib-decoder-audio')
161
+ const { FFmpegDecoder } = require('@tetherto/qvac-lib-decoder-audio')
182
162
 
183
163
  const audioFilePath = './path/to/audio/file.ogg'
184
164
  const outputFilePath = './path/to/output/file.raw'
185
165
 
186
166
  async function main () {
187
- // 1. Create a decoder instance
188
- const decoder = new GSTDecoder({
167
+ const decoder = new FFmpegDecoder({
189
168
  config: {
190
- audioFormat: 'encoded' // Use 'encoded' for OGG input
169
+ audioFormat: 's16le',
170
+ sampleRate: 16000
191
171
  }
192
172
  })
193
173
 
194
174
  try {
195
- // 2. Load the decoder
196
175
  await decoder.load()
197
176
 
198
- // 3. Create audio stream and pass it to the decoder
199
177
  const audioStream = fs.createReadStream(audioFilePath)
200
178
  const response = await decoder.run(audioStream)
201
179
 
202
180
  const decodedFileBuffer = []
203
181
 
204
- // 4. Handle response updates
205
182
  await response
206
183
  .onUpdate(output => {
207
184
  const bytes = new Uint8Array(output.outputArray)
208
-
209
- // Collect decoded bytes
210
185
  decodedFileBuffer.push(bytes)
211
186
  })
212
187
  .onFinish(() => {
213
- // Write the decoded bytes to a file on finish
214
188
  fs.writeFileSync(outputFilePath, Buffer.concat(decodedFileBuffer))
215
189
  console.log('Decoded file saved to', outputFilePath)
216
190
  })
217
191
  .await()
218
192
  } finally {
219
- // 5. Unload the decoder
220
193
  await decoder.unload()
221
194
  }
222
195
  }
@@ -267,10 +240,9 @@ Coverage reports are generated in the 'coverage/unit/' directory. Open the corre
267
240
  ## Resources
268
241
 
269
242
  * GitHub Repo: [tetherto/qvac-lib-decoder-audio](https://github.com/tetherto/qvac-lib-decoder-audio)
270
- * GStreamer: [gstreamer.freedesktop.org](https://gstreamer.freedesktop.org/)
271
243
 
272
244
  ## License
273
245
 
274
246
  This project is licensed under the Apache-2.0 License – see the [LICENSE](LICENSE) file for details.
275
247
 
276
- *For questions or issues, please open an issue on the GitHub repository.*
248
+ *For questions or issues, please open an issue on the GitHub repository.*
package/index.d.ts CHANGED
@@ -1,69 +1,51 @@
1
1
  import BaseInference = require('@qvac/infer-base/WeightsProvider/BaseInference')
2
2
  import QvacResponse from '@qvac/response'
3
3
 
4
- type AppendInput = {
5
- type: 'audio' | 'end of job'
6
- input?: ArrayBuffer
7
- priority?: number
4
+ interface AudioFormatConfig {
5
+ format: number | null
6
+ byteLength: number
8
7
  }
9
8
 
10
- interface DecoderConfig {
11
- audioFormat?: string
12
- sampleRate?: number
13
- [key: string]: any
9
+ interface SupportedAudioFormats {
10
+ s16le: AudioFormatConfig
11
+ f32le: AudioFormatConfig
14
12
  }
15
13
 
16
- interface AudioStream {
17
- [Symbol.asyncIterator](): AsyncIterator<{ buffer: Buffer }>
18
- }
19
-
20
- interface Addon {
21
- activate(): Promise<void>
22
- append(input: AppendInput): Promise<number>
23
- cancel(jobId: number): Promise<void>
24
- pause(): Promise<void>
25
- continue(): Promise<void>
26
- status(): Promise<any>
27
- loadWeights?(params: {
28
- filename: string
29
- contents?: Buffer
30
- completed: boolean
31
- }): Promise<void>
14
+ interface FFmpegDecoderConfig {
15
+ streamIndex?: number
16
+ inputBitrate?: number
17
+ audioFormat?: 's16le' | 'f32le'
18
+ sampleRate?: number
32
19
  }
33
20
 
34
- interface GSTDecoderConstructorParams {
35
- config: DecoderConfig
21
+ interface FFmpegDecoderConstructorParams {
22
+ config?: FFmpegDecoderConfig
23
+ logger?: any
24
+ streamIndex?: number
25
+ inputBitrate?: number
26
+ audioFormat?: 's16le' | 'f32le'
36
27
  [key: string]: any
37
28
  }
38
29
 
39
- /**
40
- * GSTDecoder client implementation for the Whisper transcription model
41
- */
42
- declare class GSTDecoder extends BaseInference {
43
- /**
44
- * Creates an instance of GSTDecoder.
45
- * @constructor
46
- * @param {Object} config - environment-specific inference setup configuration
47
- */
48
- constructor({ config, ...args }: GSTDecoderConstructorParams)
49
- private config: DecoderConfig
50
- private addon: Addon
51
- load(): Promise<void>
52
- createAddon(config: DecoderConfig): Addon
53
- protected _runInternal(audioStream: AudioStream): Promise<QvacResponse>
54
- getStatus(): Promise<any>
30
+ interface DecoderStatus {
31
+ loaded: boolean
32
+ active: boolean
33
+ paused: boolean
55
34
  }
56
35
 
57
- declare namespace GSTDecoder {
58
- /**
59
- * Audio formats that require decoding before processing
60
- */
61
- export const FORMATS_NEEDING_DECODE: readonly string[]
36
+ declare class FFmpegDecoder extends BaseInference {
37
+ SUPPORTED_AUDIO_FORMATS: SupportedAudioFormats
38
+ OUTPUT_CHANNEL_LAYOUT: number | null
39
+
40
+ constructor(params: FFmpegDecoderConstructorParams)
62
41
 
63
- /**
64
- * All supported audio formats (including raw)
65
- */
66
- export const SUPPORTED_AUDIO_FORMATS: readonly string[]
42
+ load(): Promise<void>
43
+ unload(): Promise<void>
44
+ run(audioStream: AsyncIterable<Buffer>): Promise<QvacResponse>
45
+ pause(): Promise<void>
46
+ unpause(): Promise<void>
47
+ stop(): Promise<void>
48
+ status(): DecoderStatus
67
49
  }
68
50
 
69
- export = GSTDecoder
51
+ export { FFmpegDecoder }