Created
September 19, 2009 22:27
-
-
Save Visuelle-Musik/189611 to your computer and use it in GitHub Desktop.
MIDI I/O with JRuby, ruby-processing example 1
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
# === MIDI-Notes to solresol-colours in RubyProcessing. Example for the use of JRmidi, very basic Midi-Implementation for JRuby, especially for use with ruby-processing === | |
# --- ruby-processing see: http://wiki.github.com/jashkenas/ruby-processing / solresol is a universal language, where words can be put out as musical notes, colours or other --- | |
# --- solresol see: http://en.wikipedia.org/wiki/Solresol --- | |
# Notes have to come as MIDI-note-events from MIDI-in, and are so we can hear them redirected to a synth as well | |
# ! ==> adopt your MIDI-ports below !!!! | |
MIDI_PORT_IN = 0 # your MIDI-input where notes will be mapped to coulors | |
MIDI_PORT_OUT = 11 # select this port so, that is redirected to MIDI-in by hardware, with JACK, ableton live or something (ports are numbered from 0 to n, no difference if in or out) | |
MIDI_OUT_SYNTH = 9 # the OS-System's synth, port number may differ on your system | |
require "jr_midi" # class JRmidi for basic MIDI I/O in JRuby... | |
BLACK = 70 | |
S = { # SOLRESOL-hash... | |
:do => 60, | |
:re => 62, | |
:mi => 64, | |
:fa => 65, | |
:sol => 67, | |
:la => 69, | |
:si => 71, | |
:black => BLACK | |
} | |
# Dore domilado solresol: I am speaking "solresol" | |
SOLRESOL_TEXT = [ S[:black], S[:do], S[:re], S[:black], S[:do], S[:mi], S[:la], S[:do], S[:black], S[:sol], S[:re], S[:sol] ] | |
class Sketch < Processing::App | |
load_library :opengl | |
include_package "processing.opengl" | |
def setup | |
@echo = JRmidi.new(self, :echo, :in_port => MIDI_PORT_IN, :out_port => MIDI_PORT_OUT) | |
@synth = JRmidi.new(self, :synth, :out_port => MIDI_OUT_SYNTH, :verbose => true) | |
# init openGL | |
library_loaded?(:opengl) ? render_mode(OPENGL) : render_mode(P3D) | |
hint(DISABLE_OPENGL_2X_SMOOTH); # save performance ? | |
hint(DISABLE_DEPTH_TEST); # save performance ? | |
no_stroke | |
frameRate(60); # default is 60, set to 25 for monitors with 75 Hz, for instance... | |
# set solresol colout-values, for 7 (12) keys... | |
@s_col = Array.new | |
@s_col[0] = @s_col[1] = 0xFFE30000 # do-red (C , C#) | |
@s_col[2] = @s_col[3] = 0xFFEA621F # re-orange (D, D#) | |
@s_col[4] = 0xFFEDED00 # mi-yellow (E) | |
@s_col[5] = @s_col[6] = 0xFF00AA00 # fa-green (F, F#) | |
@s_col[7] = @s_col[8] = 0xFF00AAAA # sol-turquoise (G, G#) | |
@s_col[9] = 0xFF0000CC # la-blue (A) | |
@s_col[11] = 0xFF6D398B # si-purple (B) | |
@cur_col = @s_col[10] = 0xFF000000 # black, set on , A#[Bb], too... | |
@text_idx = @idx = 0 | |
@len = SOLRESOL_TEXT.size | |
@key_up = true | |
@recent_note = BLACK | |
end | |
def draw | |
fill(@cur_col) # colour as set by setup(), note_on() or note_off() | |
rect(0, 0, 1024, 768) # display "colour-pad" | |
delay(500) # sustain notes for half a second... | |
if( @key_up ) | |
@note = SOLRESOL_TEXT[@text_idx] # get pitch-value for current syllable in text | |
@text_idx = (@idx+=1) % @len # cycle the text "forever" | |
@synth.send_note_on( 0, @note, 100 ) unless @note == BLACK # play current note on synth | |
# send note-on-value, will be "caught" by note_on(), see below if MIDI-OUT is redirected to MIDI-IN correctly... | |
@echo.send_note_on( 0, SOLRESOL_TEXT[@text_idx], 100 ) # we have to be one note ahead here, as the colour will be 'seen' by draw() next time | |
else | |
@synth.send_note_off( 0, @note, 60 ) | |
end | |
@key_up = !@key_up | |
end | |
# --- MIDI-in callback-methods --- | |
def note_on(channel, note, velocity ) | |
@cur_col = @s_col[note % 12] # map notes of each octave to 7 destinct solresol-colours | |
end | |
def note_off(channel, note, velocity ) | |
# puts "c: %d n: %d v: %d" % [channel, note, velocity] | |
end | |
end | |
Sketch.new(:width => 1024, :height => 768, :title => "processing-ruby: MIDI to solresol-colours") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment