rubysounds 0.3 → 0.4

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 +95 -26
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3e2a5689b4a4620398f0996cca5195527fb57027
4
- data.tar.gz: ed8321f4a97d9d98cce23929adc3cc501be7c7a9
3
+ metadata.gz: 677bcb73d8594a5fab7092c4852cfe94178d6b9f
4
+ data.tar.gz: 2b18a5e8809be35049129cef5a039c513022ccf0
5
5
  SHA512:
6
- metadata.gz: bbcd8e6a47e7d9885da28dfe636d406a41a1d1763a221724b468afdb8a8bce1057b4b0d4526b1fa7c40bb8282a251ff64d2ec118e5caaf4e7c312ff9ecbbc8bf
7
- data.tar.gz: f4ce4eed9c1c1703671bba7e4fde9e50047e1beaab48f231dda3f8531d6b0533de2023393df8e936cd2ba39eda6061531dd8d77112c6c326f63c13979cedbfd4
6
+ metadata.gz: 84ded87b8c1c86c0b0935bb03043b267a0cf9e0698ba2bd7a3eb3ff658834144ad5ac691185834dc16681bf7cf316e431359a654221a733501bb3339f4769d4c
7
+ data.tar.gz: 35b8d51c69a4404c861d40b7f4ae69642b7a6df13af81e475ce1a30c88673e3173c6f29731680017535dfe5aaee5441d58c5e4d7cbbb4d2a0ff584ab9b2bb3a3
data/lib/rubysounds.rb CHANGED
@@ -1,31 +1,38 @@
1
- require 'net/http'
1
+ require "net/http"
2
2
 
3
3
  # Check actual OS
4
- $operative_system = 'other'
4
+ $operative_system = "other"
5
5
  if Gem.win_platform?
6
- $operative_system = 'win'
6
+ $operative_system = "win"
7
7
  end
8
8
 
9
+ $winlib = false
9
10
  # Import Windows-specific libraries
10
- if $operative_system == 'win'
11
- require 'win32/sound'
12
- include Win32
11
+ if $operative_system == "win"
12
+ begin
13
+ require "win32/sound"
14
+ include Win32
15
+ $winlib = true
16
+ rescue LoadError
17
+ #puts "win32-sound gem not installed"
18
+ puts ""
19
+ end
13
20
  end
14
21
 
15
22
  # Class for text-to-speech using Google Translate
16
23
  class Speech
17
- GOOGLE_TRANSLATE_URL = 'http://translate.google.com/translate_tts'.freeze
24
+ GOOGLE_TRANSLATE_URL = "http://translate.google.com/translate_tts".freeze
18
25
 
19
26
  attr_accessor :text, :lang
20
27
 
21
- def initialize(text, lang = 'en')
28
+ def initialize(text, lang = "en")
22
29
  @text = text
23
30
  @lang = lang
24
31
  end
25
32
 
26
- def self.load(file_path, lang = 'en')
33
+ def self.load(file_path, lang = "en")
27
34
  f = File.open(file_path)
28
- new f.read.encode('UTF-16be', invalid: :replace, replace: '?').encode('UTF-8'), lang
35
+ new f.read.encode("UTF-16be", invalid: :replace, replace: "?").encode("UTF-8"), lang
29
36
  end
30
37
 
31
38
  def save(file_path)
@@ -39,14 +46,14 @@ class Speech
39
46
 
40
47
  sentences.each_with_index do |q, _idx|
41
48
  uri.query = URI.encode_www_form(
42
- ie: 'UTF-8',
49
+ ie: "UTF-8",
43
50
  q: q,
44
51
  tl: lang,
45
52
  total: sentences.length,
46
53
  idx: 0,
47
54
  textlen: q.length,
48
- client: 'tw-ob',
49
- prev: 'input'
55
+ client: "tw-ob",
56
+ prev: "input"
50
57
  )
51
58
 
52
59
  res = Net::HTTP.get_response(uri)
@@ -56,7 +63,7 @@ class Speech
56
63
  response << res.body.force_encoding(Encoding::UTF_8)
