title: Quality issue when playing sound and fast fade-out in Java
body: <p><em>Sorry for my bad english</em></p>
  
  <p>I write a Java desktop application that plays musical instruments audio files samples. </p>
  
  <p>Each time a note is received by the application, it must stop the current playing note and play the new one. if the user stops playing, the app must do a fade-out to the active note.</p>
  
  <p><em>By the way, smaller latency is better.</em></p>
  
  <p>I created a runnable which owns a play and stop method allowing me to play or stop a note(with fade out)</p>
  
  <p><strong>My code works but the sound quality is bad(many click and clipping etc)..</strong></p>
  
  <p><strong>-How to improve my code? Did I make mistake?</strong></p>
  
  <p><strong>-Otherwise it is there any other Java technology than JavaFx media player that is best suited for my need?</strong></p>
  
  <pre><code>public class SamplePlayer implements Runnable{
  
  private boolean fadeOut=false; // true when a fade out is needed
  private int soundToFadeOut=0; //the sound number to fade out
  private int sound=0; // the sound number to play (nothing if zero)
  private int lastSound=0; //the last sound number played that wee need to stop befor playing a new one
  
  
  
  MediaPlayer[] sample = new MediaPlayer[42]; //array to store all pre-loaded sounds
  
  public SamplePlayer()
  {
  
  
      for(int i=0;i&lt;42;i++) // load all sounds
      {
          Media pick = new Media(new File("./src/files/samples/1_"+(i+55)+".wav" ).toURI().toString());
          sample[i] = new MediaPlayer(pick);  
  
  
      }
  
  }
  /**
   * Here i try to play a sound each time the sound variable has changed
   * and fade out the sound specified in soundToFadeOut variable if fadeOut variable come to true;
   */
  @Override
  public void run() {
      while(true) // bad, but it's just for testing
      {
          try {
          if(sound!=0) // if a sound need to be played
          {
              if(lastSound!=0) // if i's not the first sound to played i stop the previous one.
              {  
                  sample[lastSound].stop();                   
              }
  
              sample[sound].setVolume(1); //returns the volume to 1 in case we made a fade out
              lastSound=sound; 
              sound=0;//sound = 0 to monitor new other sound       
          }
          if(fadeOut==true) // if wee need to make a fade out
          {
              fadeOut=false;
              fadeOut(sample[soundToFadeOut],50);
  
          }
  
              Thread.sleep(1); // if not the thread keep busy and play, stop method not working properly
          } catch (InterruptedException ex) {
              Logger.getLogger(SamplePlayer.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  }
  
      private void fadeOut(MediaPlayer player,double itteration) // fade out method : run a new thread to itterate on volume
      {Runnable task = () -&gt; { 
  
           try{
          for(int i=0;i&lt;itteration;i++)
          {
              double value=(itteration-i)/itteration; // set value to 1.0 to near 0.0 each itteration
              if(value&lt;0.1)
              {
                  value=0;
              }
              player.setVolume(value);
              Thread.sleep(5);
          }
  
  
      }catch(Exception e){e.printStackTrace();}
  
              };
  
  
      new Thread(task).start();
  
  
  
      }
  
      /*
       *
       * play and stop method which are called when a sound need to be  played or stoped (fade out)
       *
       */
  
      public void play(int note)
      {
  
          sound=note-55; // -55 just to match with our array range
  
  
      }
      public void stop(int note)
      {
          soundToFadeOut=note-55;
          fadeOut=true;
      }
  
  
  }   
  </code></pre>