Skip to content

Instantly share code, notes, and snippets.

@miyagawa
Created February 22, 2019 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miyagawa/7624f237f103554fdfbdad44f8857355 to your computer and use it in GitHub Desktop.
Save miyagawa/7624f237f103554fdfbdad44f8857355 to your computer and use it in GitHub Desktop.
namespace :speech do
desc "Merge WAV files and encode to FLAC"
task :encode do |t|
wav = "podcast-ep#{ENV['EP']}.wav"
flac = "podcast-ep#{ENV['EP']}.flac"
channels = []
wavs = Dir["#{ENV['HOME']}/Music/GarageBand/Podcast ep#{ENV['EP']}-*.wav"]
wavs.each_with_index do |w, i|
mono = "podcast-ep#{ENV['EP']}-ch#{i+1}.wav"
system 'sox', w, mono, 'remix', '1'
channels << mono
end
system 'sox', '-M', *channels, wav
channels.each do |file|
File.unlink file
end
system 'flac', '-0', wav, '-f', '-o', flac
File.unlink wav
end
desc "Upload FLAC to GCS"
task :upload do |t|
flac = "podcast-ep#{ENV['EP']}.flac"
system 'gsutil', 'cp', flac, 'gs://rebuild-audio/'
system 'gsutil', 'acl', 'ch', '-u', 'AllUsers:R', "gs://rebuild-audio/#{flac}"
end
desc "Request Search transcribe API and logs the operation ID"
task :request do |t|
flac = "podcast-ep#{ENV['EP']}.flac"
channel = `soxi -c #{flac}`.chomp.to_i
rate = `soxi -r #{flac}`.chomp.to_i
data = api_request(channel: channel, rate: rate)
open('_speech/operations.txt', 'a') do |f|
f.puts "#{ENV['EP']} #{data['name']}"
end
File.unlink flac
end
def api_request(channel: 2, rate: 48000)
require 'net/http'
require 'json'
phrases = File.readlines('_words.txt').map(&:chomp)
uri = URI.parse "https://speech.googleapis.com/v1/speech:longrunningrecognize?key=#{ENV['GCLOUD_API_KEY']}"
body = {
config: {
encoding: "FLAC",
sampleRateHertz: rate,
languageCode: "ja-JP",
enableWordTimeOffsets: true,
audioChannelCount: channel,
enableSeparateRecognitionPerChannel: true,
speechContexts: [
{ phrases: phrases }
]
},
audio: {
uri: "gs://rebuild-audio/podcast-ep#{ENV['EP']}.flac",
},
}
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
req = Net::HTTP::Post.new uri.request_uri
req['Content-Type'] = 'application/json'
req.body = JSON.dump body
resp = http.request req
warn resp.body
JSON.parse resp.body
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment