Add espeak feature.

This commit is contained in:
rulingcom 2022-10-13 13:06:24 +08:00
parent 87b0035bd6
commit aefc02d8a2
395 changed files with 4746 additions and 6 deletions

View File

@ -4,8 +4,13 @@ module RuCaptcha
return head :ok if request.head? return head :ok if request.head?
headers['Cache-Control'] = 'no-cache, no-store, max-age=0, must-revalidate' headers['Cache-Control'] = 'no-cache, no-store, max-age=0, must-revalidate'
headers['Pragma'] = 'no-cache' headers['Pragma'] = 'no-cache'
if params[:format] == "wav" and RuCaptcha.espeak?
data = generate_speech_rucaptcha
opts = { disposition: 'inline', type: 'audio/wav' }
else
data = generate_rucaptcha data = generate_rucaptcha
opts = { disposition: 'inline', type: 'image/gif' } opts = { disposition: 'inline', type: 'image/gif' }
end
send_data data, opts send_data data, opts
end end
end end

View File

@ -18,4 +18,18 @@ RuCaptcha.configure do
# self.strikethrough = true # self.strikethrough = true
# enable/disable Outline style # enable/disable Outline style
# self.outline = false # self.outline = false
# eSpeak
self.espeak do |espeak|
# Amplitude, 0 to 200
espeak.amplitude = 80..120
# Word gap. Pause between words
espeak.gap = 80
# Pitch adjustment, 0 to 99
espeak.pitch = 30..70
# Use voice file of this name from espeak-data/voices
espeak.voice = 'en-us'
end
end end

View File

@ -2,6 +2,7 @@ require 'rails'
require 'action_controller' require 'action_controller'
require 'active_support/all' require 'active_support/all'
require 'rucaptcha/rucaptcha' require 'rucaptcha/rucaptcha'
require 'rucaptcha/espeak'
require 'rucaptcha/version' require 'rucaptcha/version'
require 'rucaptcha/configuration' require 'rucaptcha/configuration'
require 'rucaptcha/controller_helpers' require 'rucaptcha/controller_helpers'
@ -35,6 +36,15 @@ module RuCaptcha
config.instance_exec(&block) config.instance_exec(&block)
end end
def espeak(&block)
@espeak ||= @config.instance_variable_get(:@espeak)
@espeak ||= false
end
def espeak?
not espeak === false
end
def generate() def generate()
style = config.style == :colorful ? 1 : 0 style = config.style == :colorful ? 1 : 0
length = config.length length = config.length

View File

@ -15,5 +15,20 @@ module RuCaptcha
attr_accessor :outline attr_accessor :outline
# skip_cache_store_check, default: false # skip_cache_store_check, default: false
attr_accessor :skip_cache_store_check attr_accessor :skip_cache_store_check
def espeak=(state)
if state === true
@espeak = Espeak.new
else
@espeak = false
end
end
def espeak(&block)
if block_given?
@espeak = Espeak.new &block
else
@espeak ||= false
end
end
end end
end end

View File

@ -28,6 +28,20 @@ module RuCaptcha
RuCaptcha.cache.write(rucaptcha_sesion_key_key, session_val, expires_in: RuCaptcha.config.expires_in) RuCaptcha.cache.write(rucaptcha_sesion_key_key, session_val, expires_in: RuCaptcha.config.expires_in)
res[1] res[1]
end end
def generate_speech_rucaptcha
raise RuntimeError, "espeak disabled" unless RuCaptcha.espeak?
wav_file = Tempfile.new("#{current_captcha_code}.wav")
RuCaptcha.espeak.generate(current_captcha_code, wav_file.path)
File.read(wav_file.path)
end
def current_captcha_code
if @code
@code
else
store_info = RuCaptcha.cache.read(rucaptcha_sesion_key_key)
@code = store_info[:code]
end
end
# Verify captcha code # Verify captcha code
# #

87
lib/rucaptcha/espeak.rb Normal file
View File

