Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

audio - Reading MIDI files in Java

I'm trying to read in .MID files to a Java program, and would like to separate each note/chord so as to display them on a UI of some sort. I didn't have much luck using the Sequencer API in Java, and trying to use MidiFileReader directly didn't work for me either. I'll attach the code I used here, if anyone wants to see it:

package miditest;
import java.io.File;
import java.io.IOException;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;

public class Main {
    public static void main(String[] args) throws InvalidMidiDataException, IOException, MidiUnavailableException{
    Sequence sequence = MidiSystem.getSequence(new File("test.mid"));

    // Create a sequencer for the sequence
    Sequencer sequencer = MidiSystem.getSequencer();
    sequencer.open();
    sequencer.setSequence(sequence);

    // Start playing
    sequencer.start();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've never looked deeply into the MIDI support in Java, and the last time I played seriously with MIDI programming was when Commodore Amiga was king.

It looks like you may have to do quite a bit of manual work. Here's a crude example that interprets all NOTE_ON and NOTE_OFF events, for the rest of the events it just prints the command number.

The reason it might seem trickier than one might have thought at first is because MIDI focuses on capturing the instrument events (for example, when a keyboard key was pressed, when it was released, etc), and not on sheet music notation.

This code prints out one line per event, stating with the tick (which is the timing information for the event), channel, event type, note name, key, velocity

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();
        }

    }
}

For example the fur elise.mid I had lying around here produces something like this at the beginning:

@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

UPDATE: The channels are the 16 channels of the MIDI specification.

http://www.midi.org/techspecs/gm.php

Channels: All 16 MIDI Channels are supported. Each Channel can play a variable number of voices (polyphony). Each Channel can play a different instrument (sound/patch/timbre). Key-based percussion is always on MIDI Channel 10.

And velocity is one of the attributes used to control the sounds. For example, capturing MIDI data on a keyboard it'd be the force with which you press a key. Normally it controls the volume of the sound. See here for more details: http://audio.tutsplus.com/tutorials/production/7-ways-to-use-and-edit-midi-velocity/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...