Skip to content
This repository has been archived by the owner on May 18, 2021. It is now read-only.

Commit

Permalink
Add audio_raw_to_speech, basics of gCloud Speech API
Browse files Browse the repository at this point in the history
  • Loading branch information
hdm committed Apr 29, 2016
1 parent f8d63a6 commit 49da425
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
76 changes: 76 additions & 0 deletions bin/audio_raw_to_speech.rb
@@ -0,0 +1,76 @@
#!/usr/bin/env ruby
###################

#
# Load the library path
#
base = __FILE__
while File.symlink?(base)
base = File.expand_path(File.readlink(base), File.dirname(base))
end
$:.unshift(File.join(File.expand_path(File.dirname(base)), '..', 'lib'))

require 'warvox'

require 'uri'
require 'net/http'
require 'json'

def usage
$stderr.puts "Usage: #{$0} <input.raw> <output.json>"
exit
end

#
# Script
#

inp = ARGV.shift
out = ARGV.shift

if (inp and inp == "-h") or not inp
usage()
end

raw = WarVOX::Audio::Raw.from_file(inp)
res = nil
flac = raw.to_flac
akey = WarVOX::Config.gcloud_key

if ! akey
$stderr.puts "Error: A gcloud API key needs to be configured"
exit(1)
end

uri = URI('https://speech.googleapis.com/v1/speech:recognize?key=' + akey)
req = Net::HTTP::Post.new(uri, initheader = {'Content-Type' =>'application/json'})

loop do
req.body =
{
"initialRequest" => {
"encoding" => "FLAC",
"sampleRate" => 16000,
},
"audioRequest" => {
"content" => [flac].pack("m*").gsub(/\s+/, '')
}
}.to_json


http = Net::HTTP.new(uri.hostname, uri.port)
http.use_ssl = true
res = http.request(req)

break if res.code.to_s == "200"
$stderr.puts "Retrying due to #{res.code} #{res.message}..."
sleep(1)
end

if out
::File.open(out, "wb") do |fd|
fd.write(res.body)
end
else
$stdout.write(res.body)
end
8 changes: 8 additions & 0 deletions bin/verify_install.rb
Expand Up @@ -56,6 +56,14 @@
puts "[*] The LAME binary appears to be available"


if(not WarVOX::Config.tool_path('sox'))
puts "[*] ERROR: The 'sox binary could not be installed"
puts "[*] $ sudo apt-get install sox"
exit
end
puts "[*] The SOX binary appears to be available"


puts " "
puts "[*] Congratulations! You are almost ready to run WarVOX"
puts " "
Expand Down
8 changes: 8 additions & 0 deletions config/warvox.conf
Expand Up @@ -8,8 +8,10 @@
tools:
gnuplot: gnuplot
lame: lame
sox: sox
iaxrecord: "%BASE%/bin/iaxrecord.rb"


#
# Maximum processing jobs, normally this is set to your processor core count,
# but you can limit it further here. Keep in mind that each analysis job also
Expand All @@ -32,3 +34,9 @@ classifiers: "%BASE%/config/classifiers"
# Configure the signature directory
#
signatures: "%BASE%/config/signatures"

#
# Configure cloud integrations
#
apis:
gcloud: "%BASE%/config/gcloud.key"
12 changes: 12 additions & 0 deletions lib/warvox/audio/raw.rb
@@ -1,3 +1,5 @@
require 'open3'

module WarVOX
module Audio
class Raw
Expand Down Expand Up @@ -72,6 +74,16 @@ def to_wav
raw
end

def to_flac
sox = WarVOX::Config.tool_path('sox')
if ! sox
raise RuntimeError, "The sox binary could not be find, make sure it is installed"
end

o, s = Open3.capture2("#{sox} -t raw -b 16 -e signed-integer -r 8000 - -t flac -r 16000 -", :binmode => true, :stdin_data => self.to_raw)
o
end

def to_flow(opts={})

lo_lim = (opts[:lo_lim] || 100).to_i
Expand Down
8 changes: 8 additions & 0 deletions lib/warvox/config.rb
Expand Up @@ -49,6 +49,14 @@ def self.blacklist_load

end

def self.gcloud_key
info = YAML.load_file(WarVOX::Conf)
return nil if not info
return nil if not info['apis']
return nil if not info['apis']['gcloud']
::File.read(File.expand_path(info['apis']['gcloud'].gsub('%BASE%', WarVOX::Base)))
end

def self.signatures_path
info = YAML.load_file(WarVOX::Conf)
return nil if not info
Expand Down

0 comments on commit 49da425

Please sign in to comment.