YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
GoogleSpeechTranscription.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#ifndef _USE_MATH_DEFINES
7#define _USE_MATH_DEFINES
8#endif
9
11
12#include <yarp/sig/SoundFile.h>
13
15#include <yarp/os/LogStream.h>
16#include <fstream>
17
18#include <cmath>
19
20using namespace yarp::os;
21using namespace yarp::dev;
22
23
25
26
31
32std::shared_ptr<google::cloud::speech_v1::SpeechClient> GoogleSpeechTranscription::_getClient(google::cloud::Options opts)
33{
34 return std::make_shared<google::cloud::speech_v1::SpeechClient>(google::cloud::speech_v1::MakeSpeechConnection(opts));
35}
36
38{
39 if(config.check("__offline"))
40 {
41 m_offline = config.find("__offline").asInt32() == 1;
42 }
43 if(!parseParams(config))
44 {
45 yCError(GOOGLESPEECHTRANSCR) << "Unable to correctly parse device params. Check previous errors for more info";
46 return false;
47 }
48 m_audioConfig.set_language_code(m_language_code);
49 m_audioConfig.set_encoding(google::cloud::speech::v1::RecognitionConfig::LINEAR16);
50 m_audioConfig.set_sample_rate_hertz(m_sample_rate_hertz);
51
52 return true;
53}
54
56{
57 return true;
58}
59
61{
62 if(language == "auto")
63 {
64 yCError(GOOGLESPEECHTRANSCR) << "The \"auto\" option is not supported by this device";
65
67 }
68
69 m_audioConfig.set_language_code(language);
71}
72
74{
75 language = m_audioConfig.language_code();
76
78}
79
80yarp::dev::ReturnValue GoogleSpeechTranscription::transcribe(const yarp::sig::Sound& sound, std::string& transcription, double& score)
81{
82 transcription="";
83 score = 0.0;
84
85 if (sound.getSamples() == 0 ||
86 sound.getChannels() == 0)
87 {
88 yCError(GOOGLESPEECHTRANSCR) << "Invalid Sound sample received";
90 }
91
92 google::cloud::speech::v1::RecognitionAudio audio;
94 auto rawData = std::vector<short>(rawData_tmp.begin(), rawData_tmp.end());
95 audio.set_content((char*)rawData.data(),rawData.size()*2);
96
97 // Configure client with timeout options
98 google::cloud::Options opts;
99 // Connection timeout (initial connection establishment)
100 // This is what you want to be short for detecting no internet
101 opts.set<google::cloud::GrpcChannelArgumentsOption>({
102 {"grpc.initial_reconnect_backoff_ms", "1000"}, // 1 second
103 {"grpc.min_reconnect_backoff_ms", "1000"},
104 {"grpc.max_reconnect_backoff_ms", "5000"}, // 5 seconds max
105 {"grpc.http2.min_ping_interval_without_data_ms", "30000"},
106 {"grpc.keepalive_timeout_ms", "10000"}, // 10 seconds
107 });
108
109 auto response = _getClient(opts)->Recognize(m_audioConfig,audio);
110
111 if(!response)
112 {
113 yCError(GOOGLESPEECHTRANSCR) << "Could not perform audio transcription:" << response.status().message();
115 }
116
117 yCDebug(GOOGLESPEECHTRANSCR) << "Results size:" << response->results_size();
118 for(int i=0; i<response->results_size(); i++)
119 {
120 auto result = response->results(i);
121 yCDebug(GOOGLESPEECHTRANSCR) << i << "Alternative size:" << result.alternatives_size();
122 for(int j=0; j<result.alternatives_size(); j++)
123 {
124 auto alternative = result.alternatives(j);
125 float tempConf = alternative.confidence();
126 yCDebug(GOOGLESPEECHTRANSCR) << "Alternative:" << alternative.SerializeAsString() << "Confidence:" << tempConf;
127 if(tempConf > score)
128 {
129 score = tempConf;
130 transcription = alternative.transcript();
131 }
132 }
133 }
134 yCDebug(GOOGLESPEECHTRANSCR) << "Transcription:" << transcription << "Score:" << score;
136}
const yarp::os::LogComponent & GOOGLESPEECHTRANSCR()
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
yarp::dev::ReturnValue setLanguage(const std::string &language="auto") override
Sets the language for speech transcription.
yarp::dev::ReturnValue getLanguage(std::string &language) override
Gets the current language set for speech transcription.
bool close() override
Close the DeviceDriver.
yarp::dev::ReturnValue transcribe(const yarp::sig::Sound &sound, std::string &transcription, double &score) override
Performs the speech transcription.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
@ return_value_error_generic
Method was successfully executed.
A mini-server for performing network communication in the background.
@ TraceType
Definition Log.h:92
A base class for nested structures that can be searched.
Definition Searchable.h:31
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
Definition Sound.h:39
std::vector< std::reference_wrapper< audio_sample > > getNonInterleavedAudioRawData() const
Returns a serialized version of the sound, in non-interleaved format, e.g.
Definition Sound.cpp:622
size_t getChannels() const
Get the number of channels of the sound.
Definition Sound.cpp:669
size_t getSamples() const
Get the number of samples contained in the sound.
Definition Sound.cpp:664
#define yCError(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.