@elevenlabs/elevenlabs-js 2.4.0 → 2.4.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.
@@ -1,2 +1 @@
1
- import * as stream from "stream";
2
- export declare function play(audio: stream.Readable): Promise<void>;
1
+ export declare function play(audio: AsyncIterable<Uint8Array>): Promise<void>;
@@ -1,4 +1,37 @@
1
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
+ })();
2
35
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
36
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
37
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -8,50 +41,50 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
41
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
42
  });
10
43
  };
11
- var __asyncValues = (this && this.__asyncValues) || function (o) {
12
- if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
13
- var m = o[Symbol.asyncIterator], i;
14
- return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
15
- function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
16
- function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
17
- };
18
- var __importDefault = (this && this.__importDefault) || function (mod) {
19
- return (mod && mod.__esModule) ? mod : { "default": mod };
20
- };
21
44
  Object.defineProperty(exports, "__esModule", { value: true });
22
45
  exports.play = play;
23
- const command_exists_1 = __importDefault(require("command-exists"));
24
46
  const ElevenLabsError_1 = require("../errors/ElevenLabsError");
25
- const execa_1 = __importDefault(require("execa"));
47
+ const utils_1 = require("./utils");
26
48
  function play(audio) {
27
49
  return __awaiter(this, void 0, void 0, function* () {
28
- var _a, audio_1, audio_1_1;
29
- var _b, e_1, _c, _d;
30
- var _e, _f;
31
- if (!(0, command_exists_1.default)("ffplay")) {
50
+ if (!(0, utils_1.isNode)()) {
32
51
  throw new ElevenLabsError_1.ElevenLabsError({
33
- message: `ffplay from ffmpeg not found, necessary to play audio.
34
- On mac you can install it with 'brew install ffmpeg'.
35
- On linux and windows you can install it from https://ffmpeg.org/`,
52
+ message: "The play function is only available in a Node.js environment.",
36
53
  });
37
54
  }
38
- const ffmpeg = (0, execa_1.default)("ffplay", ["-autoexit", "-", "-nodisp"]);
39
- try {
40
- for (_a = true, audio_1 = __asyncValues(audio); audio_1_1 = yield audio_1.next(), _b = audio_1_1.done, !_b; _a = true) {
41
- _d = audio_1_1.value;
42
- _a = false;
43
- const data = _d;
44
- (_e = ffmpeg.stdin) === null || _e === void 0 ? void 0 : _e.write(data);
45
- }
46
- }
47
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
48
- finally {
49
- try {
50
- if (!_a && !_b && (_c = audio_1.return)) yield _c.call(audio_1);
51
- }
52
- finally { if (e_1) throw e_1.error; }
55
+ const { spawn } = yield Promise.resolve().then(() => __importStar(require("node:child_process")));
56
+ const { Readable } = yield Promise.resolve().then(() => __importStar(require("node:stream")));
57
+ const commandExists = (yield Promise.resolve().then(() => __importStar(require("command-exists")))).default;
58
+ if (!commandExists.sync("ffplay")) {
59
+ throw new ElevenLabsError_1.ElevenLabsError({
60
+ message: `ffplay from ffmpeg not found, necessary to play audio.
61
+ On mac you can install it with 'brew install ffmpeg'.
62
+ On linux and windows you can install it from https://ffmpeg.org/`,
63
+ });
53
64
  }
54
- (_f = ffmpeg.stdin) === null || _f === void 0 ? void 0 : _f.end();
55
- yield ffmpeg;
65
+ const ffplay = spawn("ffplay", ["-autoexit", "-", "-nodisp"], {
66
+ stdio: ["pipe", "ignore", "pipe"],
67
+ });
68
+ Readable.from(audio).pipe(ffplay.stdin);
69
+ const errorChunks = [];
70
+ ffplay.stderr.on("data", (chunk) => {
71
+ errorChunks.push(chunk);
72
+ });
73
+ return new Promise((resolve, reject) => {
74
+ ffplay.on("close", (code) => {
75
+ if (code === 0) {
76
+ resolve();
77
+ }
78
+ else {
79
+ const error = Buffer.concat(errorChunks).toString();
80
+ reject(new ElevenLabsError_1.ElevenLabsError({
81
+ message: `ffplay exited with code ${code}. Stderr: ${error}`,
82
+ }));
83
+ }
84
+ });
85
+ ffplay.on("error", (err) => {
86
+ reject(new ElevenLabsError_1.ElevenLabsError({ message: `Failed to start ffplay: ${err.message}` }));
87
+ });
88
+ });
56
89
  });
57
90
  }
@@ -1,2 +1 @@
1
- import * as nodeStream from "stream";
2
- export declare function stream(audio: nodeStream.Readable): Promise<void>;
1
+ export declare function stream(audio: ReadableStream<Uint8Array>): Promise<void>;
@@ -1,4 +1,37 @@
1
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
+ })();
2
35
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
36
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
37
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -8,56 +41,50 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
41
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
42
  });
