siri と say command で喋らせたり外部に流す話

siri と say command がしゃべっている様子です。
processing に siri に話した内容、siri が返してくれる内容を渡して表示してみるようにしました。

ruby

require 'eventmachine'

class SiriProxy::Plugin::Processing < SiriProxy::Plugin

  class TextClient < EventMachine::Connection
    attr_reader :queue
    def initialize(q)
      @queue = q
    end
    def post_init
      cb = Proc.new do |msg|
        send_data(msg)
        self.queue.pop &cb
      end

      self.queue.pop &cb
    end
  end

  attr_accessor :queue, :encode

  def initialize(config = {})
    @queue = EM::Queue.new
    @encode = config["encode"] || "Shift_JIS"
    host = config["host"] || "127.0.0.1"
    port = config["port"] || 5555
    EM.connect(host, port, TextClient, @queue)
  end

  filter "AddViews", direction: :from_guzzoni do |object|
    arr = ["siri||"]
    object["properties"]["views"].each do |view|
      if view["class"] == "AssistantUtteranceView"
        arr.push view["properties"]["text"].encode(self.encode)
      end
    end
    self.queue.push(arr.join("") + "\0")
    object
  end

  filter "SpeechRecognized", direction: :from_guzzoni do |object|
    arr = ["user||"]
    object["properties"]["recognition"]["properties"]["phrases"].each do |phrase|
      phrase["properties"]["interpretations"].first["properties"]["tokens"].each do |token|
        #arr.push " " if token["properties"]["removeSpaceBefore"]
        arr.push token["properties"]["text"].encode(self.encode)
        arr.push " " unless token["properties"]["removeSpaceAfter"]
      end
    end
    p arr.join("")
    self.queue.push(arr.join("") + "\0")
    object
  end

end

processng 側

import processing.net.*;

String siriText = "";
String userText = "";
Server server;
int port = 5555;

void setup() {
  size(800, 600);
  background(0);
  textSize(32);
  server = new Server(this, port);
}

void draw() {
  background(0);
  updateTextFromSocket();
  drawUserText();
  drawSiriText(); 
}

void drawUserText() {
   text(userText, 10, 30, 500, 200);
}

void drawSiriText() {
  text(siriText, 10, 330, 500, 200);
}

void updateTextFromSocket() {
  Client c = server.available();
  if ( c != null ) {
    String input = c.readString();
    println("---input---");
    println(input);
    println("--------");
    String[] lines = split(input, "\0");
    for ( String line : lines ) {
      if (line.equals("") ) {
        continue;
      }
      println("----line----");
      println(line);
      String[] data = split(line, "||");
      println("0:"+data[0]);
      println("1:"+data[1]);
    
      if ( data[0].equals("user") ) {
        userText = data[1];
      }
      else if ( data[0].equals("siri") ) {
        siriText = data[1];
      }
    }
  }
}

EventMachine 始めて書いたけどこういう感じでいいのかな ?