class EasyCaptcha::Espeak

espeak wrapper

Attributes

amplitude[W]
gap[W]
pitch[W]
voice[W]

Public Class Methods

new() { |self| ... } click to toggle source

generator for captcha images

# File lib/easy_captcha/espeak.rb, line 6
def initialize(&block)
  defaults
  yield self if block_given?
end

Public Instance Methods

amplitude() click to toggle source

return amplitude

# File lib/easy_captcha/espeak.rb, line 22
def amplitude
  if @amplitude.is_a? Range
    @amplitude.to_a.sort_by { rand }.first
  else
    @amplitude.to_i
  end
end
defaults() click to toggle source

set default values

# File lib/easy_captcha/espeak.rb, line 12
def defaults
  @amplitude = 80..120
  @pitch     = 30..70
  @gap       = 80
  @voice     = nil
end
gap() click to toggle source
# File lib/easy_captcha/espeak.rb, line 39
def gap
  @gap.to_i
end
generate(captcha, wav_file) click to toggle source

generate wav file by captcha

# File lib/easy_captcha/espeak.rb, line 54
def generate(captcha, wav_file)
  # get code
  if captcha.is_a? Captcha
    code = captcha.code
  elsif captcha.is_a? String
    code = captcha
  else
    raise ArgumentError, "invalid captcha"
  end

  # add spaces
  code = code.each_char.to_a.join(" ")

  cmd = "espeak -g 10"
  cmd << " -a #{amplitude}" unless @amplitude.nil?
  cmd << " -p #{pitch}" unless @pitch.nil?
  cmd << " -g #{gap}" unless @gap.nil?
  cmd << " -v '#{voice}'" unless @voice.nil?
  cmd << " -w #{wav_file} '#{code}'"

  %x{#{cmd}}
  true
end
pitch() click to toggle source

return amplitude

# File lib/easy_captcha/espeak.rb, line 31
def pitch
  if @pitch.is_a? Range
    @pitch.to_a.sort_by { rand }.first
  else
    @pitch.to_i
  end
end
voice() click to toggle source
# File lib/easy_captcha/espeak.rb, line 43
def voice
  if @voice.is_a? Array
    v = @voice.sort_by { rand }.first
  else
    v = @voice
  end

  v.try :gsub, /[^A-Za-z0-9\-\+]/, ""
end