@ -0,0 +1,87 @@
module RuCaptcha
# espeak wrapper
class Espeak
require 'fileutils'
EspeakDataDir = "#{ENV['HOME']}/espeak-ng-data"
EspeakBuildDir = "#{ENV['HOME']}/.espeak_build"
EspeakLibDir = "#{ENV['HOME']}/shared_library"
unless Dir.exist?(EspeakDataDir)
FileUtils.cp_r(File.expand_path("../../../resources/espeak-ng-data", __FILE__), EspeakDataDir)
end
if Dir.exist?(EspeakBuildDir)
unless Dir.exist?(EspeakLibDir)
FileUtils.cp_r(File.expand_path("../../../resources/espeak_build/shared_library", __FILE__), EspeakLibDir)
end
else
FileUtils.cp_r(File.expand_path("../../../resources/espeak_build", __FILE__), EspeakBuildDir)
end
# generator for captcha images
def initialize(&block)
defaults
yield self if block_given?
end
# set default values
def defaults
@amplitude = 80..120
@pitch = 30..70
@gap = 80
@voice = nil
end
attr_writer :amplitude, :pitch, :gap, :voice
# return amplitude
def amplitude
if @amplitude.is_a? Range
@amplitude.to_a.sort_by { rand }.first
else
@amplitude.to_i
end
end
# return amplitude
def pitch
if @pitch.is_a? Range
@pitch.to_a.sort_by { rand }.first
else
@pitch.to_i
end
end
def gap
@gap.to_i
end
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
# generate wav file by captcha
def generate(captcha, wav_file)
# get code
if captcha.is_a? String
code = captcha
else
raise ArgumentError, "invalid captcha"
end
# add spaces
code = code.each_char.to_a.join(" ")
cmd = "~/.espeak_build/espeak-ng -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}'"
system({'LD_LIBRARY_PATH'=>"#{ENV['HOME']}/.espeak_build/shared_library"}, cmd)
true
end
end
end

View File

@ -13,11 +13,17 @@ module RuCaptcha
def rucaptcha_image_tag(opts = {}) def rucaptcha_image_tag(opts = {})
opts[:class] = opts[:class] || 'rucaptcha-image' opts[:class] = opts[:class] || 'rucaptcha-image'
opts[:src] = ru_captcha.root_path opts[:src] = ru_captcha.root_path(:locale=>nil)
opts[:onclick] = "this.src = '#{ru_captcha.root_path}?t=' + Date.now();" opts[:onclick] = "this.t=Date.now();this.src = '#{opts[:src]}?t=' + this.t;this.audio=document.getElementById('captcha_audio');this.audio ? this.audio.src= '#{opts[:src]}?format=wav&t=' + this.t : null;"
if RuCaptcha.espeak? && opts[:espeak]
opts[:onload] = "this.already_load ? null : ($(this).after('#{rucaptcha_audio_tag}') && (this.already_load = true));"
opts.delete(:espeak)
end
tag(:img, opts) tag(:img, opts)
end end
def rucaptcha_audio_tag()
"<button type=\"button\" onclick=\"document.getElementById(&quot;captcha_audio&quot;).play();\" class=\"fas fa-volume-up\" style=\"font-size: 1.5em; color: #333; cursor: pointer; border: 2px solid #333; padding: 0.2em;\"></button><audio id=\"captcha_audio\" src=\"#{ru_captcha.root_path(:format=>:wav)}\"></audio>".html_safe
end
def gotcha(opts = {}) def gotcha(opts = {})
if opts[:placeholder].blank? if opts[:placeholder].blank?
opts[:placeholder] = I18n.t('rucaptcha.placeholder') opts[:placeholder] = I18n.t('rucaptcha.placeholder')
@ -28,7 +34,8 @@ module RuCaptcha
if opts[:alt].blank? if opts[:alt].blank?
opts[:alt] = I18n.t('rucaptcha.captcha') opts[:alt] = I18n.t('rucaptcha.captcha')
end end
rucaptcha_image_tag(opts) captcha_html = rucaptcha_image_tag(opts)
captcha_html
end end
end end

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,8 @@
name Vietnamese (Northern)
language vi
words 1 2
pitch 95 175
tone 100 225 800 100 2000 50 5400 75 8000 200

