gpt4 book ai didi

java midi 延迟

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:07:08 25 4
gpt4 key购买 nike

我正在尝试制作一个能够在检测到 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 设备上播放时,我遇到了高延迟(我无法立即听到音符)。

我该如何解决这个问题?

最佳答案

由于您平台上的音频支持有限,此问题可能无法避免 - 例如,Windows 根本无法通过常用的音频 API 提供低延迟,因此 VM 也不会实现它。

OS X 和 Linux 通常都可以,但如果您调整系统音频设置/驱动程序可能会更快。

Windows 上似乎存在解决方法 ( http://www.jsresources.org/faq_misc.html#asio ),但我还没有尝试过...

关于java midi 延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9057870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com