Skip to content

Commit 368103e

Browse files
committed
Splits audio program into ALSA and file input
Bug: 78773302 Change-Id: Ia96a8dac944d547520b9f466659726b8c7a6a834
1 parent 50d0980 commit 368103e

File tree

7 files changed

+317
-57
lines changed

7 files changed

+317
-57
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ credentials.json
88
# Do not commit binaries
99
googleapis.ar
1010
run_assistant_audio
11+
run_assistant_file
1112
run_assistant_text
1213
# Generated files
1314
src/embedded_assistant*

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,16 @@ all: run_assistant
8787
googleapis.ar: $(GOOGLEAPIS_CCS:.cc=.o)
8888
$(AR) r $@ $?
8989

90-
run_assistant: run_assistant_audio run_assistant_text
90+
run_assistant: run_assistant_audio run_assistant_file run_assistant_text
9191

9292
run_assistant_audio: $(GOOGLEAPIS_ASSISTANT_CCS:.cc=.o) googleapis.ar \
9393
$(ASSISTANT_AUDIO_O)
9494
$(CXX) $^ $(LDFLAGS) -o $@
9595

96+
run_assistant_file: $(GOOGLEAPIS_ASSISTANT_CCS:.cc=.o) googleapis.ar \
97+
$(AUDIO_SRCS:.cc=.o) ./src/audio_input_file.o ./src/json_util.o ./src/run_assistant_file.o
98+
$(CXX) $^ $(LDFLAGS) -o $@
99+
96100
run_assistant_text: $(GOOGLEAPIS_ASSISTANT_CCS:.cc=.o) googleapis.ar \
97101
$(ASSISTANT_TEXT_O)
98102
$(CXX) $^ $(LDFLAGS) -o $@
@@ -112,7 +116,7 @@ protobufs: $(GOOGLEAPIS_ASSISTANT_CCS:.cc=.h) $(GOOGLEAPIS_ASSISTANT_CCS)
112116

113117
.PHONY: clean
114118
clean:
115-
rm -f run_assistant_audio run_assistant_text googleapis.ar \
119+
rm -f run_assistant_text run_assistant_audio run_assistant_file googleapis.ar \
116120
$(GOOGLEAPIS_CCS:.cc=.o) \
117121
$(GOOGLEAPIS_ASSISTANT_CCS) $(GOOGLEAPIS_ASSISTANT_CCS:.cc=.h) \
118122
$(GOOGLEAPIS_ASSISTANT_CCS:.cc=.o) \

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,16 @@ make run_assistant
8282
* Move it in this folder and rename it to `client_secret.json`
8383
* run `get_credentials.sh` in this folder. It will create the file `credentials.json`.
8484

85-
7. Start one of the `run_assistant` samples
85+
7. Start one of the `run_assistant` samples:
8686

8787
```bash
88-
./run_assistant_audio --audio_input ./resources/weather_in_mountain_view.raw --credentials_file ./credentials.json
88+
./run_assistant_file --input ./resources/weather_in_mountain_view.raw --credentials_file ./credentials.json
8989
```
9090

9191
On a Linux workstation, you can alternatively use ALSA for audio input:
9292

9393
```bash
94-
./run_assistant_audio --audio_input ALSA_INPUT --credentials_file ./credentials.json
94+
./run_assistant_audio --credentials_file ./credentials.json
9595
```
9696

9797
You can use a text-based query instead of audio. This allows you to continually enter text queries to the Assistant.

src/run_assistant_audio.cc

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ std::shared_ptr<Channel> CreateChannel(const std::string& host) {
8787

8888
void 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

9897
bool 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

152147
int 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

Comments
 (0)