为什么我只得到钢琴音符(Java)?

时间:2016-02-29 18:21:28

标签: java midi-instrument

我刚刚编写了垃圾音乐播放器。每次我们给它2个整数:“乐器”和“节点”,它将播放该乐器的节点。非常简单。 我试过了,但即使我改变了“乐器”(改变输入中的int),只有钢琴音符播放.. 代码如下:

package edu.swing.beatbox;

import java.util.Scanner;

import javax.sound.midi.*;

public class MiniMusic {

    public void play(int instrument, int note) {
        try {

            Sequencer player = MidiSystem.getSequencer();
            player.open();
            Sequence seq = new Sequence(Sequence.PPQ, 4);
            Track track = seq.createTrack();

            MidiEvent event = null;

            ShortMessage first = new ShortMessage();
            first.setMessage(192, 1, instrument, 0);
            MidiEvent changeInstrument = new MidiEvent(first, 1);
            track.add(changeInstrument);

            ShortMessage a = new ShortMessage();
            a.setMessage(144, 1, note, 100);
            MidiEvent noteOn = new MidiEvent(a, 1);
            track.add(noteOn);

            ShortMessage b = new ShortMessage();
            a.setMessage(128, 1, note, 100);
            MidiEvent noteOff = new MidiEvent(b, 3);
            track.add(noteOff);

            player.setSequence(seq);
            player.start();

        } catch (Exception exc) {
            exc.printStackTrace();
        }
    }

    public static void main(String[] args) {
        int instrument, note;
        MiniMusic mini = new MiniMusic();

        while (true) {
            Scanner inp = new Scanner(System.in);
            System.out.print("Instrument (0 --> 127) : "); instrument = inp.nextInt();
            System.out.print("Note (0 --> 127) : "); note = inp.nextInt();
            mini.play(instrument, note);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先,您必须了解MIDI及其工作原理:

并播放音符:

import javax.sound.midi.MidiChannel;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Synthesizer;
import java.util.Scanner;

public class MidiPlayer {

    private static final int DEFAULT_INSTRUMENT = 1;

    private MidiChannel channel;

    public MidiPlayer() throws MidiUnavailableException {
        this(DEFAULT_INSTRUMENT);
    }

    public MidiPlayer(int instrument) throws MidiUnavailableException {
        channel = getChannel(instrument);
    }

    public void setInstrument(int instrument) {
        channel.programChange(instrument);
    }

    public int getInstrument() {
        return channel.getProgram();
    }

    public void play(final int note) {
        channel.noteOn(note, 50);
    }

    public void release(final int note) {
        channel.noteOff(note, 50);
    }

    public void play(final int note, final long length) throws InterruptedException {
        play(note);
        Thread.sleep(length);
        release(note);
    }

    public void stop() {
        channel.allNotesOff();
    }

    private static MidiChannel getChannel(int instrument) throws MidiUnavailableException {
        Synthesizer synthesizer = MidiSystem.getSynthesizer();
        synthesizer.open();
        return synthesizer.getChannels()[instrument];
    }

    public static void main(String[] args) throws Exception {
        MidiPlayer player = new MidiPlayer();
        Scanner scanner = new Scanner(System.in);
        int note;
        while (!Thread.currentThread().isInterrupted()) {
            System.out.print("Note (1..127) : ");
            note = scanner.nextInt();
            player.play(note, 100);
        }
        scanner.close();
    }

}

请注意我已使用SynthesizerMidiChannel