Created
October 22, 2013 09:24
-
-
Save myy/7097639 to your computer and use it in GitHub Desktop.
MIDIファイルからデータをぶっこぬく
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
import javax.sound.midi.*; | |
import java.io.*; | |
/** | |
* MIDIファイルからデータをぶっこぬくコード<br> | |
* 参考:http://d.hatena.ne.jp/UDONCHAN/20090919/1253343146 | |
* @author myy | |
* | |
*/ | |
public class ExtractMidiDataSample { | |
private Track t; | |
private MidiEvent e; | |
private byte[] m; | |
public ExtractMidiDataSample(File f, int track) throws IOException, InvalidMidiDataException { | |
// ファイルとトラック番号からTrackをぶっこ抜く | |
this.t = MidiSystem.getSequence(f).getTracks()[track]; | |
System.out.println("tick, message, pitch, velocity"); | |
int messageCount = 0; | |
// midiイベントを表示する | |
for(int i=0;;) { | |
try{ | |
e = t.get(i++); | |
m = e.getMessage().getMessage(); | |
} catch(ArrayIndexOutOfBoundsException ex) { | |
break; | |
} | |
// ノートオン(0x90)のみ抜いてみる | |
// ちなみにノートオフメッセージは0x80 | |
if((m[0] & 0xF0) == 0x90 || (m[0] & 0xF0) == 0x80) { | |
System.out.println(e.getTick() + ", " + (0xFF & m[0]) + ", " + (0xFF & m[1]) + ", " + (0xFF & m[2])); | |
messageCount++; | |
} | |
} | |
System.out.println("メッセージ数: " + messageCount); | |
} | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// TODO 自動生成されたメソッド・スタブ | |
System.out.println("midiファイルからデータをぶっこ抜くテスト"); | |
File smf = new File("filename.mid"); | |
try { | |
new ExtractMidiDataSample(smf, 1); // track=1がメロディが入っているトラック | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (InvalidMidiDataException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("終了"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment