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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rubysounds.rb +119 -83
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 762fed59395e62f0e318afdb8ddd05f057feb888
4
- data.tar.gz: d1d222687a3431757d3961f9c0a118e91f7a2bac
3
+ metadata.gz: d05bfef592bbb1bede1f4c2e4174fa36152fc1e0
4
+ data.tar.gz: dad7930402ca71203d91e2aa819c4e911457b09b
5
5
  SHA512:
6
- metadata.gz: 073a49220317ea42ca3b81012302e96aa44bc809c96f172481f2b82a0e9fe4b11b8a966f8a6ecb8bbc42cef68f71d48f81ae5c56fb66a3f63e31d369b3e7c1ad
7
- data.tar.gz: 4e1ab59cff8669032c956426e4a52e37448424f4ec9439a75d6755699a6bc188c99e2c4909a549ee7d12dc605349efc3f8d8ad4b210fc515af196e6383ce2643
6
+ metadata.gz: c7f5a2fc5b37b13315eceaa3cbd5ae43cd09c251021bd4d23cc5e2961e8c05dfa0193c2e207f29ba9df92b067f808e3c9e154117da4cf0b8c8fc18fcc1ebe05a
7
+ data.tar.gz: 5c8994edf2d73aa922a396533c562024e051dc250881abcb387770948016b086c38cb81d247647f0db09b8e8e1afcd3c178cae184ef3ab27af2715ab81615d2a
data/lib/rubysounds.rb CHANGED
@@ -1,83 +1,119 @@
1
- require 'net/http'
2
- require 'win32/sound'
3
- include Win32
4
-
5
- class Speech
6
- GOOGLE_TRANSLATE_URL = 'http://translate.google.com/translate_tts'.freeze
7
-
8
- attr_accessor :text, :lang
9
-
10
- def initialize(text, lang = 'en')
11
- @text = text
12
- @lang = lang
13
- end
14
-
15
- def self.load(file_path, lang = 'en')
16
- f = File.open(file_path)
17
- new f.read.encode('UTF-16be', invalid: :replace, replace: '?').encode('UTF-8'), lang
18
- end
19
-
20
- def save(file_path)
21
- uri = URI(GOOGLE_TRANSLATE_URL)
22
-
23
- response = []
24
-
25
- sentences = text.split(/[,.\r\n]/i)
26
- sentences.reject!(&:empty?)
27
- sentences.map! { |t| divide(t.strip) }.flatten!
28
-
29
- sentences.each_with_index do |q, _idx|
30
- uri.query = URI.encode_www_form(
31
- ie: 'UTF-8',
32
- q: q,
33
- tl: lang,
34
- total: sentences.length,
35
- idx: 0,
36
- textlen: q.length,
37
- client: 'tw-ob',
38
- prev: 'input'
39
- )
40
-
41
- res = Net::HTTP.get_response(uri)
42
-
43
- next unless res.is_a?(Net::HTTPSuccess)
44
-
45
- response << res.body.force_encoding(Encoding::UTF_8)
46
- end
47
-
48
- File.open(file_path, 'wb') do |f|
49
- f.write response.join
50
- return f.path
51
- end
52
- end
53
-
54
- private
55
-
56
- def divide(text)
57
- return text if text.length < 150
58
-
59
- attempts = text.length / 150.0
60
- starts = 0
61
- arr = []
62
-
63
- attempts.ceil.times do
64
- ends = starts + 150
65
- arr << text[starts...ends]
66
- starts = ends
67
- end
68
-
69
- arr
70
- end
71
- end
72
-
73
- def speak(text)
74
- Speech.new(text).save('temp.wav')
75
- #system 'vlc/vlc.exe --intf dummy temp.wav'
76
- system 'vlc --play-and-exit --intf dummy temp.wav'
77
- #pid = fork{ exec 'mpg123','-q', temp.wav }
78
- #Sound.play("temp.wav")
79
- end
80
-
81
- def beep(a, b)
82
- Sound.beep(a, b)
83
- end
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.1'
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-10 00:00:00.000000000 Z
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.5.1
40
+ rubygems_version: 2.6.13
41
41
  signing_key:
42
42
  specification_version: 4
43
43
  summary: Ruby gem to reproduce sounds