-
Notifications
You must be signed in to change notification settings - Fork 0
/
speaker.rb
48 lines (36 loc) · 867 Bytes
/
speaker.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
require 'espeak'
include ESpeak
class Speaker
def initialize(file)
@file = file
format_items
end
def convert_time_to_seconds(t)
pattern = /^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$/i
if pattern =~ t
hours, minutes, seconds = t.match(pattern).captures.map {|n| n.to_i}
hours *= 3600
minutes *= 60
hours + minutes + seconds
else
raise 'Incorrect time format.'
end
end
def format_items
@items = @file.map do |time, message|
{time: convert_time_to_seconds(time), message: Speech.new(message)}
end
end
def speak
second = 0
until second > @items.last[:time]
print "\r#{second} seconds elapsed"
item = @items.select {|i| i[:time] == second}.first
if item
item[:message].speak
end
sleep(1)
second += 1
end
end
end