我实现了一个简单的程序,它从Midi键盘输入,然后使用javax合成器接口输出相应的声音。 这在我运行Windows的Pc上运行得非常好,但是,我想在运行Raspbian的Raspberry Pi上运行它。它实际上也可以工作,但是一旦我播放更多/更快的音符,声音开始抖动和噼啪声非常糟糕,我必须停止播放音符约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() {
}
}
}
这是由于Pi的硬件限制,还是我可以对此做些什么?
答案 0 :(得分:0)
如果你有任何循环来更新midi检索/声音输出,可能会尝试在那里进行线程休眠以给操作系统时间做些什么?除此之外,不确定。由于某些原因,原始Raspberry Pi上的USB不是很好(很多错误,性能很慢 - 但这些确实在新的Linux /固件中有所修复)。您可能还需要修改采样率以匹配当前声音输出的理想设置(如果可以访问)(采样率不匹配意味着更多转换)。 Java可能会尝试使用理想作为默认值,但可能会被操作系统错误报告?