View File

@ -0,0 +1,11 @@
name Vietnamese (Central)
language vi-vn-x-central
phonemes vi-hue
dictrules 1
words 1
pitch 82 118 //80 118
//breath 75 75 60 40 15 10
//breathw 150 150 200 200 400 400
voicing 90 //18
flutter 20

View File

@ -0,0 +1,11 @@
name Vietnamese (Southern)
language vi-vn-x-south
phonemes vi-sgn
dictrules 2
words 1
pitch 82 118 //80 118
//breath 75 75 60 40 15 10
//breathw 150 150 200 200 400 400
voicing 90 //18
flutter 20

View File

@ -0,0 +1,4 @@
name Esperanto
language eo
apostrophe 2

View File

@ -0,0 +1,2 @@
name Interlingua
language ia

View File

@ -0,0 +1,5 @@
name Ido
language io
phonemes eo
status testing

View File

@ -0,0 +1,4 @@
name Lojban
language jbo
speed 80 // speed adjustment, percentage

View File

@ -0,0 +1,8 @@
name Lingua Franca Nova
language lfn
phonemes base2
l_unpronouncable 0
numbers 2 3
stressLength 150 140 180 180 0 0 200 200

View File

@ -0,0 +1,5 @@
name Klingon
language piqd
status testing
stressRule 3

View File

@ -0,0 +1,7 @@
name Pyash
language py
maintainer Logan Streondj <logan@liberit.ca>
status testing
speed 80 // speed adjustment, percentage
stressRule 0

View File

@ -0,0 +1,6 @@
name Lang Belta
language qdb
numbers 4 3
replace 1 t ?

View File

@ -0,0 +1,4 @@
name Quenya
language qya
stressRule 2
// rule=penultimate, with qya_rules for light penultimate syllables to move primary stress to the preceding (antepenultimate) syllable

View File

@ -0,0 +1,4 @@
name Sindarin
language sjn
stressRule 2
// rule=penultimate, with sjn_rules for light penultimate syllables to move primary stress to the preceding (antepenultimate) syllable

View File

@ -0,0 +1,6 @@
name Nahuatl (Classical)
language nci
intonation 3
stressRule 2
stressLength 190 190 200 200 0 0 220 240

View File

@ -0,0 +1,2 @@
name Lithuanian
language lt

View File

@ -0,0 +1,14 @@
name Latgalian
language ltg
maintainer Valdis Vitolins <valdis.vitolins@odo.lv>
status testing
phonemes lv
dictionary lv
dictrules 2 // Setting for Latgalian pronunciation
words 0 2
pitch 64 118
breath 10 2 1 0 0 0 0 0
breathw 20 42 85 200 500 1000
tone 60 150 204 100 400 255 700 10 3000 255
stressAmp 12 10 8 8 0 0 15 16
stressLength 160 140 200 140 0 0 240 160

View File

@ -0,0 +1,11 @@
name Latvian
language lv
maintainer Valdis Vitolins <valdis.vitolins@odo.lv>
status mature
words 0 2
pitch 67 123
breath 10 2 1 0 0 0 0 0
breathw 20 42 85 200 500 1000
tone 60 150 204 100 400 255 700 10 3000 255
stressAmp 11 8 11 9 0 0 14 12
stressLength 160 120 200 130 0 0 230 180

View File

@ -0,0 +1,4 @@
name Swahili
language sw
status testing

View File

@ -0,0 +1,4 @@
name Setswana
language tn
status testing

View File

@ -0,0 +1,3 @@
name Georgian
language ka
lowercaseSentence // A period followed by a lowercase letter is considered a sentence (mkhedruli)

View File

@ -0,0 +1,4 @@
name Welsh
language cy
intonation 4

View File

@ -0,0 +1,4 @@
name Gaelic (Irish)
language ga
dictrules 1 // fix for eclipsis

View File

@ -0,0 +1,4 @@
name Gaelic (Scottish)
language gd
status testing

View File

@ -0,0 +1,4 @@
name Oromo
language om
status testing

