[英]How can I parse a tempo of midi using Java?
我不明白如何捕捉節奏。 所以仍然使用一些糟糕的方式來對我的輸出使用速度。
我真的認為只使用NOTE_ON和NOTE_OFF時間會給我實時。 但是這個輸出在C ++中仍然運行得太慢了。
PS
播放時我們只使用一個VOICE midis。 (這只是為了好玩,我們教室里的一些電腦正在同步播放2 + VOICE音樂)。
這是我的代碼:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.sound.midi.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import static java.lang.Math.*;
public class MidiReader {
public static final float DEFAULT_TEMPO = 100.0f;
public static final int NOTE_ON = 0x90;
public static final int NOTE_OFF = 0x80;
public static final float[] NOTES = {32.70f, 34.65f, 36.95f, 38.88f, 41.21f, 43.65f,
46.25f, 49.00f, 51.90f, 55.00f, 58.26f, 61.74f};
private JFrame frame = new JFrame();
private JTextArea outText = new JTextArea();
private JPanel panel = new JPanel();
private File inputFile = null;
private JButton button = new JButton("Choose file");
private JTextField inputTempo = new JTextField(Integer.toString((int) DEFAULT_TEMPO));
private float tempo = DEFAULT_TEMPO;
public void init(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setLayout(new BorderLayout());
panel.setLayout(new BorderLayout());
panel.add(outText, BorderLayout.CENTER);
panel.add(inputTempo, BorderLayout.SOUTH);
frame.add(panel, BorderLayout.CENTER);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileopen = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Midi files", "mid");
fileopen.setFileFilter(filter);
int ret = fileopen.showDialog(null, "Choose File");
if (ret == JFileChooser.APPROVE_OPTION) {
inputFile = fileopen.getSelectedFile();
}
if (inputFile != null) {
setTempo(Float.parseFloat(inputTempo.getText()));
outText.setText("");
calculate(getTempo());
}
}
});
inputTempo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setTempo(Float.parseFloat(inputTempo.getText()));
outText.setText("");
calculate(getTempo());
}
});
frame.add(button, BorderLayout.SOUTH);
frame.setTitle("Midi to C++ Beep");
}
public void setTempo(float tempo) {
this.tempo = tempo;
}
public float getTempo(){
return this.tempo;
}
public float getPitch(int key){
return NOTES[key % 12] * (float) (pow(2.0, (key / 12) - 2));
}
public MidiReader(){
init();
}
public void calculate(float tempo){
Sequence sequence = null;
try {
sequence = MidiSystem.getSequence(inputFile);
} catch (InvalidMidiDataException e) {
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
for (Track track : sequence.getTracks()) {
int key;
long startTime = 0;
long stopTime;
for (int i = 0; i < track.size(); i++) {
MidiEvent event = track.get(i);
MidiMessage message = event.getMessage();
if (message instanceof ShortMessage) {
ShortMessage sm = (ShortMessage) message;
switch(sm.getCommand()){
case NOTE_ON:
startTime = event.getTick();
break;
case NOTE_OFF:
stopTime = event.getTick();
key = sm.getData1();
outText.append(
"\t" + "Beep(" + getPitch(key) + ", " +
(int)((stopTime - startTime) * (DEFAULT_TEMPO / tempo)) + ");" + "\n");
break;
}
}
}
}
}
public static void main(String[] args){
new MidiReader();
}
}
MIDI文件使用兩個值來指定時間, 分辨率 (以每四分音符的刻度測量)和速度 (以每四分音符的微秒為單位)。
可以從序列中讀取分辨率。
速度是用MIDI信息指定的; 您必須在第一首曲目中搜索SET_TEMPO元事件,這會改變所有后續事件的速度,直到下一個速度事件。
另請參見如何從MetaMessage.getData()返回的字節數組中獲取整數值? , Midi TEMPO消息如何應用於其他曲目? ,並使用NAudio從MIDI文件中讀取筆記 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.