10
43
  };
11
- var __asyncValues = (this && this.__asyncValues) || function (o) {
12
- if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
13
- var m = o[Symbol.asyncIterator], i;
14
- return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
15
- function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
16
- function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
17
- };
18
- var __importDefault = (this && this.__importDefault) || function (mod) {
19
- return (mod && mod.__esModule) ? mod : { "default": mod };
20
- };
21
44
  Object.defineProperty(exports, "__esModule", { value: true });
22
45
  exports.stream = stream;
23
- const command_exists_1 = __importDefault(require("command-exists"));
46
+ const utils_1 = require("./utils");
24
47
  const ElevenLabsError_1 = require("../errors/ElevenLabsError");
25
- const runtime_1 = require("../core/runtime/runtime");
26
- const execa_1 = __importDefault(require("execa"));
27
48
  function stream(audio) {
28
49
  return __awaiter(this, void 0, void 0, function* () {
29
- var _a, audio_1, audio_1_1;
30
- var _b, e_1, _c, _d;
31
- var _e, _f;
32
- if (runtime_1.RUNTIME.type !== "node") {
50
+ if (!(0, utils_1.isNode)()) {
33
51
  throw new ElevenLabsError_1.ElevenLabsError({
34
- message: `This function is only supported in node environments. ${runtime_1.RUNTIME.type} is not supported`,
52
+ message: "The stream function is only available in a Node.js environment.",
35
53
  });
36
54
  }
37
- if (!(0, command_exists_1.default)("mpv")) {
55
+ const { spawn } = yield Promise.resolve().then(() => __importStar(require("node:child_process")));
56
+ const { Readable } = yield Promise.resolve().then(() => __importStar(require("node:stream")));
57
+ const commandExists = (yield Promise.resolve().then(() => __importStar(require("command-exists")))).default;
58
+ if (!commandExists.sync("mpv")) {
38
59
  throw new ElevenLabsError_1.ElevenLabsError({
39
60
  message: `mpv not found, necessary to stream audio."
40
61
  On mac you can install it with 'brew install mpv'.
41
62
  On linux and windows you can install it from https://mpv.io/`,
42
63
  });
43
64
  }
44
- const mpv = (0, execa_1.default)("mpv", ["--no-cache", "--no-terminal", "--", "fd://0"]);
45
- try {
46
- for (_a = true, audio_1 = __asyncValues(audio); audio_1_1 = yield audio_1.next(), _b = audio_1_1.done, !_b; _a = true) {
47
- _d = audio_1_1.value;
48
- _a = false;
49
- const data = _d;
50
- (_e = mpv.stdin) === null || _e === void 0 ? void 0 : _e.write(data);
51
- }
52
- }
53
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
54
- finally {
55
- try {
56
- if (!_a && !_b && (_c = audio_1.return)) yield _c.call(audio_1);
57
- }
58
- finally { if (e_1) throw e_1.error; }
59
- }
60
- (_f = mpv.stdin) === null || _f === void 0 ? void 0 : _f.end();
61
- yield mpv;
65
+ const mpv = spawn("mpv", ["--no-cache", "--no-terminal", "--", "fd://0"], {
66
+ stdio: ["pipe", "ignore", "pipe"],
67
+ });
68
+ Readable.from((0, utils_1.toAsyncIterable)(audio)).pipe(mpv.stdin);
69
+ const errorChunks = [];
70
+ mpv.stderr.on("data", (chunk) => {
71
+ errorChunks.push(chunk);
72
+ });
73
+ return new Promise((resolve, reject) => {
74
+ mpv.on("close", (code) => {
75
+ if (code === 0) {
76
+ resolve();
77
+ }
78
+ else {
79
+ const error = Buffer.concat(errorChunks).toString();
80
+ reject(new ElevenLabsError_1.ElevenLabsError({
81
+ message: `mpv exited with code ${code}. Stderr: ${error}`,
82
+ }));
83
+ }
84
+ });
85
+ mpv.on("error", (err) => {
86
+ reject(new ElevenLabsError_1.ElevenLabsError({ message: `Failed to start mpv: ${err.message}` }));
87
+ });
88
+ });
62
89
  });
63
90
  }
@@ -0,0 +1,2 @@
1
+ export declare function isNode(): boolean;
2
+ export declare function toAsyncIterable(stream: ReadableStream<Uint8Array>): AsyncGenerator<Uint8Array<ArrayBufferLike>, void, unknown>;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
3
+ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
4
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
5
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
6
+ return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
7
+ function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
8
+ function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
9
+ function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
10
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
11
+ function fulfill(value) { resume("next", value); }
12
+ function reject(value) { resume("throw", value); }
13
+ function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
14
+ };
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.isNode = isNode;
17
+ exports.toAsyncIterable = toAsyncIterable;
18
+ function isNode() {
19
+ var _a;
20
+ return (typeof process !== "undefined" &&
21
+ process.versions != null &&
22
+ process.versions.node != null &&
23
+ ((_a = process.release) === null || _a === void 0 ? void 0 : _a.name) === "node");
24
+ }
25
+ function toAsyncIterable(stream) {
26
+ return __asyncGenerator(this, arguments, function* toAsyncIterable_1() {
27
+ const reader = stream.getReader();
28
+ try {
29
+ while (true) {
30
+ const { done, value } = yield __await(reader.read());
31
+ if (done) {
32
+ return yield __await(void 0);
33
+ }
34
+ yield yield __await(value);
35
+ }
36
+ }
37
+ finally {
38
+ reader.releaseLock();
39
+ }
40
+ });
41
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elevenlabs/elevenlabs-js",
3
- "version": "v2.4.0",
3
+ "version": "v2.4.1",
4
4
  "private": false,
5
5
  "repository": "https://github.com/elevenlabs/elevenlabs-js",
6
6
  "license": "MIT",
@@ -21,8 +21,7 @@
21
21
  "node-fetch": "^2.7.0",
22
22
  "qs": "^6.13.1",
23
23
  "form-data-encoder": "^4.0.2",
24
- "command-exists": "^1.2.9",
25
- "execa": "^5.1.1"
24
+ "command-exists": "^1.2.9"
26
25
  },
27
26
  "devDependencies": {
28
27
  "@types/url-join": "4.0.1",
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "cf-worker-test",
3
+ "version": "0.0.0",
4
+ "private": true,
5
+ "scripts": {
6
+ "start": "wrangler dev",
7
+ "typecheck": "wrangler typecheck"
8
+ },
9
+ "dependencies": {
10
+ "@elevenlabs/elevenlabs-js": "file:../.."
11
+ },
12
+ "devDependencies": {
13
+ "@cloudflare/workers-types": "^4.20240512.0",
14
+ "typescript": "^5.0.4",
15
+ "wrangler": "^4.22.0"
16
+ }
17
+ }
@@ -0,0 +1,4 @@
1
+ name = "cf-worker-test"
2
+ main = "src/index.ts"
3
+ compatibility_flags = [ "nodejs_compat" ]
4
+ compatibility_date = "2024-09-23"
package/wrapper/play.d.ts CHANGED
@@ -1,2 +1 @@
1
- import * as stream from "stream";
2
- export declare function play(audio: stream.Readable): Promise<void>;
1
+ export declare function play(audio: AsyncIterable<Uint8Array>): Promise<void>;
package/wrapper/play.js CHANGED
@@ -1,4 +1,37 @@
1
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
+ })();
2
35
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
36
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
37
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -8,50 +41,50 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
41
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
42
  });
