本文介绍了Java Midi延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个Java应用程序,该应用程序能够在检测到Midi设备后在计算机上播放笔记.

I´m trying to make a java application that is able to play notes on the computer after detecting a midi device.

一旦我获得了所需的midi设备,我就在设置接收器,设备的发送器将向其发送MIDI消息.

Once I get the desired midi device I´m seting the receiver to which the device´s transmitter will deliver MIDI messages.

    device.getTransmitter().setReceiver( new MyReceiver()) ;

MyReceiver类如下:

class MyReceiver looks like:

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设备上播放时,我的等待时间很长(我无法立即听到音符).

When I run my code and start playing on my midi device I´m getting a high latency (I can´t hear notes instantly).

如何解决此问题?

推荐答案

我正在使用 asio 驱动程序以避免延迟的nofollow> JAsioHost 保护程序

I´m using the JAsioHost proyect that uses an asio driver to avoid latency

这篇关于Java Midi延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:02