summaryrefslogtreecommitdiffstats
path: root/projects/text_to_speech/utils.js
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-04-09 19:40:42 +0200
committerGitHub <noreply@github.com>2024-04-09 19:40:42 +0200
commit4c23b4cad4744e20da7ccffb303503ea627df7c2 (patch)
treea032e6ebd494136ba35e049b06aa4af45558b540 /projects/text_to_speech/utils.js
parentMerge pull request #1809 from ochen1/patch-1 (diff)
parentAdd project files (diff)
downloadgpt4free-4c23b4cad4744e20da7ccffb303503ea627df7c2.tar
gpt4free-4c23b4cad4744e20da7ccffb303503ea627df7c2.tar.gz
gpt4free-4c23b4cad4744e20da7ccffb303503ea627df7c2.tar.bz2
gpt4free-4c23b4cad4744e20da7ccffb303503ea627df7c2.tar.lz
gpt4free-4c23b4cad4744e20da7ccffb303503ea627df7c2.tar.xz
gpt4free-4c23b4cad4744e20da7ccffb303503ea627df7c2.tar.zst
gpt4free-4c23b4cad4744e20da7ccffb303503ea627df7c2.zip
Diffstat (limited to 'projects/text_to_speech/utils.js')
-rw-r--r--projects/text_to_speech/utils.js47
1 files changed, 47 insertions, 0 deletions
diff --git a/projects/text_to_speech/utils.js b/projects/text_to_speech/utils.js
new file mode 100644
index 00000000..4f53dea8
--- /dev/null
+++ b/projects/text_to_speech/utils.js
@@ -0,0 +1,47 @@
+// Adapted from https://www.npmjs.com/package/audiobuffer-to-wav
+
+export function encodeWAV(samples) {
+ let offset = 44;
+ const buffer = new ArrayBuffer(offset + samples.length * 4);
+ const view = new DataView(buffer);
+ const sampleRate = 16000;
+
+ /* RIFF identifier */
+ writeString(view, 0, 'RIFF')
+ /* RIFF chunk length */
+ view.setUint32(4, 36 + samples.length * 4, true)
+ /* RIFF type */
+ writeString(view, 8, 'WAVE')
+ /* format chunk identifier */
+ writeString(view, 12, 'fmt ')
+ /* format chunk length */
+ view.setUint32(16, 16, true)
+ /* sample format (raw) */
+ view.setUint16(20, 3, true)
+ /* channel count */
+ view.setUint16(22, 1, true)
+ /* sample rate */
+ view.setUint32(24, sampleRate, true)
+ /* byte rate (sample rate * block align) */
+ view.setUint32(28, sampleRate * 4, true)
+ /* block align (channel count * bytes per sample) */
+ view.setUint16(32, 4, true)
+ /* bits per sample */
+ view.setUint16(34, 32, true)
+ /* data chunk identifier */
+ writeString(view, 36, 'data')
+ /* data chunk length */
+ view.setUint32(40, samples.length * 4, true)
+
+ for (let i = 0; i < samples.length; ++i, offset += 4) {
+ view.setFloat32(offset, samples[i], true)
+ }
+
+ return buffer
+}
+
+function writeString(view, offset, string) {
+ for (let i = 0; i < string.length; ++i) {
+ view.setUint8(offset + i, string.charCodeAt(i))
+ }
+} \ No newline at end of file