Last active
December 13, 2015 22:19
-
-
Save Neurogami/4983601 to your computer and use it in GitHub Desktop.
An experiment in code design
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Adjust as needed to get the libraries loaded | |
$:.unshift '.' | |
$:.unshift './jleap-ng' | |
require 'jleap-ng' | |
require 'web-socket-server' | |
require 'game-demo-handlers' | |
require 'json' | |
require 'osc-ruby' | |
Thread.abort_on_exception = true | |
include Neurogami::Leap | |
class GamerListener < BasicListener | |
include GameHandlerConditions | |
def onInit controller | |
@socket_server = SocketServer.new | |
@socket_server.run | |
@send_to_port = 8000 # default Renoise port | |
@send_to_addy = "127.0.0.1" | |
@osc_client = OSC::Client.new @send_to_addy, @send_to_port | |
@last_message_time = Time.now | |
@min_osc_wait = 1.3 | |
@handlers = [] | |
load_handlers | |
end | |
def onFrame controller | |
frame = controller.frame | |
@handlers.each do |h| | |
h.run frame | |
end | |
end | |
def message message_type, *args | |
if self.respond_to? "send_#{message_type}" | |
send "send_#{message_type}", *args | |
else | |
warn "Have on idea what to do with message type '#{message_type}'" | |
end | |
end | |
def send_ws command, arg_ary=nil | |
warn "'#{command}' #{arg_ary.inspect}" | |
data = {} | |
data['command'] = command | |
data['args'] = arg_ary if arg_ary | |
@socket_server.send data.to_json | |
end | |
def send_osc command, args_ary = [] | |
warn "****** send_osc for #{command} ******" | |
tdelta = Time.now - @last_message_time | |
# Try to avoid sending a massive hail of OSC messages by requiring a wait period | |
if tdelta < @min_osc_wait | |
warn "Too soon! #{tdelta} < #{@min_osc_wait}" | |
return | |
end | |
@last_message_time = Time.now | |
case command | |
when "grow" | |
# Have Renoise play a "grow" sound, then turn the note off. | |
# The arguments for the "Trigger a Note-On" message: | |
# arg#1: instrument (-1 for the currently selected one) | |
# arg#2: track (-1 for the current one) | |
# arg#3: note value (0-119) | |
# arg#4: velocity (0-127) | |
# | |
# /renoise/trigger/note_on number, number, number, number | |
on_address = "/renoise/trigger/note_on" | |
off_address = "/renoise/trigger/note_off" | |
instr = 0 | |
track = 1 | |
note = 75 | |
vel = 127 | |
on_msg = OSC::Message.new on_address, instr, track, note, vel | |
off_msg = OSC::Message.new off_address, instr, track, note | |
warn on_msg.inspect | |
Thread.new do | |
begin | |
@osc_client.send on_msg | |
sleep @min_osc_wait * 2 | |
@osc_client.send off_msg | |
rescue | |
warn "Error: #{$!}" | |
end | |
end | |
else | |
warn "This makes more sense if you are handling more than on possible command" | |
end | |
end | |
end | |
listener = GamerListener.new | |
controller = Controller.new | |
controller.add_listener listener | |
warn "Press Enter to quit..." | |
begin | |
gets | |
rescue | |
warn "Error! #{$!}" | |
end | |
controller.remove_listener listener |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment