Class: EasyCaptcha::Espeak
- Inherits:
-
Object
- Object
- EasyCaptcha::Espeak
- Defined in:
- lib/easy_captcha/espeak.rb
Overview
espeak wrapper
Constant Summary collapse
- DEFAULT_CONFIG =
{ amplitude: 80..120, pitch: 30..70, gap: 80, voice: nil }.freeze
Instance Attribute Summary collapse
-
#amplitude ⇒ Object
return amplitude.
- #gap ⇒ Object
-
#pitch ⇒ Object
return amplitude.
- #voice ⇒ Object
Instance Method Summary collapse
- #espeak_amplitude_param ⇒ Object
- #espeak_gap_param ⇒ Object
- #espeak_pitch_param ⇒ Object
- #espeak_voice_param ⇒ Object
-
#generate(captcha, wav_file) ⇒ Object
generate wav file by captcha.
- #get_code(captcha) ⇒ Object
-
#initialize {|_self| ... } ⇒ Espeak
constructor
generator for captcha images.
-
#set_defaults ⇒ Object
set default values.
Constructor Details
#initialize {|_self| ... } ⇒ Espeak
generator for captcha images
16 17 18 19 |
# File 'lib/easy_captcha/espeak.rb', line 16 def initialize set_defaults yield self if block_given? end |
Instance Attribute Details
#amplitude ⇒ Object
return amplitude
29 30 31 |
# File 'lib/easy_captcha/espeak.rb', line 29 def amplitude @amplitude.is_a?(Range) ? @amplitude.to_a.sample : @amplitude.to_i end |
#gap ⇒ Object
38 39 40 |
# File 'lib/easy_captcha/espeak.rb', line 38 def gap @gap.to_i end |
#pitch ⇒ Object
return amplitude
34 35 36 |
# File 'lib/easy_captcha/espeak.rb', line 34 def pitch @pitch.is_a?(Range) ? @pitch.to_a.sample : @pitch.to_i end |
#voice ⇒ Object
42 43 44 |
# File 'lib/easy_captcha/espeak.rb', line 42 def voice (@voice.is_a?(Array) ? @voice.sample : @voice).try(:gsub, /[^A-Za-z0-9\-+]/, '') end |
Instance Method Details
#espeak_amplitude_param ⇒ Object
62 63 64 |
# File 'lib/easy_captcha/espeak.rb', line 62 def espeak_amplitude_param "-a #{amplitude}" unless @amplitude.nil? end |
#espeak_gap_param ⇒ Object
70 71 72 |
# File 'lib/easy_captcha/espeak.rb', line 70 def espeak_gap_param "-g #{gap}" unless @gap.nil? end |
#espeak_pitch_param ⇒ Object
66 67 68 |
# File 'lib/easy_captcha/espeak.rb', line 66 def espeak_pitch_param "-p #{pitch}" unless @pitch.nil? end |
#espeak_voice_param ⇒ Object
74 75 76 |
# File 'lib/easy_captcha/espeak.rb', line 74 def espeak_voice_param "-v '#{voice}'" unless @voice.nil? end |
#generate(captcha, wav_file) ⇒ Object
generate wav file by captcha
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# 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) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# 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 |
#set_defaults ⇒ Object
set default values
22 23 24 25 26 |
# 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 |