I do not understand how to catch tempo. So still use some shitty way to use tempo to my output.
I really thinks that just using NOTE_ON & NOTE_OFF timings will give me real time. But this output still played too slow in C++.
P.S.
We use only ONE VOICE midis when playing that. (It's just for fun, some computers in our class room is playing some 2+ VOICE music synchronized).
Here is my code:
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();
}
}