class SonicPi
Constants
- GUI_ID
- PORT_LOG_REGEX
- RUN_COMMAND
- SERVER
- STOP_COMMAND
Public Class Methods
new(port=nil)
click to toggle source
# File lib/sonic_pi.rb, line 9 def initialize(port=nil) @port = port || find_port end
Public Instance Methods
run(command)
click to toggle source
# File lib/sonic_pi.rb, line 18 def run(command) send_command(RUN_COMMAND, command) end
stop()
click to toggle source
# File lib/sonic_pi.rb, line 22 def stop send_command(STOP_COMMAND) end
test_connection!()
click to toggle source
# File lib/sonic_pi.rb, line 26 def test_connection! begin socket = UDPSocket.new socket.bind(nil, @port) abort("ERROR: Sonic Pi is not listening on #{@port} - is it running?") rescue # everything is good end end
Private Instance Methods
client()
click to toggle source
# File lib/sonic_pi.rb, line 38 def client @client ||= OSC::Client.new(SERVER, @port) end
find_port()
click to toggle source
# File lib/sonic_pi.rb, line 47 def find_port port = 4557 begin log_path = File.join(Dir.home, ".sonic-pi", "log", "server-output.log") File.open(log_path, 'r') do |f| port_log_entry = f.each_line .lazy .map { |line| line[PORT_LOG_REGEX, "port"] } .find { |match| !!match } port = port_log_entry.to_i if port_log_entry end rescue Errno::ENOENT # not to worry if the file doesn't exist end port end
send_command(call_type, command=nil)
click to toggle source
# File lib/sonic_pi.rb, line 42 def send_command(call_type, command=nil) prepared_command = OSC::Message.new(call_type, GUI_ID, command) client.send(prepared_command) end