| 1 | package Google::Cloud::Speech; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Mojo::Base -base; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use Google::Cloud::Speech::Auth; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Mojo::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use MIME::Base64; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Mojo::File; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | $Google::Cloud::Speech::VERSION = '0.06'; | |||||||||
| 12 | |||||||||||
| 13 | 1 | ▶ | has secret_file => sub { }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||
| 14 | 0 | ▶ | has ua => sub { Mojo::UserAgent->new() }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 15 | 0 | ▶ | has file => sub { croak 'you must specify the audio file'; }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 16 | 1 | has samplerate => '16000'; | |||||||||
| 17 | 1 | has language => 'en-IN'; | |||||||||
| 18 | 1 | has baseurl => 'https://speech.googleapis.com/v1'; | |||||||||
| 19 | 1 | has encoding => 'linear16'; | |||||||||
| 20 | 1 | has async_id => undef; | |||||||||
| 21 | 1 | has results => undef; | |||||||||
| 22 | |||||||||||
| 23 | 0 | ▶ | has config => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 24 | 0 | my $self = shift; | |||||||||
| 25 | |||||||||||
| 26 | return { | ||||||||||
| 27 | 0 | encoding => $self->encoding, | |||||||||
| 28 | sampleRateHertz => $self->samplerate, | ||||||||||
| 29 | languageCode => $self->language, | ||||||||||
| 30 | profanityFilter => 'false', | ||||||||||
| 31 | }; | ||||||||||
| 32 | 1 | }; | |||||||||
| 33 | |||||||||||
| 34 | 0 | ▶ | has auth_class => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 35 | 0 | my $self = shift; | |||||||||
| 36 | 0 | Google::Cloud::Speech::Auth->new(from_json => $self->secret_file); | |||||||||
| 37 | 1 | }; | |||||||||
| 38 | |||||||||||
| 39 | 0 | ▶ | sub token { | ||||||||
|
Subroutine
token: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 40 | 0 | my $self = shift; | |||||||||
| 41 | |||||||||||
| 42 | 0 | my $auth_obj = $self->auth_class; | |||||||||
| 43 | 0 | ▶ | unless ($auth_obj->has_valid_token) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 0 | return $auth_obj->request_token->token(); | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 0 | return $auth_obj->token; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 0 | ▶ | sub syncrecognize { | ||||||||
|
Subroutine
syncrecognize: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 51 | 0 | my $self = shift; | |||||||||
| 52 | |||||||||||
| 53 | 0 | my $audio_raw = Mojo::File->new( $self->file )->slurp(); | |||||||||
| 54 | |||||||||||
| 55 | 0 | my $audio = { "content" => encode_base64( $audio_raw, "" ) }; | |||||||||
| 56 | 0 | my $header = { | |||||||||
| 57 | 'Content-Type' => "application/json", | ||||||||||
| 58 | 'Authorization' => $self->token, | ||||||||||
| 59 | }; | ||||||||||
| 60 | |||||||||||
| 61 | 0 | my $hash_ref = { | |||||||||
| 62 | config => $self->config, | ||||||||||
| 63 | audio => $audio, | ||||||||||
| 64 | }; | ||||||||||
| 65 | |||||||||||
| 66 | 0 | my $url = $self->baseurl . "/speech:recognize"; | |||||||||
| 67 | 0 | my $tx = $self->ua->post( $url => $header => json => $hash_ref ); | |||||||||
| 68 | |||||||||||
| 69 | 0 | my $response = $self->handle_errors($tx)->json; | |||||||||
| 70 | 0 | ▶ | if ( my $results = $response->{'results'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 0 | return $self->results($results); | |||||||||
| 72 | } | ||||||||||
| 73 | 0 | return $self->results( [] ); | |||||||||
| 74 | |||||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 0 | ▶ | sub asyncrecognize { | ||||||||
|
Subroutine
asyncrecognize: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 78 | 0 | my $self = shift; | |||||||||
| 79 | |||||||||||
| 80 | 0 | my $audio_raw = Mojo::File->new( $self->file )->slurp(); | |||||||||
| 81 | 0 | my $audio = { "content" => encode_base64( $audio_raw, "" ) }; | |||||||||
| 82 | 0 | my $header = { | |||||||||
| 83 | 'Content-Type' => "application/json", | ||||||||||
| 84 | 'Authorization' => $self->token, | ||||||||||
| 85 | }; | ||||||||||
| 86 | |||||||||||
| 87 | 0 | my $hash_ref = { | |||||||||
| 88 | config => $self->config, | ||||||||||
| 89 | audio => $audio, | ||||||||||
| 90 | }; | ||||||||||
| 91 | |||||||||||
| 92 | 0 | my $url = $self->baseurl . "/speech:longrunningrecognize"; | |||||||||
| 93 | 0 | my $tx = $self->ua->post( $url => $header => json => $hash_ref ); | |||||||||
| 94 | |||||||||||
| 95 | 0 | my $res = $self->handle_errors($tx)->json; | |||||||||
| 96 | 0 | ▶ | if ( my $name = $res->{'name'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 0 | $self->async_id($name); | |||||||||
| 98 | |||||||||||
| 99 | 0 | return $self; | |||||||||
| 100 | } | ||||||||||
| 101 | 0 | croak 'there was an error'; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | 0 | ▶ | sub is_done { | ||||||||
|
Subroutine
is_done: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 105 | 0 | my $self = shift; | |||||||||
| 106 | |||||||||||
| 107 | 0 | my $async_id = $self->async_id; | |||||||||
| 108 | 0 | ▶ | return unless $async_id; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | |||||||||||
| 110 | 0 | my $url = $self->baseurl . "/operations/" . $async_id; | |||||||||
| 111 | 0 | my $tx = $self->ua->get( $url => { 'Authorization' => $self->token } ); | |||||||||
| 112 | |||||||||||
| 113 | 0 | my $res = $self->handle_errors($tx)->json; | |||||||||
| 114 | 0 | my $is_done = $res->{'done'}; | |||||||||
| 115 | |||||||||||
| 116 | 0 | ▶ | if ($is_done) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 0 | $self->{'results'} = $res->{'response'}->{'results'}; | |||||||||
| 118 | 0 | return 1; | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | 0 | return 0; | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | 0 | ▶ | sub handle_errors { | ||||||||
|
Subroutine
handle_errors: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 125 | 0 | my ( $self, $tx ) = @_; | |||||||||
| 126 | 0 | my $res = $tx->res; | |||||||||
| 127 | |||||||||||
| 128 | 0 | ▶ | unless ( $res->is_success ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | 0 | my $error_ref = $tx->error; | |||||||||
| 130 | 0 | croak( "invalid response: " . $error_ref->{'message'} ); | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | 0 | return $res; | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | 1 | 1; | |||||||||
| 137 | |||||||||||
| 138 | =encoding utf8 | ||||||||||
| 139 | |||||||||||
| 140 | =head1 NAME | ||||||||||
| 141 | |||||||||||
| 142 | Google::Cloud::Speech - An interface to Google cloud speech service | ||||||||||
| 143 | |||||||||||
| 144 | =head1 SYNOPSIS | ||||||||||
| 145 | |||||||||||
| 146 | use Data::Dumper; | ||||||||||
| 147 | use Google::Cloud::Speech; | ||||||||||
| 148 | |||||||||||
| 149 | my $speech = Google::Cloud::Speech->new( | ||||||||||
| 150 | file => 'test.wav', | ||||||||||
| 151 | secret_file => 'my/google/app/project/sa/json/file' | ||||||||||
| 152 | ); | ||||||||||
| 153 | |||||||||||
| 154 | # long running process | ||||||||||
| 155 | my $operation = $speech->asyncrecognize(); | ||||||||||
| 156 | my $is_done = $operation->is_done; | ||||||||||
| 157 | until($is_done) { | ||||||||||
| 158 | if ($is_done = $operation->is_done) { | ||||||||||
| 159 | print Dumper $operation->results; | ||||||||||
| 160 | } | ||||||||||
| 161 | } | ||||||||||
| 162 | |||||||||||
| 163 | =head1 DESCRIPTION | ||||||||||
| 164 | |||||||||||
| 165 | This module lets you access Google cloud speech service. | ||||||||||
| 166 | |||||||||||
| 167 | =head1 ATTRIBUTES | ||||||||||
| 168 | |||||||||||
| 169 | =head2 C<secret_file> | ||||||||||
| 170 | |||||||||||
| 171 | Loads the JSON file from Google with the client ID informations. | ||||||||||
| 172 | |||||||||||
| 173 | $speech->secret_file('/my/google/app/project/sp/json/file'); | ||||||||||
| 174 | |||||||||||
| 175 | To create, Google Service Account Key: | ||||||||||
| 176 | |||||||||||
| 177 | 1) Login to Google Apps Console and select your project | ||||||||||
| 178 | 2) Click on create credentials-> service account key. | ||||||||||
| 179 | 4) Select a service account and key type as JSON and click on create and downlaoded the JSON file. | ||||||||||
| 180 | |||||||||||
| 181 | See L<Google API Doc|https://developers.google.com/identity/protocols/application-default-credentials> for more details about API authentication. | ||||||||||
| 182 | |||||||||||
| 183 | =head2 encoding | ||||||||||
| 184 | |||||||||||
| 185 | my $encoding = $speech->encoding('linear16'); | ||||||||||
| 186 | |||||||||||
| 187 | Encoding of audio data to be recognized. | ||||||||||
| 188 | Acceptable values are: | ||||||||||
| 189 | |||||||||||
| 190 | * linear16 - Uncompressed 16-bit signed little-endian samples. | ||||||||||
| 191 | (LINEAR16) | ||||||||||
| 192 | * flac - The [Free Lossless Audio | ||||||||||
| 193 | Codec](http://flac.sourceforge.net/documentation.html) encoding. | ||||||||||
| 194 | Only 16-bit samples are supported. Not all fields in STREAMINFO | ||||||||||
| 195 | are supported. (FLAC) | ||||||||||
| 196 | * mulaw - 8-bit samples that compand 14-bit audio samples using | ||||||||||
| 197 | G.711 PCMU/mu-law. (MULAW) | ||||||||||
| 198 | * amr - Adaptive Multi-Rate Narrowband codec. (`sample_rate` must | ||||||||||
| 199 | be 8000 Hz.) (AMR) | ||||||||||
| 200 | * amr_wb - Adaptive Multi-Rate Wideband codec. (`sample_rate` must | ||||||||||
| 201 | be 16000 Hz.) (AMR_WB) | ||||||||||
| 202 | * ogg_opus - Ogg Mapping for Opus. (OGG_OPUS) | ||||||||||
| 203 | Lossy codecs do not recommend, as they result in a lower-quality | ||||||||||
| 204 | speech transcription. | ||||||||||
| 205 | * speex - Speex with header byte. (SPEEX_WITH_HEADER_BYTE) | ||||||||||
| 206 | |||||||||||
| 207 | |||||||||||
| 208 | =head2 file | ||||||||||
| 209 | |||||||||||
| 210 | my $file = $speech->file; | ||||||||||
| 211 | my $file = $speech->('path/to/audio/file.wav'); | ||||||||||
| 212 | |||||||||||
| 213 | |||||||||||
| 214 | =head2 language | ||||||||||
| 215 | |||||||||||
| 216 | my $lang = $speech->language('en-IN'); | ||||||||||
| 217 | |||||||||||
| 218 | The language of the supplied audio as a BCP-47 language tag. | ||||||||||
| 219 | Example: "en-IN" for English (United States), "en-GB" for English (United | ||||||||||
| 220 | Kingdom), "fr-FR" for French (France). See Language Support for a list of the currently supported language codes. | ||||||||||
| 221 | L<Language codes|https://cloud.google.com/speech/docs/languages> | ||||||||||
| 222 | |||||||||||
| 223 | =head2 samplrate | ||||||||||
| 224 | |||||||||||
| 225 | my $sample_rate = $speech->samplerate('16000'); | ||||||||||
| 226 | |||||||||||
| 227 | Sample rate in Hertz of the audio data to be recognized. Valid values | ||||||||||
| 228 | are: 8000-48000. 16000 is optimal. For best results, set the sampling | ||||||||||
| 229 | rate of the audio source to 16000 Hz. If that's not possible, use the | ||||||||||
| 230 | native sample rate of the audio source (instead of re-sampling). | ||||||||||
| 231 | |||||||||||
| 232 | |||||||||||
| 233 | =head1 METHODS | ||||||||||
| 234 | |||||||||||
| 235 | =head2 asyncrecognize | ||||||||||
| 236 | |||||||||||
| 237 | Performs asynchronous speech recognition: | ||||||||||
| 238 | receive results via the google.longrunning.Operations interface. | ||||||||||
| 239 | |||||||||||
| 240 | my $operation = $speech->asyncrecognize(); | ||||||||||
| 241 | my $is_done = $operation->is_done; | ||||||||||
| 242 | until($is_done) { | ||||||||||
| 243 | if ($is_done = $operation->is_done) { | ||||||||||
| 244 | print Dumper $operation->results; | ||||||||||
| 245 | } | ||||||||||
| 246 | } | ||||||||||
| 247 | |||||||||||
| 248 | |||||||||||
| 249 | =head2 syncrecognize | ||||||||||
| 250 | |||||||||||
| 251 | Performs synchronous speech recognition: receive results after all audio has been sent and processed. | ||||||||||
| 252 | |||||||||||
| 253 | my $operation = $speech->syncrecognize; | ||||||||||
| 254 | print $operation->results; | ||||||||||
| 255 | |||||||||||
| 256 | =head2 is_done | ||||||||||
| 257 | |||||||||||
| 258 | Checks if the speech-recognition processing of the audio data is complete. | ||||||||||
| 259 | return 1 when complete, 0 otherwise. | ||||||||||
| 260 | |||||||||||
| 261 | =head2 results | ||||||||||
| 262 | |||||||||||
| 263 | returns the transcribed data as Arrayref. | ||||||||||
| 264 | |||||||||||
| 265 | print Dumper $speech->syncrecognize->results; | ||||||||||
| 266 | |||||||||||
| 267 | =head1 AUTHOR | ||||||||||
| 268 | |||||||||||
| 269 | Prajith P C<me@prajith.in> | ||||||||||
| 270 | |||||||||||
| 271 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 272 | |||||||||||
| 273 | This software is Copyright (c) 2017, Prajith P. | ||||||||||
| 274 | |||||||||||
| 275 | This is free software, you can redistribute it and/or modify it under | ||||||||||
| 276 | the same terms as Perl language system itself. | ||||||||||
| 277 | |||||||||||
| 278 | |||||||||||
| 279 | =head1 SEE ALSO | ||||||||||
| 280 | |||||||||||
| 281 | =over | ||||||||||
| 282 | |||||||||||
| 283 | =item * L<Google Cloud Speech API|https://cloud.google.com/speech/reference/rest/> | ||||||||||
| 284 | |||||||||||
| 285 | =back | ||||||||||
| 286 | |||||||||||
| 287 | =cut | ||||||||||
| 288 | |||||||||||
| 289 | =head1 DEVELOPMENT | ||||||||||
| 290 | |||||||||||
| 291 | This project is hosted on Github, at | ||||||||||
| 292 | L<https://github.com/Prajithp/p5-google-cloud-speech> | ||||||||||
| 293 | |||||||||||
| 294 | =cut | ||||||||||