用Java读取MIDI文件

发布于 2024-09-26 06:10:45 字数 865 浏览 1 评论 0原文

我正在尝试将 .MID 文件读入 Java 程序,并希望将每个音符/和弦分开,以便将它们显示在某种 UI 上。我在 Java 中使用 Sequencer API 的运气并不好,并且尝试直接使用 MidiFileReader 对我来说也不起作用。如果有人想看的话,我将附上我在这里使用的代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活雷疯 2024-10-03 06:10:45

我从来没有深入研究过 Java 中的 MIDI 支持,最后一次认真玩 MIDI 编程是在 Commodore Amiga 称王的时候。

看起来您可能需要做相当多的手动工作。下面是一个粗略的示例,它解释了所有 NOTE_ON 和 NOTE_OFF 事件,对于其余事件,它仅打印命令号。

它看起来比人们一开始想象的要棘手的原因是因为 MIDI 专注于捕获乐器事件(例如,按下键盘键时、释放键盘键时等),而不是乐谱符号。

这段代码打印出每个事件一行,用勾号(这是事件的计时信息)、通道、事件类型、音符名称、键、速度来说明

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

    }
}

例如我躺在这里的 Fur elise.mid 会产生类似的内容这在开头:

@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

更新:
通道是MIDI规范的16个通道。

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

通道:所有 16 个 MIDI 通道
支持。每个频道可以播放一个
可变数量的声音(复调)。
每个频道可以播放不同的
乐器(声音/音色/音色)。
基于琴键的打击乐始终在 MIDI 上
频道 10。

速度是用于控制声音的属性之一。例如,在键盘上捕获 MIDI 数据将是您按下按键的力量。通常它控制声音的音量。请参阅此处了解更多详细信息:http: //audio.tutsplus.com/tutorials/production/7-ways-to-use-and-edit-midi-velocity/

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文