Skip to content

Commit d52902e

Browse files
committed
Refactoring, EmblaRESTAPI->EmblaAPI + moved all speech synthesis functionality into api.dart
1 parent 83d6cb1 commit d52902e

File tree

6 files changed

+65
-97
lines changed

6 files changed

+65
-97
lines changed

example/pubspec.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ packages:
139139
dependency: transitive
140140
description:
141141
name: http
142-
sha256: "6aa2946395183537c8b880962d935877325d6a09a2867c3970c05c0fed6ac482"
142+
sha256: "5895291c13fa8a3bd82e76d5627f69e0d85ca6a30dcac95c4ea19a5d555879c2"
143143
url: "https://pub.dev"
144144
source: hosted
145-
version: "0.13.5"
145+
version: "0.13.6"
146146
http_parser:
147147
dependency: transitive
148148
description:
@@ -448,10 +448,10 @@ packages:
448448
dependency: transitive
449449
description:
450450
name: web_socket_channel
451-
sha256: ca49c0bc209c687b887f30527fb6a9d80040b072cc2990f34b9bec3e7663101b
451+
sha256: d88238e5eac9a42bb43ca4e721edba3c08c6354d4a53063afaa568516217621b
452452
url: "https://pub.dev"
453453
source: hosted
454-
version: "2.3.0"
454+
version: "2.4.0"
455455
win32:
456456
dependency: transitive
457457
description:

lib/embla_core.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ library embla_core;
2323

2424
export 'package:embla_core/src/session.dart' show EmblaSession, EmblaSessionState;
2525
export 'package:embla_core/src/config.dart' show EmblaSessionConfig;
26-
export 'package:embla_core/src/speech.dart' show EmblaSpeechSynthesizer;
27-
export 'package:embla_core/src/api.dart' show EmblaRESTAPI;
26+
export 'package:embla_core/src/api.dart' show EmblaAPI;
2827
export 'package:embla_core/src/audio.dart' show AudioPlayer;
2928
export 'package:embla_core/src/recorder.dart' show AudioRecorder;
3029
export 'package:embla_core/src/common.dart' show kEmblaCoreVersion;

lib/src/api.dart

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import 'package:http/http.dart' as http;
2727
import './common.dart';
2828

2929
/// Send JSON POST request to API server.
30-
Future<Response?> _makeRequest(String apiURL, String apiKey, Map<String, dynamic> qargs,
30+
Future<Response?> _makePOSTRequest(String apiURL, String apiKey, Map<String, dynamic> qargs,
3131
[void Function(Map? result)? handler]) async {
3232
dlog("Sending query POST request to $apiURL: ${qargs.toString()}");
3333
Response? response;
@@ -68,8 +68,8 @@ Future<Response?> _makeRequest(String apiURL, String apiKey, Map<String, dynamic
6868
return response;
6969
}
7070

71-
/// Static class wrapper for functions communicating with the Embla REST API.
72-
class EmblaRESTAPI {
71+
/// Static class wrapper for functions communicating directly with the Embla REST API.
72+
class EmblaAPI {
7373
/// Send request to clear query history for a given device ID.
7474
/// Boolean [allData] param determines whether all device-specific
7575
/// data or only query history should be deleted server-side.
@@ -83,6 +83,59 @@ class EmblaRESTAPI {
8383
};
8484

8585
final String apiURL = "$serverURL$kClearHistoryEndpoint";
86-
await _makeRequest(apiURL, apiKey, qargs, completionHandler);
86+
await _makePOSTRequest(apiURL, apiKey, qargs, completionHandler);
87+
}
88+
89+
/// Send request to speech synthesis API (static method).
90+
///
91+
/// [text] Text to be speech synthesized
92+
/// [apiKey] Server API key
93+
/// Returns the resulting audio file URL or null if an error occurred.
94+
static Future<String?> synthesize(String text, String? apiKey,
95+
{String voiceID = kDefaultSpeechSynthesisVoice,
96+
double voiceSpeed = kDefaultSpeechSynthesisSpeed,
97+
String apiURL = "$kDefaultServer$kSpeechSynthesisEndpoint"}) async {
98+
// Set URI
99+
Uri uri;
100+
try {
101+
uri = Uri.parse(apiURL);
102+
} on FormatException {
103+
dlog("Invalid URL specified for TTS synthesis.");
104+
return null;
105+
}
106+
// Set request headers
107+
Map<String, String> headers = {
108+
"X-API-Key": apiKey ?? "",
109+
"Content-Type": "application/json",
110+
"Accept": "application/json"
111+
};
112+
// Set request body
113+
String body = json.encode({
114+
'text': text,
115+
'options': {'voice_id': voiceID, 'voice_speed': voiceSpeed.toString()}
116+
});
117+
118+
dlog("Sending POST request to $apiURL: $body");
119+
return await http.post(uri, headers: headers, body: body).timeout(kRequestTimeout).then(
120+
(response) {
121+
dlog("Response status: ${response.statusCode}");
122+
dlog("Response body: ${response.body}");
123+
if (response.statusCode != 200) {
124+
dlog("Received invalid status code from TTS service.");
125+
return null;
126+
}
127+
dynamic arg = json.decode(response.body);
128+
if (arg is Map && arg.containsKey("audio_url")) {
129+
// Valid response body
130+
// Return the resulting audio file URL
131+
return arg["audio_url"];
132+
}
133+
dlog("Invalid response body from TTS service.");
134+
return null;
135+
}, onError: (e) {
136+
dlog("Error in speech synthesis: $e");
137+
// Errors during request, return null
138+
return null;
139+
});
87140
}
88141
}

lib/src/session.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ class EmblaSession {
200200
final String json = greetings.toJSON();
201201
dlog("Sending initial greetings message: $json");
202202
_channel?.sink.add(json);
203+
// Immediately start streaming audio
203204
await _startStreaming();
204205
} catch (e) {
205206
await _error("Error connecting to server: $e");

lib/src/speech.dart

Lines changed: 0 additions & 85 deletions
This file was deleted.

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ dependencies:
1919
logger: '^1.3.0'
2020
flutter_sound: '^9.2.13'
2121
audio_session: '^0.1.14'
22-
web_socket_channel: '^2.3.0'
23-
http: '^0.13.5'
22+
web_socket_channel: '^2.4.0'
23+
http: '^0.13.6'
2424
mp3_info: '^0.2.1'
2525

2626
dev_dependencies:

0 commit comments

Comments
 (0)