View File

@ -0,0 +1,5 @@
name Kannada
language kn
intonation 2
//consonants 80

View File

@ -0,0 +1,5 @@
name Malayalam
language ml
intonation 2
//consonants 80

View File

@ -0,0 +1,5 @@
name Tamil
language ta
intonation 2
consonants 80

View File

@ -0,0 +1,7 @@
name Telugu
language te
status testing
intonation 2
//consonants 80

View File

@ -0,0 +1,3 @@
name Greenlandic
language kl

View File

@ -0,0 +1,5 @@
name Basque
language eu
status testing
stressRule 15

View File

@ -0,0 +1,4 @@
name Danish
language da
tunes s2 c2 q2 e2

View File

@ -0,0 +1,2 @@
name Icelandic
language is

View File

@ -0,0 +1,7 @@
name Norwegian Bokmål
language nb
language no
phonemes no
dictionary no
intonation 4

View File

@ -0,0 +1,2 @@
name Swedish
language sv

View File

@ -0,0 +1,8 @@
name Afrikaans
language af
maintainer Christo de Klerk <christodeklerk@gmail.com>
status mature
roughness 0
pitch 63 120

View File

@ -0,0 +1,3 @@
name German
language de
tunes s4 c1 q4 e1

View File

@ -0,0 +1,8 @@
name English (Great Britain)
language en-gb 2
language en 2
maintainer Reece H. Dunn <msclrhd@gmail.com>
status mature
tunes s1 c1 q1 e1

View File

