echogarden 0.0.1 → 0.1.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 (295) hide show
  1. package/README.md +59 -1
  2. package/data/lexicons/heteronyms.json +674 -0
  3. package/data/schemas/options.json +1057 -0
  4. package/data/tables/lcid-table.json +7427 -0
  5. package/dist/alignment/DTWMfccSequenceAlignment.d.ts +2 -0
  6. package/dist/alignment/DTWMfccSequenceAlignment.js +26 -0
  7. package/dist/alignment/DTWMfccSequenceAlignment.js.map +1 -0
  8. package/dist/alignment/DTWSequenceAlignment.d.ts +5 -0
  9. package/dist/alignment/DTWSequenceAlignment.js +99 -0
  10. package/dist/alignment/DTWSequenceAlignment.js.map +1 -0
  11. package/dist/alignment/DTWSequenceAlignmentWindowed.d.ts +5 -0
  12. package/dist/alignment/DTWSequenceAlignmentWindowed.js +161 -0
  13. package/dist/alignment/DTWSequenceAlignmentWindowed.js.map +1 -0
  14. package/dist/alignment/LevenshteinSequenceAlignment.d.ts +5 -0
  15. package/dist/alignment/LevenshteinSequenceAlignment.js +102 -0
  16. package/dist/alignment/LevenshteinSequenceAlignment.js.map +1 -0
  17. package/dist/alignment/SpeechAlignment.d.ts +27 -0
  18. package/dist/alignment/SpeechAlignment.js +266 -0
  19. package/dist/alignment/SpeechAlignment.js.map +1 -0
  20. package/dist/api/API.d.ts +8 -0
  21. package/dist/api/API.js +10 -0
  22. package/dist/api/API.js.map +1 -0
  23. package/dist/api/APIOptions.d.ts +12 -0
  24. package/dist/api/APIOptions.js +2 -0
  25. package/dist/api/APIOptions.js.map +1 -0
  26. package/dist/api/Alignment.d.ts +32 -0
  27. package/dist/api/Alignment.js +134 -0
  28. package/dist/api/Alignment.js.map +1 -0
  29. package/dist/api/Denoising.d.ts +14 -0
  30. package/dist/api/Denoising.js +66 -0
  31. package/dist/api/Denoising.js.map +1 -0
  32. package/dist/api/Globals.d.ts +1 -0
  33. package/dist/api/Globals.js +2 -0
  34. package/dist/api/Globals.js.map +1 -0
  35. package/dist/api/LanguageDetection.d.ts +49 -0
  36. package/dist/api/LanguageDetection.js +118 -0
  37. package/dist/api/LanguageDetection.js.map +1 -0
  38. package/dist/api/Recognition.d.ts +49 -0
  39. package/dist/api/Recognition.js +162 -0
  40. package/dist/api/Recognition.js.map +1 -0
  41. package/dist/api/Synthesis.d.ts +125 -0
  42. package/dist/api/Synthesis.js +892 -0
  43. package/dist/api/Synthesis.js.map +1 -0
  44. package/dist/api/Translation.d.ts +25 -0
  45. package/dist/api/Translation.js +69 -0
  46. package/dist/api/Translation.js.map +1 -0
  47. package/dist/api/Vad.d.ts +25 -0
  48. package/dist/api/Vad.js +88 -0
  49. package/dist/api/Vad.js.map +1 -0
  50. package/dist/audio/AudioBufferConversion.d.ts +15 -0
  51. package/dist/audio/AudioBufferConversion.js +228 -0
  52. package/dist/audio/AudioBufferConversion.js.map +1 -0
  53. package/dist/audio/AudioPlayer.d.ts +9 -0
  54. package/dist/audio/AudioPlayer.js +232 -0
  55. package/dist/audio/AudioPlayer.js.map +1 -0
  56. package/dist/audio/AudioRecorder.d.ts +3 -0
  57. package/dist/audio/AudioRecorder.js +68 -0
  58. package/dist/audio/AudioRecorder.js.map +1 -0
  59. package/dist/audio/AudioUtilities.d.ts +48 -0
  60. package/dist/audio/AudioUtilities.js +209 -0
  61. package/dist/audio/AudioUtilities.js.map +1 -0
  62. package/dist/audio/SoxPath.d.ts +1 -0
  63. package/dist/audio/SoxPath.js +19 -0
  64. package/dist/audio/SoxPath.js.map +1 -0
  65. package/dist/cli/CLI.d.ts +8 -0
  66. package/dist/cli/CLI.js +860 -0
  67. package/dist/cli/CLI.js.map +1 -0
  68. package/dist/cli/CLIConfigFile.d.ts +3 -0
  69. package/dist/cli/CLIConfigFile.js +62 -0
  70. package/dist/cli/CLIConfigFile.js.map +1 -0
  71. package/dist/cli/CLILauncher.d.ts +2 -0
  72. package/dist/cli/CLILauncher.js +22 -0
  73. package/dist/cli/CLILauncher.js.map +1 -0
  74. package/dist/cli/CLIOptionsSchema.d.ts +5 -0
  75. package/dist/cli/CLIOptionsSchema.js +36 -0
  76. package/dist/cli/CLIOptionsSchema.js.map +1 -0
  77. package/dist/cli/CLIParser.d.ts +6 -0
  78. package/dist/cli/CLIParser.js +34 -0
  79. package/dist/cli/CLIParser.js.map +1 -0
  80. package/dist/cli/CLIStarter.d.ts +1 -0
  81. package/dist/cli/CLIStarter.js +3 -0
  82. package/dist/cli/CLIStarter.js.map +1 -0
  83. package/dist/codecs/FFMpegTranscoder.d.ts +20 -0
  84. package/dist/codecs/FFMpegTranscoder.js +169 -0
  85. package/dist/codecs/FFMpegTranscoder.js.map +1 -0
  86. package/dist/codecs/TIMITCodec.d.ts +9 -0
  87. package/dist/codecs/TIMITCodec.js +14 -0
  88. package/dist/codecs/TIMITCodec.js.map +1 -0
  89. package/dist/codecs/WaveCodec.d.ts +19 -0
  90. package/dist/codecs/WaveCodec.js +208 -0
  91. package/dist/codecs/WaveCodec.js.map +1 -0
  92. package/dist/denoising/RNNoise.d.ts +6 -0
  93. package/dist/denoising/RNNoise.js +68 -0
  94. package/dist/denoising/RNNoise.js.map +1 -0
  95. package/dist/dsp/BiquadFilter.d.ts +26 -0
  96. package/dist/dsp/BiquadFilter.js +399 -0
  97. package/dist/dsp/BiquadFilter.js.map +1 -0
  98. package/dist/dsp/FFT.d.ts +9 -0
  99. package/dist/dsp/FFT.js +135 -0
  100. package/dist/dsp/FFT.js.map +1 -0
  101. package/dist/dsp/MFCC.d.ts +25 -0
  102. package/dist/dsp/MFCC.js +162 -0
  103. package/dist/dsp/MFCC.js.map +1 -0
  104. package/dist/dsp/MelSpectogram.d.ts +19 -0
  105. package/dist/dsp/MelSpectogram.js +102 -0
  106. package/dist/dsp/MelSpectogram.js.map +1 -0
  107. package/dist/dsp/Rubberband.d.ts +52 -0
  108. package/dist/dsp/Rubberband.js +186 -0
  109. package/dist/dsp/Rubberband.js.map +1 -0
  110. package/dist/dsp/Sonic.d.ts +2 -0
  111. package/dist/dsp/Sonic.js +39 -0
  112. package/dist/dsp/Sonic.js.map +1 -0
  113. package/dist/dsp/SpeexResampler.d.ts +3 -0
  114. package/dist/dsp/SpeexResampler.js +53 -0
  115. package/dist/dsp/SpeexResampler.js.map +1 -0
  116. package/dist/math/VectorMath.d.ts +70 -0
  117. package/dist/math/VectorMath.js +564 -0
  118. package/dist/math/VectorMath.js.map +1 -0
  119. package/dist/nlp/ChineseSegmentation.d.ts +1 -0
  120. package/dist/nlp/ChineseSegmentation.js +53 -0
  121. package/dist/nlp/ChineseSegmentation.js.map +1 -0
  122. package/dist/nlp/CompromiseNLP.d.ts +15 -0
  123. package/dist/nlp/CompromiseNLP.js +66 -0
  124. package/dist/nlp/CompromiseNLP.js.map +1 -0
  125. package/dist/nlp/EspeakPhonemizer.d.ts +4 -0
  126. package/dist/nlp/EspeakPhonemizer.js +133 -0
  127. package/dist/nlp/EspeakPhonemizer.js.map +1 -0
  128. package/dist/nlp/IPA.d.ts +19 -0
  129. package/dist/nlp/IPA.js +113 -0
  130. package/dist/nlp/IPA.js.map +1 -0
  131. package/dist/nlp/JapaneseSegmentation.d.ts +1 -0
  132. package/dist/nlp/JapaneseSegmentation.js +40 -0
  133. package/dist/nlp/JapaneseSegmentation.js.map +1 -0
  134. package/dist/nlp/Lexicon.d.ts +17 -0
  135. package/dist/nlp/Lexicon.js +6 -0
  136. package/dist/nlp/Lexicon.js.map +1 -0
  137. package/dist/nlp/PhoneConversion.d.ts +6 -0
  138. package/dist/nlp/PhoneConversion.js +467 -0
  139. package/dist/nlp/PhoneConversion.js.map +1 -0
  140. package/dist/nlp/Segmentation.d.ts +41 -0
  141. package/dist/nlp/Segmentation.js +158 -0
  142. package/dist/nlp/Segmentation.js.map +1 -0
  143. package/dist/nlp/TextNormalizer.d.ts +4 -0
  144. package/dist/nlp/TextNormalizer.js +69 -0
  145. package/dist/nlp/TextNormalizer.js.map +1 -0
  146. package/dist/recognition/AmazonTranscribeSTT.d.ts +6 -0
  147. package/dist/recognition/AmazonTranscribeSTT.js +79 -0
  148. package/dist/recognition/AmazonTranscribeSTT.js.map +1 -0
  149. package/dist/recognition/AzureCognitiveServicesSTT.d.ts +7 -0
  150. package/dist/recognition/AzureCognitiveServicesSTT.js +51 -0
  151. package/dist/recognition/AzureCognitiveServicesSTT.js.map +1 -0
  152. package/dist/recognition/GoogleCloudSTT.d.ts +7 -0
  153. package/dist/recognition/GoogleCloudSTT.js +66 -0
  154. package/dist/recognition/GoogleCloudSTT.js.map +1 -0
  155. package/dist/recognition/SileroSTT.d.ts +9 -0
  156. package/dist/recognition/SileroSTT.js +125 -0
  157. package/dist/recognition/SileroSTT.js.map +1 -0
  158. package/dist/recognition/VoskSTT.d.ts +10 -0
  159. package/dist/recognition/VoskSTT.js +78 -0
  160. package/dist/recognition/VoskSTT.js.map +1 -0
  161. package/dist/recognition/WhisperSTT.d.ts +69 -0
  162. package/dist/recognition/WhisperSTT.js +977 -0
  163. package/dist/recognition/WhisperSTT.js.map +1 -0
  164. package/dist/server/Server.d.ts +1 -0
  165. package/dist/server/Server.js +18 -0
  166. package/dist/server/Server.js.map +1 -0
  167. package/dist/speech-language-detection/SileroLanguageDetection.d.ts +9 -0
  168. package/dist/speech-language-detection/SileroLanguageDetection.js +47 -0
  169. package/dist/speech-language-detection/SileroLanguageDetection.js.map +1 -0
  170. package/dist/subtitles/Subtitles.d.ts +25 -0
  171. package/dist/subtitles/Subtitles.js +286 -0
  172. package/dist/subtitles/Subtitles.js.map +1 -0
  173. package/dist/synthesis/AwsPollyTTS.d.ts +7 -0
  174. package/dist/synthesis/AwsPollyTTS.js +51 -0
  175. package/dist/synthesis/AwsPollyTTS.js.map +1 -0
  176. package/dist/synthesis/AzureCognitiveServicesTTS.d.ts +9 -0
  177. package/dist/synthesis/AzureCognitiveServicesTTS.js +103 -0
  178. package/dist/synthesis/AzureCognitiveServicesTTS.js.map +1 -0
  179. package/dist/synthesis/CoquiServerTTS.d.ts +6 -0
  180. package/dist/synthesis/CoquiServerTTS.js +22 -0
  181. package/dist/synthesis/CoquiServerTTS.js.map +1 -0
  182. package/dist/synthesis/ElevenLabsTTS.d.ts +8 -0
  183. package/dist/synthesis/ElevenLabsTTS.js +48 -0
  184. package/dist/synthesis/ElevenLabsTTS.js.map +1 -0
  185. package/dist/synthesis/EspeakTTS.d.ts +46 -0
  186. package/dist/synthesis/EspeakTTS.js +353 -0
  187. package/dist/synthesis/EspeakTTS.js.map +1 -0
  188. package/dist/synthesis/FliteTTS.d.ts +17 -0
  189. package/dist/synthesis/FliteTTS.js +326 -0
  190. package/dist/synthesis/FliteTTS.js.map +1 -0
  191. package/dist/synthesis/GoogleCloudTTS.d.ts +16 -0
  192. package/dist/synthesis/GoogleCloudTTS.js +72 -0
  193. package/dist/synthesis/GoogleCloudTTS.js.map +1 -0
  194. package/dist/synthesis/GoogleTranslateTTS.d.ts +13 -0
  195. package/dist/synthesis/GoogleTranslateTTS.js +177 -0
  196. package/dist/synthesis/GoogleTranslateTTS.js.map +1 -0
  197. package/dist/synthesis/MicrosoftEdgeTTS.d.ts +10 -0
  198. package/dist/synthesis/MicrosoftEdgeTTS.js +216 -0
  199. package/dist/synthesis/MicrosoftEdgeTTS.js.map +1 -0
  200. package/dist/synthesis/SamTTS.d.ts +4 -0
  201. package/dist/synthesis/SamTTS.js +21 -0
  202. package/dist/synthesis/SamTTS.js.map +1 -0
  203. package/dist/synthesis/SapiTTS.d.ts +9 -0
  204. package/dist/synthesis/SapiTTS.js +211 -0
  205. package/dist/synthesis/SapiTTS.js.map +1 -0
  206. package/dist/synthesis/StreamlabsPollyTTS.d.ts +12 -0
  207. package/dist/synthesis/StreamlabsPollyTTS.js +88 -0
  208. package/dist/synthesis/StreamlabsPollyTTS.js.map +1 -0
  209. package/dist/synthesis/SvoxPicoTTS.d.ts +11 -0
  210. package/dist/synthesis/SvoxPicoTTS.js +236 -0
  211. package/dist/synthesis/SvoxPicoTTS.js.map +1 -0
  212. package/dist/synthesis/VitsTTS.d.ts +27 -0
  213. package/dist/synthesis/VitsTTS.js +359 -0
  214. package/dist/synthesis/VitsTTS.js.map +1 -0
  215. package/dist/tests/Test.d.ts +1 -0
  216. package/dist/tests/Test.js +10 -0
  217. package/dist/tests/Test.js.map +1 -0
  218. package/dist/text-language-detection/FastTextLanguageDetection.d.ts +2 -0
  219. package/dist/text-language-detection/FastTextLanguageDetection.js +38 -0
  220. package/dist/text-language-detection/FastTextLanguageDetection.js.map +1 -0
  221. package/dist/text-language-detection/TinyLDLanguageDetection.d.ts +2 -0
  222. package/dist/text-language-detection/TinyLDLanguageDetection.js +12 -0
  223. package/dist/text-language-detection/TinyLDLanguageDetection.js.map +1 -0
  224. package/dist/utilities/BinaryArrayConversion.d.ts +15 -0
  225. package/dist/utilities/BinaryArrayConversion.js +115 -0
  226. package/dist/utilities/BinaryArrayConversion.js.map +1 -0
  227. package/dist/utilities/Compression.d.ts +4 -0
  228. package/dist/utilities/Compression.js +67 -0
  229. package/dist/utilities/Compression.js.map +1 -0
  230. package/dist/utilities/FileDownloader.d.ts +3 -0
  231. package/dist/utilities/FileDownloader.js +147 -0
  232. package/dist/utilities/FileDownloader.js.map +1 -0
  233. package/dist/utilities/FileSystem.d.ts +30 -0
  234. package/dist/utilities/FileSystem.js +141 -0
  235. package/dist/utilities/FileSystem.js.map +1 -0
  236. package/dist/utilities/Hashing.d.ts +10 -0
  237. package/dist/utilities/Hashing.js +169 -0
  238. package/dist/utilities/Hashing.js.map +1 -0
  239. package/dist/utilities/Locale.d.ts +9 -0
  240. package/dist/utilities/Locale.js +65 -0
  241. package/dist/utilities/Locale.js.map +1 -0
  242. package/dist/utilities/Logger.d.ts +11 -0
  243. package/dist/utilities/Logger.js +49 -0
  244. package/dist/utilities/Logger.js.map +1 -0
  245. package/dist/utilities/NdArrayUtilities.d.ts +3 -0
  246. package/dist/utilities/NdArrayUtilities.js +22 -0
  247. package/dist/utilities/NdArrayUtilities.js.map +1 -0
  248. package/dist/utilities/ObjectUtilities.d.ts +4 -0
  249. package/dist/utilities/ObjectUtilities.js +132 -0
  250. package/dist/utilities/ObjectUtilities.js.map +1 -0
  251. package/dist/utilities/OpenPromise.d.ts +6 -0
  252. package/dist/utilities/OpenPromise.js +12 -0
  253. package/dist/utilities/OpenPromise.js.map +1 -0
  254. package/dist/utilities/PackageManager.d.ts +4 -0
  255. package/dist/utilities/PackageManager.js +46 -0
  256. package/dist/utilities/PackageManager.js.map +1 -0
  257. package/dist/utilities/RandomGenerator.d.ts +35 -0
  258. package/dist/utilities/RandomGenerator.js +149 -0
  259. package/dist/utilities/RandomGenerator.js.map +1 -0
  260. package/dist/utilities/TarballMaker.d.ts +4 -0
  261. package/dist/utilities/TarballMaker.js +50 -0
  262. package/dist/utilities/TarballMaker.js.map +1 -0
  263. package/dist/utilities/Timeline.d.ts +20 -0
  264. package/dist/utilities/Timeline.js +110 -0
  265. package/dist/utilities/Timeline.js.map +1 -0
  266. package/dist/utilities/Timer.d.ts +13 -0
  267. package/dist/utilities/Timer.js +70 -0
  268. package/dist/utilities/Timer.js.map +1 -0
  269. package/dist/utilities/Utilities.d.ts +68 -0
  270. package/dist/utilities/Utilities.js +305 -0
  271. package/dist/utilities/Utilities.js.map +1 -0
  272. package/dist/utilities/WasmMemoryManager.d.ts +142 -0
  273. package/dist/utilities/WasmMemoryManager.js +407 -0
  274. package/dist/utilities/WasmMemoryManager.js.map +1 -0
  275. package/dist/utilities/WebReader.d.ts +1 -0
  276. package/dist/utilities/WebReader.js +47 -0
  277. package/dist/utilities/WebReader.js.map +1 -0
  278. package/dist/utilities/WikipediaReader.d.ts +1 -0
  279. package/dist/utilities/WikipediaReader.js +31 -0
  280. package/dist/utilities/WikipediaReader.js.map +1 -0
  281. package/dist/voice-activity-detection/SileroVAD.d.ts +13 -0
  282. package/dist/voice-activity-detection/SileroVAD.js +58 -0
  283. package/dist/voice-activity-detection/SileroVAD.js.map +1 -0
  284. package/dist/voice-activity-detection/WebRtcVAD.d.ts +3 -0
  285. package/dist/voice-activity-detection/WebRtcVAD.js +53 -0
  286. package/dist/voice-activity-detection/WebRtcVAD.js.map +1 -0
  287. package/docs/CLI.md +234 -0
  288. package/docs/Development.md +3 -0
  289. package/docs/Engines.md +80 -0
  290. package/docs/Licenses.md +37 -0
  291. package/docs/Options.md +192 -0
  292. package/docs/Roadmap.md +16 -0
  293. package/docs/Technical.md +72 -0
  294. package/package.json +119 -19
  295. package/cli.js +0 -3
@@ -0,0 +1,208 @@
1
+ import * as AudioBufferConversion from '../audio/AudioBufferConversion.js';
2
+ import { logToStderr } from '../utilities/Utilities.js';
3
+ const log = logToStderr;
4
+ export function encodeWave(rawAudio, bitDepth = 16, sampleFormat, speakerPositionMask = 0) {
5
+ const audioChannels = rawAudio.audioChannels;
6
+ const sampleRate = rawAudio.sampleRate;
7
+ const audioBuffer = AudioBufferConversion.encodeToAudioBuffer(audioChannels, bitDepth, sampleFormat);
8
+ const audioDataLength = audioBuffer.length;
9
+ const shouldUseExtensibleFormat = bitDepth > 16 || audioChannels.length > 2;
10
+ const formatSubChunk = new WaveFormat(audioChannels.length, sampleRate, bitDepth, sampleFormat, speakerPositionMask);
11
+ const formatSubChunkBuffer = formatSubChunk.serialize(shouldUseExtensibleFormat);
12
+ const dataSubChunkBuffer = Buffer.alloc(4 + 4 + audioDataLength);
13
+ dataSubChunkBuffer.write("data", 0, "ascii");
14
+ dataSubChunkBuffer.writeUint32LE(audioDataLength, 4);
15
+ dataSubChunkBuffer.set(audioBuffer, 8);
16
+ const riffChunkHeaderBuffer = Buffer.alloc(12);
17
+ riffChunkHeaderBuffer.write("RIFF", 0, "ascii");
18
+ riffChunkHeaderBuffer.writeUint32LE(4 + formatSubChunkBuffer.length + dataSubChunkBuffer.length, 4);
19
+ riffChunkHeaderBuffer.write("WAVE", 8, "ascii");
20
+ return Buffer.concat([riffChunkHeaderBuffer, formatSubChunkBuffer, dataSubChunkBuffer]);
21
+ }
22
+ export function decodeWave(waveData, ignoreTruncatedChunks = false) {
23
+ let readOffset = 0;
24
+ const riffId = waveData.subarray(readOffset, readOffset + 4).toString("ascii");
25
+ if (riffId != "RIFF") {
26
+ throw new Error("Not a valid wave file. No RIFF id found at offset 0.");
27
+ }
28
+ readOffset += 4;
29
+ const riffChunkSize = waveData.readUInt32LE(readOffset);
30
+ readOffset += 4;
31
+ const waveId = waveData.subarray(readOffset, readOffset + 4).toString("ascii");
32
+ if (waveId != "WAVE") {
33
+ throw new Error("Not a valid wave file. No WAVE id found at offset 8.");
34
+ }
35
+ if (riffChunkSize < waveData.length - 8) {
36
+ throw new Error(`RIFF chunk length ${riffChunkSize} is smaller than the remaining size of the buffer (${waveData.length - 8})`);
37
+ }
38
+ if (!ignoreTruncatedChunks && riffChunkSize > waveData.length - 8) {
39
+ throw new Error(`RIFF chunk length (${riffChunkSize}) is greater than the remaining size of the buffer (${waveData.length - 8})`);
40
+ }
41
+ readOffset += 4;
42
+ let formatSubChunkBodyBuffer;
43
+ const dataBuffers = [];
44
+ while (true) {
45
+ const subChunkIdentifier = waveData.subarray(readOffset, readOffset + 4).toString("ascii");
46
+ readOffset += 4;
47
+ const subChunkSize = waveData.readUInt32LE(readOffset);
48
+ readOffset += 4;
49
+ if (!ignoreTruncatedChunks && subChunkSize > waveData.length - readOffset) {
50
+ 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})`);
51
+ }
52
+ if (subChunkIdentifier == "fmt ") {
53
+ formatSubChunkBodyBuffer = waveData.subarray(readOffset, readOffset + subChunkSize);
54
+ }
55
+ else if (subChunkIdentifier == "data") {
56
+ if (!formatSubChunkBodyBuffer) {
57
+ throw new Error("A data subchunk was encountered before a format subchunk");
58
+ }
59
+ // If the data chunk is truncated, but truncations are ignored,
60
+ // it would be read up to the end of the buffer
61
+ dataBuffers.push(waveData.subarray(readOffset, readOffset + subChunkSize));
62
+ }
63
+ // All sub chunks other than 'data' (e.g. 'LIST', 'fact', 'plst', 'junk' etc.) are ignored
64
+ // This addition operation may overflow if JavaScript integers were 32 bits,
65
+ // but since they are 52 bits, it is okay:
66
+ readOffset += subChunkSize;
67
+ // Break if readOffset is equal to or is greater than the size of the buffer
68
+ if (readOffset >= waveData.length) {
69
+ break;
70
+ }
71
+ }
72
+ if (!formatSubChunkBodyBuffer) {
73
+ throw new Error("No format subchunk was found in the wave file");
74
+ }
75
+ const waveFormat = WaveFormat.deserializeFrom(formatSubChunkBodyBuffer);
76
+ const sampleFormat = waveFormat.sampleFormat;
77
+ const channelCount = waveFormat.channelCount;
78
+ const sampleRate = waveFormat.sampleRate;
79
+ const bitDepth = waveFormat.bitDepth;
80
+ const speakerPositionMask = waveFormat.speakerPositionMask;
81
+ const concatenatedDataBuffers = Buffer.concat(dataBuffers);
82
+ const audioChannels = AudioBufferConversion.decodeToChannels(concatenatedDataBuffers, channelCount, bitDepth, sampleFormat);
83
+ return {
84
+ rawAudio: { audioChannels, sampleRate },
85
+ sourceSampleFormat: sampleFormat,
86
+ sourceBitDepth: bitDepth,
87
+ sourceSpeakerPositionMask: speakerPositionMask
88
+ };
89
+ }
90
+ export function repairWave(waveData) {
91
+ const { rawAudio, sourceSampleFormat, sourceBitDepth } = decodeWave(waveData);
92
+ return encodeWave(rawAudio, sourceBitDepth, sourceSampleFormat);
93
+ }
94
+ class WaveFormat {
95
+ sampleFormat; // 2 bytes LE
96
+ channelCount; // 2 bytes LE
97
+ sampleRate; // 4 bytes LE
98
+ get byteRate() { return this.sampleRate * this.bytesPerSample * this.channelCount; } // 4 bytes LE
99
+ get blockAlign() { return this.bytesPerSample * this.channelCount; } // 2 bytes LE
100
+ bitDepth; // 2 bytes LE
101
+ speakerPositionMask; // 4 bytes LE
102
+ get guid() { return sampleFormatToGuid[this.sampleFormat]; } // 16 bytes BE
103
+ // helpers:
104
+ get bytesPerSample() { return this.bitDepth / 8; }
105
+ constructor(channelCount, sampleRate, bitDepth, sampleFormat, speakerPositionMask = 0) {
106
+ this.sampleFormat = sampleFormat;
107
+ this.channelCount = channelCount;
108
+ this.sampleRate = sampleRate;
109
+ this.bitDepth = bitDepth;
110
+ this.speakerPositionMask = speakerPositionMask;
111
+ }
112
+ serialize(useExtensibleFormat) {
113
+ let sampleFormatId = this.sampleFormat;
114
+ if (useExtensibleFormat) {
115
+ sampleFormatId = 65534;
116
+ }
117
+ const serializedSize = sampleFormatToSerializedSize[sampleFormatId];
118
+ const result = Buffer.alloc(serializedSize);
119
+ result.write("fmt ", 0, "ascii"); // + 4
120
+ result.writeUint32LE(serializedSize - 8, 4); // + 4
121
+ result.writeUint16LE(sampleFormatId, 8); // + 2
122
+ result.writeUint16LE(this.channelCount, 10); // + 2
123
+ result.writeUint32LE(this.sampleRate, 12); // + 4
124
+ result.writeUint32LE(this.byteRate, 16); // + 4
125
+ result.writeUint16LE(this.blockAlign, 20); // + 2
126
+ result.writeUint16LE(this.bitDepth, 22); // + 2
127
+ if (useExtensibleFormat) {
128
+ result.writeUint16LE(serializedSize - 26, 24); // + 2 (extension size)
129
+ result.writeUint16LE(this.bitDepth, 26); // + 2 (valid bits per sample)
130
+ result.writeUint32LE(this.speakerPositionMask, 28); // + 2 (speaker position mask)
131
+ if (this.sampleFormat == SampleFormat.PCM || this.sampleFormat == SampleFormat.Float) {
132
+ result.set(Buffer.from(this.guid, "hex"), 32);
133
+ }
134
+ else {
135
+ throw new Error(`Extensible format is not supported for sample format ${this.sampleFormat}`);
136
+ }
137
+ }
138
+ return result;
139
+ }
140
+ static deserializeFrom(formatChunkBody) {
141
+ let sampleFormat = formatChunkBody.readUint16LE(0); // + 2
142
+ const channelCount = formatChunkBody.readUint16LE(2); // + 2
143
+ const sampleRate = formatChunkBody.readUint32LE(4); // + 4
144
+ const bitDepth = formatChunkBody.readUint16LE(14);
145
+ let speakerPositionMask = 0;
146
+ if (sampleFormat == 65534) {
147
+ if (formatChunkBody.length < 40) {
148
+ 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`);
149
+ }
150
+ speakerPositionMask = formatChunkBody.readUint16LE(20);
151
+ const guid = formatChunkBody.subarray(24, 40).toString("hex");
152
+ if (guid == sampleFormatToGuid[SampleFormat.PCM]) {
153
+ sampleFormat = SampleFormat.PCM;
154
+ }
155
+ else if (guid == sampleFormatToGuid[SampleFormat.Float]) {
156
+ sampleFormat = SampleFormat.Float;
157
+ }
158
+ else {
159
+ throw new Error(`Unsupported format GUID in extended format subchunk: ${guid}`);
160
+ }
161
+ }
162
+ if (sampleFormat == SampleFormat.PCM) {
163
+ if (bitDepth != 8 && bitDepth != 16 && bitDepth != 24 && bitDepth != 32) {
164
+ throw new Error(`PCM audio has a bit depth of ${bitDepth}, which is not supported`);
165
+ }
166
+ }
167
+ else if (sampleFormat == SampleFormat.Float) {
168
+ if (bitDepth != 32 && bitDepth != 64) {
169
+ throw new Error(`IEEE float audio has a bit depth of ${bitDepth}, which is not supported`);
170
+ }
171
+ }
172
+ else if (sampleFormat == SampleFormat.Alaw) {
173
+ if (bitDepth != 8) {
174
+ throw new Error(`Alaw audio has a bit depth of ${bitDepth}, which is not supported`);
175
+ }
176
+ }
177
+ else if (sampleFormat == SampleFormat.Mulaw) {
178
+ if (bitDepth != 8) {
179
+ throw new Error(`Mulaw audio has a bit depth of ${bitDepth}, which is not supported`);
180
+ }
181
+ }
182
+ else {
183
+ throw new Error(`Wave audio format id ${sampleFormat} is not supported`);
184
+ }
185
+ return new WaveFormat(channelCount, sampleRate, bitDepth, sampleFormat, speakerPositionMask);
186
+ }
187
+ }
188
+ export var SampleFormat;
189
+ (function (SampleFormat) {
190
+ SampleFormat[SampleFormat["PCM"] = 1] = "PCM";
191
+ SampleFormat[SampleFormat["Float"] = 2] = "Float";
192
+ SampleFormat[SampleFormat["Alaw"] = 6] = "Alaw";
193
+ SampleFormat[SampleFormat["Mulaw"] = 7] = "Mulaw";
194
+ })(SampleFormat = SampleFormat || (SampleFormat = {}));
195
+ const sampleFormatToSerializedSize = {
196
+ [SampleFormat.PCM]: 24,
197
+ [SampleFormat.Float]: 26,
198
+ [SampleFormat.Alaw]: 26,
199
+ [SampleFormat.Mulaw]: 26,
200
+ 65534: 48
201
+ };
202
+ const sampleFormatToGuid = {
203
+ [SampleFormat.PCM]: "0100000000001000800000aa00389b71",
204
+ [SampleFormat.Float]: "0300000000001000800000aa00389b71",
205
+ [SampleFormat.Alaw]: "",
206
+ [SampleFormat.Mulaw]: "",
207
+ };
208
+ //# sourceMappingURL=WaveCodec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WaveCodec.js","sourceRoot":"","sources":["../../src/codecs/WaveCodec.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,qBAAqB,MAAM,mCAAmC,CAAA;AAE1E,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAA;AAEvD,MAAM,GAAG,GAAG,WAAW,CAAA;AAEvB,MAAM,UAAU,UAAU,CAAC,QAAkB,EAAE,WAAkC,EAAE,EAAE,YAA0B,EAAE,mBAAmB,GAAG,CAAC;IACvI,MAAM,aAAa,GAAG,QAAQ,CAAC,aAAa,CAAA;IAC5C,MAAM,UAAU,GAAG,QAAQ,CAAC,UAAU,CAAA;IAEtC,MAAM,WAAW,GAAG,qBAAqB,CAAC,mBAAmB,CAAC,aAAa,EAAE,QAAQ,EAAE,YAAY,CAAC,CAAA;IACpG,MAAM,eAAe,GAAG,WAAW,CAAC,MAAM,CAAA;IAE1C,MAAM,yBAAyB,GAAG,QAAQ,GAAG,EAAE,IAAI,aAAa,CAAC,MAAM,GAAG,CAAC,CAAA;IAE3E,MAAM,cAAc,GAAG,IAAI,UAAU,CAAC,aAAa,CAAC,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,YAAY,EAAE,mBAAmB,CAAC,CAAA;IACpH,MAAM,oBAAoB,GAAG,cAAc,CAAC,SAAS,CAAC,yBAAyB,CAAC,CAAA;IAEhF,MAAM,kBAAkB,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,G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,YAAY,CAAC,IAAI,CAAC,EAAE,EAAE;IACvB,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,EAAE;IACxB,KAAK,EAAE,EAAE;CACT,CAAA;AAED,MAAM,kBAAkB,GAAG;IAC1B,CAAC,YAAY,CAAC,GAAG,CAAC,EAAE,kCAAkC;IACtD,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,kCAAkC;IACxD,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,EAAE;IACvB,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE,EAAE;CACxB,CAAA"}
@@ -0,0 +1,6 @@
1
+ import { RawAudio } from "../audio/AudioUtilities.js";
2
+ export declare function denoiseAudio(rawAudio: RawAudio): Promise<{
3
+ denoisedRawAudio: RawAudio;
4
+ frameVadProbabilities: number[];
5
+ }>;
6
+ export declare function getRnnoiseInstance(): Promise<any>;
@@ -0,0 +1,68 @@
1
+ import { float32ToInt16Pcm } from "../audio/AudioBufferConversion.js";
2
+ import { concatFloat32Arrays } from "../utilities/Utilities.js";
3
+ import { WasmMemoryManager } from "../utilities/WasmMemoryManager.js";
4
+ import { Logger } from "../utilities/Logger.js";
5
+ import { cloneRawAudio } from "../audio/AudioUtilities.js";
6
+ let rnnoiseInstance;
7
+ export async function denoiseAudio(rawAudio) {
8
+ const logger = new Logger();
9
+ if (rawAudio.sampleRate != 48000) {
10
+ throw new Error("Sample rate must be 48000");
11
+ }
12
+ if (rawAudio.audioChannels.length != 1) {
13
+ throw new Error("Channel count must be 1");
14
+ }
15
+ if (rawAudio.audioChannels[0].length == 0) {
16
+ return { denoisedRawAudio: cloneRawAudio(rawAudio), frameVadProbabilities: [] };
17
+ }
18
+ logger.start("Get RNNoise WASM instance");
19
+ const m = await getRnnoiseInstance();
20
+ logger.start("Process with RNNoise");
21
+ const wasmMemory = new WasmMemoryManager(m);
22
+ const stateSize = m._rnnoise_get_size();
23
+ const frameSize = m._rnnoise_get_frame_size();
24
+ const denoiseState = m._rnnoise_create(0);
25
+ const inputRef = wasmMemory.allocFloat32Array(frameSize);
26
+ const outputRef = wasmMemory.allocFloat32Array(frameSize);
27
+ const floatSamples = rawAudio.audioChannels[0];
28
+ const int16Samples = float32ToInt16Pcm(floatSamples);
29
+ const int16SamplesAsFloats = new Float32Array(int16Samples);
30
+ const processedFrames = [];
31
+ const frameVadProbabilities = [];
32
+ function outputNewFrame(newFrame, vadProbability) {
33
+ processedFrames.push(newFrame);
34
+ frameVadProbabilities.push(vadProbability);
35
+ }
36
+ for (let readOffset = 0; readOffset < int16Samples.length; readOffset += frameSize) {
37
+ let frame = int16SamplesAsFloats.subarray(readOffset, readOffset + frameSize);
38
+ if (frame.length < frameSize) {
39
+ frame = concatFloat32Arrays([frame, new Float32Array(frameSize - frame.length)]);
40
+ }
41
+ inputRef.view.set(frame);
42
+ const vadProbability = m._rnnoise_process_frame(denoiseState, outputRef.address, inputRef.address);
43
+ // Latency compensation: don't write an output frame for the first read frame
44
+ if (readOffset > 0) {
45
+ outputNewFrame(outputRef.view.slice(), vadProbability);
46
+ }
47
+ }
48
+ // Latency compensation: process an empty input frame for the last output frame
49
+ inputRef.view.set(new Float32Array(frameSize));
50
+ const lastFrameVadProbability = m._rnnoise_process_frame(denoiseState, outputRef.address, inputRef.address);
51
+ outputNewFrame(outputRef.view.slice(), lastFrameVadProbability);
52
+ m._rnnoise_destroy(denoiseState);
53
+ wasmMemory.freeAll();
54
+ const int16DenoisedSamplesAsFloats = concatFloat32Arrays(processedFrames);
55
+ let denoisedSamples = int16DenoisedSamplesAsFloats.map(sample => sample / 32768);
56
+ denoisedSamples = denoisedSamples.subarray(0, floatSamples.length);
57
+ const denoisedRawAudio = { audioChannels: [denoisedSamples], sampleRate: 48000 };
58
+ logger.end();
59
+ return { denoisedRawAudio, frameVadProbabilities };
60
+ }
61
+ export async function getRnnoiseInstance() {
62
+ if (!rnnoiseInstance) {
63
+ const { default: initializer } = await import('@echogarden/rnnoise-wasm');
64
+ rnnoiseInstance = await initializer();
65
+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,0 +1,26 @@
1
+ import { RawAudio } from "../audio/AudioUtilities.js";
2
+ export declare function lowpassFilter(rawAudio: RawAudio, cutoffFrequency: number, q?: number): RawAudio;
3
+ export declare function highpassFilter(rawAudio: RawAudio, cutoffFrequency: number, q?: number): RawAudio;
4
+ export declare function bandpassFilter(rawAudio: RawAudio, centerFrequency: number, q?: number): RawAudio;
5
+ export declare function lowshelfFilter(rawAudio: RawAudio, midpointFrequency: number, gain: number): RawAudio;
6
+ export declare function highshelfFilter(rawAudio: RawAudio, midpointFrequency: number, gain: number): RawAudio;
7
+ export declare function peakingFilter(rawAudio: RawAudio, centerFrequency: number, gain: number, q?: number): RawAudio;
8
+ export declare function notchFilter(rawAudio: RawAudio, centerFrequency: number, q?: number): RawAudio;
9
+ export declare function allpassFilter(rawAudio: RawAudio, centerFrequency: number, q?: number): RawAudio;
10
+ export declare function filter(rawAudio: RawAudio, filterType: FilterType, frequency: number, gain: number, q: number): RawAudio;
11
+ export declare function filterUsingCoefficients(samples: Float32Array, filterCoefficients: FilterCoefficients): Float32Array;
12
+ export declare function createFilter(filterType: FilterType, sampleRate: number, centerFrequency: number, q: number, gain: number): any;
13
+ export declare function createLowpassFilter(freqRatio: number, q: number, gain: number): FilterCoefficients;
14
+ export declare function bandwidthToQFactor(bandwidth: number): number;
15
+ export declare function qFactorToBandwidth(q: number): number;
16
+ export declare const filterTypeName: {
17
+ [name in FilterType]: string;
18
+ };
19
+ export type FilterType = 'lowpass' | 'highpass' | 'bandpass' | 'lowshelf' | 'highshelf' | 'peaking' | 'notch' | 'allpass';
20
+ export type FilterCoefficients = {
21
+ b0: number;
22
+ b1: number;
23
+ b2: number;
24
+ a1: number;
25
+ a2: number;
26
+ };