10
43
  };
11
- var __asyncValues = (this && this.__asyncValues) || function (o) {
12
- if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
13
- var m = o[Symbol.asyncIterator], i;
14
- return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
15
- function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
16
- function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
17
- };
18
- var __importDefault = (this && this.__importDefault) || function (mod) {
19
- return (mod && mod.__esModule) ? mod : { "default": mod };
20
- };
21
44
  Object.defineProperty(exports, "__esModule", { value: true });
22
45
  exports.play = play;
23
- const command_exists_1 = __importDefault(require("command-exists"));
24
46
  const ElevenLabsError_1 = require("../errors/ElevenLabsError");
25
- const execa_1 = __importDefault(require("execa"));
47
+ const utils_1 = require("./utils");
26
48
  function play(audio) {
27
49
  return __awaiter(this, void 0, void 0, function* () {
28
- var _a, audio_1, audio_1_1;
29
- var _b, e_1, _c, _d;
30
- var _e, _f;
31
- if (!(0, command_exists_1.default)("ffplay")) {
50
+ if (!(0, utils_1.isNode)()) {
32
51
  throw new ElevenLabsError_1.ElevenLabsError({
33
- message: `ffplay from ffmpeg not found, necessary to play audio.
34
- On mac you can install it with 'brew install ffmpeg'.
35
- On linux and windows you can install it from https://ffmpeg.org/`,
52
+ message: "The play function is only available in a Node.js environment.",
36
53
  });
37
54
  }
38
- const ffmpeg = (0, execa_1.default)("ffplay", ["-autoexit", "-", "-nodisp"]);
39
- try {
40
- for (_a = true, audio_1 = __asyncValues(audio); audio_1_1 = yield audio_1.next(), _b = audio_1_1.done, !_b; _a = true) {
41
- _d = audio_1_1.value;
42
- _a = false;
43
- const data = _d;
44
- (_e = ffmpeg.stdin) === null || _e === void 0 ? void 0 : _e.write(data);
45
- }
46
- }
47
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
48
- finally {
49
- try {
50
- if (!_a && !_b && (_c = audio_1.return)) yield _c.call(audio_1);
51
- }
52
- finally { if (e_1) throw e_1.error; }
55
+ const { spawn } = yield Promise.resolve().then(() => __importStar(require("node:child_process")));
56
+ const { Readable } = yield Promise.resolve().then(() => __importStar(require("node:stream")));
57
+ const commandExists = (yield Promise.resolve().then(() => __importStar(require("command-exists")))).default;
58
+ if (!commandExists.sync("ffplay")) {
59
+ throw new ElevenLabsError_1.ElevenLabsError({
60
+ message: `ffplay from ffmpeg not found, necessary to play audio.
61
+ On mac you can install it with 'brew install ffmpeg'.
62
+ On linux and windows you can install it from https://ffmpeg.org/`,
63
+ });
53
64
  }
54
- (_f = ffmpeg.stdin) === null || _f === void 0 ? void 0 : _f.end();
55
- yield ffmpeg;
65
+ const ffplay = spawn("ffplay", ["-autoexit", "-", "-nodisp"], {
66
+ stdio: ["pipe", "ignore", "pipe"],
67
+ });
68
+ Readable.from(audio).pipe(ffplay.stdin);
69
+ const errorChunks = [];
70
+ ffplay.stderr.on("data", (chunk) => {
71
+ errorChunks.push(chunk);
72
+ });
73
+ return new Promise((resolve, reject) => {
74
+ ffplay.on("close", (code) => {
75
+ if (code === 0) {
76
+ resolve();
77
+ }
78
+ else {
79
+ const error = Buffer.concat(errorChunks).toString();
80
+ reject(new ElevenLabsError_1.ElevenLabsError({
81
+ message: `ffplay exited with code ${code}. Stderr: ${error}`,
82
+ }));
83
+ }
84
+ });
85
+ ffplay.on("error", (err) => {
86
+ reject(new ElevenLabsError_1.ElevenLabsError({ message: `Failed to start ffplay: ${err.message}` }));
87
+ });
88
+ });
56
89
  });
57
90
  }
@@ -1,2 +1 @@
1
- import * as nodeStream from "stream";
2
- export declare function stream(audio: nodeStream.Readable): Promise<void>;
1
+ export declare function stream(audio: ReadableStream<Uint8Array>): Promise<void>;
package/wrapper/stream.js CHANGED
@@ -1,4 +1,37 @@
1
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
+ })();
2
35
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
36
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
37
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -8,56 +41,50 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
41
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
42
  });
