sarvamai 1.1.0 → 1.1.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.
Files changed (23) hide show
  1. package/dist/cjs/BaseClient.js +2 -2
  2. package/dist/cjs/api/resources/speechToTextStreaming/types/SpeechToTextStreamingInputAudioCodec.d.ts +1 -21
  3. package/dist/cjs/api/resources/speechToTextStreaming/types/SpeechToTextStreamingInputAudioCodec.js +1 -21
  4. package/dist/cjs/api/resources/speechToTextTranslateStreaming/types/SpeechToTextTranslateStreamingInputAudioCodec.d.ts +1 -21
  5. package/dist/cjs/api/resources/speechToTextTranslateStreaming/types/SpeechToTextTranslateStreamingInputAudioCodec.js +1 -21
  6. package/dist/cjs/api/types/AudioData.d.ts +2 -22
  7. package/dist/cjs/api/types/AudioData.js +1 -21
  8. package/dist/cjs/play.d.ts +25 -0
  9. package/dist/cjs/play.js +235 -0
  10. package/dist/cjs/version.d.ts +1 -1
  11. package/dist/cjs/version.js +1 -1
  12. package/dist/esm/BaseClient.mjs +2 -2
  13. package/dist/esm/api/resources/speechToTextStreaming/types/SpeechToTextStreamingInputAudioCodec.d.mts +1 -21
  14. package/dist/esm/api/resources/speechToTextStreaming/types/SpeechToTextStreamingInputAudioCodec.mjs +1 -21
  15. package/dist/esm/api/resources/speechToTextTranslateStreaming/types/SpeechToTextTranslateStreamingInputAudioCodec.d.mts +1 -21
  16. package/dist/esm/api/resources/speechToTextTranslateStreaming/types/SpeechToTextTranslateStreamingInputAudioCodec.mjs +1 -21
  17. package/dist/esm/api/types/AudioData.d.mts +2 -22
  18. package/dist/esm/api/types/AudioData.mjs +1 -21
  19. package/dist/esm/play.d.mts +25 -0
  20. package/dist/esm/play.mjs +197 -0
  21. package/dist/esm/version.d.mts +1 -1
  22. package/dist/esm/version.mjs +1 -1
  23. package/package.json +1 -1