@ -0,0 +1,20 @@
name English (Caribbean)
language en-029
language en 10
maintainer Reece H. Dunn <msclrhd@gmail.com>
status mature
phonemes en-wi
dictrules 8
stressLength 175 175 175 175 220 220 250 290
replace 00 D d
replace 00 T t[
replace 00 U@ o@
replace 03 @ a#
replace 03 3 a#
replace 03 N n
formant 1 98 100 100
formant 2 98 100 100

View File

@ -0,0 +1,17 @@
name English (Scotland)
language en-gb-scotland
language en 4
maintainer Reece H. Dunn <msclrhd@gmail.com>
status mature
phonemes en-sc
dictrules 2 5 6 7
stressLength 180 130 200 200 0 0 250 270
replace 03 @ V
replace 03 I i
replace 03 I2 i
replace 01 aI aI2
replace 02 a a/
replace 02 u: U

View File

@ -0,0 +1,14 @@
name English (Lancaster)
language en-gb-x-gbclan
language en-gb 3
language en 5
maintainer Reece H. Dunn <msclrhd@gmail.com>
status mature
phonemes en-n
stressLength 160 150 180 180 220 220 290 290
replace 03 N n
replace 03 i I2

View File

@ -0,0 +1,12 @@
name English (West Midlands)
language en-gb-x-gbcwmd
language en-gb 9
language en 9
phonemes en-wm
replace 00 h NULL
replace 00 o@ O@
dictrules 6
intonation 4
stressAdd 0 0 0 0 0 0 0 20

View File

@ -0,0 +1,15 @@
name English (Received Pronunciation)
language en-gb-x-rp
language en-gb 4
language en 5
maintainer Reece H. Dunn <msclrhd@gmail.com>
status mature
phonemes en-rp
replace 00 o@ O@
replace 03 I i
replace 03 I2 i
replace 03 @ a#
replace 03 3 a#

View File

@ -0,0 +1,15 @@
name English (America)
language en-us 2
language en 3
maintainer Reece H. Dunn <msclrhd@gmail.com>
status mature
phonemes en-us
dictrules 3 6
stressLength 140 120 190 170 0 0 255 300
stressAmp 17 16 19 19 19 19 21 19
replace 03 I i
replace 03 I2 i

View File

@ -0,0 +1,14 @@
name English (America, New York City)
language en-us-nyc
maintainer Richard Calvi <richard.calvi@gmail.com>
status testing
phonemes en-us-nyc
dictrules 3 6
stressLength 140 120 190 170 0 0 255 300
stressAmp 17 16 19 19 19 19 21 19
replace 03 I i
replace 03 I2 i

View File

@ -0,0 +1,2 @@
name Luxembourgish
language lb

View File

@ -0,0 +1,2 @@
name Dutch
language nl

View File

@ -0,0 +1,2 @@
name Greek
language el

View File

@ -0,0 +1,6 @@
name Greek (Ancient)
language grc
stressLength 170 170 190 190 0 0 230 240
dictrules 1
words 3

View File

@ -0,0 +1,4 @@
name Assamese
language as
status testing

View File

@ -0,0 +1,2 @@
name Bengali
language bn

View File

@ -0,0 +1,2 @@
name Bishnupriya Manipuri
language bpy

View File

@ -0,0 +1,4 @@
name Gujarati
language gu
status testing

View File

@ -0,0 +1,2 @@
name Hindi
language hi

View File

@ -0,0 +1,2 @@
name Konkani
language kok

View File

@ -0,0 +1,4 @@
name Marathi
language mr
status testing

View File

@ -0,0 +1,4 @@
name Nepali
language ne
dictrules 1

View File

@ -0,0 +1,4 @@
name Oriya
language or
status testing

View File

@ -0,0 +1,2 @@
name Punjabi
language pa

View File

@ -0,0 +1,3 @@
name Sindhi
language sd
maintainer Ejaz Shah <eashah67@gmail.com>

View File

@ -0,0 +1,6 @@
name Sinhala
language si
status testing
intonation 2

View File

@ -0,0 +1,7 @@
name Urdu
language ur
maintainer Ejaz Shah <eashah67@gmail.com>
status testing
stressRule 6

View File

@ -0,0 +1,3 @@
name Armenian (East Armenia)
language hy
language hy-arevela

View File

@ -0,0 +1,24 @@
name Armenian (West Armenia)
language hyw
language hy-arevmda
language hy 8
dictionary hy
dictrules 1
phonemes hy
// change consonants for West Armenian pronunciation
replace 00 b p#
replace 00 d t#
replace 00 dz ts#
replace 00 dZ tS#
replace 00 g k#
replace 00 p b
replace 00 t d
replace 00 ts dz
replace 00 tS dZ
replace 00 k g
replace 00 R2 R // ??

View File

@ -0,0 +1,5 @@
name Albanian
language sq
// add this line to remove 'ë' at the end of words
// replace 00 @/ NULL

View File

@ -0,0 +1,4 @@
name Persian
language fa
maintainer Shadyar Khodayari <shadyar81@gmail.com>
status mature

View File

@ -0,0 +1,8 @@
name Persian (Pinglish)
// Sometimes, Farsi speakers write Farsi words using English characters, particularly in Chat and SMS (texte messages).), called Pinglish
language fa-latn
maintainer Shadyar Khodayari <shadyar81@gmail.com>
status mature
dictrules 1
phonemes fa

View File

@ -0,0 +1,5 @@
name Kurdish
language ku
//words 1 48

View File

@ -0,0 +1,27 @@
name Cherokee //https://github.com/espeak-ng/espeak-ng/blob/master/docs/voices.md
language chr-US-Qaaa-x-west 5
maintainer Michael Conrad <m.conrad.202@gmail.com>
status testing
pitch 90 160
voicing 100
consonants 100 100
speed 100
words 2 1
phonemes chr
//stress on all syllables to simulate stress on no syllables
stressRule 9
stressLength 175 175 175 175 175 175 175 175 //all vowels the same length regardless of stress
stressAmp 10 10 10 10 10 10 10 10 //all vowels the same strength regardless of marked stress
intonation 1
tunes chrs chrc chrq chre

View File

@ -0,0 +1,12 @@
name Latin
language la
stressRule 2 0 2
// rule=penultimate
// unstressed_wd1=0
// unstressed_wd2=2
stressOpt 0 5 // flags=0100001 (no automatic secondary stress + don't stres monosyllables)
// short gap between words
words 2
// Note: The Latin voice needs long vowels to be marked with macrons

View File

@ -0,0 +1,5 @@
name Japanese
language ja
phonemes ja
intonation 4

View File

@ -0,0 +1,5 @@
name Korean
language ko
pitch 80 118
intonation 2

View File

@ -0,0 +1,3 @@
name Hawaiian
language haw
status testing

View File

@ -0,0 +1,8 @@
name Totontepec Mixe
language mto
maintainer Bill Dengler <codeofdusk@gmail.com> and Elizabeth Resendiz <e.r.resendiz7@gmail.com>
status testing
lowercaseSentence
tunes s6 c6 q6 e6

View File

@ -0,0 +1,6 @@
name K'iche'
language quc
status testing
stressRule 3 // stress on final syllable
stressAmp 8 8 20 15 0 0 25 25 // reduce unstressed vowels
stressLength 120 120 200 150 0 0 250 250 // reduce unstressed vowels

View File

@ -0,0 +1,7 @@
name Indonesian
language id
stressLength 160 200 180 180 0 0 220 240
stressAmp 16 18 18 18 0 0 22 21
consonants 80 80

View File

@ -0,0 +1,21 @@
name Māori
language mi
status testing
// https://github.com/espeak-ng/espeak-ng/blob/master/docs/voices.md#words
words 1 2
// taken from Jacky
pitch 115 130
formant 0 150 155 100
formant 1 90 155 70
formant 2 95 70 64
formant 3 15 20 30
formant 4 20 30 40
formant 5 65 20 65
formant 6 70 80 100
formant 7 20 80 100
formant 8 100 95 80
voicing 135
consonants 110

View File

@ -0,0 +1,14 @@
// Last updated: 14 October 2010, Jason Ong (jason@portalgroove.com)
name Malay
language ms
phonemes id
stressLength 160 200 180 180 0 0 220 240
stressAmp 16 18 18 18 0 0 22 21
intonation 3 // Less intonation, and comma does not raise the pitch.
// Nuance - Peninsula Malaysia
// replace 3 a @ // change 'saya' to 'saye'
// (only the last phoneme of a word, only in unstressed syllables)
consonants 80 80

View File

@ -0,0 +1,5 @@
name Quechua
language qu
stressRule 2 // stress on penultimate syllable
status testing

View File

@ -0,0 +1,2 @@
name Aragonese
language an

View File

@ -0,0 +1,2 @@
name Catalan
language ca

View File

@ -0,0 +1,4 @@
name Spanish (Spain)
language es
dictrules 1
tunes s6 c6 q6 e6

View File

@ -0,0 +1,11 @@
name Spanish (Latin America)
language es-419
language es-mx 6
language es 6
phonemes es-la
dictrules 2
intonation 2
stressLength 170 200 230 180 0 0 250 280
replace 00 T s

View File

@ -0,0 +1,6 @@
name French (France)
language fr-fr
language fr
dictrules 1
tunes s3 c3 q3 e3

View File

@ -0,0 +1,8 @@
name French (Belgium)
language fr-be
language fr 8
dictrules 2
tunes s3 c3 q3 e3

View File

@ -0,0 +1,6 @@
name French (Switzerland)
language fr-ch
language fr 8
dictrules 3
tunes s3 c3 q3 e3

View File

@ -0,0 +1,8 @@
name Haitian Creole
language ht
status testing
maintainer // TODO somebody should take responsibility for this
phonemes ht
dictionary ht

View File

@ -0,0 +1,7 @@
name Italian
language it
maintainer Christian Leo M <llajta2012@gmail.com>
status mature
tunes s4 c4 q4 e4

View File

@ -0,0 +1,7 @@
name Papiamento
language pap
status testing
phonemes base2

View File

@ -0,0 +1,7 @@
name Portuguese (Portugal)
language pt
language pt-pt
phonemes pt-pt
dictrules 1
intonation 2

View File

@ -0,0 +1,7 @@
name Portuguese (Brazil)
language pt-br
language pt 6
dictrules 2
stressLength 200 115 230 230 0 0 250 270

View File

@ -0,0 +1,2 @@
name Romanian
language ro

Some files were not shown because too many files have changed in this diff Show More