Created
November 5, 2011 09:12
-
-
Save tyama/1341305 to your computer and use it in GitHub Desktop.
きのくにやPiano
This file contains hidden or 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
import javax.sound.midi.* | |
//thx to https://gist.github.com/250489 | |
int ticks = 16 | |
def addNote(track, pitch, start, duration=ticks, velocity=64) { | |
message = new ShortMessage() | |
message.setMessage(ShortMessage.NOTE_ON, pitch, velocity) | |
track.add(new MidiEvent(message, start)) | |
message = new ShortMessage() | |
message.setMessage(ShortMessage.NOTE_OFF, pitch, velocity) | |
track.add(new MidiEvent(message, start + duration)) | |
return start + duration | |
} | |
def instChange(track, instNo=0) { | |
message = new ShortMessage() | |
message.setMessage(ShortMessage.PROGRAM_CHANGE, 0, instNo, 0) | |
track.add(new MidiEvent(message, 0)) | |
} | |
// | |
def noteArray = ['C','C#','D','D#','E','F','F#','G','G#','A','A#','B'] | |
def octArray = [-36,-24,-12,0,12,24,36] | |
def dur = ["1":64,"2":32,"2.":48,"4":16,"4.":24,"8":8,"8.":12,"16":1,,"16.":3] | |
def toNoteNum = {notesStr-> | |
def notes = notesStr.split(' ') | |
def octave,note,snum,noteNums = [] | |
notes.each{ | |
def sp = it.split('/') | |
def n = sp[0],d="4" | |
if(sp.size()==2){ d=sp[1] } | |
note = n[0..n.length()-2] | |
if(note=='n'){ | |
snum=null | |
}else{ | |
snum= 60 + octArray[(n[n.length()-1..-1]) as int] + noteArray.indexOf(note) | |
} | |
noteNums << [snum, dur[d]] | |
} | |
return noteNums | |
} | |
def player = {score,bpm-> | |
def nums = toNoteNum score | |
def seq = new Sequence(Sequence.PPQ, ticks) | |
def track = seq.createTrack() | |
instChange(track, 1) | |
int t = 0 | |
nums.each{pitch, duration-> | |
t = pitch ? addNote(track, pitch, t, duration) : t + duration | |
} | |
MidiSystem.sequencer.with{ | |
tempoInBPM = bpm | |
sequence = seq | |
open() | |
start() | |
while(isRunning())Thread.sleep(1000) | |
close() | |
} | |
} | |
if(args){ | |
player args[0], 70.0 | |
}else{ | |
println 'usage: groovy rentaro.groovy "C3/4. C3/8 E3/4. E3/8 G3/8. G3/16 G3/4 C4/4 A3/8. A3/16 G3/4 n/4"' | |
} | |
/* | |
groovy rentaro.groovy "n/2 E4/4 F#4/4 G#4/1 E4/4 F#4/4 G#4/1 E4/4 F#4/4 G#4/2 G#4/4 A4/4 B4/2 G#4/2 F#4/2 E4/2 C#5/4 C#5/4 C#5/4 A4/4 F#4/4 F#4/4 G#4/4 A4/4 B4/4 B4/4 B4/4 G#4/4 E4/2. G#4/4 A4/2 C#4/2 E4/4 D#4/4 C#4/4 D#4/4 E4/1 G#4/4 A4/2 C#4/2 E4/4. D#4/4. C#4/4. D#4/2 E4/1" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment