【问题标题】:Java MIDI: Connecting MIDI Keyboard to Default SynthesizerJava MIDI:将 MIDI 键盘连接到默认合成器
【发布时间】:2016-09-30 05:10:53
【问题描述】:

我需要一个 MIDI 键盘。 我想要的只是一个示例,说明如何设置默认软件合成器以在我弹奏键盘时播放声音。

package cleffsgame;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;

public class CheckDevices {

    public static void main(String[] args) {
        MidiDevice device;
        // display each device's properties
        for (MidiDevice.Info info: MidiSystem.getMidiDeviceInfo()) {

            try {
                device = MidiSystem.getMidiDevice(info);

                System.out.println("\nDevice: ");
                System.out.println("Name: " + device.getDeviceInfo().getName());
                System.out.println("Vendor: " + device.getDeviceInfo().getVendor());
                System.out.println("Version: " + device.getDeviceInfo().getVersion());
                System.out.println("Description: " + device.getDeviceInfo().getDescription());
                System.out.println("Transmitters: " + device.getMaxTransmitters());
                System.out.println("Receivers: " + device.getMaxReceivers());

            } catch (MidiUnavailableException ex) {
                Logger.getLogger(CheckDevices.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

通过运行上面的代码,我得到以下输出:

运行:

设备:名称:Gervill 供应商:OpenJDK 版本:1.0 描述: 软件 MIDI 合成器发射器:0 接收器:-1

设备:名称:Oxygen 49 供应商:M-Audio 版本:未知版本 描述:氧气 49 发射器:-1 接收器:0

设备:名称:Oxygen 49 供应商:M-Audio 版本:未知版本 描述:氧气 49 发射器:0 接收器:-1

设备:名称:Real Time Sequencer 供应商:Oracle Corporation 版本: 版本 1.0 描述:软件定序器 发射器:-1 接收器:-1 构建成功(总时间:2 秒)

但是当我运行下面的代码时,按键时没有声音。

package cleffsgame;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Receiver;
import javax.sound.midi.Synthesizer;
import javax.sound.midi.Transmitter;

/**
 *
 * @author umberto
 */
public class Test {

    public static void main(String[] args) {
        MidiDevice inputDevice = null, synthDevice = null;
        Transmitter transmitter = null;
        Synthesizer synthesizer = null;
        Receiver receiver = null;

        try {
            inputDevice = MidiSystem.getMidiDevice(MidiSystem.getMidiDeviceInfo()[1]);
            synthDevice = MidiSystem.getMidiDevice(MidiSystem.getMidiDeviceInfo()[0]);

        } catch (MidiUnavailableException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }

        // goodDevice must be Oxygen 49 transmitter (MIDI Input)
        if (inputDevice != null && synthDevice != null) {
            try {
                transmitter = inputDevice.getTransmitter();
                System.out.println("Transmitter: " + inputDevice.getDeviceInfo());
                System.out.println(String.format("T/R: %s/%s", inputDevice.getMaxTransmitters(), inputDevice.getMaxReceivers()));

                receiver = synthDevice.getReceiver();
                System.out.println("Receiver: " + synthDevice.getDeviceInfo());
                System.out.println(String.format("T/R: %s/%s", synthDevice.getMaxTransmitters(), synthDevice.getMaxReceivers()));

                transmitter.setReceiver(receiver);
                System.out.println("GoodDevice is open... check sound\n");
                inputDevice.open();
                synthDevice.open();

            } catch (Exception ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }


}

似乎除了将 Synthesizer.receiver 连接到 InputDevice.transmitter 之外,还有更多工作要做,正如我从文档 https://docs.oracle.com/javase/tutorial/sound/MIDI-synth.html 中获得的那样。

【问题讨论】:

  • 您不需要为合成器分配一个补丁乐器吗?
  • @Ishmael 如果可以,请检查下面的代码。在操作它们之前,我打开了 kb 设备和合成器。我也装了一个乐器,但根本没有声音。如果你能写一些 hello world 片段,那真的很有帮助,因为我以前从未使用过 midi。
  • these examples 工作吗?
  • 我看到了它们,但我没有下载,因为只有 MidiDump.java 应该从我的键盘捕获输入并将其发送到控制台而不是合成器。我试过这个,它确认我的键盘正确地将 MidiEvents 发送到系统。 stackoverflow.com/questions/6937760/….

标签: java input keyboard midi


【解决方案1】:

在获取发射器/接收器之前打开设备可能是个好主意。

但实际的问题是这个程序会立即退出,所以设备在有用的时间内没有打开。

【讨论】:

  • 你认为下面的代码行得通吗? ` public static void main(String[] args) { Synthesizer s = MS.getSynthesizer(); s.open();仪器[] inst = s.getAvailableInstruments(); s.loadInstrument(inst[0]);接收者 r = s.getReceiver(); MidiDevice kb = MidiSystem.getMidiDevice(MidiSystem.getMidiDeviceInfo()[1]); kb.open();发射器 t = kb.getTransmitter(); t.setReceiver(r); // 保持活动状态.. JFrame frame = new JFrame("Test");框架.add(...); } `
  • 抱歉上面的未格式化代码。无论如何,我在调用其他方法之前尝试打开 MIDI 键盘和合成器,但我根本没有声音。我的键盘在 Logic Pro 和 Virtual Midi Piano Kb 等其他应用程序中运行良好。我需要一些 Hello World 代码来从 MIDI 键盘获取声音。
  • 那么,您的程序是否保持打开状态?
  • 是的,确实如此。您是否有 MIDI 键盘来测试该代码是否可以在您的机器上运行@cl?
【解决方案2】:

这对我有用

transmitter.setReceiver(new Receiver() {
    @Override
    public void send(MidiMessage message, long timeStamp) {
        receiver.send(message, -1);
    }
    @Override
    public void close() {
        receiver.close();
    }
});

【讨论】:

    猜你喜欢
    • 2011-09-24
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 2019-07-27
    • 2012-01-06
    相关资源
    最近更新 更多