Skip to content

Commit

Permalink
Add new Google speech API
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliv4945 committed Sep 8, 2017
1 parent 4cdd001 commit 2bc3f1e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 5 deletions.
59 changes: 59 additions & 0 deletions stt_engines/google/google-speech-api.perl
@@ -0,0 +1,59 @@
#!/usr/bin/env perl
# Render speech to text using the google cloud speech engine.
#
# Kruft Industries Sept. 2016
#
#
# Intended to replace work by the following(not sure where this is hosted): GNU General Public License Version 2 Copyright (C) 2011 - 2012, Lefteris Zafiris
# <zaf.000@gmail.com>
#
#
# The script takes as input flac files at 8kHz and returns the following values: status : Return status. 0 means success, non zero values indicating different
# errors.
#
# Outputs a voice transcription that satisfies the input of sendmailmp3 for freepbx authored by the above Zafiris I am by no means an expert with the perl
# language, Please forgive any blaring ugliness :)

use utf8;
use MIME::Base64;
use strict;
use warnings;
use LWP::UserAgent;

my $apiKey = $ARGV[0];
my $url = "https://speech.googleapis.com/v1beta1/speech:syncrecognize?key=${apiKey}";

my @file_list = $ARGV[1]; foreach my $file
(@file_list) {
open(my $fh, "<", "$file") or die "Cant read file: $!";
my $audio = do { local $/; <$fh> };
close($fh);

my $flac = encode_base64url($audio);

my $json = '{"config":{"encoding":"LINEAR16","sample_rate":16000,"language_code":"fr-FR"},"audio":{"content":"' . $flac . '"}}';

my $req = HTTP::Request->new( 'POST', $url );
$req->header( 'Content-Type' => 'application/json' );
$req->content( $json );

my $lwp = LWP::UserAgent->new;
my $response = $lwp->request($req);

#print $response->as_string; #debug output google's reply headers and message
last if (!$response->is_success);

print $response->content; #debug output the full transcript
}

sub encode_base64url{
my($data) = @_;
return 0 unless $data;
$data = encode_base64($data);
$data =~ s/\+/-/g;
$data =~ s/\//_/g;
$data =~ s/\=//g;
$data =~ s/\n//g;
return($data);
}
exit;
13 changes: 10 additions & 3 deletions stt_engines/google_old/main.sh → stt_engines/google/main.sh
Expand Up @@ -7,9 +7,16 @@ _google_transcribe () {
exit 1 # TODO doesn't really exit because launched with & forjv_spinner
fi

json=`wget -q --post-file $audiofile --header="Content-Type: audio/l16; rate=16000" -O - "http://www.google.com/speech-api/v2/recognize?client=chromium&lang=$language&key=$google_speech_api_key"`
$verbose && jv_debug "DEBUG: $json"
echo $json | perl -lne 'print $1 if m{"transcript":"([^"]*)"}' > $forder
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
transcribed="`$DIR/google-speech-api.perl $google_speech_api_key $audiofile | jq '.results[0].alternatives[0].transcript'`"

$verbose && jv_debug "DEBUG: $transcribed"
if [[ $transcribed == "null" ]]; then
jv_error "ERROR: Google recognition failed"
exit 1
fi

echo $transcribed > $forder
}

google_STT () { # STT () {} Listen & transcribes audio file then writes corresponding text in $forder
Expand Down
File renamed without changes.
9 changes: 7 additions & 2 deletions utils/configure.sh
@@ -1,7 +1,8 @@
#!/bin/bash
# Configuration
configure () {
local variables=('bing_speech_api_key'
local variables=('google_speech_api_key'
'bing_speech_api_key'
'check_updates'
'command_stt'
'conversation_mode'
Expand Down Expand Up @@ -53,6 +54,7 @@ configure () {
'stop_speaking'
'listening_timeout')
case "$1" in
google_speech_api_key) eval "$1=\"$(dialog_input "Google Speech API Key\nNot free, see https://cloud.google.com/speech/docs/getting-started" "${!1}" true)\"";;
bing_speech_api_key) eval "$1=\"$(dialog_input "Bing Speech API Key\nHow to get one: http://openjarvis.com/content/bing" "${!1}" true)\"";;
check_updates) options=('Always' 'Daily' 'Weekly' 'Never')
case "$(dialog_select "Check Updates when Jarvis starts up\nRecommended: Daily" options[@] "Daily")" in
Expand All @@ -61,7 +63,7 @@ configure () {
Weekly) check_updates=7;;
Never) check_updates=false;;
esac;;
command_stt) options=('bing' 'wit' 'snowboy' 'pocketsphinx')
command_stt) options=('google' 'bing' 'wit' 'snowboy' 'pocketsphinx')
eval "$1=\"$(dialog_select "Which engine to use for the recognition of commands\nVisit http://openjarvis.com/content/stt\nRecommended: bing" options[@] "${!1}")\""
[ "$command_stt" == "snowboy" ] && dialog_msg "Attention: Snowboy for commands will only be able to understand trained commands.\nTrain your commands in Settings > Voice Reco > Snowboy Settings > Train..."
source stt_engines/$command_stt/main.sh;;
Expand Down Expand Up @@ -278,6 +280,9 @@ EOM
if [ $trigger_stt = 'wit' ] || [ $command_stt = 'wit' ]; then
configure "wit_server_access_token"
fi
if [ $trigger_stt = 'google' ] || [ $command_stt = 'google' ]; then
configure "google_speech_api_key"
fi
if [ $trigger_stt = 'bing' ] || [ $command_stt = 'bing' ]; then
configure "bing_speech_api_key"
fi
Expand Down
9 changes: 9 additions & 0 deletions utils/menu.sh
Expand Up @@ -140,6 +140,7 @@ EOM
options=("Recognition of magic word ($trigger_stt)"
"Recognition of commands ($command_stt)"
"Snowboy settings"
"Google settings"
"Bing settings"
"Wit settings"
"PocketSphinx setting")
Expand All @@ -165,6 +166,14 @@ EOM
*) break;;
esac
done;;
Google*)
while true; do
options=("Google key ($google_speech_api_key)")
case "`dialog_menu 'Settings > Voice recognition > Google' options[@]`" in
Google*key*) configure "google_speech_api_key";;
*) break;;
esac
done;;
Bing*)
while true; do
options=("Bing key ($bing_speech_api_key)")
Expand Down

0 comments on commit 2bc3f1e

Please sign in to comment.