10
43
  };
11
- var __asyncValues = (this && this.__asyncValues) || function (o) {
12
- if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
13
- var m = o[Symbol.asyncIterator], i;
14
- return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
15
- function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
16
- function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
17
- };
18
- var __importDefault = (this && this.__importDefault) || function (mod) {
19
- return (mod && mod.__esModule) ? mod : { "default": mod };
20
- };
21
44
  Object.defineProperty(exports, "__esModule", { value: true });
22
45
  exports.stream = stream;
23
- const command_exists_1 = __importDefault(require("command-exists"));
46
+ const utils_1 = require("./utils");
24
47
  const ElevenLabsError_1 = require("../errors/ElevenLabsError");
25
- const runtime_1 = require("../core/runtime/runtime");
26
- const execa_1 = __importDefault(require("execa"));
27
48
  function stream(audio) {
28
49
  return __awaiter(this, void 0, void 0, function* () {
29
- var _a, audio_1, audio_1_1;
30
- var _b, e_1, _c, _d;
31
- var _e, _f;
32
- if (runtime_1.RUNTIME.type !== "node") {
50
+ if (!(0, utils_1.isNode)()) {
33
51
  throw new ElevenLabsError_1.ElevenLabsError({
34
- message: `This function is only supported in node environments. ${runtime_1.RUNTIME.type} is not supported`,
52
+ message: "The stream function is only available in a Node.js environment.",
35
53
  });
36
54
  }
37
- if (!(0, command_exists_1.default)("mpv")) {
55
+ const { spawn } = yield Promise.resolve().then(() => __importStar(require("node:child_process")));
56
+ const { Readable } = yield Promise.resolve().then(() => __importStar(require("node:stream")));
57
+ const commandExists = (yield Promise.resolve().then(() => __importStar(require("command-exists")))).default;
58
+ if (!commandExists.sync("mpv")) {
38
59
  throw new ElevenLabsError_1.ElevenLabsError({
39
60
  message: `mpv not found, necessary to stream audio."
40
61
  On mac you can install it with 'brew install mpv'.
41
62
  On linux and windows you can install it from https://mpv.io/`,
42
63
  });
43
64
  }
44
- const mpv = (0, execa_1.default)("mpv", ["--no-cache", "--no-terminal", "--", "fd://0"]);
45
- try {
46
- for (_a = true, audio_1 = __asyncValues(audio); audio_1_1 = yield audio_1.next(), _b = audio_1_1.done, !_b; _a = true) {
47
- _d = audio_1_1.value;
48
- _a = false;
49
- const data = _d;
50
- (_e = mpv.stdin) === null || _e === void 0 ? void 0 : _e.write(data);
51
- }
52
- }
53
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
54
- finally {
55
- try {
56
- if (!_a && !_b && (_c = audio_1.return)) yield _c.call(audio_1);
57
- }
58
- finally { if (e_1) throw e_1.error; }
59
- }
60
- (_f = mpv.stdin) === null || _f === void 0 ? void 0 : _f.end();
61
- yield mpv;
65
+ const mpv = spawn("mpv", ["--no-cache", "--no-terminal", "--", "fd://0"], {
66
+ stdio: ["pipe", "ignore", "pipe"],
67
+ });
68
+ Readable.from((0, utils_1.toAsyncIterable)(audio)).pipe(mpv.stdin);
69
+ const errorChunks = [];
70
+ mpv.stderr.on("data", (chunk) => {
71
+ errorChunks.push(chunk);
72
+ });
73
+ return new Promise((resolve, reject) => {
74
+ mpv.on("close", (code) => {
75
+ if (code === 0) {
76
+ resolve();
77
+ }
78
+ else {
79
+ const error = Buffer.concat(errorChunks).toString();
80
+ reject(new ElevenLabsError_1.ElevenLabsError({
81
+ message: `mpv exited with code ${code}. Stderr: ${error}`,
82
+ }));
83
+ }
84
+ });
85
+ mpv.on("error", (err) => {
86
+ reject(new ElevenLabsError_1.ElevenLabsError({ message: `Failed to start mpv: ${err.message}` }));
87
+ });
88
+ });
62
89
  });
63
90
  }
@@ -0,0 +1,2 @@
1
+ export declare function isNode(): boolean;
2
+ export declare function toAsyncIterable(stream: ReadableStream<Uint8Array>): AsyncGenerator<Uint8Array<ArrayBufferLike>, void, unknown>;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
3
+ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
4
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
5
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
6
+ return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
7
+ function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
8
+ function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
9
+ function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
10
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
11
+ function fulfill(value) { resume("next", value); }
12
+ function reject(value) { resume("throw", value); }
13
+ function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
14
+ };
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.isNode = isNode;
17
+ exports.toAsyncIterable = toAsyncIterable;
18
+ function isNode() {
19
+ var _a;
20
+ return (typeof process !== "undefined" &&
21
+ process.versions != null &&
22
+ process.versions.node != null &&
23
+ ((_a = process.release) === null || _a === void 0 ? void 0 : _a.name) === "node");
24
+ }
25
+ function toAsyncIterable(stream) {
26
+ return __asyncGenerator(this, arguments, function* toAsyncIterable_1() {
27
+ const reader = stream.getReader();
28
+ try {
29
+ while (true) {
30
+ const { done, value } = yield __await(reader.read());
31
+ if (done) {
32
+ return yield __await(void 0);
33
+ }
34
+ yield yield __await(value);
35
+ }
36
+ }
37
+ finally {
38
+ reader.releaseLock();
39
+ }
40
+ });
41
+ }