Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8087996

Timing problems when playing multiple AudioClips consecutively

XMLWordPrintable

      When issuing a play invocation to multiple AudioClips one after the other, audible timing problems are caused.

      Code to reproduce:


      import javafx.application.Application;
      import javafx.scene.media.AudioClip;
      import javafx.stage.Stage;

      /**
       * Demonstrate that sample played directly one after the other lead to timing problems
       */
      public class AudioClipDelayTest extends Application{
          @Override
          public void start(Stage primaryStage) throws Exception {
              // download at https://www.dropbox.com/s/u23j6piv7lr2l7y/bassdrum.wav
              final AudioClip bassdrum = new AudioClip("file:" + System.getProperty("user.home") + "/tmp/bassdrum.wav");
              // download at https://www.dropbox.com/s/r17gc34hh5f6o7l/snare.wav
              final AudioClip snare = new AudioClip("file:" + System.getProperty("user.home") + "/tmp/snare.wav");
              // download at https://www.dropbox.com/s/rwk6c1km2xa8lz8/hihat.wav
              final AudioClip hihat = new AudioClip("file:" + System.getProperty("user.home") + "/tmp/hihat.wav");

              boolean allowMultiBeat = true;

              new Thread(()->{
                  final int numBeats = 8 * 4;
                  for(int i=0;i< numBeats;i++){
                      int bim = i % 8;
                      if(allowMultiBeat){
                          if(bim == 0 || bim == 2 || bim == 4 || bim == 6){
                              bassdrum.play();
                          }
                          if(bim == 2 || bim == 6){
                              snare.play();
                          }
                          hihat.play();
                      } else{
                          if(bim == 0 || bim == 4){
                              bassdrum.play();
                          } else if(bim == 2 || bim == 6){
                              snare.play();
                          } else {
                              hihat.play();
                          }
                      }
                      try {
                          Thread.sleep(250);
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                  }
              }).start();

          }
      }

      When the variable allowMultiBeat is set to true this should result in a standard rock drum rythm but it sounds rather erratic. When you set to allowMultiBeat to false, it is avoided to play more than one sample on the same beat and the result sounds like it should, so it does not seem to be a general issue of playback latency but more likely (I am guessing here) that playing more than one sound somehow causes interferences.

      Note that this is _not_ a general hardware or platform latency issue. The same code using libgdx for sample playback plays both beats with perfect timing on the same machine/os installation.

      It is IMO a serious issue as it renders the audio subsystem useless for a wide range of applications where timing matters, e.g. music, games some healthcare applications etc..

            ddehaven David Dehaven (Inactive)
            rkruegerjfx Robert Krueger (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: