echogarden 1.3.2 → 1.4.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 (57) hide show
  1. package/data/schemas/options.json +3 -1
  2. package/dist/alignment/DTWSequenceAlignmentWindowed.js +68 -27
  3. package/dist/alignment/DTWSequenceAlignmentWindowed.js.map +1 -1
  4. package/dist/alignment/SpeechAlignment.d.ts +1 -1
  5. package/dist/alignment/SpeechAlignment.js +6 -21
  6. package/dist/alignment/SpeechAlignment.js.map +1 -1
  7. package/dist/api/Alignment.js +47 -28
  8. package/dist/api/Alignment.js.map +1 -1
  9. package/dist/api/LanguageDetection.js +3 -3
  10. package/dist/api/LanguageDetection.js.map +1 -1
  11. package/dist/api/Recognition.js +1 -1
  12. package/dist/api/Recognition.js.map +1 -1
  13. package/dist/api/Synthesis.js +1 -1
  14. package/dist/api/Synthesis.js.map +1 -1
  15. package/dist/api/Translation.js +1 -1
  16. package/dist/api/Translation.js.map +1 -1
  17. package/dist/api/TranslationAlignment.js +1 -1
  18. package/dist/api/TranslationAlignment.js.map +1 -1
  19. package/dist/audio/AudioBufferConversion.d.ts +2 -2
  20. package/dist/audio/AudioBufferConversion.js +46 -36
  21. package/dist/audio/AudioBufferConversion.js.map +1 -1
  22. package/dist/cli/CLI.js +1 -0
  23. package/dist/cli/CLI.js.map +1 -1
  24. package/dist/codecs/FFMpegTranscoder.js +2 -1
  25. package/dist/codecs/FFMpegTranscoder.js.map +1 -1
  26. package/dist/codecs/WaveCodec.js +8 -3
  27. package/dist/codecs/WaveCodec.js.map +1 -1
  28. package/dist/recognition/WhisperSTT.d.ts +1 -0
  29. package/dist/recognition/WhisperSTT.js +13 -6
  30. package/dist/recognition/WhisperSTT.js.map +1 -1
  31. package/dist/synthesis/EspeakTTS.js +5 -0
  32. package/dist/synthesis/EspeakTTS.js.map +1 -1
  33. package/dist/utilities/Compression.d.ts +1 -0
  34. package/dist/utilities/Compression.js +11 -1
  35. package/dist/utilities/Compression.js.map +1 -1
  36. package/dist/utilities/LEB128.d.ts +5 -0
  37. package/dist/utilities/LEB128.js +168 -0
  38. package/dist/utilities/LEB128.js.map +1 -0
  39. package/docs/Options.md +2 -1
  40. package/docs/Tasklist.md +3 -3
  41. package/package.json +7 -7
  42. package/src/alignment/DTWSequenceAlignmentWindowed.ts +69 -29
  43. package/src/alignment/SpeechAlignment.ts +7 -23
  44. package/src/api/Alignment.ts +48 -29
  45. package/src/api/LanguageDetection.ts +3 -3
  46. package/src/api/Recognition.ts +1 -1
  47. package/src/api/Synthesis.ts +1 -1
  48. package/src/api/Translation.ts +1 -1
  49. package/src/api/TranslationAlignment.ts +1 -1
  50. package/src/audio/AudioBufferConversion.ts +46 -36
  51. package/src/cli/CLI.ts +1 -0
  52. package/src/codecs/FFMpegTranscoder.ts +3 -1
  53. package/src/codecs/WaveCodec.ts +11 -3
  54. package/src/recognition/WhisperSTT.ts +14 -7
  55. package/src/synthesis/EspeakTTS.ts +7 -1
  56. package/src/utilities/Compression.ts +16 -1
  57. package/src/utilities/LEB128.ts +237 -0
@@ -0,0 +1,237 @@
1
+ import { logToStderr } from "./Utilities.js"
2
+
3
+ ////////////////////////////////////////////////////////////////////////////////////
4
+ // Encode
5
+ ////////////////////////////////////////////////////////////////////////////////////
6
+ export function encodeSignedInt32(value: number, outEncodedData: number[]) {
7
+ if (value < -2147483648 || value > 2147483647) {
8
+ throw new Error('Value must be between -2147483648 and 2147483647')
9
+ }
10
+
11
+ while (true) {
12
+ const lowest7Bits = value & 127
13
+
14
+ value >>= 7
15
+
16
+ if (
17
+ (value === 0 && (lowest7Bits & 64) === 0) ||
18
+ (value === -1 && (lowest7Bits & 64) !== 0)) {
19
+ outEncodedData.push(lowest7Bits)
20
+
21
+ return outEncodedData
22
+ } else {
23
+ outEncodedData.push(lowest7Bits | 128)
24
+ }
25
+ }
26
+ }
27
+
28
+ export function encodeSignedInt32sFast(value: number, outEncodedData: number[]) {
29
+ const absValue = Math.abs(value)
30
+ //const absMask = value >> 31
31
+ //const absValue = (value ^ absMask) - absMask
32
+
33
+ if (absValue < (2 ** 6)) {
34
+ outEncodedData.push(
35
+ (value & 127)
36
+ )
37
+ } else if (absValue < (2 ** 13)) {
38
+ outEncodedData.push(
39
+ (value & 127) | 128,
40
+ (value >> 7) & 127
41
+ )
42
+ } else if (absValue < (2 ** 20)) {
43
+ outEncodedData.push(
44
+ (value & 127) | 128,
45
+ ((value >> 7) & 127) | 128,
46
+ (value >> 14) & 127
47
+ )
48
+ } else if (absValue < (2 ** 27)) {
49
+ outEncodedData.push(
50
+ (value & 127) | 128,
51
+ ((value >> 7) & 127) | 128,
52
+ ((value >> 14) & 127) | 128,
53
+ (value >> 21) & 127
54
+ )
55
+ } else if (value < (2 ** 31) && value >= -(2 ** 31)) {
56
+ outEncodedData.push(
57
+ (value & 127) | 128,
58
+ ((value >> 7) & 127) | 128,
59
+ ((value >> 14) & 127) | 128,
60
+ ((value >> 21) & 127) | 128,
61
+ (value >> 28) & 127
62
+ )
63
+ } else {
64
+ throw new Error(`Value must be between -2147483648 and 2147483647`)
65
+ }
66
+ }
67
+
68
+ ////////////////////////////////////////////////////////////////////////////////////
69
+ // Decode
70
+ ////////////////////////////////////////////////////////////////////////////////////
71
+ export function decodeSignedInt32s(encodedData: ArrayLike<number>, outDecodedValues: number[]) {
72
+ for (let readIndex = 0; readIndex < encodedData.length;) {
73
+ let currentDecodedValue = 0
74
+ let shiftAmount = 0
75
+
76
+ while (true) {
77
+ const encodedByte = encodedData[readIndex++]
78
+ const lowest7Bits = encodedByte & 127
79
+
80
+ currentDecodedValue |= lowest7Bits << shiftAmount
81
+
82
+ // If 8th bit is 0, then this is the last byte in the sequence
83
+ if ((encodedByte & 128) === 0) {
84
+ // If 7th bit is 1, then the value is negative
85
+ if ((encodedByte & 64) !== 0) {
86
+ // If the value should be negative
87
+ // Ensure that the value is encoded as a negative number by
88
+ // setting all higher bits to 1
89
+ currentDecodedValue |= -1 << Math.min(shiftAmount + 7, 31)
90
+ }
91
+
92
+ break
93
+ }
94
+
95
+ if (readIndex === encodedData.length) {
96
+ throw new Error(`Invalid LEB128 data. Last encoded byte sequence is truncated.`)
97
+ }
98
+
99
+ shiftAmount += 7
100
+
101
+ if (shiftAmount > 31) {
102
+ throw new Error(`LEB128 sequence can't be decoded. Byte sequence extends beyond the range of a signed 32 bit integer.`)
103
+ }
104
+ }
105
+
106
+ outDecodedValues.push(currentDecodedValue)
107
+ }
108
+
109
+ return outDecodedValues
110
+ }
111
+
112
+ export function decodeSignedInt32sFast(encodedData: ArrayLike<number>, outDecodedValues: number[]) {
113
+ for (let readIndex = 0; readIndex < encodedData.length;) {
114
+ const byte0 = encodedData[readIndex++]
115
+
116
+ if ((byte0 & 128) === 0) {
117
+ let decodedValue =
118
+ (byte0 & 127)
119
+
120
+ if ((byte0 & 64) !== 0) {
121
+ decodedValue |= -1 << 7
122
+ }
123
+
124
+ outDecodedValues.push(decodedValue)
125
+
126
+ continue
127
+ }
128
+
129
+ const byte1 = encodedData[readIndex++]
130
+
131
+ if ((byte1 & 128) === 0) {
132
+ let decodedValue =
133
+ (byte0 & 127) |
134
+ (byte1 & 127) << 7
135
+
136
+ if ((byte1 & 64) !== 0) {
137
+ decodedValue |= -1 << 14
138
+ }
139
+
140
+ outDecodedValues.push(decodedValue)
141
+
142
+ continue
143
+ }
144
+
145
+ const byte2 = encodedData[readIndex++]
146
+
147
+ if ((byte2 & 128) === 0) {
148
+ let decodedValue =
149
+ (byte0 & 127) |
150
+ (byte1 & 127) << 7 |
151
+ (byte2 & 127) << 14
152
+
153
+ if ((byte2 & 64) !== 0) {
154
+ decodedValue |= -1 << 21
155
+ }
156
+
157
+ outDecodedValues.push(decodedValue)
158
+
159
+ continue
160
+ }
161
+
162
+ const byte3 = encodedData[readIndex++]
163
+
164
+ if ((byte3 & 128) === 0) {
165
+ let decodedValue =
166
+ (byte0 & 127) |
167
+ (byte1 & 127) << 7 |
168
+ (byte2 & 127) << 14 |
169
+ (byte3 & 127) << 21
170
+
171
+ if ((byte3 & 64) !== 0) {
172
+ decodedValue |= -1 << 28
173
+ }
174
+
175
+ outDecodedValues.push(decodedValue)
176
+
177
+ continue
178
+ }
179
+
180
+ const byte4 = encodedData[readIndex++]
181
+
182
+ if ((byte4 & 128) === 0) {
183
+ let decodedValue =
184
+ (byte0 & 127) |
185
+ (byte1 & 127) << 7 |
186
+ (byte2 & 127) << 14 |
187
+ (byte3 & 127) << 21 |
188
+ (byte4 & 127) << 28
189
+
190
+ if ((byte4 & 64) !== 0) {
191
+ decodedValue |= -1 << 31
192
+ }
193
+
194
+ outDecodedValues.push(decodedValue)
195
+
196
+ continue
197
+ }
198
+
199
+ if (readIndex >= encodedData.length) {
200
+ throw new Error(`Invalid LEB128 data. Last encoded byte sequence is truncated.`)
201
+ } else {
202
+ throw new Error(`LEB128 sequence can't be decoded. Encoded byte sequence represents a value that extends beyond the range of a signed 32 bit integer.`)
203
+ }
204
+ }
205
+
206
+ return outDecodedValues
207
+ }
208
+
209
+ ////////////////////////////////////////////////////////////////////////////////////
210
+ // Tests
211
+ ////////////////////////////////////////////////////////////////////////////////////
212
+ export function testLeb128() {
213
+ const encodedBytes: number[] = []
214
+ const decodedValues: number[] = []
215
+
216
+ function runTest(testValue: number) {
217
+ encodedBytes.length = 0
218
+ decodedValues.length = 0
219
+
220
+ encodeSignedInt32sFast(testValue, encodedBytes)
221
+ decodeSignedInt32sFast(encodedBytes, decodedValues)
222
+
223
+ if (decodedValues[0] !== testValue) {
224
+ throw new Error(`Expected ${testValue} but got ${decodedValues[0]}`)
225
+ }
226
+ }
227
+
228
+ for (let i = -(2 ** 22); i < 2 ** 22; i++) {
229
+ if (i % 1000000 === 0) {
230
+ logToStderr(i)
231
+ }
232
+
233
+ runTest(i)
234
+ }
235
+
236
+ const x = 1
237
+ }