echogarden 2.6.0 → 2.7.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.
Files changed (51) hide show
  1. package/dist/api/SourceSeparation.js +1 -1
  2. package/dist/audio/AudioBufferConversion.d.ts +5 -6
  3. package/dist/audio/AudioBufferConversion.js +16 -248
  4. package/dist/audio/AudioBufferConversion.js.map +1 -1
  5. package/dist/audio/AudioPlayer.js.map +1 -1
  6. package/dist/audio/AudioRecorder.js +1 -1
  7. package/dist/audio/AudioRecorder.js.map +1 -1
  8. package/dist/audio/AudioUtilities.d.ts +3 -9
  9. package/dist/audio/AudioUtilities.js +4 -3
  10. package/dist/audio/AudioUtilities.js.map +1 -1
  11. package/dist/codecs/FFMpegTranscoder.d.ts +1 -4
  12. package/dist/codecs/TIMITCodec.js +1 -2
  13. package/dist/codecs/TIMITCodec.js.map +1 -1
  14. package/dist/encodings/Ascii.js +1 -1
  15. package/dist/encodings/Ascii.js.map +1 -1
  16. package/dist/encodings/Utf16.js +1 -1
  17. package/dist/encodings/Utf16.js.map +1 -1
  18. package/dist/encodings/Utf8.js +1 -1
  19. package/dist/encodings/Utf8.js.map +1 -1
  20. package/dist/synthesis/AwsPollyTTS.d.ts +1 -4
  21. package/dist/synthesis/CoquiServerTTS.d.ts +1 -4
  22. package/dist/synthesis/DeepgramTTS.d.ts +1 -4
  23. package/dist/synthesis/ElevenLabsTTS.d.ts +1 -4
  24. package/dist/synthesis/FliteTTS.d.ts +1 -4
  25. package/dist/synthesis/GnuSpeechTTS.d.ts +1 -7
  26. package/dist/synthesis/GoogleCloudTTS.d.ts +1 -4
  27. package/dist/synthesis/SapiTTS.js +1 -2
  28. package/dist/synthesis/SapiTTS.js.map +1 -1
  29. package/dist/synthesis/SvoxPicoTTS.js +1 -2
  30. package/dist/synthesis/SvoxPicoTTS.js.map +1 -1
  31. package/dist/utilities/WasmMemoryManager.d.ts +1 -1
  32. package/package.json +2 -2
  33. package/src/api/SourceSeparation.ts +1 -1
  34. package/src/audio/AudioBufferConversion.ts +17 -263
  35. package/src/audio/AudioPlayer.ts +1 -1
  36. package/src/audio/AudioRecorder.ts +1 -1
  37. package/src/audio/AudioUtilities.ts +5 -3
  38. package/src/codecs/TIMITCodec.ts +1 -2
  39. package/src/encodings/Ascii.ts +1 -1
  40. package/src/encodings/Utf16.ts +1 -1
  41. package/src/encodings/Utf8.ts +1 -1
  42. package/src/synthesis/SapiTTS.ts +1 -2
  43. package/src/synthesis/SvoxPicoTTS.ts +1 -2
  44. package/dist/codecs/WaveCodec.d.ts +0 -19
  45. package/dist/codecs/WaveCodec.js +0 -224
  46. package/dist/codecs/WaveCodec.js.map +0 -1
  47. package/dist/utilities/BinaryArrayConversion.d.ts +0 -14
  48. package/dist/utilities/BinaryArrayConversion.js +0 -92
  49. package/dist/utilities/BinaryArrayConversion.js.map +0 -1
  50. package/src/codecs/WaveCodec.ts +0 -281
  51. package/src/utilities/BinaryArrayConversion.ts +0 -117
