@fonoster/apiserver 0.7.36 → 0.7.37

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.
@@ -21,6 +21,7 @@ declare class ElevenLabs extends AbstractTextToSpeech<typeof ENGINE_NAME> {
21
21
  ref: string;
22
22
  stream: Readable;
23
23
  }>;
24
+ private doSynthesize;
24
25
  static getConfigValidationSchema(): z.Schema;
25
26
  static getCredentialsValidationSchema(): z.Schema;
26
27
  }
@@ -43,12 +43,32 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
43
43
  };
44
44
  Object.defineProperty(exports, "__esModule", { value: true });
45
45
  exports.ElevenLabs = exports.ENGINE_NAME = void 0;
46
- const common_1 = require("@fonoster/common");
46
+ /*
47
+ * Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
48
+ * http://github.com/fonoster/fonoster
49
+ *
50
+ * This file is part of Fonoster
51
+ *
52
+ * Licensed under the MIT License (the "License");
53
+ * you may not use this file except in compliance with
54
+ * the License. You may obtain a copy of the License at
55
+ *
56
+ * https://opensource.org/licenses/MIT
57
+ *
58
+ * Unless required by applicable law or agreed to in writing, software
59
+ * distributed under the License is distributed on an "AS IS" BASIS,
60
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
61
+ * See the License for the specific language governing permissions and
62
+ * limitations under the License.
63
+ */
64
+ const stream_1 = require("stream");
47
65
  const logger_1 = require("@fonoster/logger");
48
66
  const elevenlabs_1 = require("elevenlabs");
49
67
  const z = __importStar(require("zod"));
50
68
  const AbstractTextToSpeech_1 = require("./AbstractTextToSpeech");
51
69
  const isSsml_1 = require("./isSsml");
70
+ const streamToBuffer_1 = require("./streamToBuffer");
71
+ const textChunksByFirstNaturalPause_1 = require("../handlers/utils/textChunksByFirstNaturalPause"); // Assuming this is the chunking function
52
72
  const ENGINE_NAME = "tts.elevenlabs";
53
73
  exports.ENGINE_NAME = ENGINE_NAME;
54
74
  const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filename });
@@ -65,27 +85,52 @@ class ElevenLabs extends AbstractTextToSpeech_1.AbstractTextToSpeech {
65
85
  return __awaiter(this, void 0, void 0, function* () {
66
86
  logger.verbose(`synthesize [input: ${text}, isSsml=${(0, isSsml_1.isSsml)(text)} options: ${JSON.stringify(options)}]`);
67
87
  const { voice } = this.engineConfig.config;
68
- const audioStream = yield this.client.generate({
88
+ const ref = this.createMediaReference();
89
+ const chunks = (0, textChunksByFirstNaturalPause_1.textChunksByFirstNaturalPause)(text);
90
+ const stream = new stream_1.Readable({ read() { } });
91
+ const results = new Array(chunks.length);
92
+ let nextIndexToPush = 0;
93
+ function observeQueue() {
94
+ if (nextIndexToPush < results.length &&
95
+ results[nextIndexToPush] !== undefined) {
96
+ stream.push(results[nextIndexToPush]);
97
+ nextIndexToPush++;
98
+ setImmediate(observeQueue);
99
+ }
100
+ else if (nextIndexToPush < results.length) {
101
+ setTimeout(observeQueue, 10);
102
+ }
103
+ else {
104
+ stream.push(null);
105
+ }
106
+ }
107
+ observeQueue();
108
+ chunks.forEach((textChunk, index) => {
109
+ this.doSynthesize(textChunk, voice)
110
+ .then((synthesizedText) => {
111
+ results[index] = synthesizedText;
112
+ })
113
+ .catch((error) => {
114
+ stream.emit("error", error);
115
+ });
116
+ });
117
+ return { ref, stream };
118
+ });
119
+ }
120
+ doSynthesize(text, voice) {
121
+ return __awaiter(this, void 0, void 0, function* () {
122
+ const response = yield this.client.generate({
69
123
  stream: true,
70
124
  voice,
71
125
  text,
72
- // TODO: This should be configurable
73
126
  model_id: "eleven_turbo_v2_5",
74
127
  output_format: "pcm_16000"
75
128
  });
76
- const ref = this.createMediaReference();
77
- audioStream.on("error", (error) => {
78
- logger.error(`Error reading file: ${error.message}`);
79
- });
80
- return { ref, stream: audioStream };
129
+ return (yield (0, streamToBuffer_1.streamToBuffer)(response));
81
130
  });
82
131
  }
83
132
  static getConfigValidationSchema() {
84
- return z.object({
85
- voice: z.nativeEnum(common_1.ElevenLabsVoice, {
86
- message: "Invalid ElevenLabs voice."
87
- })
88
- });
133
+ return z.object({});
89
134
  }
90
135
  static getCredentialsValidationSchema() {
91
136
  return z.object({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fonoster/apiserver",
3
- "version": "0.7.36",
3
+ "version": "0.7.37",
4
4
  "description": "APIServer for Fonoster",
5
5
  "author": "Pedro Sanders <psanders@fonoster.com>",
6
6
  "homepage": "https://github.com/fonoster/fonoster#readme",
@@ -21,12 +21,12 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@deepgram/sdk": "^3.5.1",
24
- "@fonoster/common": "^0.7.36",
25
- "@fonoster/identity": "^0.7.36",
26
- "@fonoster/logger": "^0.7.36",
27
- "@fonoster/sipnet": "^0.7.36",
28
- "@fonoster/streams": "^0.7.36",
29
- "@fonoster/types": "^0.7.36",
24
+ "@fonoster/common": "^0.7.37",
25
+ "@fonoster/identity": "^0.7.37",
26
+ "@fonoster/logger": "^0.7.37",
27
+ "@fonoster/sipnet": "^0.7.37",
28
+ "@fonoster/streams": "^0.7.37",
29
+ "@fonoster/types": "^0.7.37",
30
30
  "@google-cloud/speech": "^6.6.0",
31
31
  "@google-cloud/text-to-speech": "^5.3.0",
32
32
  "@grpc/grpc-js": "~1.10.6",
@@ -72,5 +72,5 @@
72
72
  "@types/uuid": "^9.0.8",
73
73
  "@types/validator": "^13.12.0"
74
74
  },
75
- "gitHead": "616656596fe60b32caccad2e63f628fb35b139d0"
75
+ "gitHead": "b9696f512b3abfd6a798045b16ed9f102d58b11a"
76
76
  }