| audio_raw_to_speech.rb - warvox - VoIP based wardialing tool, forked from rapid… | |
| git clone git://jay.scot/warvox | |
| Log | |
| Files | |
| Refs | |
| README | |
| --- | |
| audio_raw_to_speech.rb (1596B) | |
| --- | |
| 1 #!/usr/bin/env ruby | |
| 2 ################### | |
| 3 | |
| 4 # | |
| 5 # Load the library path | |
| 6 # | |
| 7 base = __FILE__ | |
| 8 while File.symlink?(base) | |
| 9 base = File.expand_path(File.readlink(base), File.dirname(base)) | |
| 10 end | |
| 11 $:.unshift(File.join(File.expand_path(File.dirname(base)), '..', 'lib')) | |
| 12 | |
| 13 require 'warvox' | |
| 14 | |
| 15 require 'uri' | |
| 16 require 'net/http' | |
| 17 require 'json' | |
| 18 | |
| 19 def usage | |
| 20 $stderr.puts "Usage: #{$0} <input.raw> <output.json>" | |
| 21 exit | |
| 22 end | |
| 23 | |
| 24 # | |
| 25 # Script | |
| 26 # | |
| 27 | |
| 28 inp = ARGV.shift | |
| 29 out = ARGV.shift | |
| 30 | |
| 31 if (inp and inp == "-h") or not inp | |
| 32 usage() | |
| 33 end | |
| 34 | |
| 35 if out && File.exists?(out) | |
| 36 $stderr.puts "Error: The output file already exists: #{out}" | |
| 37 exit(0) | |
| 38 end | |
| 39 | |
| 40 raw = WarVOX::Audio::Raw.from_file(inp) | |
| 41 res = nil | |
| 42 flac = raw.to_flac | |
| 43 akey = WarVOX::Config.gcloud_key | |
| 44 | |
| 45 if ! akey | |
| 46 $stderr.puts "Error: A gcloud API key needs to be configured" | |
| 47 exit(1) | |
| 48 end | |
| 49 | |
| 50 uri = URI('https://speech.googleapis.com/v1/speech:recognize?key=' + ake… | |
| 51 req = Net::HTTP::Post.new(uri, initheader = {'Content-Type' =>'applicati… | |
| 52 | |
| 53 loop do | |
| 54 req.body = | |
| 55 { | |
| 56 "initialRequest" => { | |
| 57 "encoding" => "FLAC", | |
| 58 "sampleRate" => 16000, | |
| 59 }, | |
| 60 "audioRequest" => { | |
| 61 "content" => [flac].pack("m*").gsub(/\s+/, '') | |
| 62 } | |
| 63 }.to_json | |
| 64 | |
| 65 begin | |
| 66 http = Net::HTTP.new(uri.hostname, uri.port) | |
| 67 http.use_ssl = true | |
| 68 res = http.request(req) | |
| 69 | |
| 70 break if res.code.to_s == "200" | |
| 71 $stderr.puts "Retrying due to #{res.code} #{res.message}..." | |
| 72 rescue ::Interrupt | |
| 73 exit(0) | |
| 74 rescue ::Exception | |
| 75 $stderr.puts "Exception: #{$!} #{$!.backtrace}" | |
| 76 end | |
| 77 sleep(1) | |
| 78 end | |
| 79 | |
| 80 if out | |
| 81 ::File.open(out, "wb") do |fd| | |
| 82 fd.write(res.body) | |
| 83 end | |
| 84 else | |
| 85 $stdout.write(res.body) | |
| 86 end |