Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 120 lines (102 sloc) 3.114 kb
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
framework 'AppKit'

require File.expand_path '../inputs/base', __FILE__
require File.expand_path '../inputs/audio', __FILE__
require File.expand_path '../inputs/terminal', __FILE__

require File.expand_path '../outputs/audio', __FILE__
require File.expand_path '../outputs/terminal', __FILE__

require File.expand_path '../builtin/core_dialogue', __FILE__

module James

  class Controller

    attr_reader :visitor

    # This puts together the core dialogue and the user
    # ones that are hooked into it.
    #
    def initialize
      user_visitor = initialize_dialogues.visitor
      system_visitor = Visitor.new CoreDialogue.new.state_for(:awake)
      @visitor = Visitors.new system_visitor, user_visitor
    end

    def applicationDidFinishLaunching notification
      load_voices
      start_output
      start_input
    end
    def windowWillClose notification
      exit
    end

    # Load voices from yaml.
    #
    def load_voices
      # yaml_voices = ''
      # File.open('voices.yml') do |f| yaml_voices << f.read end
      # voices = YAML.load(yaml_voices)
      # @male_voice = voices['male']
      # @female_voice = voices['female']
      # Commented voices are Apple built-in voices. Can be changed by replacing the last part e.g.'Vicki' with e.g.'Laura'
      # much better female voice from iVox:
      # female: com.acapela.iVox.voice.iVoxHeather22k
      # much better male voice from iVox:
      # male: com.acapela.iVox.voice.iVoxRyan22k
      # female: com.apple.speech.synthesis.voice.Vicki
      # male: com.apple.speech.synthesis.voice.Bruce
    end

    # Initialize and "parse" the
    # dialogues.
    #
    def initialize_dialogues
      dialogues = Dialogues.new
      dialogues.resolve
      dialogues
    end
    # Start recognizing words.
    #
    def start_input
      @input = Inputs::Audio.new self
      @input.listen
    end
    # Start speaking.
    #
    def start_output
      @output = Outputs::Audio.new
    end

    # Callback method from dialogue.
    #
    def say text
      @output.say text
    end
    def hear text
      @visitor.hear text do |response|
        say response
      end
    end
    def expects
      @visitor.expects
    end

    def listen
      app = NSApplication.sharedApplication
      app.delegate = self

      # window = NSWindow.alloc.initWithContentRect([100, 300, 300, 100],
      # styleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask,
      # backing:NSBackingStoreBuffered,
      # defer:false)
      # window.title = 'James Debug/Info'
      # window.level = 3
      # window.delegate = app.delegate

      # @button = NSButton.alloc.initWithFrame([10, 10, 400, 10])
      # @button.bezelStyle = 4
      # @button.title = ''
      # @button.target = app.delegate
      # @button.action = 'say_hello:'
      #
      # window.contentView.addSubview(@button)

      # window.display
      # window.orderFrontRegardless

      app.run
    end
    # Simply put, if there is a controller, it is listening.
    #
    def listening?
      true
    end

  end

end
Something went wrong with that request. Please try again.