-
Notifications
You must be signed in to change notification settings - Fork 111
/
streaming_transcribe.cc
108 lines (104 loc) · 3.99 KB
/
streaming_transcribe.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <grpc++/grpc++.h>
#include <chrono>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <thread>
#include "google/cloud/speech/v1/cloud_speech.grpc.pb.h"
#include "parse_arguments.h"
using google::cloud::speech::v1::Speech;
using google::cloud::speech::v1::StreamingRecognizeRequest;
using google::cloud::speech::v1::StreamingRecognizeResponse;
// [START speech_streaming_recognize]
static const char kUsage[] =
"Usage:\n"
" streaming_transcribe "
"[--bitrate N] audio.(raw|ulaw|flac|amr|awb)\n";
// Write the audio in 64k chunks at a time, simulating audio content arriving
// from a microphone.
static void MicrophoneThreadMain(
grpc::ClientReaderWriterInterface<StreamingRecognizeRequest,
StreamingRecognizeResponse>* streamer,
char* file_path) {
StreamingRecognizeRequest request;
std::ifstream file_stream(file_path);
const size_t chunk_size = 64 * 1024;
std::vector<char> chunk(chunk_size);
while (true) {
// Read another chunk from the file.
std::streamsize bytes_read =
file_stream.rdbuf()->sgetn(&chunk[0], chunk.size());
// And write the chunk to the stream.
request.set_audio_content(&chunk[0], bytes_read);
std::cout << "Sending " << bytes_read / 1024 << "k bytes." << std::endl;
streamer->Write(request);
if (bytes_read < chunk.size()) {
// Done reading everything from the file, so done writing to the stream.
streamer->WritesDone();
break;
}
// Wait a second before writing the next chunk.
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
int main(int argc, char** argv) {
// Create a Speech Stub connected to the speech service.
auto creds = grpc::GoogleDefaultCredentials();
auto channel = grpc::CreateChannel("speech.googleapis.com", creds);
std::unique_ptr<Speech::Stub> speech(Speech::NewStub(channel));
// Parse command line arguments.
StreamingRecognizeRequest request;
auto* streaming_config = request.mutable_streaming_config();
char* file_path =
ParseArguments(argc, argv, streaming_config->mutable_config());
if (nullptr == file_path) {
std::cerr << kUsage;
return -1;
}
// Begin a stream.
grpc::ClientContext context;
auto streamer = speech->StreamingRecognize(&context);
// Write the first request, containing the config only.
streaming_config->set_interim_results(true);
streamer->Write(request);
// The microphone thread writes the audio content.
std::thread microphone_thread(&MicrophoneThreadMain, streamer.get(),
file_path);
// Read responses.
StreamingRecognizeResponse response;
while (streamer->Read(&response)) { // Returns false when no more to read.
// Dump the transcript of all the results.
for (int r = 0; r < response.results_size(); ++r) {
const auto& result = response.results(r);
std::cout << "Result stability: " << result.stability() << std::endl;
for (int a = 0; a < result.alternatives_size(); ++a) {
const auto& alternative = result.alternatives(a);
std::cout << alternative.confidence() << "\t"
<< alternative.transcript() << std::endl;
}
}
}
grpc::Status status = streamer->Finish();
microphone_thread.join();
if (!status.ok()) {
// Report the RPC failure.
std::cerr << status.error_message() << std::endl;
return -1;
}
return 0;
}
// [END speech_streaming_recognize]