57
64
  end
58
65
 
59
- File.open(file_path, 'wb') do |f|
66
+ File.open(file_path, "wb") do |f|
60
67
  f.write response.join
61
68
  return f.path
62
69
  end
@@ -82,15 +89,69 @@ class Speech
82
89
  end
83
90
 
84
91
 
85
- def vlcplay(target)
86
- system('vlc --play-and-exit --intf dummy ' + target)
92
+ #########################################################
93
+
94
+
95
+ # Array to store VLC sound processes
96
+ $children_sounds = Array.new
97
+
98
+ # Catching CTRL-C Keyboard Interrupt signal
99
+ trap "SIGINT" do
100
+ puts "Exiting"
101
+ # Kill all children sounds
102
+ for child_sound in $children_sounds
103
+ #puts child_sound.pid
104
+ # Kill child process...
105
+ #Process.kill("QUIT", child_sound.pid)
106
+ if $operative_system == "win"
107
+ system("Taskkill /PID " + child_sound.pid.to_s + " /F")
108
+ else
109
+ system("kill " + child_sound.pid.to_s)
110
+ end
111
+ #$children_sounds.delete(child_sound)
112
+ # This prevents the process from becoming defunct
113
+ #child_sound.close
114
+ end
115
+ # Kill main process
116
+ exit 130
87
117
  end
88
118
 
89
119
 
90
- def speak(text)
91
- Speech.new(text).save('temp.wav')
92
- system('vlc --play-and-exit --intf dummy temp.wav')
93
- File.delete('temp.wav')
120
+ def vlcplay(target, wait: true, bg: false, dummy: true)
121
+
122
+ # VLC command options
123
+ if $operative_system == "win"
124
+ vlccmd = "vlc "
125
+ if bg
126
+ vlccmd += "-I null "
127
+ end
128
+
129
+ vlccmd += "--play-and-exit "
130
+
131
+ if (bg == false) && (dummy == true)
132
+ vlccmd += "--intf dummy "
133
+ end
134
+ else
135
+ vlccmd = "cvlc --play-and-exit "
136
+ end
137
+
138
+ io = IO.popen(vlccmd + target)
139
+ $children_sounds.push(io)
140
+ # Wait for the sound to end option
141
+ if wait
142
+ Process.wait(io.pid)
143
+ end
144
+
145
+ #system(vlccmd + target)
146
+ #Thread.new { system(vlccmd + target) }
147
+
148
+ end
149
+
150
+
151
+ def speak(text, language: "en", wait: true, bg: true)
152
+ Speech.new(text, language).save("temp.wav")
153
+ vlcplay("temp.wav", wait: wait, bg: bg)
154
+ File.delete("temp.wav")
94
155
  end
95
156
 
96
157
 
@@ -100,21 +161,29 @@ def dub(text)
100
161
  end
101
162
 
102
163
 
103
- def play(path)
104
- vlcplay(path)
164
+ def play(path, wait: true, bg: true)
165
+ vlcplay(path, wait: wait, bg: bg)
105
166
  end
106
167
 
107
168
 
108
169
  # Windows-specific methods
109
170
  def beep(a, b)
110
- if $operative_system == 'win'
111
- Sound.beep(a, b)
171
+ if $winlib
172
+ if $operative_system == "win"
173
+ Sound.beep(a, b)
174
+ else
175
+ dub("the beep method is only avaible on Windows operative system")
176
+ end
112
177
  else
113
- dub('the beep method is only avaible on Windows operative system')
178
+ dub("Beep is not avaible. You need to gem install win32-sounds")
114
179
  end
115
180
  end
116
181
 
117
182
 
118
183
  def ms_win_play(path)
119
- Sound.play(path)
184
+ if $winlib
185
+ Sound.play(path)
186
+ else
187
+ dub("Beep is not avaible. You need to gem install win32-sounds")
188
+ end
120
189
  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.3'
4
+ version: '0.4'
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 00:00:00.000000000 Z
11
+ date: 2017-10-14 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