rubysounds 0.1 → 0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/rubysounds.rb +119 -83
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d05bfef592bbb1bede1f4c2e4174fa36152fc1e0
|
|
4
|
+
data.tar.gz: dad7930402ca71203d91e2aa819c4e911457b09b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c7f5a2fc5b37b13315eceaa3cbd5ae43cd09c251021bd4d23cc5e2961e8c05dfa0193c2e207f29ba9df92b067f808e3c9e154117da4cf0b8c8fc18fcc1ebe05a
|
|
7
|
+
data.tar.gz: 5c8994edf2d73aa922a396533c562024e051dc250881abcb387770948016b086c38cb81d247647f0db09b8e8e1afcd3c178cae184ef3ab27af2715ab81615d2a
|
data/lib/rubysounds.rb
CHANGED
|
@@ -1,83 +1,119 @@
|
|
|
1
|
-
require 'net/http'
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
1
|
+
require 'net/http'
|
|
2
|
+
|
|
3
|
+
# Check actual OS
|
|
4
|
+
$operative_system = 'other'
|
|
5
|
+
if Gem.win_platform?
|
|
6
|
+
$operative_system = 'win'
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
# Import Windows-specific libraries
|
|
10
|
+
if $operative_system == 'win'
|
|
11
|
+
require 'win32/sound'
|
|
12
|
+
include Win32
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Class for text-to-speech using Google Translate
|
|
16
|
+
class Speech
|
|
17
|
+
GOOGLE_TRANSLATE_URL = 'http://translate.google.com/translate_tts'.freeze
|
|
18
|
+
|
|
19
|
+
attr_accessor :text, :lang
|
|
20
|
+
|
|
21
|
+
def initialize(text, lang = 'en')
|
|
22
|
+
@text = text
|
|
23
|
+
@lang = lang
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.load(file_path, lang = 'en')
|
|
27
|
+
f = File.open(file_path)
|
|
28
|
+
new f.read.encode('UTF-16be', invalid: :replace, replace: '?').encode('UTF-8'), lang
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def save(file_path)
|
|
32
|
+
uri = URI(GOOGLE_TRANSLATE_URL)
|
|
33
|
+
|
|
34
|
+
response = []
|
|
35
|
+
|
|
36
|
+
sentences = text.split(/[,.\r\n]/i)
|
|
37
|
+
sentences.reject!(&:empty?)
|
|
38
|
+
sentences.map! { |t| divide(t.strip) }.flatten!
|
|
39
|
+
|
|
40
|
+
sentences.each_with_index do |q, _idx|
|
|
41
|
+
uri.query = URI.encode_www_form(
|
|
42
|
+
ie: 'UTF-8',
|
|
43
|
+
q: q,
|
|
44
|
+
tl: lang,
|
|
45
|
+
total: sentences.length,
|
|
46
|
+
idx: 0,
|
|
47
|
+
textlen: q.length,
|
|
48
|
+
client: 'tw-ob',
|
|
49
|
+
prev: 'input'
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
res = Net::HTTP.get_response(uri)
|
|
53
|
+
|
|
54
|
+
next unless res.is_a?(Net::HTTPSuccess)
|
|
55
|
+
|
|
56
|
+
response << res.body.force_encoding(Encoding::UTF_8)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
File.open(file_path, 'wb') do |f|
|
|
60
|
+
f.write response.join
|
|
61
|
+
return f.path
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
def divide(text)
|
|
68
|
+
return text if text.length < 150
|
|
69
|
+
|
|
70
|
+
attempts = text.length / 150.0
|
|
71
|
+
starts = 0
|
|
72
|
+
arr = []
|
|
73
|
+
|
|
74
|
+
attempts.ceil.times do
|
|
75
|
+
ends = starts + 150
|
|
76
|
+
arr << text[starts...ends]
|
|
77
|
+
starts = ends
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
arr
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def vlcplay(target)
|
|
86
|
+
system('vlc --play-and-exit --intf dummy ' + target)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def speak(text)
|
|
91
|
+
Speech.new(text).save('temp.wav')
|
|
92
|
+
system('vlc --play-and-exit --intf dummy temp.wav')
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def dub(text)
|
|
97
|
+
puts(text)
|
|
98
|
+
speak(text)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def play(path)
|
|
103
|
+
vlcplay(path)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
# Windows-specific methods
|
|
108
|
+
def beep(a, b)
|
|
109
|
+
if $operative_system == 'win'
|
|
110
|
+
Sound.beep(a, b)
|
|
111
|
+
else
|
|
112
|
+
dub('the beep method is only avaible on Windows operative system')
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def ms_win_play(path)
|
|
118
|
+
Sound.play(path)
|
|
119
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rubysounds
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: '0.
|
|
4
|
+
version: '0.2'
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Alessandro Norfo
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2017-10-
|
|
11
|
+
date: 2017-10-11 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: Ruby library to manage sounds using Google Translate & command line VLC.
|
|
14
14
|
email: ale.norfo@gmail.com
|
|
@@ -37,7 +37,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
37
37
|
version: '0'
|
|
38
38
|
requirements: []
|
|
39
39
|
rubyforge_project:
|
|
40
|
-
rubygems_version: 2.
|
|
40
|
+
rubygems_version: 2.6.13
|
|
41
41
|
signing_key:
|
|
42
42
|
specification_version: 4
|
|
43
43
|
summary: Ruby gem to reproduce sounds
|