@fonoster/apiserver 0.15.20 → 0.16.0

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 (38) hide show
  1. package/dist/applications/createGetFnUtil.d.ts +3 -3
  2. package/dist/core/httpBridge.js +0 -26
  3. package/dist/voice/VoiceDispatcher.js +1 -0
  4. package/dist/voice/client/AudioSocketHandler.d.ts +35 -0
  5. package/dist/voice/client/AudioSocketHandler.js +68 -0
  6. package/dist/voice/client/AuthorizationHandler.d.ts +13 -0
  7. package/dist/voice/client/AuthorizationHandler.js +78 -0
  8. package/dist/voice/client/ExternalMediaHandler.d.ts +32 -0
  9. package/dist/voice/client/ExternalMediaHandler.js +59 -0
  10. package/dist/voice/client/GrpcClientHandler.d.ts +36 -0
  11. package/dist/voice/client/GrpcClientHandler.js +101 -0
  12. package/dist/voice/client/SpeechHandler.d.ts +60 -0
  13. package/dist/voice/client/SpeechHandler.js +116 -0
  14. package/dist/voice/{VoiceClientImpl.d.ts → client/VoiceClientImpl.d.ts} +17 -17
  15. package/dist/voice/client/VoiceClientImpl.js +158 -0
  16. package/dist/voice/client/index.d.ts +24 -0
  17. package/dist/voice/client/index.js +40 -0
  18. package/dist/voice/connectToAri.js +1 -1
  19. package/dist/voice/createCreateVoiceClient.d.ts +1 -1
  20. package/dist/voice/createCreateVoiceClient.js +3 -3
  21. package/dist/voice/handlers/createSayHandler.js +2 -15
  22. package/dist/voice/handlers/createStopSayHandler.d.ts +3 -0
  23. package/dist/voice/handlers/createStopSayHandler.js +34 -0
  24. package/dist/voice/handlers/index.d.ts +1 -0
  25. package/dist/voice/handlers/index.js +1 -0
  26. package/dist/voice/tts/Azure.js +1 -1
  27. package/dist/voice/tts/Deepgram.d.ts +1 -1
  28. package/dist/voice/tts/Deepgram.js +1 -1
  29. package/dist/voice/tts/ElevenLabs.js +45 -13
  30. package/dist/voice/tts/Google.d.ts +1 -1
  31. package/dist/voice/tts/Google.js +1 -1
  32. package/dist/voice/tts/utils/convertUlawToPCM16.d.ts +26 -0
  33. package/dist/voice/tts/utils/convertUlawToPCM16.js +40 -0
  34. package/dist/voice/tts/utils/index.d.ts +23 -0
  35. package/dist/voice/tts/utils/index.js +39 -0
  36. package/dist/voice/types/voice.d.ts +1 -0
  37. package/package.json +11 -10
  38. package/dist/voice/VoiceClientImpl.js +0 -281
@@ -17,37 +17,34 @@
17
17
  * limitations under the License.
18
18
  */
19
19
  import { Stream } from "stream";
20
- import { SayOptions, VoiceClientConfig, VoiceIn, VoiceSessionStreamClient } from "@fonoster/common";
21
- import { AudioSocket } from "@fonoster/streams";
20
+ import { SayOptions, VoiceClientConfig, VoiceIn } from "@fonoster/common";
22
21
  import { Bridge, Client } from "ari-client";
23
- import { SpeechResult } from "./stt/types";
24
- import { GRPCClient, SpeechToText, TextToSpeech, VoiceClient } from "./types";
22
+ import { SpeechResult } from "../stt/types";
23
+ import { SpeechToText, TextToSpeech, VoiceClient } from "../types";
25
24
  declare class VoiceClientImpl implements VoiceClient {
26
25
  config: VoiceClientConfig;
27
26
  verbsStream: Stream;
28
27
  transcriptionsStream: Stream;
29
- voice: VoiceSessionStreamClient;
30
- tts: TextToSpeech;
31
- stt: SpeechToText;
32
- grpcClient: GRPCClient;
33
- audioSocket: AudioSocket;
34
- asStream: Stream;
35
28
  ari: Client;
36
29
  bridge: Bridge;
37
- filesServer: any;
30
+ private authHandler;
31
+ private audioSocketHandler;
32
+ private externalMediaHandler;
33
+ private grpcHandler;
34
+ private speechHandler;
35
+ private _tts;
36
+ private _stt;
38
37
  constructor(params: {
39
38
  ari: Client;
40
39
  config: VoiceClientConfig;
41
40
  tts: TextToSpeech;
42
41
  stt: SpeechToText;
43
- }, filesServer: any);
42
+ });
44
43
  connect(): Promise<void>;
