我从来没有深入研究过Java对MIDI的支持,而上一次我认真地进行MIDI编程是在Commodore Amiga成为国王的时候。
看来您可能需要做大量的手动工作。这是一个粗略的示例,可解释所有NOTE_ON和NOTE_OFF事件,对于其余事件,它仅显示命令号。
之所以看起来比最初想像的要复杂,是因为MIDI专注于捕获乐器事件(例如,何时按下键盘琴键,何时释放琴键等等),而不是在乐谱符号上。
该代码为每个事件打印出一行,并标明刻度线(这是事件的计时信息),通道,事件类型,音符名称,键,力度
import java.io.File;import javax.sound.midi.MidiEvent;import javax.sound.midi.MidiMessage;import javax.sound.midi.MidiSystem;import javax.sound.midi.Sequence;import javax.sound.midi.ShortMessage;import javax.sound.midi.Track;public class Test { public static final int NOTE_ON = 0x90; public static final int NOTE_OFF = 0x80; public static final String[] NOTE_NAMES = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"}; public static void main(String[] args) throws Exception { Sequence sequence = MidiSystem.getSequence(new File("test.mid")); int trackNumber = 0; for (Track track : sequence.getTracks()) { trackNumber++; System.out.println("Track " + trackNumber + ": size = " + track.size()); System.out.println(); for (int i=0; i < track.size(); i++) { MidiEvent event = track.get(i); System.out.print("@" + event.getTick() + " "); MidiMessage message = event.getMessage(); if (message instanceof ShortMessage) { ShortMessage sm = (ShortMessage) message; System.out.print("Channel: " + sm.getChannel() + " "); if (sm.getCommand() == NOTE_ON) { int key = sm.getData1(); int octave = (key / 12)-1; int note = key % 12; String noteName = NOTE_NAMES[note]; int velocity = sm.getData2(); System.out.println("Note on, " + noteName + octave + " key=" + key + " velocity: " + velocity); } else if (sm.getCommand() == NOTE_OFF) { int key = sm.getData1(); int octave = (key / 12)-1; int note = key % 12; String noteName = NOTE_NAMES[note]; int velocity = sm.getData2(); System.out.println("Note off, " + noteName + octave + " key=" + key + " velocity: " + velocity); } else { System.out.println("Command:" + sm.getCommand()); } } else { System.out.println("Other message: " + message.getClass()); } } System.out.println(); } }}
例如,我躺在这里的毛皮elise.mid在开始时会产生类似以下的内容:
@0 Channel: 1 Note on, E5 key=76 velocity: 127@192 Channel: 1 Note off, E5 key=76 velocity: 64@192 Channel: 1 Note on, D#5 key=75 velocity: 127@384 Channel: 1 Note off, D#5 key=75 velocity: 64@384 Channel: 1 Note on, E5 key=76 velocity: 127@576 Channel: 1 Note off, E5 key=76 velocity: 64@576 Channel: 1 Note on, D#5 key=75 velocity: 127@768 Channel: 1 Note off, D#5 key=75 velocity: 64@768 Channel: 1 Note on, E5 key=76 velocity: 127@960 Channel: 1 Note off, E5 key=76 velocity: 64@960 Channel: 1 Note on, B4 key=71 velocity: 127@1152 Channel: 1 Note off, B4 key=71 velocity: 64@1152 Channel: 1 Note on, D5 key=74 velocity: 127@1344 Channel: 1 Note off, D5 key=74 velocity: 64@1344 Channel: 1 Note on, C5 key=72 velocity: 127@1536 Channel: 1 Note off, C5 key=72 velocity: 64@1536 Channel: 1 Note on, A4 key=69 velocity: 127@1920 Channel: 1 Note off, A4 key=69 velocity: 64
更新:通道是MIDI规范的16个通道。
http://www.midi.org/techspecs/gm.php
通道:支持所有16个MIDI通道。每个通道可以播放可变数量的声音(复音)。每个通道可以演奏不同的乐器(声音/音色/音色)。基于键的打击乐始终在MIDI通道10上。
速度是用于控制声音的属性之一。例如,在键盘上捕获MIDI数据就是您按下琴键的力量。通常,它控制声音的音量。详情请参阅这里:http :
//audio.tutsplus.com/tutorials/production/7-ways-to-use-and-edit-midi-
velocity/
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)