sarvamai 1.1.0 → 1.1.1-alpha.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/dist/cjs/BaseClient.js +2 -2
- package/dist/cjs/play.d.ts +25 -0
- package/dist/cjs/play.js +235 -0
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/BaseClient.mjs +2 -2
- package/dist/esm/play.d.mts +25 -0
- package/dist/esm/play.mjs +197 -0
- package/dist/esm/version.d.mts +1 -1
- package/dist/esm/version.mjs +1 -1
- package/package.json +1 -1
package/dist/cjs/BaseClient.js
CHANGED
|
@@ -43,8 +43,8 @@ function normalizeClientOptions(options) {
|
|
|
43
43
|
const headers = (0, headers_js_1.mergeHeaders)({
|
|
44
44
|
"X-Fern-Language": "JavaScript",
|
|
45
45
|
"X-Fern-SDK-Name": "sarvamai",
|
|
46
|
-
"X-Fern-SDK-Version": "1.1.
|
|
47
|
-
"User-Agent": "sarvamai/1.1.
|
|
46
|
+
"X-Fern-SDK-Version": "1.1.1-alpha.1",
|
|
47
|
+
"User-Agent": "sarvamai/1.1.1-alpha.1",
|
|
48
48
|
"X-Fern-Runtime": core.RUNTIME.type,
|
|
49
49
|
"X-Fern-Runtime-Version": core.RUNTIME.version,
|
|
50
50
|
}, options === null || options === void 0 ? void 0 : options.headers);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { TextToSpeechResponse } from "./api/types/TextToSpeechResponse.js";
|
|
2
|
+
export type TextToSpeechLike = Pick<TextToSpeechResponse, "audios">;
|
|
3
|
+
export interface PlayOptions {
|
|
4
|
+
/**
|
|
5
|
+
* - In Node: when true, uses `ffplay` (from ffmpeg). When false, throws (no built-in fallback).
|
|
6
|
+
* - In browsers: ignored (uses HTMLAudioElement).
|
|
7
|
+
*/
|
|
8
|
+
useFfmpeg?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface SaveOptions {
|
|
11
|
+
/**
|
|
12
|
+
* If provided, saves only a single element from `audios`.
|
|
13
|
+
* If omitted, concatenates all `audios` like the Python helper.
|
|
14
|
+
*/
|
|
15
|
+
index?: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Concatenates `TextToSpeechResponse.audios` WAV base64 chunks into a single WAV byte array
|
|
19
|
+
* (matching the Python SDK helper).
|
|
20
|
+
*
|
|
21
|
+
* If `options.index` is provided, it decodes only that one entry (no concatenation).
|
|
22
|
+
*/
|
|
23
|
+
export declare function toWavBytes(audio: TextToSpeechLike | string[], options?: SaveOptions): Uint8Array;
|
|
24
|
+
export declare function save(audio: TextToSpeechLike | string[], filename: string, options?: SaveOptions): Promise<void>;
|
|
25
|
+
export declare function play(audio: TextToSpeechLike | string[], options?: PlayOptions): Promise<void>;
|
package/dist/cjs/play.js
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
|
+
exports.toWavBytes = toWavBytes;
|
|
46
|
+
exports.save = save;
|
|
47
|
+
exports.play = play;
|
|
48
|
+
const index_js_1 = require("./core/runtime/index.js");
|
|
49
|
+
/**
|
|
50
|
+
* Concatenates `TextToSpeechResponse.audios` WAV base64 chunks into a single WAV byte array
|
|
51
|
+
* (matching the Python SDK helper).
|
|
52
|
+
*
|
|
53
|
+
* If `options.index` is provided, it decodes only that one entry (no concatenation).
|
|
54
|
+
*/
|
|
55
|
+
function toWavBytes(audio, options = {}) {
|
|
56
|
+
const audios = Array.isArray(audio) ? audio : audio === null || audio === void 0 ? void 0 : audio.audios;
|
|
57
|
+
if (!Array.isArray(audios)) {
|
|
58
|
+
const keys = audio != null && typeof audio === "object"
|
|
59
|
+
? Object.keys(audio)
|
|
60
|
+
.slice(0, 20)
|
|
61
|
+
.join(", ")
|
|
62
|
+
: "";
|
|
63
|
+
throw new Error("Invalid audio input: expected `string[]` (base64 WAV chunks) or `{ audios: string[] }` (TextToSpeechResponse). " +
|
|
64
|
+
"If you're using streaming TTS, collect `msg.data.audio` chunks into an array and pass that array to `audio.toWavBytes(...)` / `audio.save(...)`." +
|
|
65
|
+
(keys ? ` Received object keys: ${keys}` : ""));
|
|
66
|
+
}
|
|
67
|
+
if (audios.length === 0) {
|
|
68
|
+
throw new Error("No audio data found (audios is empty).");
|
|
69
|
+
}
|
|
70
|
+
if (options.index != null) {
|
|
71
|
+
const chunk = audios[options.index];
|
|
72
|
+
if (chunk == null) {
|
|
73
|
+
throw new Error(`Invalid audio index ${options.index}.`);
|
|
74
|
+
}
|
|
75
|
+
return base64ToBytes(chunk);
|
|
76
|
+
}
|
|
77
|
+
if (audios.length === 1) {
|
|
78
|
+
return base64ToBytes(audios[0]);
|
|
79
|
+
}
|
|
80
|
+
const segments = [];
|
|
81
|
+
const first = base64ToBytes(audios[0]);
|
|
82
|
+
segments.push(first);
|
|
83
|
+
for (let i = 1; i < audios.length; i++) {
|
|
84
|
+
const chunk = base64ToBytes(audios[i]);
|
|
85
|
+
const dataPos = findAscii(chunk, "data");
|
|
86
|
+
if (dataPos === -1) {
|
|
87
|
+
// If we can't find a data chunk, fall back to appending everything (best effort)
|
|
88
|
+
segments.push(chunk);
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const audioDataStart = dataPos + 8; // "data" (4) + chunkSize (4)
|
|
92
|
+
segments.push(chunk.subarray(audioDataStart));
|
|
93
|
+
}
|
|
94
|
+
const combined = concatBytes(segments);
|
|
95
|
+
// Update RIFF chunk size at bytes [4..7]
|
|
96
|
+
// RIFF size = fileSize - 8
|
|
97
|
+
writeUint32LE(combined, 4, combined.length - 8);
|
|
98
|
+
// Update data chunk size at "data"+4
|
|
99
|
+
const dataPos = findAscii(combined, "data");
|
|
100
|
+
if (dataPos !== -1) {
|
|
101
|
+
const dataSize = combined.length - dataPos - 8;
|
|
102
|
+
writeUint32LE(combined, dataPos + 4, dataSize);
|
|
103
|
+
}
|
|
104
|
+
return combined;
|
|
105
|
+
}
|
|
106
|
+
function save(audio_1, filename_1) {
|
|
107
|
+
return __awaiter(this, arguments, void 0, function* (audio, filename, options = {}) {
|
|
108
|
+
var _a;
|
|
109
|
+
const bytes = toWavBytes(audio, options);
|
|
110
|
+
if (index_js_1.RUNTIME.type === "browser") {
|
|
111
|
+
if (typeof document === "undefined") {
|
|
112
|
+
throw new Error("Browser save requires `document` to trigger a download.");
|
|
113
|
+
}
|
|
114
|
+
// `BlobPart` typings require an ArrayBuffer-backed view; copy to ensure `ArrayBuffer` (not `ArrayBufferLike`).
|
|
115
|
+
const blobBytes = new Uint8Array(bytes);
|
|
116
|
+
const blob = new Blob([blobBytes], { type: "audio/wav" });
|
|
117
|
+
const url = URL.createObjectURL(blob);
|
|
118
|
+
try {
|
|
119
|
+
const a = document.createElement("a");
|
|
120
|
+
a.href = url;
|
|
121
|
+
a.download = filename;
|
|
122
|
+
a.style.display = "none";
|
|
123
|
+
document.body.appendChild(a);
|
|
124
|
+
a.click();
|
|
125
|
+
a.remove();
|
|
126
|
+
}
|
|
127
|
+
finally {
|
|
128
|
+
URL.revokeObjectURL(url);
|
|
129
|
+
}
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
// Node / Bun / Deno best-effort: try `fs` dynamic import (like other parts of this SDK)
|
|
133
|
+
const fs = yield Promise.resolve().then(() => __importStar(require("fs")));
|
|
134
|
+
if (!((_a = fs === null || fs === void 0 ? void 0 : fs.promises) === null || _a === void 0 ? void 0 : _a.writeFile)) {
|
|
135
|
+
throw new Error("Saving to a file is not supported in this environment.");
|
|
136
|
+
}
|
|
137
|
+
yield fs.promises.writeFile(filename, bytes);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
function play(audio_1) {
|
|
141
|
+
return __awaiter(this, arguments, void 0, function* (audio, options = {}) {
|
|
142
|
+
var _a;
|
|
143
|
+
const bytes = toWavBytes(audio);
|
|
144
|
+
if (index_js_1.RUNTIME.type === "browser") {
|
|
145
|
+
// `BlobPart` typings require an ArrayBuffer-backed view; copy to ensure `ArrayBuffer` (not `ArrayBufferLike`).
|
|
146
|
+
const blobBytes = new Uint8Array(bytes);
|
|
147
|
+
const blob = new Blob([blobBytes], { type: "audio/wav" });
|
|
148
|
+
const url = URL.createObjectURL(blob);
|
|
149
|
+
try {
|
|
150
|
+
const el = new Audio(url);
|
|
151
|
+
// Autoplay behavior depends on browser policies; caller may need a user gesture.
|
|
152
|
+
yield el.play();
|
|
153
|
+
}
|
|
154
|
+
finally {
|
|
155
|
+
// Let playback start; revoke soon after to avoid leaks.
|
|
156
|
+
setTimeout(() => URL.revokeObjectURL(url), 10000);
|
|
157
|
+
}
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const useFfmpeg = (_a = options.useFfmpeg) !== null && _a !== void 0 ? _a : true;
|
|
161
|
+
if (!useFfmpeg) {
|
|
162
|
+
throw new Error("`useFfmpeg=false` is not supported in Node (no built-in audio output).");
|
|
163
|
+
}
|
|
164
|
+
const childProcess = yield Promise.resolve().then(() => __importStar(require("child_process")));
|
|
165
|
+
yield new Promise((resolve, reject) => {
|
|
166
|
+
var _a, _b, _c;
|
|
167
|
+
const proc = childProcess.spawn("ffplay", ["-autoexit", "-", "-nodisp"], {
|
|
168
|
+
stdio: ["pipe", "ignore", "pipe"],
|
|
169
|
+
});
|
|
170
|
+
proc.on("error", (err) => {
|
|
171
|
+
if ((err === null || err === void 0 ? void 0 : err.code) === "ENOENT") {
|
|
172
|
+
reject(new Error("ffplay (from ffmpeg) not found, necessary to play audio. " +
|
|
173
|
+
"On mac you can install it with `brew install ffmpeg`. " +
|
|
174
|
+
"On linux/windows you can install from https://ffmpeg.org/."));
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
reject(err);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
(_a = proc.stderr) === null || _a === void 0 ? void 0 : _a.on("data", () => {
|
|
181
|
+
// ignore ffplay logs
|
|
182
|
+
});
|
|
183
|
+
proc.on("close", () => resolve());
|
|
184
|
+
// Write WAV bytes to stdin
|
|
185
|
+
(_b = proc.stdin) === null || _b === void 0 ? void 0 : _b.write(typeof Buffer !== "undefined" ? Buffer.from(bytes) : bytes);
|
|
186
|
+
(_c = proc.stdin) === null || _c === void 0 ? void 0 : _c.end();
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
/** -------- internals -------- */
|
|
191
|
+
function base64ToBytes(base64) {
|
|
192
|
+
if (typeof Buffer !== "undefined") {
|
|
193
|
+
const buf = Buffer.from(base64, "base64");
|
|
194
|
+
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
195
|
+
}
|
|
196
|
+
if (typeof atob !== "function") {
|
|
197
|
+
throw new Error("Base64 decoding is not supported in this environment.");
|
|
198
|
+
}
|
|
199
|
+
const bin = atob(base64);
|
|
200
|
+
const out = new Uint8Array(bin.length);
|
|
201
|
+
for (let i = 0; i < bin.length; i++)
|
|
202
|
+
out[i] = bin.charCodeAt(i);
|
|
203
|
+
return out;
|
|
204
|
+
}
|
|
205
|
+
function concatBytes(chunks) {
|
|
206
|
+
let total = 0;
|
|
207
|
+
for (const c of chunks)
|
|
208
|
+
total += c.length;
|
|
209
|
+
const out = new Uint8Array(total);
|
|
210
|
+
let offset = 0;
|
|
211
|
+
for (const c of chunks) {
|
|
212
|
+
out.set(c, offset);
|
|
213
|
+
offset += c.length;
|
|
214
|
+
}
|
|
215
|
+
return out;
|
|
216
|
+
}
|
|
217
|
+
function writeUint32LE(buf, offset, value) {
|
|
218
|
+
buf[offset] = value & 0xff;
|
|
219
|
+
buf[offset + 1] = (value >>> 8) & 0xff;
|
|
220
|
+
buf[offset + 2] = (value >>> 16) & 0xff;
|
|
221
|
+
buf[offset + 3] = (value >>> 24) & 0xff;
|
|
222
|
+
}
|
|
223
|
+
function findAscii(haystack, needle) {
|
|
224
|
+
const n = new Uint8Array(needle.length);
|
|
225
|
+
for (let i = 0; i < needle.length; i++)
|
|
226
|
+
n[i] = needle.charCodeAt(i);
|
|
227
|
+
outer: for (let i = 0; i <= haystack.length - n.length; i++) {
|
|
228
|
+
for (let j = 0; j < n.length; j++) {
|
|
229
|
+
if (haystack[i + j] !== n[j])
|
|
230
|
+
continue outer;
|
|
231
|
+
}
|
|
232
|
+
return i;
|
|
233
|
+
}
|
|
234
|
+
return -1;
|
|
235
|
+
}
|
package/dist/cjs/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "1.1.
|
|
1
|
+
export declare const SDK_VERSION = "1.1.1-alpha.1";
|
package/dist/cjs/version.js
CHANGED
package/dist/esm/BaseClient.mjs
CHANGED
|
@@ -6,8 +6,8 @@ export function normalizeClientOptions(options) {
|
|
|
6
6
|
const headers = mergeHeaders({
|
|
7
7
|
"X-Fern-Language": "JavaScript",
|
|
8
8
|
"X-Fern-SDK-Name": "sarvamai",
|
|
9
|
-
"X-Fern-SDK-Version": "1.1.
|
|
10
|
-
"User-Agent": "sarvamai/1.1.
|
|
9
|
+
"X-Fern-SDK-Version": "1.1.1-alpha.1",
|
|
10
|
+
"User-Agent": "sarvamai/1.1.1-alpha.1",
|
|
11
11
|
"X-Fern-Runtime": core.RUNTIME.type,
|
|
12
12
|
"X-Fern-Runtime-Version": core.RUNTIME.version,
|
|
13
13
|
}, options === null || options === void 0 ? void 0 : options.headers);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { TextToSpeechResponse } from "./api/types/TextToSpeechResponse.mjs";
|
|
2
|
+
export type TextToSpeechLike = Pick<TextToSpeechResponse, "audios">;
|
|
3
|
+
export interface PlayOptions {
|
|
4
|
+
/**
|
|
5
|
+
* - In Node: when true, uses `ffplay` (from ffmpeg). When false, throws (no built-in fallback).
|
|
6
|
+
* - In browsers: ignored (uses HTMLAudioElement).
|
|
7
|
+
*/
|
|
8
|
+
useFfmpeg?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface SaveOptions {
|
|
11
|
+
/**
|
|
12
|
+
* If provided, saves only a single element from `audios`.
|
|
13
|
+
* If omitted, concatenates all `audios` like the Python helper.
|
|
14
|
+
*/
|
|
15
|
+
index?: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Concatenates `TextToSpeechResponse.audios` WAV base64 chunks into a single WAV byte array
|
|
19
|
+
* (matching the Python SDK helper).
|
|
20
|
+
*
|
|
21
|
+
* If `options.index` is provided, it decodes only that one entry (no concatenation).
|
|
22
|
+
*/
|
|
23
|
+
export declare function toWavBytes(audio: TextToSpeechLike | string[], options?: SaveOptions): Uint8Array;
|
|
24
|
+
export declare function save(audio: TextToSpeechLike | string[], filename: string, options?: SaveOptions): Promise<void>;
|
|
25
|
+
export declare function play(audio: TextToSpeechLike | string[], options?: PlayOptions): Promise<void>;
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { RUNTIME } from "./core/runtime/index.mjs";
|
|
11
|
+
/**
|
|
12
|
+
* Concatenates `TextToSpeechResponse.audios` WAV base64 chunks into a single WAV byte array
|
|
13
|
+
* (matching the Python SDK helper).
|
|
14
|
+
*
|
|
15
|
+
* If `options.index` is provided, it decodes only that one entry (no concatenation).
|
|
16
|
+
*/
|
|
17
|
+
export function toWavBytes(audio, options = {}) {
|
|
18
|
+
const audios = Array.isArray(audio) ? audio : audio === null || audio === void 0 ? void 0 : audio.audios;
|
|
19
|
+
if (!Array.isArray(audios)) {
|
|
20
|
+
const keys = audio != null && typeof audio === "object"
|
|
21
|
+
? Object.keys(audio)
|
|
22
|
+
.slice(0, 20)
|
|
23
|
+
.join(", ")
|
|
24
|
+
: "";
|
|
25
|
+
throw new Error("Invalid audio input: expected `string[]` (base64 WAV chunks) or `{ audios: string[] }` (TextToSpeechResponse). " +
|
|
26
|
+
"If you're using streaming TTS, collect `msg.data.audio` chunks into an array and pass that array to `audio.toWavBytes(...)` / `audio.save(...)`." +
|
|
27
|
+
(keys ? ` Received object keys: ${keys}` : ""));
|
|
28
|
+
}
|
|
29
|
+
if (audios.length === 0) {
|
|
30
|
+
throw new Error("No audio data found (audios is empty).");
|
|
31
|
+
}
|
|
32
|
+
if (options.index != null) {
|
|
33
|
+
const chunk = audios[options.index];
|
|
34
|
+
if (chunk == null) {
|
|
35
|
+
throw new Error(`Invalid audio index ${options.index}.`);
|
|
36
|
+
}
|
|
37
|
+
return base64ToBytes(chunk);
|
|
38
|
+
}
|
|
39
|
+
if (audios.length === 1) {
|
|
40
|
+
return base64ToBytes(audios[0]);
|
|
41
|
+
}
|
|
42
|
+
const segments = [];
|
|
43
|
+
const first = base64ToBytes(audios[0]);
|
|
44
|
+
segments.push(first);
|
|
45
|
+
for (let i = 1; i < audios.length; i++) {
|
|
46
|
+
const chunk = base64ToBytes(audios[i]);
|
|
47
|
+
const dataPos = findAscii(chunk, "data");
|
|
48
|
+
if (dataPos === -1) {
|
|
49
|
+
// If we can't find a data chunk, fall back to appending everything (best effort)
|
|
50
|
+
segments.push(chunk);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const audioDataStart = dataPos + 8; // "data" (4) + chunkSize (4)
|
|
54
|
+
segments.push(chunk.subarray(audioDataStart));
|
|
55
|
+
}
|
|
56
|
+
const combined = concatBytes(segments);
|
|
57
|
+
// Update RIFF chunk size at bytes [4..7]
|
|
58
|
+
// RIFF size = fileSize - 8
|
|
59
|
+
writeUint32LE(combined, 4, combined.length - 8);
|
|
60
|
+
// Update data chunk size at "data"+4
|
|
61
|
+
const dataPos = findAscii(combined, "data");
|
|
62
|
+
if (dataPos !== -1) {
|
|
63
|
+
const dataSize = combined.length - dataPos - 8;
|
|
64
|
+
writeUint32LE(combined, dataPos + 4, dataSize);
|
|
65
|
+
}
|
|
66
|
+
return combined;
|
|
67
|
+
}
|
|
68
|
+
export function save(audio_1, filename_1) {
|
|
69
|
+
return __awaiter(this, arguments, void 0, function* (audio, filename, options = {}) {
|
|
70
|
+
var _a;
|
|
71
|
+
const bytes = toWavBytes(audio, options);
|
|
72
|
+
if (RUNTIME.type === "browser") {
|
|
73
|
+
if (typeof document === "undefined") {
|
|
74
|
+
throw new Error("Browser save requires `document` to trigger a download.");
|
|
75
|
+
}
|
|
76
|
+
// `BlobPart` typings require an ArrayBuffer-backed view; copy to ensure `ArrayBuffer` (not `ArrayBufferLike`).
|
|
77
|
+
const blobBytes = new Uint8Array(bytes);
|
|
78
|
+
const blob = new Blob([blobBytes], { type: "audio/wav" });
|
|
79
|
+
const url = URL.createObjectURL(blob);
|
|
80
|
+
try {
|
|
81
|
+
const a = document.createElement("a");
|
|
82
|
+
a.href = url;
|
|
83
|
+
a.download = filename;
|
|
84
|
+
a.style.display = "none";
|
|
85
|
+
document.body.appendChild(a);
|
|
86
|
+
a.click();
|
|
87
|
+
a.remove();
|
|
88
|
+
}
|
|
89
|
+
finally {
|
|
90
|
+
URL.revokeObjectURL(url);
|
|
91
|
+
}
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
// Node / Bun / Deno best-effort: try `fs` dynamic import (like other parts of this SDK)
|
|
95
|
+
const fs = yield import("fs");
|
|
96
|
+
if (!((_a = fs === null || fs === void 0 ? void 0 : fs.promises) === null || _a === void 0 ? void 0 : _a.writeFile)) {
|
|
97
|
+
throw new Error("Saving to a file is not supported in this environment.");
|
|
98
|
+
}
|
|
99
|
+
yield fs.promises.writeFile(filename, bytes);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
export function play(audio_1) {
|
|
103
|
+
return __awaiter(this, arguments, void 0, function* (audio, options = {}) {
|
|
104
|
+
var _a;
|
|
105
|
+
const bytes = toWavBytes(audio);
|
|
106
|
+
if (RUNTIME.type === "browser") {
|
|
107
|
+
// `BlobPart` typings require an ArrayBuffer-backed view; copy to ensure `ArrayBuffer` (not `ArrayBufferLike`).
|
|
108
|
+
const blobBytes = new Uint8Array(bytes);
|
|
109
|
+
const blob = new Blob([blobBytes], { type: "audio/wav" });
|
|
110
|
+
const url = URL.createObjectURL(blob);
|
|
111
|
+
try {
|
|
112
|
+
const el = new Audio(url);
|
|
113
|
+
// Autoplay behavior depends on browser policies; caller may need a user gesture.
|
|
114
|
+
yield el.play();
|
|
115
|
+
}
|
|
116
|
+
finally {
|
|
117
|
+
// Let playback start; revoke soon after to avoid leaks.
|
|
118
|
+
setTimeout(() => URL.revokeObjectURL(url), 10000);
|
|
119
|
+
}
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const useFfmpeg = (_a = options.useFfmpeg) !== null && _a !== void 0 ? _a : true;
|
|
123
|
+
if (!useFfmpeg) {
|
|
124
|
+
throw new Error("`useFfmpeg=false` is not supported in Node (no built-in audio output).");
|
|
125
|
+
}
|
|
126
|
+
const childProcess = yield import("child_process");
|
|
127
|
+
yield new Promise((resolve, reject) => {
|
|
128
|
+
var _a, _b, _c;
|
|
129
|
+
const proc = childProcess.spawn("ffplay", ["-autoexit", "-", "-nodisp"], {
|
|
130
|
+
stdio: ["pipe", "ignore", "pipe"],
|
|
131
|
+
});
|
|
132
|
+
proc.on("error", (err) => {
|
|
133
|
+
if ((err === null || err === void 0 ? void 0 : err.code) === "ENOENT") {
|
|
134
|
+
reject(new Error("ffplay (from ffmpeg) not found, necessary to play audio. " +
|
|
135
|
+
"On mac you can install it with `brew install ffmpeg`. " +
|
|
136
|
+
"On linux/windows you can install from https://ffmpeg.org/."));
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
reject(err);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
(_a = proc.stderr) === null || _a === void 0 ? void 0 : _a.on("data", () => {
|
|
143
|
+
// ignore ffplay logs
|
|
144
|
+
});
|
|
145
|
+
proc.on("close", () => resolve());
|
|
146
|
+
// Write WAV bytes to stdin
|
|
147
|
+
(_b = proc.stdin) === null || _b === void 0 ? void 0 : _b.write(typeof Buffer !== "undefined" ? Buffer.from(bytes) : bytes);
|
|
148
|
+
(_c = proc.stdin) === null || _c === void 0 ? void 0 : _c.end();
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
/** -------- internals -------- */
|
|
153
|
+
function base64ToBytes(base64) {
|
|
154
|
+
if (typeof Buffer !== "undefined") {
|
|
155
|
+
const buf = Buffer.from(base64, "base64");
|
|
156
|
+
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
157
|
+
}
|
|
158
|
+
if (typeof atob !== "function") {
|
|
159
|
+
throw new Error("Base64 decoding is not supported in this environment.");
|
|
160
|
+
}
|
|
161
|
+
const bin = atob(base64);
|
|
162
|
+
const out = new Uint8Array(bin.length);
|
|
163
|
+
for (let i = 0; i < bin.length; i++)
|
|
164
|
+
out[i] = bin.charCodeAt(i);
|
|
165
|
+
return out;
|
|
166
|
+
}
|
|
167
|
+
function concatBytes(chunks) {
|
|
168
|
+
let total = 0;
|
|
169
|
+
for (const c of chunks)
|
|
170
|
+
total += c.length;
|
|
171
|
+
const out = new Uint8Array(total);
|
|
172
|
+
let offset = 0;
|
|
173
|
+
for (const c of chunks) {
|
|
174
|
+
out.set(c, offset);
|
|
175
|
+
offset += c.length;
|
|
176
|
+
}
|
|
177
|
+
return out;
|
|
178
|
+
}
|
|
179
|
+
function writeUint32LE(buf, offset, value) {
|
|
180
|
+
buf[offset] = value & 0xff;
|
|
181
|
+
buf[offset + 1] = (value >>> 8) & 0xff;
|
|
182
|
+
buf[offset + 2] = (value >>> 16) & 0xff;
|
|
183
|
+
buf[offset + 3] = (value >>> 24) & 0xff;
|
|
184
|
+
}
|
|
185
|
+
function findAscii(haystack, needle) {
|
|
186
|
+
const n = new Uint8Array(needle.length);
|
|
187
|
+
for (let i = 0; i < needle.length; i++)
|
|
188
|
+
n[i] = needle.charCodeAt(i);
|
|
189
|
+
outer: for (let i = 0; i <= haystack.length - n.length; i++) {
|
|
190
|
+
for (let j = 0; j < n.length; j++) {
|
|
191
|
+
if (haystack[i + j] !== n[j])
|
|
192
|
+
continue outer;
|
|
193
|
+
}
|
|
194
|
+
return i;
|
|
195
|
+
}
|
|
196
|
+
return -1;
|
|
197
|
+
}
|
package/dist/esm/version.d.mts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "1.1.
|
|
1
|
+
export declare const SDK_VERSION = "1.1.1-alpha.1";
|
package/dist/esm/version.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const SDK_VERSION = "1.1.
|
|
1
|
+
export const SDK_VERSION = "1.1.1-alpha.1";
|