树莓派上的Midi输入

3
我已经实现了一个简单的程序,该程序从Midi键盘输入音符,然后使用javax synthesizer接口输出相应的声音。在我的Windows电脑上,这个程序运行得非常好,但是我想在运行Raspbian的树莓派上运行它。它实际上也可以工作,但是一旦我播放更多/更快的音符,声音就开始变得很卡顿和噼啪作响,我必须停止演奏约2秒钟才能让卡顿消失。我已经使用了外部USB声卡适配器,但效果不是很明显。这是处理midi输入的类:
public class MidiHandler {

    public MidiHandler() {
        MidiDevice device;
        MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
        for (int i = 0; i < infos.length; i++) {
            try {
                device = MidiSystem.getMidiDevice(infos[i]);
                // does the device have any transmitters?
                // if it does, add it to the device list
                System.out.println(infos[i]);

                // get all transmitters
                List<Transmitter> transmitters = device.getTransmitters();
                // and for each transmitter

                for (int j = 0; j < transmitters.size(); j++) {
                    // create a new receiver
                    transmitters.get(j).setReceiver(
                    // using my own MidiInputReceiver
                            new MidiInputReceiver(device.getDeviceInfo()
                                    .toString()));
                }

                Transmitter trans = device.getTransmitter();
                trans.setReceiver(new MidiInputReceiver(device.getDeviceInfo()
                        .toString()));

                // open each device
                device.open();
                // if code gets this far without throwing an exception
                // print a success message
            } catch (MidiUnavailableException e) {
            }
        }

    }

    // tried to write my own class. I thought the send method handles an
    // MidiEvents sent to it
    public class MidiInputReceiver implements Receiver {
        Synthesizer synth;
        MidiChannel[] mc;
        Instrument[] instr;
        int instrument;
        int channel;



        public MidiInputReceiver(String name) {
            try
            {
                patcher p = new patcher();
                this.instrument = p.getInstrument();
                this.channel = p.getChannel();
                this.synth = MidiSystem.getSynthesizer();
                this.synth.open();
                this.mc = synth.getChannels();
                instr = synth.getDefaultSoundbank().getInstruments();
                this.synth.loadInstrument(instr[1]);
                mc[this.channel].programChange(0, this.instrument);
                System.out.println(this.channel + ", " + this.instrument);

            }
            catch (MidiUnavailableException e)
            {
                e.printStackTrace();
                System.exit(1);
            }
        }

        public void send(MidiMessage msg, long timeStamp) {
            /*
             * Use to display midi message
             * 
            for(int i = 0; i < msg.getMessage().length; i++) {
                System.out.print("[" + msg.getMessage()[i] + "] ");

            }
            System.out.println();
            */
            if (msg.getMessage()[0] == -112) {
                mc[this.channel].noteOn(msg.getMessage()[1], msg.getMessage()[2]+1000);
            }

            if (msg.getMessage()[0] == -128) {
                mc[this.channel].noteOff(msg.getMessage()[1], msg.getMessage()[2]+1000);
            }

        }

        public void close() {
        }
    }
}

这是由于树莓派的硬件限制导致的,还是我可以采取一些措施解决这个问题?

什么是CPU利用率? - CL.
@CL 当程序运行时,CPU 使用率在 30% 到 50% 之间。当我演奏单个音符时,它会上升到 60% 到 70% 之间。每当 CPU 使用率达到 ~90% 以上时,声音故障就会发生。 - user3194972
1个回答

0

如果您有任何用于更新MIDI检索/声音输出的循环,也许可以在其中进行线程休眠,以便给操作系统时间来处理一些事情?除此之外,不确定。由于某种原因,原始Raspberry Pi上的USB不是很好(存在许多错误,性能较慢--但这些问题在更新的Linux /固件中得到了一定程度的解决)。如果可访问当前声音输出的理想设置,则还可能需要修改采样率以匹配理想设置(采样率不匹配意味着需要更多转换)。Java可能会尝试使用理想设置作为默认设置,但可能会被操作系统错误报告?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接