如何使用Java解析midi的节奏?

时间:2014-10-24 20:24:21

标签: java midi

我不明白如何捕捉节奏。 所以仍然使用一些糟糕的方式来为我的输出使用速度。

我真的认为只使用NOTE_ON& NOTE_OFF时间将给我实时。 但是这个输出在C ++中仍然发挥得太慢。

P.S。
播放时我们只使用一个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();
}
}

1 个答案:

答案 0 :(得分:2)

MIDI文件使用两个值来指定时间,分辨率(以每季度音符为单位测量)和速度(以每季度音符的微秒为单位)。

可以从Sequence读取分辨率。

速度是用MIDI信息指定的;你必须在第一首曲目中搜索SET_TEMPO元事件,它会改变所有后续事件的速度,直到下一个速度事件。

另请参阅How to get integer value from byte array returned by MetaMessage.getData()?How does Midi TEMPO message apply to other tracks?Reading notes from MIDI file using NAudio