This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtxt2speech.rb
369 lines (308 loc) · 8.5 KB
/
txt2speech.rb
1
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
require 'sqlite3'
require 'audio-playback'
require 'ruby-audio'
require 'tmpdir'
require './string_helper'
include RubyAudio
class TXT2Speech
def initialize
current = Time.now
day = current.day
month = current.month
year = current.year
hour = current.hour
min = current.min
sec = current.sec
hour > 12 ? (hour = hour - 12; meridan = "chiều") : (meridan = "sáng")
@input = TimeNormalizationToVietnamese.timestandard(year,month,day,hour,min,sec,meridan)
@db_instance = DBExec.new
end
def action
@speaker = SoundCutter.new
@db_instance.create_db
arr = get_system_time_in_telex
arr.each_with_index {|txt, i|
i - 1 < 0 ? previous_word = nil : previous_word = arr[i-1]
current_word = txt
i + 1 < arr.length ? next_word = arr[i+1] : next_word = nil
file, occur_from, occur_to = @db_instance.query_db(previous_word, current_word, next_word, arr, i)
@speaker.create_snippet(file, occur_from, occur_to)
}
@speaker.play
puts "Completed"
end
def get_system_time_in_telex
# will be an array
@input.split(" ").map {|item| item.get_converted_prefix}
end
end
class DBExec
DBNAME = "speech.db"
TABLENAME = "speech_patterns"
def initialize
@alignment = File.open(File.join("mlf", "aligned.mlf"))
@db = SQLite3::Database.new DBNAME
@first_priority_file = nil
end
def data_existed?
count = @db.execute("select count(1) from #{TABLENAME}").first.first
if count > 0
return true
end
return false
rescue
return false
end
def create_db
return if data_existed?
create_schema
save_alignment_info
end
def create_schema
@db.execute <<-SQL
CREATE TABLE IF NOT EXISTS #{TABLENAME}
(
filename text,
previous_word text,
word text,
next_word text,
occur_from text,
occur_to text
)
SQL
end
def save_alignment_info
@filename = nil
@word = nil
@next_word = nil
@previous_word = nil
@occur_from = nil
@occur_to = nil
@alignment.each do |line|
line.chomp!
next if line == '#!MLF!#' ||
line == "."
linefeed = line.split(" ")
next if linefeed.last == "SENT-END" && @word == nil
if linefeed.size == 1
@filename = linefeed.first.gsub(".lab", ".wav").gsub(/^"|"$/, '')
@word = nil
@next_word = nil
@previous_word = nil
@occur_from = nil
@occur_to = nil
next
end
if linefeed.last == "SENT-END" && @word
@occur_to = linefeed.first
@next_word = nil
insert_speech_to_db(@filename, @word, @next_word, @previous_word, @occur_from, @occur_to)
@previous_word = @word
next
end
if linefeed.size == 4
if @word
@next_word = linefeed.last
@occur_to = linefeed.first
insert_speech_to_db(@filename, @word, @next_word, @previous_word, @occur_from, @occur_to)
@previous_word = @word
end
@word = linefeed.last
@occur_from = linefeed.first
end
end
end
# bay gio la chin gio muoi mot phut
def query_db(previous_word, word, next_word, array, index)
raise "DB is not found" if !File.exists? DBNAME
# uu tien trong cung file
if @first_priority_file
result = @db.execute(
"SELECT filename, occur_from, occur_to
FROM #{TABLENAME}
WHERE previous_word = ? AND word = ? AND next_word = ?
AND filename = ?
LIMIT 1",
[previous_word.to_s, word.to_s, next_word.to_s, @first_priority_file]
).first
if result.present?
return result
else
result = @db.execute(
"SELECT filename, occur_from, occur_to
FROM #{TABLENAME}
WHERE previous_word = ? AND word = ?
AND filename = ?
LIMIT 1",
[previous_word.to_s, word.to_s, @first_priority_file]
).first
return result if result.present?
end
end
# query 3 tu truoc
result = @db.execute(
"SELECT filename, occur_from, occur_to
FROM #{TABLENAME}
WHERE previous_word = ? AND word = ? AND next_word = ?
LIMIT 1",
[previous_word.to_s, word.to_s, next_word.to_s]
).first
# query tu dung truoc va tu hien tai
result = @db.execute(
"SELECT filename, occur_from, occur_to
FROM #{TABLENAME}
WHERE previous_word = ? AND word = ?
LIMIT 1",
[previous_word.to_s, word.to_s]
).first unless result.present?
# query tu dung sau va tu hien tai
result = @db.execute(
"SELECT filename, occur_from, occur_to
FROM #{TABLENAME}
WHERE word = ? AND next_word = ?
LIMIT 1",
[word.to_s, next_word.to_s]
).first unless result.present?
# query chi mot tu khong quan tam dung truoc hay dung sau
result = @db.execute(
"SELECT filename, occur_from, occur_to
FROM #{TABLENAME}
WHERE word = ?
LIMIT 1",
[word.to_s]
).first unless result.present?
raise "No word found in aligned.mlf: #{word.to_s}" unless result.present?
@first_priority_file = result.first
return result
end
def insert_speech_to_db(filename, word, next_word, previous_word, occur_from, occur_to)
# puts "Item: #{filename} |#{previous_word}| |#{word}| |#{next_word}| #{occur_from} #{occur_to}"
@db.execute(
"INSERT INTO #{TABLENAME} (filename, previous_word, word, next_word, occur_from, occur_to)
VALUES (?, ?, ?, ?, ?, ?)",
[filename.to_s, previous_word.to_s, word.to_s, next_word.to_s, occur_from.to_s, occur_to.to_s]
)
end
end
class TimeNormalizationToVietnamese
HASH = {
1 => "một",
2 => "hai",
3 => "ba",
4 => "bốn",
5 => "năm",
6 => "sáu",
7 => "bảy",
8 => "tám",
9 => "chín",
0 => "không"
}
def self.timestandard(year, month, day, hour, min, sec, meridan)
return ["bây giờ là",
"#{self.numstandard(hour)} giờ",
"#{self.numstandard(min)} phút",
"#{self.numstandard(sec)} giây",
"#{meridan}",
"ngày #{self.numstandard(day)}",
"tháng #{self.numstandard(month)}",
"năm #{self.numstandard(year)}"
].join(" ")
end
def self.numstandard(int, odd = nil)
# 2015 = hai nghin khong tram muoi lam
# return if over 9999
raise "DayTime number is too high: #{int}" if int > 9999
if int < 100
unit = int % 10
tenth = int / 10
if odd && tenth == 0
return "lẻ " + HASH[unit]
end
if unit == 0
case tenth
when 0
return HASH[tenth]
when 1
return "mười"
else
return HASH[tenth] + " mươi"
end
end
if unit == 5
case tenth
when 0
return HASH[unit]
when 1
return "mười lăm"
else
return HASH[tenth] + " lăm"
end
end
if tenth == 1
return "mười " + HASH[unit]
end
if tenth == 0
return HASH[unit]
end
return [HASH[tenth], "mươi", HASH[unit]].join(" ")
elsif int < 1000
# hundred
hundredth = int / 100
the_rest = int % 100
return [HASH[hundredth], "trăm", numstandard(the_rest, true)].join(" ")
elsif int < 10000
# thousand
thousandth = int / 1000
the_rest = int % 1000
return [HASH[thousandth], "nghìn", numstandard(the_rest, true)].join(" ")
end
end
end
class SoundCutter
# increase latency between words | default 0
SHORT_PAUSE_BUFFER = 0.03
def initialize
puts "Chon thiet bi playback truoc khi phat ra am thanh"
@output = AudioPlayback::Device::Output.gets
@os_temp_dir = Dir.tmpdir()
@snippets = []
end
def create_snippet(file, occur_from, occur_to)
first_seen = occur_from.to_i / 10_000_000.to_f
last_seen = occur_to.to_i / 10_000_000.to_f + SHORT_PAUSE_BUFFER
duration = last_seen - first_seen
sound_original_comp = file
sound_original_extn = File.extname sound_original_comp
# grab the base filename for later
sound_original_name = File.basename sound_original_comp, sound_original_extn
Sound.open(sound_original_comp) do |snd|
info = snd.info
# pick first_seen as starting point in the file
snip_time_begin = first_seen
# create a buffer as big as the snippet
bytes_to_read = (info.samplerate * duration).to_i
buf = Buffer.new("float", bytes_to_read, info.channels)
# seek to where the snippet begins and grab the audio
snd.seek(info.samplerate * snip_time_begin)
snd.read(buf, bytes_to_read)
# create new file's name from original to tmp dir
sndsnip_name = File.join(@os_temp_dir, sound_original_name + Random.new.rand(10000).to_s + "_snippet.wav")
# write the new snippet to a file
out = Sound.open(sndsnip_name, "w", info.clone)
out.write(buf)
@snippets << sndsnip_name
end
end
def play
@sounds = @snippets.map { |file| AudioPlayback::Sound.load(file) }
@stream = nil
@sounds.each_with_index do |sound, i|
@playback = AudioPlayback::Playback.new(@sounds[i], @output, :stream => @stream)
@stream ||= @playback.stream
# Start playback
@playback.start
# Play in foreground
@playback.block
end
end
end