【发布时间】:2012-01-29 23:46:21
【问题描述】:
我正在尝试制作一个能够在检测到 midi 设备后在计算机上播放音符的 java 应用程序。
一旦获得所需的 MIDI 设备,我将设置接收器,设备的发射器将向其发送 MIDI 消息。
device.getTransmitter().setReceiver( new MyReceiver()) ;
MyReceiver 类看起来像:
public class MyReceiver implements Receiver {
MidiChannel[] channels ;
public MyReceiver (){
try {
Synthesizer synthesizer = MidiSystem.getSynthesizer();
synthesizer.open();
channels = synthesizer.getChannels();
channels[0].programChange( 22 ) ;
}catch ( Exception e ) {
e.printStackTrace() ;
}
}
public void noteOff ( int nota ) {
channels[0].noteOff(nota);
}
public void noteOn ( int nota ) {
channels[0].noteOn( nota , 100);
}
public void send(MidiMessage msg, long timeStamp ) {
byte[] b = msg.getMessage ();
String tmp = bits ( b [0] ) ;
int message = convertBits ( tmp ) ;
int note1 = convertBits ( bits ( b [ 1 ] ) ) ;
// note on in the first channel
if ( message == 144 ) {
noteOn( note1 ) ;
}
// note off in the first channel
if ( message == 128 ) {
noteOff( note1 ) ;
}
}
public String bits(byte b)
{
String bits = "";
for(int bit=7;bit>=0;--bit)
{
bits = bits + ((b >>> bit) & 1);
}
return bits;
}
public int convertBits ( String bits ) {
int res = 0 ;
int size = bits.length () ;
for ( int i = size-1 ; i >= 0 ; i -- ){
if ( bits.charAt( i ) == '1' ) {
res += 1 <<(size-i-1) ;
}
}
return res ;
}
public void close() {}
}
当我运行我的代码并开始在我的 MIDI 设备上播放时,我的延迟很高(我无法立即听到音符)。
我该如何解决这个问题?
【问题讨论】:
-
我的延迟为 69660(微秒)...
标签: java device midi latency javasound