7 require 'std_msgs/String'
11 @logger = Logger.new(STDOUT)
12 @publisher = node.advertise('asr_result', Std_msgs::String)
13 @pipeline = Gst.parse_launch('alsasrc device="plughw:1,0" ! audio/x-raw,format=S16LE,channels=1,rate=16000 ! cutter leaky=true name=cutter'\
14 ' ! tee name=jsgf ! queue leaky=downstream ! valve name=valve_jsgf drop=true ! pocketsphinx name=asr_jsgf ! fakesink async=false jsgf.'\
15 ' ! pocketsphinx name=asr_kws ! fakesink async=false'\
17 # Ignore everything below the configured volume
18 cutter = @pipeline.get_by_name('cutter')
19 cutter.set_property('threshold-dB', -20)
20 cutter.set_property('pre-length', 100000000) # pocketsphinx needs about 0.1s before start
21 cutter.set_property('run-length', 1300000000)
23 asr_jsgf = @pipeline.get_by_name('asr_jsgf')
24 asr_jsgf.set_property('hmm', 'pocketsphinx/adapt/cmusphinx-en-us-5.2')
25 asr_jsgf.set_property('mllr', 'pocketsphinx/adapt/mllr_matrix')
26 asr_jsgf.set_property('jsgf', 'data/robot.jsgf')
28 asr_kws = @pipeline.get_by_name('asr_kws')
29 asr_kws.set_property('hmm', 'pocketsphinx/adapt/cmusphinx-en-us-5.2')
30 asr_kws.set_property('mllr', 'pocketsphinx/adapt/mllr_matrix')
31 asr_kws.set_property('kws', 'data/keywords.kws')
34 bus.add_watch do |bus, message|
36 when Gst::MessageType::EOS
38 when Gst::MessageType::ERROR
40 binding.pry # open console
42 when Gst::MessageType::ELEMENT
43 if message.src.name == "asr_kws"
44 if message.structure.get_value(:final).value
45 keyword_detect(message.structure.get_value(:hypothesis).value, message.structure.get_value(:confidence).value)
47 elsif message.src.name == "asr_jsgf"
48 if message.structure.get_value(:final).value
49 final_result(message.structure.get_value(:hypothesis).value, message.structure.get_value(:confidence).value)
51 elsif message.src.name == "cutter"
52 if message.structure.get_value(:above).value
53 @logger.debug "Start recording.."
55 @logger.debug "Stop recording"
65 # Enables/Disables the jsgf pipeline branch
66 def enable_jsgf(bEnable)
67 valve = @pipeline.get_by_name('valve_jsgf')
68 valve.set_property("drop", !bEnable)
71 # Result of jsgf pipeline branch
72 def final_result(hyp, confidence)
73 @logger.info "final: " + hyp + " " + confidence.to_s
76 # Publish pocketsphinx result as ros message
77 msg = Std_msgs::String.new
79 @publisher.publish(msg)
82 def keyword_detect(hyp, confidence)
83 @logger.debug "Got keyword: " + hyp
93 node = ROS::Node.new('pocketsphinx')
95 loop = GLib::MainLoop.new(nil, false)