45
- setupAudioSocket(port: number): void;
46
- on(type: string, callback: (data: VoiceIn) => void): void;
47
44
  sendResponse(response: VoiceIn): void;
48
- getTranscriptionsStream(): Stream;
49
- setupExternalMedia(port: number): Promise<void>;
45
+ on(type: string, callback: (data: VoiceIn) => void): void;
50
46
  synthesize(text: string, options: SayOptions): Promise<string>;
47
+ stopSynthesis(): Promise<void>;
51
48
  transcribe(): Promise<SpeechResult>;
52
49
  startSpeechGather(callback: (stream: {
53
50
  speech: string;
@@ -56,7 +53,6 @@ declare class VoiceClientImpl implements VoiceClient {
56
53
  startDtmfGather(sessionRef: string, callback: (event: {
57
54
  digit: string;
58
55
  }) => void): Promise<void>;
59
- stopStreamGather(): Promise<void>;
60
56
  waitForDtmf(params: {
61
57
  sessionRef: string;
62
58
  finishOnKey: string;
@@ -66,6 +62,10 @@ declare class VoiceClientImpl implements VoiceClient {
66
62
  }): Promise<{
67
63
  digits: string;
68
64
  }>;
65
+ stopStreamGather(): void;
66
+ getTranscriptionsStream(): Stream;
69
67
  close(): void;
68
+ get tts(): TextToSpeech;
69
+ get stt(): SpeechToText;
70
70
  }
71
71
  export { VoiceClientImpl };
@@ -0,0 +1,158 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.VoiceClientImpl = void 0;
13
+ /**
14
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
15
+ * http://github.com/fonoster/fonoster
16
+ *
17
+ * This file is part of Fonoster
18
+ *
19
+ * Licensed under the MIT License (the "License");
20
+ * you may not use this file except in compliance with
21
+ * the License. You may obtain a copy of the License at
22
+ *
23
+ * https://opensource.org/licenses/MIT
24
+ *
25
+ * Unless required by applicable law or agreed to in writing, software
26
+ * distributed under the License is distributed on an "AS IS" BASIS,
27
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28
+ * See the License for the specific language governing permissions and
29
+ * limitations under the License.
30
+ */
31
+ const stream_1 = require("stream");
32
+ const logger_1 = require("@fonoster/logger");
33
+ const pick_port_1 = require("pick-port");
34
+ const AudioSocketHandler_1 = require("./AudioSocketHandler");
35
+ const AuthorizationHandler_1 = require("./AuthorizationHandler");
36
+ const ExternalMediaHandler_1 = require("./ExternalMediaHandler");
37
+ const GrpcClientHandler_1 = require("./GrpcClientHandler");
38
+ const SpeechHandler_1 = require("./SpeechHandler");
39
+ const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filename });
40
+ class VoiceClientImpl {
41
+ constructor(params) {
42
+ const { config, tts, stt, ari } = params;
43
+ this.config = config;
44
+ this.ari = ari;
45
+ this._tts = tts;
46
+ this._stt = stt;
47
+ this.verbsStream = new stream_1.Stream();
48
+ this.transcriptionsStream = new stream_1.Stream();
49
+ // Initialize component handlers
50
+ this.authHandler = new AuthorizationHandler_1.AuthorizationHandler({
51
+ config: this.config,
52
+ ari: this.ari
53
+ });
54
+ this.audioSocketHandler = new AudioSocketHandler_1.AudioSocketHandler({
55
+ transcriptionsStream: this.transcriptionsStream,
56
+ config: this.config
57
+ });
58
+ this.externalMediaHandler = new ExternalMediaHandler_1.ExternalMediaHandler({
59
+ ari: this.ari,
60
+ config: this.config
61
+ });
62
+ this.grpcHandler = new GrpcClientHandler_1.GrpcClientHandler({
63
+ config: this.config,
64
+ verbsStream: this.verbsStream
65
+ });
66
+ }
67
+ connect() {
68
+ return __awaiter(this, void 0, void 0, function* () {
69
+ // Check authorization
70
+ const isAuthorized = yield this.authHandler.checkAuthorization();
71
+ if (!isAuthorized) {
72
+ return;
73
+ }
74
+ // Set up GRPC client
75
+ yield this.grpcHandler.setupGrpcClient();
76
+ // Set up audio socket and external media
77
+ const externalMediaPort = yield (0, pick_port_1.pickPort)({ type: "tcp" });
78
+ logger.verbose("picked external media port", { port: externalMediaPort });
79
+ // Wait for both audio socket and external media setup to complete
80
+ yield Promise.all([
81
+ this.audioSocketHandler.setupAudioSocket(externalMediaPort),
82
+ this.externalMediaHandler.setupExternalMedia(externalMediaPort)
83
+ ]);
84
+ // Get the bridge from the external media handler
85
+ this.bridge = this.externalMediaHandler.getBridge();
86
+ // Initialize speech handler now that we have the audio stream
87
+ this.speechHandler = new SpeechHandler_1.SpeechHandler({
88
+ tts: this._tts,
89
+ stt: this._stt,
90
+ ari: this.ari,
91
+ transcriptionsStream: this.transcriptionsStream,
92
+ audioStream: this.audioSocketHandler.getAudioStream(),
93
+ sessionRef: this.config.sessionRef
94
+ });
95
+ logger.verbose("voice client setup completed");
96
+ });
97
+ }
98
+ // Public API methods required by VoiceClient interface
99
+ sendResponse(response) {
100
+ this.grpcHandler.sendResponse(response);
101
+ }
102
+ on(type, callback) {
103
+ this.verbsStream.on(type.toString(), (data) => {
104
+ callback(data[type]);
105
+ });
106
+ }
107
+ synthesize(text, options) {
108
+ return __awaiter(this, void 0, void 0, function* () {
109
+ return this.speechHandler.synthesize(text, options);
110
+ });
111
+ }
112
+ stopSynthesis() {
113
+ return __awaiter(this, void 0, void 0, function* () {
114
+ return this.speechHandler.stopSynthesis();
115
+ });
116
+ }
117
+ transcribe() {
118
+ return __awaiter(this, void 0, void 0, function* () {
119
+ return this.speechHandler.transcribe();
120
+ });
121
+ }
122
+ startSpeechGather(callback) {
123
+ this.speechHandler.startSpeechGather(callback);
124
+ }
125
+ startDtmfGather(sessionRef, callback) {
126
+ return __awaiter(this, void 0, void 0, function* () {
127
+ return this.speechHandler.startDtmfGather(callback);
128
+ });
129
+ }
130
+ waitForDtmf(params) {
131
+ return __awaiter(this, void 0, void 0, function* () {
132
+ const { finishOnKey, maxDigits, timeout, onDigitReceived } = params;
133
+ return this.speechHandler.waitForDtmf({
134
+ finishOnKey,
135
+ maxDigits,
136
+ timeout,
137
+ onDigitReceived
138
+ });
139
+ });
140
+ }
141
+ stopStreamGather() {
142
+ this.speechHandler.stopStreamGather();
143
+ }
144
+ getTranscriptionsStream() {
145
+ return this.transcriptionsStream;
146
+ }
147
+ close() {
148
+ this.grpcHandler.close();
149
+ this.audioSocketHandler.close();
150
+ }
151
+ get tts() {
152
+ return this._tts;
153
+ }
154
+ get stt() {
155
+ return this._stt;
156
+ }
157
+ }
158
+ exports.VoiceClientImpl = VoiceClientImpl;
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
3
+ * http://github.com/fonoster/fonoster
4
+ *
5
+ * This file is part of Fonoster
6
+ *
7
+ * Licensed under the MIT License (the "License");
8
+ * you may not use this file except in compliance with
9
+ * the License. You may obtain a copy of the License at
10
+ *
11
+ * https://opensource.org/licenses/MIT
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+ export * from "./VoiceClientImpl";
20
+ export * from "./AudioSocketHandler";
21
+ export * from "./ExternalMediaHandler";
22
+ export * from "./AuthorizationHandler";
23
+ export * from "./GrpcClientHandler";
24
+ export * from "./SpeechHandler";
@@ -0,0 +1,40 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ /**
18
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
19
+ * http://github.com/fonoster/fonoster
20
+ *
21
+ * This file is part of Fonoster
22
+ *
23
+ * Licensed under the MIT License (the "License");
24
+ * you may not use this file except in compliance with
25
+ * the License. You may obtain a copy of the License at
26
+ *
27
+ * https://opensource.org/licenses/MIT
28
+ *
29
+ * Unless required by applicable law or agreed to in writing, software
30
+ * distributed under the License is distributed on an "AS IS" BASIS,
31
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32
+ * See the License for the specific language governing permissions and
33
+ * limitations under the License.
34
+ */
35
+ __exportStar(require("./VoiceClientImpl"), exports);
36
+ __exportStar(require("./AudioSocketHandler"), exports);
37
+ __exportStar(require("./ExternalMediaHandler"), exports);
38
+ __exportStar(require("./AuthorizationHandler"), exports);
39
+ __exportStar(require("./GrpcClientHandler"), exports);
40
+ __exportStar(require("./SpeechHandler"), exports);
@@ -64,7 +64,7 @@ function connectToAri(filesServer) {
64
64
  logger.info("asterisk is ready");
65
65
  const createContainer = (0, integrations_1.createCreateContainer)(db_1.prisma, envs_1.INTEGRATIONS_FILE);
66
66
  const nats = yield (0, nats_1.connect)({ servers: envs_1.NATS_URL, maxReconnectAttempts: -1 });
67
- const dispatcher = new VoiceDispatcher_1.VoiceDispatcher(ari, nats, (0, createCreateVoiceClient_1.createCreateVoiceClient)(createContainer, filesServer));
67
+ const dispatcher = new VoiceDispatcher_1.VoiceDispatcher(ari, nats, (0, createCreateVoiceClient_1.createCreateVoiceClient)(createContainer));
68
68
  dispatcher.start();
69
69
  }
70
70
  else {
@@ -1,7 +1,7 @@
1
1
  import { Channel, Client, StasisStart } from "ari-client";
2
2
  import { CreateContainer } from "./integrations/types";
3
3
  import { VoiceClient } from "./types";
4
- declare function createCreateVoiceClient(createContainer: CreateContainer, filesServer: any): (params: {
4
+ declare function createCreateVoiceClient(createContainer: CreateContainer): (params: {
5
5
  ari: Client;
6
6
  event: StasisStart;
7
7
  channel: Channel;
@@ -32,13 +32,13 @@ const identity_1 = require("@fonoster/identity");
32
32
  const logger_1 = require("@fonoster/logger");
33
33
  const identityConfig_1 = require("../core/identityConfig");
34
34
  const mapCallDirectionToEnum_1 = require("../events/mapCallDirectionToEnum");
35
+ const client_1 = require("./client");
35
36
  const types_1 = require("./types");
36
37
  const createGetChannelVarWithoutThrow_1 = require("./utils/createGetChannelVarWithoutThrow");
37
- const VoiceClientImpl_1 = require("./VoiceClientImpl");
38
38
  const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filename });
39
39
  const generateCallAccessToken = (0, identity_1.createGenerateCallAccessToken)(identityConfig_1.identityConfig);
40
40
  // Note: By the time the call arrives here the owner of the app MUST be authenticated
41
- function createCreateVoiceClient(createContainer, filesServer) {
41
+ function createCreateVoiceClient(createContainer) {
42
42
  return function createVoiceClient(params) {
43
43
  return __awaiter(this, void 0, void 0, function* () {
44
44
  var _a, _b, _c, _d, _e;
@@ -70,7 +70,7 @@ function createCreateVoiceClient(createContainer, filesServer) {
70
70
  callerNumber,
71
71
  ingressNumber
72
72
  });
73
- return new VoiceClientImpl_1.VoiceClientImpl({ ari, config, tts, stt }, filesServer);
73
+ return new client_1.VoiceClientImpl({ ari, config, tts, stt });
74
74
  });
75
75
  };
76
76
  }
@@ -10,34 +10,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.createSayHandler = createSayHandler;
13
- const nanoid_1 = require("nanoid");
14
13
  const pb_util_1 = require("pb-util");
15
14
  const zod_1 = require("zod");
16
- const envs_1 = require("../../envs");
17
- const awaitForPlaybackFinished_1 = require("./utils/awaitForPlaybackFinished");
18
15
  const withErrorHandling_1 = require("./utils/withErrorHandling");
19
16
  const sayRequestSchema = zod_1.z.object({
20
17
  text: zod_1.z.string(),
21
18
  sessionRef: zod_1.z.string(),
22
- playbackRef: zod_1.z.string().optional(),
23
19
  options: zod_1.z.record(zod_1.z.unknown()).optional()
24
20
  });
25
- const getMediaUrl = (filename) => `sound:http://${envs_1.APISERVER_HOST}:${envs_1.HTTP_BRIDGE_PORT}/api/sounds/${filename}.sln16`;
26
21
  function createSayHandler(ari, voiceClient) {
27
22
  return (0, withErrorHandling_1.withErrorHandling)((request) => __awaiter(this, void 0, void 0, function* () {
28
- const { sessionRef: channelId } = request;
29
23
  sayRequestSchema.parse(request);
30
- const playbackRef = request.playbackRef || (0, nanoid_1.nanoid)(10);
31
- const mediaId = yield voiceClient.synthesize(request.text, request.options ? pb_util_1.struct.decode(request.options) : {});
32
- yield ari.channels.play({
33
- channelId,
34
- media: getMediaUrl(mediaId),
35
- playbackId: playbackRef
36
- });
37
- yield (0, awaitForPlaybackFinished_1.awaitForPlaybackFinished)(ari, playbackRef);
24
+ yield voiceClient.synthesize(request.text, request.options ? pb_util_1.struct.decode(request.options) : {});
38
25
  voiceClient.sendResponse({
39
26
  sayResponse: {
40
- playbackRef
27
+ sessionRef: request.sessionRef
41
28
  }
42
29
  });
43
30
  }));
@@ -0,0 +1,3 @@
1
+ import { VoiceClient } from "../types";
2
+ declare function createStopSayHandler(voiceClient: VoiceClient): (request: import("@fonoster/common").VerbRequest) => Promise<void>;
3
+ export { createStopSayHandler };
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.createStopSayHandler = createStopSayHandler;
13
+ const zod_1 = require("zod");
14
+ const withErrorHandling_1 = require("./utils/withErrorHandling");
15
+ const requestSchema = zod_1.z.object({
16
+ sessionRef: zod_1.z.string()
17
+ });
18
+ function createStopSayHandler(voiceClient) {
19
+ return (0, withErrorHandling_1.withErrorHandling)((stopSayReq) => __awaiter(this, void 0, void 0, function* () {
20
+ requestSchema.parse(stopSayReq);
21
+ const { sessionRef } = stopSayReq;
22
+ try {
23
+ voiceClient.stopSynthesis();
24
+ }
25
+ catch (err) {
26
+ // We can only try
27
+ }
28
+ voiceClient.sendResponse({
29
+ stopSayResponse: {
30
+ sessionRef
31
+ }
32
+ });
33
+ }));
34
+ }
@@ -29,3 +29,4 @@ export * from "./createStreamGatherHandler";
29
29
  export * from "./createUnmuteHandler";
30
30
  export * from "./dial/createDialHandler";
31
31
  export * from "./gather/createGatherHandler";
32
+ export * from "./createStopSayHandler";
@@ -45,3 +45,4 @@ __exportStar(require("./createStreamGatherHandler"), exports);
45
45
  __exportStar(require("./createUnmuteHandler"), exports);
46
46
  __exportStar(require("./dial/createDialHandler"), exports);
47
47
  __exportStar(require("./gather/createGatherHandler"), exports);
48
+ __exportStar(require("./createStopSayHandler"), exports);
@@ -67,7 +67,7 @@ class Azure extends AbstractTextToSpeech_1.AbstractTextToSpeech {
67
67
  const speechConfig = sdk.SpeechConfig.fromSubscription(subscriptionKey, serviceRegion);
68
68
  speechConfig.speechSynthesisVoiceName = voice;
69
69
  speechConfig.speechSynthesisOutputFormat =
70
- sdk.SpeechSynthesisOutputFormat.Riff16Khz16BitMonoPcm;
70
+ sdk.SpeechSynthesisOutputFormat.Raw8Khz16BitMonoPcm;
71
71
  const stream = (0, createChunkedSynthesisStream_1.createChunkedSynthesisStream)(text, (chunkText) => __awaiter(this, void 0, void 0, function* () {
72
72
  const synthesizer = new sdk.SpeechSynthesizer(speechConfig);
73
73
  const isSSML = (0, isSsml_1.isSsml)(chunkText);
@@ -29,7 +29,7 @@ declare class Deepgram extends AbstractTextToSpeech<typeof ENGINE_NAME> {
29
29
  protected readonly OUTPUT_FORMAT = "sln16";
30
30
  protected readonly CACHING_FIELDS: string[];
31
31
  protected readonly AUDIO_ENCODING: "linear16";
32
- protected readonly SAMPLE_RATE_HERTZ = 16000;
32
+ protected readonly SAMPLE_RATE_HERTZ = 8000;
33
33
  constructor(config: DeepgramTtsConfig);
34
34
  synthesize(text: string, options: SynthOptions): {
35
35
  ref: string;
@@ -58,7 +58,7 @@ class Deepgram extends AbstractTextToSpeech_1.AbstractTextToSpeech {
58
58
  this.OUTPUT_FORMAT = "sln16";
59
59
  this.CACHING_FIELDS = ["voice"];
60
60
  this.AUDIO_ENCODING = "linear16";
61
- this.SAMPLE_RATE_HERTZ = 16000;
61
+ this.SAMPLE_RATE_HERTZ = 8000;
62
62
  this.client = (0, sdk_1.createClient)(config.credentials.apiKey);
63
63
  this.engineConfig = config;
64
64
  }
@@ -43,18 +43,40 @@ 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
+ /**
47
+ * Copyright (C) 2025 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");
65
+ const logger_1 = require("@fonoster/logger");
46
66
  const elevenlabs_1 = require("elevenlabs");
47
67
  const z = __importStar(require("zod"));
48
68
  const AbstractTextToSpeech_1 = require("./AbstractTextToSpeech");
69
+ const convertUlawToPCM16_1 = require("./utils/convertUlawToPCM16");
49
70
  const createChunkedSynthesisStream_1 = require("./utils/createChunkedSynthesisStream");
50
71
  const streamToBuffer_1 = require("./utils/streamToBuffer");
72
+ const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filename });
51
73
  const ENGINE_NAME = "tts.elevenlabs";
52
74
  exports.ENGINE_NAME = ENGINE_NAME;
53
75
  class ElevenLabs extends AbstractTextToSpeech_1.AbstractTextToSpeech {
54
76
  constructor(config) {
55
77
  super();
56
78
  this.engineName = ENGINE_NAME;
57
- this.OUTPUT_FORMAT = "sln16";
79
+ this.OUTPUT_FORMAT = "sln16"; // TODO: Ask the team at ElevenLabs to provde PCM 16-bit at 8kHz
58
80
  this.CACHING_FIELDS = ["voice", "text"];
59
81
  this.client = new elevenlabs_1.ElevenLabsClient(config.credentials);
60
82
  this.engineConfig = config;
@@ -64,18 +86,28 @@ class ElevenLabs extends AbstractTextToSpeech_1.AbstractTextToSpeech {
64
86
  const { voice, model } = this.engineConfig.config;
65
87
  const ref = this.createMediaReference();
66
88
  const stream = (0, createChunkedSynthesisStream_1.createChunkedSynthesisStream)(text, (chunkText) => __awaiter(this, void 0, void 0, function* () {
67
- const response = yield this.client.generate({
68
- stream: true,
69
- voice,
70
- text: chunkText,
71
- model_id: model !== null && model !== void 0 ? model : "eleven_flash_v2_5",
72
- output_format: "pcm_16000",
73
- // TODO: Make this configurable
74
- optimize_streaming_latency: 2
75
- }, {
76
- maxRetries: 3
77
- });
78
- return (yield (0, streamToBuffer_1.streamToBuffer)(response));
89
+ try {
90
+ const response = yield this.client.generate({
91
+ stream: true,
92
+ voice,
93
+ text: chunkText,
94
+ model_id: model !== null && model !== void 0 ? model : "eleven_flash_v2_5",
95
+ output_format: "ulaw_8000",
96
+ // TODO: Make this configurable
97
+ optimize_streaming_latency: 2
98
+ }, {
99
+ maxRetries: 3
100
+ });
101
+ const ulawBuffer = yield (0, streamToBuffer_1.streamToBuffer)(response);
102
+ const pcmBuffer = yield (0, convertUlawToPCM16_1.convertUlawToPCM16)(stream_1.Readable.from(ulawBuffer));
103
+ return pcmBuffer;
104
+ }
105
+ catch (error) {
106
+ logger.error(`error in ElevenLabs synthesis: ${error.message}`, {
107
+ stack: error.stack
108
+ });
109
+ throw error;
110
+ }
79
111
  }));
80
112
  return { ref, stream };
81
113
  }
@@ -29,7 +29,7 @@ declare class Google extends AbstractTextToSpeech<typeof ENGINE_NAME> {
29
29
  protected readonly OUTPUT_FORMAT = "sln16";
30
30
  protected readonly CACHING_FIELDS: string[];
31
31
  protected readonly AUDIO_ENCODING: "LINEAR16";
32
- protected readonly SAMPLE_RATE_HERTZ = 16000;
32
+ protected readonly SAMPLE_RATE_HERTZ = 8000;
33
33
  constructor(config: GoogleTtsConfig);
34
34
  synthesize(text: string, options: SynthOptions): {
35
35
  ref: string;
@@ -58,7 +58,7 @@ class Google extends AbstractTextToSpeech_1.AbstractTextToSpeech {
58
58
  this.OUTPUT_FORMAT = "sln16";
59
59
  this.CACHING_FIELDS = ["voice"];
60
60
  this.AUDIO_ENCODING = "LINEAR16";
61
- this.SAMPLE_RATE_HERTZ = 16000;
61
+ this.SAMPLE_RATE_HERTZ = 8000;
62
62
  this.client = new text_to_speech_1.TextToSpeechClient(config);
63
63
  this.engineConfig = config;
64
64
  }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
3
+ * http://github.com/fonoster/fonoster
4
+ *
5
+ * This file is part of Fonoster
6
+ *
7
+ * Licensed under the MIT License (the "License");
8
+ * you may not use this file except in compliance with
9
+ * the License. You may obtain a copy of the License at
10
+ *
11
+ * https://opensource.org/licenses/MIT
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+ import { Readable } from "stream";
20
+ /**
21
+ * Converts a ulaw stream to PCM 16-bit at 8kHz
22
+ *
23
+ * @param readableStream - The input ulaw audio stream to convert
24
+ * @returns A Promise that resolves to a Buffer containing PCM 16-bit audio data at 8kHz
25
+ */
26
+ export declare function convertUlawToPCM16(readableStream: Readable): Promise<Buffer>;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.convertUlawToPCM16 = convertUlawToPCM16;
13
+ const logger_1 = require("@fonoster/logger");
14
+ const wavefile_1 = require("wavefile");
15
+ const streamToBuffer_1 = require("./streamToBuffer");
16
+ const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filename });
17
+ /**
18
+ * Converts a ulaw stream to PCM 16-bit at 8kHz
19
+ *
20
+ * @param readableStream - The input ulaw audio stream to convert
21
+ * @returns A Promise that resolves to a Buffer containing PCM 16-bit audio data at 8kHz
22
+ */
23
+ function convertUlawToPCM16(readableStream) {
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ try {
26
+ const inputBuffer = yield (0, streamToBuffer_1.streamToBuffer)(readableStream);
27
+ const wav = new wavefile_1.WaveFile();
28
+ wav.fromScratch(1, 8000, "8m", inputBuffer);
29
+ wav.fromMuLaw();
30
+ const waveBuffer = wav.toBuffer();
31
+ return Buffer.from(waveBuffer.buffer, waveBuffer.byteOffset + 44, waveBuffer.byteLength - 44);
32
+ }
33
+ catch (error) {
34
+ logger.error(`error converting audio format: ${error.message}`, {
35
+ stack: error.stack
36
+ });
37
+ throw new Error(`Audio conversion failed: ${error.message}`);
38
+ }
39
+ });
40
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
3
+ * http://github.com/fonoster/fonoster
4
+ *
5
+ * This file is part of Fonoster
6
+ *
7
+ * Licensed under the MIT License (the "License");
8
+ * you may not use this file except in compliance with
9
+ * the License. You may obtain a copy of the License at
10
+ *
11
+ * https://opensource.org/licenses/MIT
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+ export * from "./createChunkedSynthesisStream";
20
+ export * from "./createErrorStream";
21
+ export * from "./isSsml";
22
+ export * from "./streamToBuffer";
23
+ export * from "./convertUlawToPCM16";