@@ -1,281 +0,0 @@
1
- import * as AudioBufferConversion from '../audio/AudioBufferConversion.js'
2
- import { RawAudio } from '../audio/AudioUtilities.js'
3
- import { readUint16LE, readUint32LE, writeAscii, writeUint16LE, writeUint32LE } from '../utilities/BinaryUtilities.js'
4
- import { encodeHex, decodeHex } from '../encodings/Hex.js'
5
- import { concatUint8Arrays, logToStderr } from '../utilities/Utilities.js'
6
- import { decodeAscii } from '../encodings/Ascii.js'
7
-
8
- const log = logToStderr
9
-
10
- export function encodeWave(rawAudio: RawAudio, bitDepth: BitDepth = 16, sampleFormat: SampleFormat = SampleFormat.PCM, speakerPositionMask = 0) {
11
- const audioChannels = rawAudio.audioChannels
12
- const sampleRate = rawAudio.sampleRate
13
-
14
- const audioBuffer = AudioBufferConversion.encodeToAudioBuffer(audioChannels, bitDepth, sampleFormat)
15
- const audioDataLength = audioBuffer.length
16
-
17
- const shouldUseExtensibleFormat = bitDepth > 16 || audioChannels.length > 2
18
-
19
- const formatSubChunk = new WaveFormat(audioChannels.length, sampleRate, bitDepth, sampleFormat, speakerPositionMask)
20
- const formatSubChunkBuffer = formatSubChunk.serialize(shouldUseExtensibleFormat)
21
-
22
- const dataSubChunkBuffer = new Uint8Array(4 + 4 + audioDataLength)
23
- writeAscii(dataSubChunkBuffer, 'data', 0)
24
- const dataChunkLength = Math.min(audioDataLength, 4294967295) // Ensure large data chunk length is clipped to max
25
- writeUint32LE(dataSubChunkBuffer, dataChunkLength, 4)
26
- dataSubChunkBuffer.set(audioBuffer, 8)
27
-
28
- const riffChunkHeaderBuffer = new Uint8Array(12)
29
- writeAscii(riffChunkHeaderBuffer, 'RIFF', 0)
30
- const riffChunkLength = Math.min(4 + formatSubChunkBuffer.length + dataSubChunkBuffer.length, 4294967295) // Ensure large RIFF chunk length is clipped to max
31
- writeUint32LE(riffChunkHeaderBuffer, riffChunkLength, 4)
32
- writeAscii(riffChunkHeaderBuffer, 'WAVE', 8)
33
-
34
- return concatUint8Arrays([riffChunkHeaderBuffer, formatSubChunkBuffer, dataSubChunkBuffer])
35
- }
36
-
37
- export function decodeWave(waveData: Uint8Array, ignoreTruncatedChunks = true, ignoreOverflowingDataChunks = true) {
38
- let readOffset = 0
39
-
40
- const riffId = decodeAscii(waveData.subarray(readOffset, readOffset + 4))
41
-
42
- if (riffId != 'RIFF') {
43
- throw new Error('Not a valid wave file. No RIFF id found at offset 0.')
44
- }
45
-
46
- readOffset += 4
47
-
48
- let riffChunkSize = readUint32LE(waveData, readOffset)
49
-
50
- readOffset += 4
51
-
52
- const waveId = decodeAscii(waveData.subarray(readOffset, readOffset + 4))
53
-
54
- if (waveId != 'WAVE') {
55
- throw new Error('Not a valid wave file. No WAVE id found at offset 8.')
56
- }
57
-
58
- if (ignoreOverflowingDataChunks && riffChunkSize === 4294967295) {
59
- riffChunkSize = waveData.length - 8
60
- }
61
-
62
- if (riffChunkSize < waveData.length - 8) {
63
- throw new Error(`RIFF chunk length ${riffChunkSize} is smaller than the remaining size of the buffer (${waveData.length - 8})`)
64
- }
65
-
66
- if (!ignoreTruncatedChunks && riffChunkSize > waveData.length - 8) {
67
- throw new Error(`RIFF chunk length (${riffChunkSize}) is greater than the remaining size of the buffer (${waveData.length - 8})`)
68
- }
69
-
70
- readOffset += 4
71
-
72
- let formatSubChunkBodyBuffer: Uint8Array | undefined
73
- const dataBuffers: Uint8Array[] = []
74
-
75
- while (true) {
76
- const subChunkIdentifier = decodeAscii(waveData.subarray(readOffset, readOffset + 4))
77
- readOffset += 4
78
-
79
- let subChunkSize = readUint32LE(waveData, readOffset)
80
- readOffset += 4
81
-
82
- if (!ignoreTruncatedChunks && subChunkSize > waveData.length - readOffset) {
83
- throw new Error(`Encountered a '${subChunkIdentifier}' subchunk with a size of ${subChunkSize} which is greater than the remaining size of the buffer (${waveData.length - readOffset})`)
84
- }
85
-
86
- if (subChunkIdentifier == 'fmt ') {
87
- formatSubChunkBodyBuffer = waveData.subarray(readOffset, readOffset + subChunkSize)
88
- } else if (subChunkIdentifier == 'data') {
89
- if (!formatSubChunkBodyBuffer) {
90
- throw new Error('A data subchunk was encountered before a format subchunk')
91
- }
92
-
93
- // If the data chunk is truncated or extended beyond 4 GiB,
94
- // the data would be read up to the end of the buffer
95
- if (ignoreOverflowingDataChunks && subChunkSize === 4294967295) {
96
- subChunkSize = waveData.length - readOffset
97
- }
98
-
99
- const subChunkData = waveData.subarray(readOffset, readOffset + subChunkSize)
100
-
101
- dataBuffers.push(subChunkData)
102
- }
103
- // All sub chunks other than 'data' (e.g. 'LIST', 'fact', 'plst', 'junk' etc.) are ignored
104
-
105
- // This addition operation may overflow if JavaScript integers were 32 bits,
106
- // but since they are 52 bits, it is okay:
107
- readOffset += subChunkSize
108
-
109
- // Break if readOffset is equal to or is greater than the size of the buffer
110
- if (readOffset >= waveData.length) {
111
- break
112
- }
113
- }
114
-
115
- if (!formatSubChunkBodyBuffer) {
116
- throw new Error('No format subchunk was found in the wave file')
117
- }
118
-
119
- if (dataBuffers.length === 0) {
120
- throw new Error('No data subchunks were found in the wave file')
121
- }
122
-
123
- const waveFormat = WaveFormat.deserializeFrom(formatSubChunkBodyBuffer)
124
-
125
- const sampleFormat = waveFormat.sampleFormat
126
- const channelCount = waveFormat.channelCount
127
- const sampleRate = waveFormat.sampleRate
128
- const bitDepth = waveFormat.bitDepth
129
- const speakerPositionMask = waveFormat.speakerPositionMask
130
-
131
- const concatenatedDataBuffers = concatUint8Arrays(dataBuffers)
132
- dataBuffers.length = 0 // Allow the garbage collector to free up memory held by the data buffers
133
-
134
- const audioChannels = AudioBufferConversion.decodeToChannels(concatenatedDataBuffers, channelCount, bitDepth, sampleFormat)
135
-
136
- return {
137
- rawAudio: { audioChannels, sampleRate },
138
-
139
- sourceSampleFormat: sampleFormat,
140
- sourceBitDepth: bitDepth,
141
- sourceSpeakerPositionMask: speakerPositionMask
142
- }
143
- }
144
-
145
- export function repairWave(waveData: Uint8Array) {
146
- const { rawAudio, sourceSampleFormat, sourceBitDepth } = decodeWave(waveData)
147
-
148
- return encodeWave(rawAudio, sourceBitDepth, sourceSampleFormat)
149
- }
150
-
151
- class WaveFormat { // 24 bytes total for PCM, 26 for float
152
- sampleFormat: SampleFormat // 2 bytes LE
153
- channelCount: number // 2 bytes LE
154
- sampleRate: number // 4 bytes LE
155
- get byteRate() { return this.sampleRate * this.bytesPerSample * this.channelCount } // 4 bytes LE
156
- get blockAlign() { return this.bytesPerSample * this.channelCount } // 2 bytes LE
157
- bitDepth: BitDepth // 2 bytes LE
158
-
159
- speakerPositionMask: number // 4 bytes LE
160
- get guid() { return sampleFormatToGuid[this.sampleFormat] } // 16 bytes BE
161
-
162
- // helpers:
163
- get bytesPerSample() { return this.bitDepth / 8 }
164
-
165
- constructor(channelCount: number, sampleRate: number, bitDepth: BitDepth, sampleFormat: SampleFormat, speakerPositionMask = 0) {
166
- this.sampleFormat = sampleFormat
167
- this.channelCount = channelCount
168
- this.sampleRate = sampleRate
169
- this.bitDepth = bitDepth
170
-
171
- this.speakerPositionMask = speakerPositionMask
172
- }
173
-
174
- serialize(useExtensibleFormat: boolean) {
175
- let sampleFormatId = this.sampleFormat
176
-
177
- if (useExtensibleFormat) {
178
- sampleFormatId = 65534 as number
179
- }
180
-
181
- const serializedSize = sampleFormatToSerializedSize[sampleFormatId]
182
-
183
- const result = new Uint8Array(serializedSize)
184
-
185
- writeAscii(result, 'fmt ', 0) // + 4
186
- writeUint32LE(result, serializedSize - 8, 4) // + 4
187
-
188
- writeUint16LE(result, sampleFormatId, 8) // + 2
189
- writeUint16LE(result, this.channelCount, 10) // + 2
190
- writeUint32LE(result, this.sampleRate, 12) // + 4
191
- writeUint32LE(result, this.byteRate, 16) // + 4
192
- writeUint16LE(result, this.blockAlign, 20) // + 2
193
- writeUint16LE(result, this.bitDepth, 22) // + 2
194
-
195
- if (useExtensibleFormat) {
196
- writeUint16LE(result, serializedSize - 26, 24) // + 2 (extension size)
197
- writeUint16LE(result, this.bitDepth, 26) // + 2 (valid bits per sample)
198
- writeUint32LE(result, this.speakerPositionMask, 28) // + 2 (speaker position mask)
199
-
200
- if (this.sampleFormat == SampleFormat.PCM || this.sampleFormat == SampleFormat.Float) {
201
- result.set(decodeHex(this.guid), 32)
202
- } else {
203
- throw new Error(`Extensible format is not supported for sample format ${this.sampleFormat}`)
204
- }
205
- }
206
-
207
- return result
208
- }
209
-
210
- static deserializeFrom(formatChunkBody: Uint8Array) { // chunkBody should not include the first 8 bytes
211
- let sampleFormat = readUint16LE(formatChunkBody, 0) // + 2
212
- const channelCount = readUint16LE(formatChunkBody, 2) // + 2
213
- const sampleRate = readUint32LE(formatChunkBody, 4) // + 4
214
- const bitDepth = readUint16LE(formatChunkBody, 14)
215
- let speakerPositionMask = 0
216
-
217
- if (sampleFormat == 65534) {
218
- if (formatChunkBody.length < 40) {
219
- throw new Error(`Format subchunk specifies a format id of 65534 (extensible) but its body size is ${formatChunkBody.length} bytes, which is smaller than the minimum expected of 40 bytes`)
220
- }
221
-
222
- speakerPositionMask = readUint16LE(formatChunkBody, 20)
223
-
224
- const guid = encodeHex(formatChunkBody.subarray(24, 40))
225
-
226
- if (guid == sampleFormatToGuid[SampleFormat.PCM]) {
227
- sampleFormat = SampleFormat.PCM
228
- } else if (guid == sampleFormatToGuid[SampleFormat.Float]) {
229
- sampleFormat = SampleFormat.Float
230
- } else {
231
- throw new Error(`Unsupported format GUID in extended format subchunk: ${guid}`)
232
- }
233
- }
234
-
235
- if (sampleFormat == SampleFormat.PCM) {
236
- if (bitDepth != 8 && bitDepth != 16 && bitDepth != 24 && bitDepth != 32) {
237
- throw new Error(`PCM audio has a bit depth of ${bitDepth}, which is not supported`)
238
- }
239
- } else if (sampleFormat == SampleFormat.Float) {
240
- if (bitDepth != 32 && bitDepth != 64) {
241
- throw new Error(`IEEE float audio has a bit depth of ${bitDepth}, which is not supported`)
242
- }
243
- } else if (sampleFormat == SampleFormat.Alaw) {
244
- if (bitDepth != 8) {
245
- throw new Error(`Alaw audio has a bit depth of ${bitDepth}, which is not supported`)
246
- }
247
- } else if (sampleFormat == SampleFormat.Mulaw) {
248
- if (bitDepth != 8) {
249
- throw new Error(`Mulaw audio has a bit depth of ${bitDepth}, which is not supported`)
250
- }
251
- } else {
252
- throw new Error(`Wave audio format id ${sampleFormat} is not supported`)
253
- }
254
-
255
- return new WaveFormat(channelCount, sampleRate, bitDepth, sampleFormat, speakerPositionMask)
256
- }
257
- }
258
-
259
- export enum SampleFormat {
260
- PCM = 1,
261
- Float = 3,
262
- Alaw = 6,
263
- Mulaw = 7,
264
- }
265
-
266
- export type BitDepth = 8 | 16 | 24 | 32 | 64
267
-
268
- const sampleFormatToSerializedSize = {
269
- [SampleFormat.PCM]: 24,
270
- [SampleFormat.Float]: 26,
271
- [SampleFormat.Alaw]: 26,
272
- [SampleFormat.Mulaw]: 26,
273
- 65534: 48
274
- }
275
-
276
- const sampleFormatToGuid = {
277
- [SampleFormat.PCM]: '0100000000001000800000aa00389b71',
278
- [SampleFormat.Float]: '0300000000001000800000aa00389b71',
279
- [SampleFormat.Alaw]: '',
280
- [SampleFormat.Mulaw]: '',
281
- }
@@ -1,117 +0,0 @@
1
- // Typed arrays to Uint8Array buffers (little endian) conversions
2
- //
3
- // The conversion methods (other than the methods for int8 and int24) would only work correctly
4
- // on little-endian architectures, since they assume the byte order of the underlying architecture.
5
- //
6
- // Since Echogarden only supports little-endian architectures, this shouldn't matter.
7
-
8
- // int8 <-> bufferLE
9
- export function int8ToBuffer(int8s: Int8Array) {
10
- return new Uint8Array(int8s.buffer, int8s.byteOffset, int8s.byteLength)
11
- }
12
-
13
- export function bufferToInt8(buffer: Uint8Array) {
14
- return new Int8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength)
15
- }
16
-
17
- // int16 <-> bufferLE
18
- export function int16ToBufferLE(int16s: Int16Array) {
19
- return new Uint8Array(int16s.buffer, int16s.byteOffset, int16s.byteLength)
20
- }
21
-
22
- export function bufferLEToInt16(buffer: Uint8Array) {
23
- return new Int16Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / 2)
24
- }
25
-
26
- // int24 <-> bufferLE (uses int32 for storage)
27
- export function int24ToBufferLE(int24s: Int32Array) {
28
- const buffer = new Uint8Array(int24s.length * 3)
29
-
30
- let readOffset = 0
31
- let writeOffset = 0
32
-
33
- while (readOffset < int24s.length) {
34
- const signedValue = int24s[readOffset++]
35
-
36
- let unsignedValue: number
37
-
38
- if (signedValue >= 0) {
39
- unsignedValue = signedValue
40
- } else {
41
- unsignedValue = signedValue + (2 ** 24)
42
- }
43
-
44
- buffer[writeOffset++] = (unsignedValue) & 0xff
45
- buffer[writeOffset++] = (unsignedValue >> 8) & 0xff
46
- buffer[writeOffset++] = (unsignedValue >> 16) & 0xff
47
- }
48
-
49
- return buffer
50
- }
51
-
52
- export function bufferLEToInt24(buffer: Uint8Array) {
53
- if (buffer.length % 3 !== 0) {
54
- throw new Error(`Buffer has a length of ${buffer.length}, which is not a multiple of 3`)
55
- }
56
-
57
- const result = new Int32Array(buffer.length / 3)
58
-
59
- let readOffset = 0
60
- let writeOffset = 0
61
-
62
- while (writeOffset < result.length) {
63
- const b0 = buffer[readOffset++]
64
- const b1 = buffer[readOffset++]
65
- const b2 = buffer[readOffset++]
66
-
67
- const unsignedValue = (b0) | (b1 << 8) | (b2 << 16)
68
-
69
- let signedValue: number
70
-
71
- if (unsignedValue < 2 ** 23) {
72
- signedValue = unsignedValue
73
- } else {
74
- signedValue = unsignedValue - (2 ** 24)
75
- }
76
-
77
- result[writeOffset++] = signedValue
78
- }
79
-
80
- return result
81
- }
82
-
83
- // int32 <-> bufferLE
84
- export function int32ToBufferLE(int32s: Int32Array) {
85
- return new Uint8Array(int32s.buffer, int32s.byteOffset, int32s.byteLength)
86
- }
87
-
88
- export function bufferLEToInt32(buffer: Uint8Array) {
89
- return new Int32Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / 4)
90
- }
91
-
92
- // float32 <-> bufferLE
93
- export function float32ToBufferLE(float32s: Float32Array) {
94
- return new Uint8Array(float32s.buffer, float32s.byteOffset, float32s.byteLength)
95
- }
96
-
97
- export function bufferLEToFloat32(buffer: Uint8Array) {
98
- return new Float32Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / 4)
99
- }
100
-
101
- // float64 <-> bufferLE
102
- export function float64ToBufferLE(float64s: Float64Array) {
103
- return new Uint8Array(float64s.buffer, float64s.byteOffset, float64s.byteLength)
104
- }
105
-
106
- export function bufferLEToFloat64(buffer: Uint8Array) {
107
- return new Float64Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / 8)
108
- }
109
-
110
- // float64 <-> float32
111
- export function float64Tofloat32(float64s: Float64Array) {
112
- return Float32Array.from(float64s)
113
- }
114
-
115
- export function float32Tofloat64(float32s: Float32Array) {
116
- return Float64Array.from(float32s)
117
- }