Class | Listener |
In: |
listener.rb
|
Parent: | Object |
Object initialization
# File listener.rb, line 221 def initialize `mkfifo command` @player = Amarok.new @not_listen = false @answer_buffer = "" @mutex = Mutex.new @cv_answer = ConditionVariable.new start_recognizer end
Get the user answer. Stops listening to commands and get answer for question, then start listening again and retunr the answer.
# File listener.rb, line 244 def get_answer buffer = "" @mutex.synchronize { @not_listen = true # now wait for the answer @cv_answer.wait(@mutex) buffer = get_buffer @not_listen = false } return buffer end
Start listening from named pipe. Runs function listen_in_thread in new thread.
# File listener.rb, line 235 def listen @listening_thread = Thread.new{ listen_in_thread } end
Finalizer.
# File listener.rb, line 33 def finalize(id) `rm command` @player = nil listen_stop recognize_stop puts "FIFO removed, #{id} die" end
Wait while answer buffer is not empty and then return it.
# File listener.rb, line 209 def get_buffer buffer = @answer_buffer @answer_buffer = "" return buffer end
This function run in separate thread. It is listening and read command from named pipe and execute it.
# File listener.rb, line 69 def listen_in_thread puts "start listening" loop do message = `cat command`.chomp() command = message.to_s().downcase puts command skip_case = false @mutex.synchronize { if @not_listen == true @answer_buffer = command # send signal that buffer is set @cv_answer.signal skip_case = true end } if skip_case next end # what to do case command when 'clear' @player.clear when 'play' @player.play when 'stop' @player.stop when 'mute' @player.mute when 'pause' @player.play_pause when 'next' @player.next when 'previous' @player.prev when 'info' track = Track.new info = track.text $speaker.say(info) @player.show_osd when 'volume up' @player.volumeUp when 'volume down' @player.volumeDown when /play me */ @player.stop @player.clear splitted_command = command.split(" ") case splitted_command[2] when 'track' name = splitted_command[3,splitted_command.length-1].join(" ") start_playing_thread = Thread.new{ $collection.add_track(name) @player.play } when 'album' name = splitted_command[3,splitted_command.length-1].join(" ") start_playing_thread = Thread.new{ $collection.add_album(name) @player.play } when 'artist' name = splitted_command[3,splitted_command.length-1].join(" ") start_playing_thread = Thread.new{ $collection.add_artist(name) @player.play } when 'genre' name = splitted_command[3,splitted_command.length-1].join(" ") start_playing_thread = Thread.new{ $collection.add_genre(name) @player.play } else name = splitted_command[2,splitted_command.length-1].join(" ") puts "Adding " + name start_playing_thread = Thread.new{ $collection.add_to_playlist(name) @player.play } end #case when /add to playlist */ splitted_command = command.split(" ") case splitted_command[3] when 'track' name = splitted_command[4,splitted_command.length-1].join(" ") start_playing_thread = Thread.new{ $collection.add_track(name) } when 'album' name = splitted_command[4,splitted_command.length-1].join(" ") start_playing_thread = Thread.new{ $collection.add_album(name) } when 'artist' name = splitted_command[4,splitted_command.length-1].join(" ") start_playing_thread = Thread.new{ $collection.add_artist(name) } when 'genre' name = splitted_command[4,splitted_command.length-1].join(" ") start_playing_thread = Thread.new{ $collection.add_genre(name) } else name = splitted_command[3,splitted_command.length-1].join(" ") puts "Adding " + name start_playing_thread = Thread.new{ $collection.add_to_playlist(name) } end #case when nil # nic ... else puts "Error: unknown command." end #case command end end
Kill the listening thread.
# File listener.rb, line 195 def listen_stop Thread.kill(@listening_thread) end
Kill the recognizer thread.
# File listener.rb, line 202 def recognizer_stop Thread.kill(@recognizer_thread) end
Load path to regognizer program and start it.
# File listener.rb, line 46 def start_recognizer recognizer = `cat ascon.conf`.chomp! updater = `cat update.conf`.chomp! # throw exception if no config path raise "Recognizer not configured" if recognizer.nil? puts "updating grammar ... " if !safe_command(updater) raise "Problem during updating grammar." end puts "start recognition ..." @recognizer_thread = Thread.new { if !safe_command("#{recognizer} > command") raise "Recognizer error." end } end