class EasyCaptcha::Espeak

espeak wrapper

Constants

DEFAULT_CONFIG

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 16
def initialize
  set_defaults
  yield self if block_given?
end

Public Instance Methods

amplitude() click to toggle source

return amplitude

# File lib/easy_captcha/espeak.rb, line 29
def amplitude
  @amplitude.is_a?(Range) ? @amplitude.to_a.sample : @amplitude.to_i
end
espeak_amplitude_param() click to toggle source
# File lib/easy_captcha/espeak.rb, line 62
def espeak_amplitude_param
  "-a #{amplitude}" unless @amplitude.nil?
end
espeak_gap_param() click to toggle source
# File lib/easy_captcha/espeak.rb, line 70
def espeak_gap_param
  "-g #{gap}" unless @gap.nil?
end
espeak_pitch_param() click to toggle source
# File lib/easy_captcha/espeak.rb, line 66
def espeak_pitch_param
  "-p #{pitch}" unless @pitch.nil?
end
espeak_voice_param() click to toggle source
# File lib/easy_captcha/espeak.rb, line 74
def espeak_voice_param
  "-v '#{voice}'" unless @voice.nil?
end
gap() click to toggle source
# File lib/easy_captcha/espeak.rb, line 38
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 47
def generate(captcha, wav_file)
  cmd = [
    'espeak -g 10',
    espeak_amplitude_param,
    espeak_pitch_param,
    espeak_gap_param,
    espeak_voice_param,
    "-w #{wav_file}",
    "'#{get_code(captcha)}'"
  ].compact.join(' ')

  `#{cmd}`
  true
end
get_code(captcha) click to toggle source
# File lib/easy_captcha/espeak.rb, line 78
def get_code(captcha)
  case captcha
  when Captcha
    code = captcha.code
  when String
    code = captcha
  else
    fail ArgumentError, 'invalid captcha'
  end
  # add spaces
  code.each_char.to_a.join(' ')
end
pitch() click to toggle source

return amplitude

# File lib/easy_captcha/espeak.rb, line 34
def pitch
  @pitch.is_a?(Range) ? @pitch.to_a.sample : @pitch.to_i
end
set_defaults() click to toggle source

set default values

# File lib/easy_captcha/espeak.rb, line 22
def set_defaults
  DEFAULT_CONFIG.map do |k, v|
    send("#{k}=", v) if respond_to? "#{k}=".to_sym
  end
end
voice() click to toggle source
# File lib/easy_captcha/espeak.rb, line 42
def voice
  (@voice.is_a?(Array) ? @voice.sample : @voice).try(:gsub, /[^A-Za-z0-9\-+]/, '')
end