@@ -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.0",
47
- "User-Agent": "sarvamai/1.1.0",
46
+ "X-Fern-SDK-Version": "1.1.1",
47
+ "User-Agent": "sarvamai/1.1.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);
@@ -1,26 +1,6 @@
1
- /** Audio codec/format of the input file. Our API automatically detects all codec formats, but for PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
1
+ /** Audio codec/format of the input file. For PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
2
2
  export declare const SpeechToTextStreamingInputAudioCodec: {
3
3
  readonly Wav: "wav";
4
- readonly XWav: "x-wav";
5
- readonly Wave: "wave";
6
- readonly Mp3: "mp3";
7
- readonly Mpeg: "mpeg";
8
- readonly Mpeg3: "mpeg3";
9
- readonly XMp3: "x-mp3";
10
- readonly XMpeg3: "x-mpeg-3";
11
- readonly Aac: "aac";
12
- readonly XAac: "x-aac";
13
- readonly Aiff: "aiff";
14
- readonly XAiff: "x-aiff";
15
- readonly Ogg: "ogg";
16
- readonly Opus: "opus";
17
- readonly Flac: "flac";
18
- readonly XFlac: "x-flac";
19
- readonly Mp4: "mp4";
20
- readonly Xm4A: "x-m4a";
21
- readonly Amr: "amr";
22
- readonly XMsWma: "x-ms-wma";
23
- readonly Webm: "webm";
24
4
  readonly PcmS16Le: "pcm_s16le";
25
5
  readonly PcmL16: "pcm_l16";
26
6
  readonly PcmRaw: "pcm_raw";
@@ -2,29 +2,9 @@
2
2
  // This file was auto-generated by Fern from our API Definition.
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.SpeechToTextStreamingInputAudioCodec = void 0;
5
- /** Audio codec/format of the input file. Our API automatically detects all codec formats, but for PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
5
+ /** Audio codec/format of the input file. For PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
6
6
  exports.SpeechToTextStreamingInputAudioCodec = {
7
7
  Wav: "wav",
8
- XWav: "x-wav",
9
- Wave: "wave",
10
- Mp3: "mp3",
11
- Mpeg: "mpeg",
12
- Mpeg3: "mpeg3",
13
- XMp3: "x-mp3",
14
- XMpeg3: "x-mpeg-3",
15
- Aac: "aac",
16
- XAac: "x-aac",
17
- Aiff: "aiff",
18
- XAiff: "x-aiff",
19
- Ogg: "ogg",
20
- Opus: "opus",
21
- Flac: "flac",
22
- XFlac: "x-flac",
23
- Mp4: "mp4",
24
- Xm4A: "x-m4a",
25
- Amr: "amr",
26
- XMsWma: "x-ms-wma",
27
- Webm: "webm",
28
8
  PcmS16Le: "pcm_s16le",
29
9
  PcmL16: "pcm_l16",
30
10
  PcmRaw: "pcm_raw",
@@ -1,26 +1,6 @@
1
- /** Audio codec/format of the input file. Our API automatically detects all codec formats, but for PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
1
+ /** Audio codec/format of the input file. For PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
2
2
  export declare const SpeechToTextTranslateStreamingInputAudioCodec: {
3
3
  readonly Wav: "wav";
4
- readonly XWav: "x-wav";
5
- readonly Wave: "wave";
6
- readonly Mp3: "mp3";
7
- readonly Mpeg: "mpeg";
8
- readonly Mpeg3: "mpeg3";
9
- readonly XMp3: "x-mp3";
10
- readonly XMpeg3: "x-mpeg-3";
11
- readonly Aac: "aac";
12
- readonly XAac: "x-aac";
13
- readonly Aiff: "aiff";
14
- readonly XAiff: "x-aiff";
15
- readonly Ogg: "ogg";
16
- readonly Opus: "opus";
17
- readonly Flac: "flac";
18
- readonly XFlac: "x-flac";
19
- readonly Mp4: "mp4";
20
- readonly Xm4A: "x-m4a";
21
- readonly Amr: "amr";
22
- readonly XMsWma: "x-ms-wma";
23
- readonly Webm: "webm";
24
4
  readonly PcmS16Le: "pcm_s16le";
25
5
  readonly PcmL16: "pcm_l16";
26
6
  readonly PcmRaw: "pcm_raw";
@@ -2,29 +2,9 @@
2
2
  // This file was auto-generated by Fern from our API Definition.
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.SpeechToTextTranslateStreamingInputAudioCodec = void 0;
5
- /** Audio codec/format of the input file. Our API automatically detects all codec formats, but for PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
5
+ /** Audio codec/format of the input file. For PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
6
6
  exports.SpeechToTextTranslateStreamingInputAudioCodec = {
7
7
  Wav: "wav",
8
- XWav: "x-wav",
9
- Wave: "wave",
10
- Mp3: "mp3",
11
- Mpeg: "mpeg",
12
- Mpeg3: "mpeg3",
13
- XMp3: "x-mp3",
14
- XMpeg3: "x-mpeg-3",
15
- Aac: "aac",
16
- XAac: "x-aac",
17
- Aiff: "aiff",
18
- XAiff: "x-aiff",
19
- Ogg: "ogg",
20
- Opus: "opus",
21
- Flac: "flac",
22
- XFlac: "x-flac",
23
- Mp4: "mp4",
24
- Xm4A: "x-m4a",
25
- Amr: "amr",
26
- XMsWma: "x-ms-wma",
27
- Webm: "webm",
28
8
  PcmS16Le: "pcm_s16le",
29
9
  PcmL16: "pcm_l16",
30
10
  PcmRaw: "pcm_raw",
@@ -13,33 +13,13 @@ export interface AudioData {
13
13
  sample_rate: number;
14
14
  /** Audio encoding format */
15
15
  encoding: "audio/wav";
16
- /** Audio codec/format of the input file. Our API automatically detects all codec formats, but for PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
16
+ /** Audio codec/format of the input file. For PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
17
17
  input_audio_codec?: AudioData.InputAudioCodec;
18
18
  }
19
19
  export declare namespace AudioData {
20
- /** Audio codec/format of the input file. Our API automatically detects all codec formats, but for PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
20
+ /** Audio codec/format of the input file. For PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
21
21
  const InputAudioCodec: {
22
22
  readonly Wav: "wav";
23
- readonly XWav: "x-wav";
24
- readonly Wave: "wave";
25
- readonly Mp3: "mp3";
26
- readonly Mpeg: "mpeg";
27
- readonly Mpeg3: "mpeg3";
28
- readonly XMp3: "x-mp3";
29
- readonly XMpeg3: "x-mpeg-3";
30
- readonly Aac: "aac";
31
- readonly XAac: "x-aac";
32
- readonly Aiff: "aiff";
33
- readonly XAiff: "x-aiff";
34
- readonly Ogg: "ogg";
35
- readonly Opus: "opus";
36
- readonly Flac: "flac";
37
- readonly XFlac: "x-flac";
38
- readonly Mp4: "mp4";
39
- readonly Xm4A: "x-m4a";
40
- readonly Amr: "amr";
41
- readonly XMsWma: "x-ms-wma";
42
- readonly Webm: "webm";
43
23
  readonly PcmS16Le: "pcm_s16le";
44
24
  readonly PcmL16: "pcm_l16";
45
25
  readonly PcmRaw: "pcm_raw";
@@ -4,29 +4,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.AudioData = void 0;
5
5
  var AudioData;
6
6
  (function (AudioData) {
7
- /** Audio codec/format of the input file. Our API automatically detects all codec formats, but for PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
7
+ /** Audio codec/format of the input file. For PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
8
8
  AudioData.InputAudioCodec = {
9
9
  Wav: "wav",
10
- XWav: "x-wav",
11
- Wave: "wave",
12
- Mp3: "mp3",
13
- Mpeg: "mpeg",
14
- Mpeg3: "mpeg3",
15
- XMp3: "x-mp3",
16
- XMpeg3: "x-mpeg-3",
17
- Aac: "aac",
18
- XAac: "x-aac",
19
- Aiff: "aiff",
20
- XAiff: "x-aiff",
21
- Ogg: "ogg",
22
- Opus: "opus",
23
- Flac: "flac",
24
- XFlac: "x-flac",
25
- Mp4: "mp4",
26
- Xm4A: "x-m4a",
27
- Amr: "amr",
28
- XMsWma: "x-ms-wma",
29
- Webm: "webm",
30
10
  PcmS16Le: "pcm_s16le",
31
11
  PcmL16: "pcm_l16",
32
12
  PcmRaw: "pcm_raw",
@@ -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>;
@@ -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
+ }
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "1.1.0";
1
+ export declare const SDK_VERSION = "1.1.1";
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SDK_VERSION = void 0;
4
- exports.SDK_VERSION = "1.1.0";
4
+ exports.SDK_VERSION = "1.1.1";
@@ -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.0",
10
- "User-Agent": "sarvamai/1.1.0",
9
+ "X-Fern-SDK-Version": "1.1.1",
10
+ "User-Agent": "sarvamai/1.1.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);
@@ -1,26 +1,6 @@
1
- /** Audio codec/format of the input file. Our API automatically detects all codec formats, but for PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
1
+ /** Audio codec/format of the input file. For PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
2
2
  export declare const SpeechToTextStreamingInputAudioCodec: {
3
3
  readonly Wav: "wav";
4
- readonly XWav: "x-wav";
5
- readonly Wave: "wave";
6
- readonly Mp3: "mp3";
7
- readonly Mpeg: "mpeg";
8
- readonly Mpeg3: "mpeg3";
9
- readonly XMp3: "x-mp3";
10
- readonly XMpeg3: "x-mpeg-3";
11
- readonly Aac: "aac";
12
- readonly XAac: "x-aac";
13
- readonly Aiff: "aiff";
14
- readonly XAiff: "x-aiff";
15
- readonly Ogg: "ogg";
16
- readonly Opus: "opus";
17
- readonly Flac: "flac";
18
- readonly XFlac: "x-flac";
19
- readonly Mp4: "mp4";
20
- readonly Xm4A: "x-m4a";
21
- readonly Amr: "amr";
22
- readonly XMsWma: "x-ms-wma";
23
- readonly Webm: "webm";
24
4
  readonly PcmS16Le: "pcm_s16le";
25
5
  readonly PcmL16: "pcm_l16";
26
6
  readonly PcmRaw: "pcm_raw";
@@ -1,27 +1,7 @@
1
1
  // This file was auto-generated by Fern from our API Definition.
2
- /** Audio codec/format of the input file. Our API automatically detects all codec formats, but for PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
2
+ /** Audio codec/format of the input file. For PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
3
3
  export const SpeechToTextStreamingInputAudioCodec = {
4
4
  Wav: "wav",
5
- XWav: "x-wav",
6
- Wave: "wave",
7
- Mp3: "mp3",
8
- Mpeg: "mpeg",
9
- Mpeg3: "mpeg3",
10
- XMp3: "x-mp3",
11
- XMpeg3: "x-mpeg-3",
12
- Aac: "aac",
13
- XAac: "x-aac",
14
- Aiff: "aiff",
15
- XAiff: "x-aiff",
16
- Ogg: "ogg",
17
- Opus: "opus",
18
- Flac: "flac",
19
- XFlac: "x-flac",
20
- Mp4: "mp4",
21
- Xm4A: "x-m4a",
22
- Amr: "amr",
23
- XMsWma: "x-ms-wma",
24
- Webm: "webm",
25
5
  PcmS16Le: "pcm_s16le",
26
6
  PcmL16: "pcm_l16",
27
7
  PcmRaw: "pcm_raw",
@@ -1,26 +1,6 @@
1
- /** Audio codec/format of the input file. Our API automatically detects all codec formats, but for PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
1
+ /** Audio codec/format of the input file. For PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
2
2
  export declare const SpeechToTextTranslateStreamingInputAudioCodec: {
3
3
  readonly Wav: "wav";
4
- readonly XWav: "x-wav";
5
- readonly Wave: "wave";
6
- readonly Mp3: "mp3";
7
- readonly Mpeg: "mpeg";
8
- readonly Mpeg3: "mpeg3";
9
- readonly XMp3: "x-mp3";
10
- readonly XMpeg3: "x-mpeg-3";
11
- readonly Aac: "aac";
12
- readonly XAac: "x-aac";
13
- readonly Aiff: "aiff";
14
- readonly XAiff: "x-aiff";
15
- readonly Ogg: "ogg";
16
- readonly Opus: "opus";
17
- readonly Flac: "flac";
18
- readonly XFlac: "x-flac";
19
- readonly Mp4: "mp4";
20
- readonly Xm4A: "x-m4a";
21
- readonly Amr: "amr";
22
- readonly XMsWma: "x-ms-wma";
23
- readonly Webm: "webm";
24
4
  readonly PcmS16Le: "pcm_s16le";
25
5
  readonly PcmL16: "pcm_l16";
26
6
  readonly PcmRaw: "pcm_raw";
@@ -1,27 +1,7 @@
1
1
  // This file was auto-generated by Fern from our API Definition.
2
- /** Audio codec/format of the input file. Our API automatically detects all codec formats, but for PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
2
+ /** Audio codec/format of the input file. For PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
3
3
  export const SpeechToTextTranslateStreamingInputAudioCodec = {
4
4
  Wav: "wav",
5
- XWav: "x-wav",
6
- Wave: "wave",
7
- Mp3: "mp3",
8
- Mpeg: "mpeg",
9
- Mpeg3: "mpeg3",
10
- XMp3: "x-mp3",
11
- XMpeg3: "x-mpeg-3",
12
- Aac: "aac",
13
- XAac: "x-aac",
14
- Aiff: "aiff",
15
- XAiff: "x-aiff",
16
- Ogg: "ogg",
17
- Opus: "opus",
18
- Flac: "flac",
19
- XFlac: "x-flac",
20
- Mp4: "mp4",
21
- Xm4A: "x-m4a",
22
- Amr: "amr",
23
- XMsWma: "x-ms-wma",
24
- Webm: "webm",
25
5
  PcmS16Le: "pcm_s16le",
26
6
  PcmL16: "pcm_l16",
27
7
  PcmRaw: "pcm_raw",
@@ -13,33 +13,13 @@ export interface AudioData {
13
13
  sample_rate: number;
14
14
  /** Audio encoding format */
