Google Cloud Web Client Text to Speech at breakfast at the Saint-Gervais Bastion

From a very young age, from the very first reading of The Three Musketeers in one gulp, the author is unshakably convinced that the best way to learn something is practice. The theory can be further learned and sharpened, the Musketeer friends "will show some nice tricks", no problem. But, according to the venerable Maitre Dumas (and the author believes him), the best way to integrate into any essence new to oneself is to plunge into it headlong, and then whatever goes on. Where ours did not disappear, and, as you know, any Gascon from childhood is an academician.





, , 90% , ... , , . : , . , , , - - dΓ©butants - , , Ruby, API Google Cloud Text to Speech, PaaS- Heroku git.





. , "Is ruby dead?", , ... ----- , , , - ` , , , , /, Ruby Developer. , , "" , CTO Armand-Jean du Plessis, duc de Richelieu... , - , HR , .





. - , Human Resource, , ( - , ). ... , , , , . ", ! , !"





. - , API Google Cloud Text to Speech, - , . , , , IBM Text-to-Speech API , , , ... "" , IBM API , ( Terms of Service). , ", , " JSON, , , MasterCard . , $1, ? - , - , - , .





Free account Heroku, Ruby on Rails, git Heroku toolbelt OS.





. , rails-app, - API Google Cloud Text to Speech: device ( , , - ), , . : Heroku , email, ( , , ).





, Bootstrap 4 - ; flash- - ajax, Ruby on Rails. -, rails-app, , - " , ", ... , , . , , - .





, " ". Google-Cloud-TTS-Rails ( SSML) 18 , , , : MP3 (MPEG Audio Layer III), WAV (LINEAR16) and OGG (OGG_OPUS), voice type: WaveNet Basic. ...





... , , . , HTML (drop-down list), 0.25 4.0 ( API), 0.25, - 1.0. , :





module SoundHelper
  def speaking_rate
    select_tag 'speaking_rate', options_for_select(
      0.25.step(by: 0.25, to: 4.0), selected: '1.0'
    ), { class: 'btn' }
  end
end
      
      



, . , , , ... / , , , , . , . , in two words , Heroku , Heroku CLI git, . Ruby postgreSQL .





, :





git clone https://github.com/cmirnow/Google-Cloud-TTS-Rails.git
cd Google-Cloud-TTS-Rails
      
      



, YOUR_KEY_NAME.json, ( ). :





git add .   
git commit -m "my first commit"
heroku create
git push heroku master
heroku run rake db:migrate
      
      



Heroku, YOUR_NEW_APPLICATION -> 'Settings' -> 'Reveal Config Vars', key/value:






key: GOOGLE_APPLICATION_CREDENTIALS value: YOUR_KEY_NAME.json





key: DOMAIN_NAME value: YOUR_HEROKU_DOMAIN ### i.e 'https://***************.herokuapp.com' without quotes.





key: GMAIL_USER_NAME value: YOUR_GMAIL_LOGIN





key: GMAIL_PASSWORD value: YOUR_GMAIL_PASSWORD ### (An App Password is a 16-digit passcode that gives an app or device restricted access to your Google Account without having to divulge your personal password and complete access to your Google Account).






, , Google-Cloud-TTS-Rails , . - , . " " - 5000 , Google. , .





. rails-, , , , , :





class Validation
	include ActiveModel::Model
	attr_accessor :request
	validates :request, presence: true, length: {in:3..4999}
end
      
      



, , , TtsConversion :





  def conversion
    audio_format = TtsConversion.index(client, synthesis_input, voice, audio, params[:codec])
    success_info(audio_format)
  end

  def client
    Google::Cloud::TextToSpeech.text_to_speech
  end

  def synthesis_input
    { params[:text_or_ssml] => params[:request] }
  end

  def voice
    { language_code: params[:lang], name: params[:voicename] }
  end

  def audio
    { audio_encoding: params[:codec], speaking_rate: params[:speaking_rate].to_f }
  end
      
      



class TtsConversion
  def self.index(*args)
    response = args[0].synthesize_speech input: args[1], voice: args[2], audio_config: args[3]
    File.open 'public/output/output.' + audio_format(args[4]).to_s, 'wb' do |file|
      file.write response.audio_content
      audio_format(args[4]).to_s
    end
  end

  def self.audio_format(codec)
  	case codec
  	when "LINEAR16"
  		'wav'
    when "OGG_OPUS"
      'ogg'
  	else
  		'mp3'
  	end
  end
end
      
      



All the work is done, in fact, by the gem 'google-cloud-text_to_speech', passing the converted text and the selected parameters to the Google Cloud Text to Speech API and receiving sound back in digital. That's probably all.








All Articles