bc7394937c57342208c3e7237089cbf5bbea0092
[ros_wild_thumper.git] / scripts / asr_vosk.rb
1 #!/usr/bin/ruby
2 # export AUDIODEV=plughw:CARD=ArrayUAC10,0
3 # rec -q -t alsa -c 1 -b 16 -r 16000 -t wav - silence -l 1 0.1 0.3% -1 2.0 0.3% | ./asr_vosk.rb -
4
5 require 'logger'
6 require 'websocket-eventmachine-client'
7 require 'json'
8 require 'ros'
9 require 'std_msgs/String'
10
11 KEYWORDS = ["wild thumper"]
12 CONFIG = {
13         "config": {
14                 "phrase_list": ["angle", "backward", "by", "centimeter", "compass", "current", "decrease", "default", "degree", "down", "eight", "eighteen", "eighty", "eleven", "fifteen", "fifty", "five", "forty", "forward", "four", "fourteen", "get", "go", "hundred", "increase", "left", "light", "lights", "meter", "mic", "minus", "motion", "mute", "nine", "nineteen", "ninety", "off", "on", "one", "position", "pressure", "right", "secure", "set", "seven", "seventeen", "seventy", "silence", "six", "sixteen", "sixty", "speed", "stop", "temp", "temperature", "ten", "thirteen", "thirty", "three", "to", "turn", "twelve", "twenty", "two", "up", "velocity", "voltage", "volume", "wild thumper", "zero"],
15                 "sample_rate": 16000.0
16         }
17 }
18
19 class Speak
20         def initialize(node)
21                 @logger = Logger.new(STDOUT)
22                 @jsgf_enabled = false
23                 @publisher = node.advertise('asr_result', Std_msgs::String)
24
25                 EM.run do
26                         Signal.trap("INT")  { send_eof }
27                         @ws = WebSocket::EventMachine::Client.connect(:uri => 'ws://192.168.36.4:2700')
28
29                         def send_eof
30                                 @ws.send '{"eof" : 1}'
31                         end
32
33                         def run
34                                 while true do
35                                         data = ARGF.read(16000)
36                                         if data
37                                                 @ws.send data, :type => :binary
38                                         else
39                                                 send_eof
40                                                 break
41                                         end
42                                 end
43                         end
44
45                         @ws.onopen do
46                                 @logger.info "Running.."
47                                 @ws.send CONFIG.to_json
48
49                                 Thread.new {
50                                         run
51                                 }
52                         end
53
54                         @ws.onmessage do |msg, type|
55                                 d = JSON.parse(msg)
56                                 handle_result(d)
57                         end
58
59                         @ws.onclose do |code, reason|
60                                 puts "Disconnected with status code: #{code}"
61                                 exit
62                         end
63                 end
64         end
65
66         def handle_result(msg)
67                 if msg.has_key? "result"
68                         msg["result"].each do |result|
69                                 @logger.debug "word=" + result["word"]
70                         end
71
72                         text = msg["text"]
73                         @logger.debug "text=" + msg["text"]
74                         if KEYWORDS.include? text
75                                 keyword_detect(text)
76                                 return
77                         end
78                         if @jsgf_enabled
79                                 final_result(msg["text"])
80                         end
81                 end
82         end
83
84         # Enables/Disables the jsgf pipeline branch
85         def enable_jsgf(bEnable)
86                 @jsgf_enabled = bEnable
87         end
88
89         # Result of jsgf pipeline branch
90         def final_result(hyp)
91                 @logger.info "final: " + hyp
92                 enable_jsgf(false)
93
94                 # Publish pocketsphinx result as ros message
95                 msg = Std_msgs::String.new
96                 msg.data = hyp
97                 @publisher.publish(msg)
98         end
99
100         def keyword_detect(hyp)
101                 @logger.debug "Got keyword: " + hyp
102                 enable_jsgf(true)
103         end
104 end
105
106 if __FILE__ == $0
107         node = ROS::Node.new('asr_vosk')
108         app = Speak.new(node)
109         begin
110                 node.spin
111         rescue Interrupt
112         ensure
113                 node.shutdown
114         end
115 end