15
15
  encoding: "audio/wav";
16
- /** Audio codec/format of the input file. Our API automatically detects all codec formats, but for PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
16
+ /** Audio codec/format of the input file. For PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
17
17
  input_audio_codec?: AudioData.InputAudioCodec;
18
18
  }
19
19
  export declare namespace AudioData {
20
- /** Audio codec/format of the input file. Our API automatically detects all codec formats, but for PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
20
+ /** Audio codec/format of the input file. For PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
21
21
  const InputAudioCodec: {
22
22
  readonly Wav: "wav";
23
- readonly XWav: "x-wav";
24
- readonly Wave: "wave";
25
- readonly Mp3: "mp3";
26
- readonly Mpeg: "mpeg";
27
- readonly Mpeg3: "mpeg3";
28
- readonly XMp3: "x-mp3";
29
- readonly XMpeg3: "x-mpeg-3";
30
- readonly Aac: "aac";
31
- readonly XAac: "x-aac";
32
- readonly Aiff: "aiff";
33
- readonly XAiff: "x-aiff";
34
- readonly Ogg: "ogg";
35
- readonly Opus: "opus";
36
- readonly Flac: "flac";
37
- readonly XFlac: "x-flac";
38
- readonly Mp4: "mp4";
39
- readonly Xm4A: "x-m4a";
40
- readonly Amr: "amr";
41
- readonly XMsWma: "x-ms-wma";
42
- readonly Webm: "webm";
43
23
  readonly PcmS16Le: "pcm_s16le";
44
24
  readonly PcmL16: "pcm_l16";
45
25
  readonly PcmRaw: "pcm_raw";
@@ -1,29 +1,9 @@
1
1
  // This file was auto-generated by Fern from our API Definition.
2
2
  export var AudioData;
3
3
  (function (AudioData) {
4
- /** Audio codec/format of the input file. Our API automatically detects all codec formats, but for PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
4
+ /** Audio codec/format of the input file. For PCM files specifically (pcm_s16le, pcm_l16, pcm_raw), you must pass this parameter. PCM files supports sample rate 16000 and 8000. */
5
5
  AudioData.InputAudioCodec = {
6
6
  Wav: "wav",
7
- XWav: "x-wav",
8
- Wave: "wave",
9
- Mp3: "mp3",
10
- Mpeg: "mpeg",
11
- Mpeg3: "mpeg3",
12
- XMp3: "x-mp3",
13
- XMpeg3: "x-mpeg-3",
14
- Aac: "aac",
15
- XAac: "x-aac",
16
- Aiff: "aiff",
17
- XAiff: "x-aiff",
18
- Ogg: "ogg",
19
- Opus: "opus",
20
- Flac: "flac",
21
- XFlac: "x-flac",
22
- Mp4: "mp4",
23
- Xm4A: "x-m4a",
24
- Amr: "amr",
25
- XMsWma: "x-ms-wma",
26
- Webm: "webm",
27
7
  PcmS16Le: "pcm_s16le",
28
8
  PcmL16: "pcm_l16",
29
9
  PcmRaw: "pcm_raw",
@@ -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
+ }
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "1.1.0";
1
+ export declare const SDK_VERSION = "1.1.1";
@@ -1 +1 @@
1
- export const SDK_VERSION = "1.1.0";
1
+ export const SDK_VERSION = "1.1.1";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sarvamai",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "private": false,
5
5
  "repository": "github:sarvamai/sarvam-javascript-sdk",
6
6
  "type": "commonjs",