@@ -87,7 +87,6 @@ std::shared_ptr<Channel> CreateChannel(const std::string& host) {
8787
8888void PrintUsage () {
8989 std::cerr << " Usage: ./run_assistant_audio "
90- << " --audio_input [<" << kALSAAudioInput << " >|<audio_file>] "
9190 << " --credentials_file <credentials_file> "
9291 << " [--credentials_type <" << kCredentialsTypeUserAccount << " >] "
9392 << " [--api_endpoint <API endpoint>] "
@@ -96,11 +95,10 @@ void PrintUsage() {
9695}
9796
9897bool GetCommandLineFlags (
99- int argc, char ** argv, std::string* audio_input, std::string* credentials_file_path,
98+ int argc, char ** argv, std::string* credentials_file_path,
10099 std::string* credentials_type, std::string* api_endpoint,
101100 std::string* locale) {
102101 const struct option long_options[] = {
103- {" audio_input" , required_argument, nullptr , ' a' },
104102 {" credentials_file" , required_argument, nullptr , ' f' },
105103 {" credentials_type" , required_argument, nullptr , ' c' },
106104 {" api_endpoint" , required_argument, nullptr , ' e' },
@@ -112,14 +110,11 @@ bool GetCommandLineFlags(
112110 while (true ) {
113111 int option_index;
114112 int option_char =
115- getopt_long (argc, argv, " a: f:c:e:l:v" , long_options, &option_index);
113+ getopt_long (argc, argv, " f:c:e:l:v" , long_options, &option_index);
116114 if (option_char == -1 ) {
117115 break ;
118116 }
119117 switch (option_char) {
120- case ' a' :
121- *audio_input = optarg;
122- break ;
123118 case ' f' :
124119 *credentials_file_path = optarg;
125120 break ;
@@ -150,12 +145,18 @@ bool GetCommandLineFlags(
150145}
151146
152147int main (int argc, char ** argv) {
153- std::string audio_input_source, credentials_file_path, credentials_type,
148+ std::string credentials_file_path, credentials_type,
154149 api_endpoint, locale;
150+ #ifndef ENABLE_ALSA
151+ std::cerr << " ALSA audio input is not supported on this platform."
152+ << std::endl;
153+ return -1 ;
154+ #endif
155+
155156 // Initialize gRPC and DNS resolvers
156157 // https://github.com/grpc/grpc/issues/11366#issuecomment-328595941
157158 grpc_init ();
158- if (!GetCommandLineFlags (argc, argv, &audio_input_source, & credentials_file_path,
159+ if (!GetCommandLineFlags (argc, argv, &credentials_file_path,
159160 &credentials_type, &api_endpoint, &locale)) {
160161 return -1 ;
161162 }
@@ -188,15 +189,10 @@ int main(int argc, char** argv) {
188189 assist_config->mutable_audio_out_config ()->set_sample_rate_hertz (16000 );
189190
190191 std::unique_ptr<AudioInput> audio_input;
191- if (!audio_input_source.empty ()) {
192- // Set the AudioInConfig of the AssistRequest
193- assist_config->mutable_audio_in_config ()->set_encoding (
194- AudioInConfig::LINEAR16);
195- assist_config->mutable_audio_in_config ()->set_sample_rate_hertz (16000 );
196- } else {
197- std::cerr << " requires --audio_input" << std::endl;
198- return -1 ;
199- }
192+ // Set the AudioInConfig of the AssistRequest
193+ assist_config->mutable_audio_in_config ()->set_encoding (
194+ AudioInConfig::LINEAR16);
195+ assist_config->mutable_audio_in_config ()->set_sample_rate_hertz (16000 );
200196
201197 // Read credentials file.
202198 std::ifstream credentials_file (credentials_file_path);
@@ -235,24 +231,7 @@ int main(int argc, char** argv) {
235231 }
236232 stream->Write (request);
237233
238- if (!audio_input_source.empty ()) {
239- if (audio_input_source == kALSAAudioInput ) {
240- #ifdef ENABLE_ALSA
241- audio_input.reset (new AudioInputALSA ());
242- #else
243- std::cerr << " ALSA audio input is not supported on this platform."
244- << std::endl;
245- return -1 ;
246- #endif
247- } else {
248- std::ifstream audio_file (audio_input_source);
249- if (!audio_file) {
250- std::cerr << " Audio input file \" " << audio_input_source
251- << " \" does not exist." << std::endl;
252- return -1 ;
253- }
254- audio_input.reset (new AudioInputFile (audio_input_source));
255- }
234+ audio_input.reset (new AudioInputALSA ());
256235
257236 audio_input->AddDataListener (
258237 [stream, &request](std::shared_ptr<std::vector<unsigned char >> data) {
@@ -263,12 +242,9 @@ int main(int argc, char** argv) {
263242 stream->WritesDone ();
264243 });
265244 audio_input->Start ();
266- }
267245
268- #ifdef ENABLE_ALSA
269246 AudioOutputALSA audio_output;
270247 audio_output.Start ();
271- #endif
272248
273249 // Read responses.
274250 if (verbose) {
@@ -285,14 +261,14 @@ int main(int argc, char** argv) {
285261 }
286262 if (response.has_audio_out ()) {
287263 // CUSTOMIZE: play back audio_out here.
288- # ifdef ENABLE_ALSA
264+
289265 std::shared_ptr<std::vector<unsigned char >>
290266 data (new std::vector<unsigned char >);
291267 data->resize (response.audio_out ().audio_data ().length ());
292268 memcpy (&((*data)[0 ]), response.audio_out ().audio_data ().c_str (),
293269 response.audio_out ().audio_data ().length ());
294270 audio_output.Send (data);
295- # endif
271+
296272 }
297273 // CUSTOMIZE: render spoken request on screen
298274 for (int i = 0 ; i < response.speech_results_size (); i++) {
@@ -313,9 +289,9 @@ int main(int argc, char** argv) {
313289 }
314290 }
315291
316- # ifdef ENABLE_ALSA
292+
317293 audio_output.Stop ();
318- # endif
294+
319295
320296 grpc::Status status = stream->Finish ();
321297 if (!status.ok ()) {
@@ -324,13 +300,6 @@ int main(int argc, char** argv) {
324300 status.error_message () << std::endl;
325301 return -1 ;
326302 }
327-
328- if (audio_input_source != kALSAAudioInput ) {
329- // A filepath was used as the audio input to this program
330- // If this is the case, then we can stop the program after
331- // one turn.
332- return 0 ;
333- }
334303 }
335304
336305 return 0 ;
0 commit comments