public SonarSoundEngine(int maxChannels) throws LineUnavailableException {
    silentSample = new SonarSample(new float[] {0}, 44100);
    Mixer mixer = AudioSystem.getMixer(null);

    sdl = (SourceDataLine) mixer.getLine(new Line.Info(SourceDataLine.class));
    sdl.open(new AudioFormat(rate, 16, 2, true, false), bufferSize * 2 * 2 * 2 * 2 * 2);
    soundBuffer.order(ByteOrder.LITTLE_ENDIAN);
    sdl.start();

    try {
      /*            FloatControl volumeControl = (FloatControl) sdl.getControl(FloatControl.Type.MASTER_GAIN);
      volumeControl.setValue(volumeControl.getMaximum());*/
    } catch (IllegalArgumentException e) {
      System.out.println("Failed to set the sound volume");
    }

    listenerMixer = new ListenerMixer(maxChannels);

    leftBuf = new float[bufferSize];
    rightBuf = new float[bufferSize];

    Thread thread = new Thread(this);
    thread.setDaemon(true);
    thread.setPriority(10);
    thread.start();
  }
  public void tick() {
    soundBuffer.clear();

    //        targetAmplitude = (targetAmplitude - 1) * 0.9f + 1;
    //        targetAmplitude = (targetAmplitude - 1) * 0.9f + 1;
    synchronized (listenerMixer) {
      @SuppressWarnings("unused")
      float maxAmplitude = listenerMixer.read(leftBuf, rightBuf, rate);
      //            if (maxAmplitude > targetAmplitude) targetAmplitude = maxAmplitude;
    }

    soundBuffer.clear();
    float gain = 32000;
    for (int i = 0; i < bufferSize; i++) {
      //            amplitude += (targetAmplitude - amplitude) / rate;
      //          amplitude = 1;
      //              float gain = 30000;
      int l = (int) (leftBuf[i] * gain);
      int r = (int) (rightBuf[i] * gain);
      if (l > 32767) l = 32767;
      if (r > 32767) r = 32767;
      if (l < -32767) l = -32767;
      if (r < -32767) r = -32767;
      soundBuffer.putShort((short) l);
      soundBuffer.putShort((short) r);
    }

    sdl.write(soundBuffer.array(), 0, bufferSize * 2 * 2);
  }