gpt4 book ai didi

java - 如何使用 Java 解析 midi 的速度?

转载 作者:行者123 更新时间:2023-11-29 05:15:53 25 4
gpt4 key购买 nike

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

我真的认为只使用 NOTE_ON 和 NOTE_OFF 计时就能给我实时。但是这个输出在 C++ 中仍然播放得太慢。

附言
演奏时我们只使用一个 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 文件使用两个值来指定时间,分辨率(以每四分音符的节拍为单位)和节奏(以每四分音符的微秒为单位)。

可以从 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 .

关于java - 如何使用 Java 解析 midi 的速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26555718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com