Skip to content

Commit f88a48e

Browse files
committed
Adds file output parameter to save audio data
Bug: 78773302 Change-Id: I62f414e6dd49c3756073d0b0b77d8fdcfb6179e5
1 parent d86f665 commit f88a48e

File tree

1 file changed

+41
-17
lines changed

1 file changed

+41
-17
lines changed

src/run_assistant_file.cc

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,28 @@ int main(int argc, char** argv) {
167167
AudioOutConfig::LINEAR16);
168168
assist_config->mutable_audio_out_config()->set_sample_rate_hertz(16000);
169169

170-
std::unique_ptr<AudioInput> audio_input;
171-
if (!audio_input_source.empty()) {
172-
// Set the AudioInConfig of the AssistRequest
173-
assist_config->mutable_audio_in_config()->set_encoding(
174-
AudioInConfig::LINEAR16);
175-
assist_config->mutable_audio_in_config()->set_sample_rate_hertz(16000);
176-
} else {
170+
if (audio_input_source.empty()) {
177171
std::cerr << "requires --input" << std::endl;
178-
return -1;
172+
return -2;
173+
}
174+
// Make sure the input file exists
175+
std::ifstream audio_input_file(audio_input_source);
176+
if (!audio_input_file) {
177+
std::cerr << "Audio input file \"" << audio_input_source
178+
<< "\" does not exist." << std::endl;
179+
return -2;
180+
}
181+
if (audio_output_source.empty()) {
182+
std::clog << "requires --output" << std::endl;
183+
return -3;
179184
}
180185

186+
std::unique_ptr<AudioInput> audio_input;
187+
// Set the AudioInConfig of the AssistRequest
188+
assist_config->mutable_audio_in_config()->set_encoding(
189+
AudioInConfig::LINEAR16);
190+
assist_config->mutable_audio_in_config()->set_sample_rate_hertz(16000);
191+
181192
// Read credentials file.
182193
std::ifstream credentials_file(credentials_file_path);
183194
if (!credentials_file) {
@@ -215,12 +226,6 @@ int main(int argc, char** argv) {
215226
}
216227
stream->Write(request);
217228

218-
std::ifstream audio_file(audio_input_source);
219-
if (!audio_file) {
220-
std::cerr << "Audio input file \"" << audio_input_source
221-
<< "\" does not exist." << std::endl;
222-
return -1;
223-
}
224229
audio_input.reset(new AudioInputFile(audio_input_source));
225230

226231
audio_input->AddDataListener(
@@ -238,6 +243,20 @@ int main(int argc, char** argv) {
238243
std::clog << "assistant_sdk waiting for response ... " << std::endl;
239244
}
240245
AssistResponse response;
246+
247+
// Create an audio file to store the response
248+
std::ofstream audio_output_file;
249+
// Make sure to rewrite contents of file
250+
audio_output_file.open(audio_output_source, std::ofstream::out | std::ofstream::trunc);
251+
// Check whether file was opened correctly
252+
if (audio_output_file.fail()) {
253+
std::cerr << "error opening file " << audio_output_source << std::endl;
254+
return -3;
255+
}
256+
if (verbose) {
257+
std::clog << "assistant_sdk writing audio response to " << audio_output_source << std::endl;
258+
}
259+
241260
while (stream->Read(&response)) { // Returns false when no more to read.
242261
if (response.has_audio_out() ||
243262
response.event_type() == AssistResponse_EventType_END_OF_UTTERANCE) {
@@ -246,6 +265,9 @@ int main(int argc, char** argv) {
246265
audio_input->Stop();
247266
}
248267
}
268+
if (response.has_audio_out()) {
269+
audio_output_file << response.audio_out().audio_data();
270+
}
249271
// CUSTOMIZE: render spoken request on screen
250272
for (int i = 0; i < response.speech_results_size(); i++) {
251273
google::assistant::embedded::v1alpha2::SpeechRecognitionResult result =
@@ -264,14 +286,16 @@ int main(int argc, char** argv) {
264286
<< std::endl;
265287
}
266288
}
289+
audio_input_file.close();
290+
audio_output_file.close();
267291

268292
grpc::Status status = stream->Finish();
269293
if (!status.ok()) {
270294
// Report the RPC failure.
271-
std::cerr << "assistant_sdk failed, error: "
272-
<< status.error_message() << std::endl;
295+
std::cerr << "assistant_sdk failed, error: " <<
296+
status.error_message() << std::endl;
273297
return -1;
274298
}
275299

276300
return 0;
277-
}
301+
}

0 commit comments

Comments
 (0)