java midi 延迟

标签 java device midi latency javasound

我正在尝试制作一个能够在检测到 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/

相关文章:

Java 序列监听器

java - Mapstruct 多对一映射

Java接口(interface)MouseMotionListener抽象方法: What's behind the scene?

c++ - Windows 中的麦克风列表(输入设备列表)

php - 从不同设备向服务器发送 html 请求

ios - 我的 iOS 设备没有出现在 xcode 设备列表中,显示在管理器中

python - 当我尝试发送控制更改消息时,MIDI 消息数据无效

java - 将带小数的毫秒转换为毫秒

java - 阿米蒂奇浮点异常

java.lang.ClassNotFoundException : Didn't find class "com.google.android.maps.MapView"