Class: EasyCaptcha::Espeak

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_captcha/espeak.rb

Overview

espeak wrapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Espeak

generator for captcha images

Yields:

  • (_self)

Yield Parameters:



6
7
8
9
# File 'lib/easy_captcha/espeak.rb', line 6

def initialize(&block)
  defaults
  yield self if block_given?
end

Instance Attribute Details

#amplitudeObject

return amplitude



22
23
24
25
26
27
28
# 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

#gapObject



39
40
41
# File 'lib/easy_captcha/espeak.rb', line 39

def gap
  @gap.to_i
end

#pitchObject

return amplitude



31
32
33
34
35
36
37
# 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

#voiceObject



43
44
45
46
47
48
49
50
51
# 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

Instance Method Details

#defaultsObject

set default values



12
13
14
15
16
17
# File 'lib/easy_captcha/espeak.rb', line 12

def defaults
  @amplitude = 80..120
  @pitch     = 30..70
  @gap       = 80
  @voice     = nil
end

#generate(captcha, wav_file) ⇒ Object

generate wav file by captcha



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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