Example #1
0
  /**
   * Attempt to set the global gain (volume ish) for the play back. If the control is not supported
   * this method has no effect. 1.0 will set maximum gain, 0.0 minimum gain
   *
   * @param gain The gain value
   */
  public void setVolume(float gain) { // **** CAMBIADO GAIN POR VOLUME ****
    if (gain != -1.0f) {
      if (gain > 1.0f) {
        gain = 1.0f;
      } else if (gain < 0.0f) {
        gain = 0.0f;
      }
    }

    this.gain = gain;

    if (outputLine == null) {
      return;
    }

    try {
      FloatControl control = (FloatControl) outputLine.getControl(FloatControl.Type.MASTER_GAIN);
      if (gain == -1) {
        control.setValue(0.0f);
      } else {
        float max = control.getMaximum();
        float min = control.getMinimum(); // negative values all seem to be zero?
        float range = max - min;
        control.setValue(min + (range * gain));
        Log.log5(
            String.format(
                "Current volume: %.2f, Range: %.2f, Max: %.2f, Min: %.2f",
                control.getValue(), range, max, min));
      }
    } catch (IllegalArgumentException e) {
      // gain not supported
      e.printStackTrace();
    }
  }
Example #2
0
  /**
   * Attempt to set the global gain (volume ish) for the play back. If the control is not supported
   * this method has no effect. 1.0 will set maximum gain, 0.0 minimum gain
   *
   * @param gain The gain value
   */
  public void setGain(float gain) {
    if (gain != -1) {
      if ((gain < 0) || (gain > 1)) {
        throw new IllegalArgumentException("Volume must be between 0.0 and 1.0");
      }
    }

    this.gain = gain;

    if (outputLine == null) {
      return;
    }

    try {
      FloatControl control = (FloatControl) outputLine.getControl(FloatControl.Type.MASTER_GAIN);
      if (gain == -1) {
        control.setValue(0);
      } else {
        float max = control.getMaximum();
        float min = control.getMinimum(); // negative values all seem to
        // be zero?
        float range = max - min;

        control.setValue(min + (range * gain));
      }
    } catch (IllegalArgumentException e) {
      // gain not supported
      e.printStackTrace();
    }
  }
  /* package */ void populateVolume(SourceDataLine line) {
    try {
      if (line != null && line.isControlSupported(FloatControl.Type.VOLUME)) {
        FloatControl c = (FloatControl) line.getControl(FloatControl.Type.VOLUME);
        float interval = c.getMaximum() - c.getMinimum();
        float cv = currentVolume;

        interval *= cv;
        c.setValue(c.getMinimum() + interval);
        return;
      }
    } catch (Throwable ignore) {
    }
    try {
      if (line != null && line.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
        FloatControl c = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
        float interval = c.getMaximum() - c.getMinimum();
        float cv = currentVolume;

        interval *= cv;
        c.setValue(c.getMinimum() + interval);
      }
    } catch (Throwable ignore) {
    }
  }
Example #4
0
  public void run() {

    File soundFile = new File(filename);
    if (!soundFile.exists()) {
      System.err.println("Wave file not found: " + filename);
      Dialog.erreur(null, "Le fichier " + filename + " n'a pas été trouver.");
      return;
    }

    AudioInputStream audioInputStream = null;
    try {
      audioInputStream = AudioSystem.getAudioInputStream(soundFile);
    } catch (UnsupportedAudioFileException e1) {
      e1.printStackTrace();
      return;
    } catch (IOException e1) {
      e1.printStackTrace();
      return;
    }

    AudioFormat format = audioInputStream.getFormat();
    SourceDataLine auline = null;
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

    try {
      auline = (SourceDataLine) AudioSystem.getLine(info);
      auline.open(format);
    } catch (LineUnavailableException e) {
      e.printStackTrace();
      return;
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }

    if (auline.isControlSupported(FloatControl.Type.PAN)) {
      FloatControl pan = (FloatControl) auline.getControl(FloatControl.Type.PAN);
      if (curPosition == Position.RIGHT) pan.setValue(1.0f);
      else if (curPosition == Position.LEFT) pan.setValue(-1.0f);
    }

    auline.start();
    int nBytesRead = 0;
    byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

    try {
      while (nBytesRead != -1) {
        nBytesRead = audioInputStream.read(abData, 0, abData.length);
        if (nBytesRead >= 0) auline.write(abData, 0, nBytesRead);
      }
    } catch (IOException e) {
      e.printStackTrace();
      return;
    } finally {
      auline.drain();
      auline.close();
    }
  }
Example #5
0
  @Override
  public void onVolumeChanged(short volume) {
    log.info("apply_volume_callback");
    log.debug("volume: " + volume);
    if (audioLine == null) {
      log.warn("audioLine not ready");
      return;
    }
    FloatControl volumeControl = (FloatControl) audioLine.getControl(FloatControl.Type.MASTER_GAIN);
    float maxDb = volumeControl.getMaximum();
    log.debug("maxDb : " + maxDb);
    float minDbOrig = volumeControl.getMinimum();
    float minDb = minDbOrig + ((maxDb - minDbOrig) / 3);
    log.debug("minDb : " + minDb);
    float newVolume = 0;

    float volumePercent = (float) (volume / 655.35);
    if (volumePercent < 0) {
      volumePercent = 100 + volumePercent;
    }
    log.debug("volume percent : " + volumePercent);
    newVolume = (volumePercent * minDb / 100);
    newVolume = (newVolume - minDb) * -1;

    if (volume == 0) {
      log.debug("volume 0, setting max");
      newVolume = minDbOrig;
    }

    log.debug("newVolume : " + newVolume);
    volumeControl.setValue(newVolume);
  }
Example #6
0
 /*
   x долже быть в пределах от 0 до 1 (от самого тихого к самому громкому)
 */
 public void setVolume(float x) {
   if (x < 0) x = 0;
   if (x > 1) x = 1;
   float min = volumeC.getMinimum();
   float max = volumeC.getMaximum();
   volumeC.setValue((max - min) * x + min);
 }
Example #7
0
 public void reset() {
   for (HSvolume vs : volumeControl) {
     if (vs != null) {
       vs.breakFade();
     }
   }
   for (FloatControl c : gainControl) {
     c.setValue(c.getMinimum());
   }
   for (AudioInputStream s : streams) {
     try {
       s.close();
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
   for (Clip c : clips) {
     c.stop();
     c.flush();
     c.drain();
   }
   gainControl = new ArrayList<>();
   volumeControl = new ArrayList<>();
   clips = new ArrayList<>();
 }
Example #8
0
 public void setVolume(int instanceNumber, double volume) {
   if (volumeControl.get(instanceNumber) != null) {
     volumeControl.get(instanceNumber).breakFade();
   }
   float vol = HSvolume.calcVol(volume);
   FloatControl gc = gainControl.get(instanceNumber);
   gc.setValue((float) vol);
 }
Example #9
0
 public void SILENCE(int instanceNumber) {
   if (volumeControl.get(instanceNumber) != null) {
     volumeControl.get(instanceNumber).breakFade();
   }
   clips.get(instanceNumber).close();
   FloatControl gainControl =
       (FloatControl) clips.get(instanceNumber).getControl(FloatControl.Type.MASTER_GAIN);
   gainControl.setValue(gainControl.getMinimum());
 }
Example #10
0
  public void setVolume(float vol) {
    FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);

    try {
      gainControl.setValue(convert(gainControl, vol));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 /**
  * Sets Pan value. Line should be opened before calling this method. Linear scale : -1.0 <--> +1.0
  */
 public void setPan(double fPan) throws BasicPlayerException {
   if (hasPanControl()) {
     log.fine("Pan : " + fPan);
     m_panControl.setValue((float) fPan);
     notifyEvent(BasicPlayerEvent.PAN, getEncodedStreamPosition(), fPan, null);
   } else {
     throw new BasicPlayerException(BasicPlayerException.PANCONTROLNOTSUPPORTED);
   }
 }
Example #12
0
  public boolean play() {

    try {
      if (playState != STOPPED) playStop();

      if (audioBytes == null) return false;

      DataLine.Info info = new DataLine.Info(Clip.class, format);

      clip = (Clip) AudioSystem.getLine(info);
      clip.addLineListener(new ClipListener());

      long clipStart = (long) (audioBytes.length * getStartTime() / (getDuration() * 1000.0));
      long clipEnd = (long) (audioBytes.length * getEndTime() / (getDuration() * 1000.0));
      if ((clipEnd - clipStart) > MAX_CLIP_LENGTH) clipEnd = clipStart + MAX_CLIP_LENGTH;
      byte[] clipBytes = new byte[(int) (clipEnd - clipStart)];
      System.arraycopy(audioBytes, (int) clipStart, clipBytes, 0, clipBytes.length);
      clip.open(format, clipBytes, 0, clipBytes.length);

      FloatControl panControl = (FloatControl) clip.getControl(FloatControl.Type.PAN);

      panControl.setValue((float) panSetting / 100.0f);

      double value = (double) gainSetting;

      FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
      float dB = (float) (Math.log(value == 0.0 ? 0.0001 : value) / Math.log(10.0) * 20.0);
      gainControl.setValue(dB);
      double playStartTime = (player.getSeekTime() / 100) * (playGetLength());
      clip.setMicrosecondPosition((long) playStartTime);

      clip.start();

      playState = PLAYING;

      return true;

    } catch (Exception ex) {
      ex.printStackTrace();
      playState = STOPPED;
      clip = null;
      return false;
    }
  }
Example #13
0
  public void run() {

    File soundFile = new File(this.filename);
    if (!soundFile.exists()) {
      System.err.println("nicht gefunden: " + filename);
      return;
    }

    AudioInputStream audioInputStream = null;
    try {
      audioInputStream = AudioSystem.getAudioInputStream(soundFile);
    } catch (UnsupportedAudioFileException e1) {
      e1.printStackTrace();
      return;
    } catch (IOException e1) {
      e1.printStackTrace();
      return;
    }

    AudioFormat format = audioInputStream.getFormat();
    SourceDataLine auline = null;
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

    try {
      auline = (SourceDataLine) AudioSystem.getLine(info);
      auline.open(format);
    } catch (LineUnavailableException e) {
      e.printStackTrace();
      return;
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }

    FloatControl rate = (FloatControl) auline.getControl(FloatControl.Type.SAMPLE_RATE);

    rate.setValue(rate.getValue() * 5f);

    auline.start();
    int nBytesRead = 0;
    byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

    try {
      while (nBytesRead != -1) {
        nBytesRead = audioInputStream.read(abData, 0, abData.length);
        if (nBytesRead >= 0) auline.write(abData, 0, nBytesRead);
      }
    } catch (IOException e) {
      e.printStackTrace();
      return;
    } finally {
      auline.drain();
      auline.close();
    }
  }
Example #14
0
 public Sound(String fileName) {
   try {
     AudioInputStream ais = AudioSystem.getAudioInputStream(Sound.class.getResource(fileName));
     clip = AudioSystem.getClip();
     clip.open(ais);
     FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
     gainControl.setValue(-20.0f);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Example #15
0
 public void updateVolume(int percent) {
   if (out != null && out.isOpen()) {
     try {
       FloatControl c = (FloatControl) out.getControl(FloatControl.Type.MASTER_GAIN);
       float min = c.getMinimum();
       float v = percent * (c.getMaximum() - min) / 100f + min;
       c.setValue(v);
     } catch (IllegalArgumentException e) {
     }
   }
   volume = percent;
 }
Example #16
0
 /** setVol(float) adjusts overall volume. Checks for min/max limits to avoid crashes. */
 public void setVol(float newvol) {
   if (newvol > -40.0f && newvol < 6.020f) {
     try {
       volCtrl.setValue(newvol);
     } catch (Exception e) {
       PApplet.println(e.getMessage());
     }
   } else {
     PApplet.println(
         "Error - volume '" + newvol + "' is out of range. Must be between -40.0f and 6.020f!");
   }
 }
Example #17
0
 /**
  * setPanning(float) takes a float from -1.0f to 1.0f to pan the overall audio output across the
  * stereo spectrum. The centre-point is 0.
  */
 public void setPanning(float panval) {
   if (panval > -1.0f && panval < 1.0f) {
     try {
       FloatControl panctrl =
           (FloatControl) player.output_line.getControl(FloatControl.Type.BALANCE);
       panctrl.setValue(panval);
       panning = panval;
     } catch (Exception e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
   }
 }
Example #18
0
 Sound(URL soundPath, float gain) {
   this.soundPath = soundPath;
   try {
     AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundPath);
     sound = AudioSystem.getClip();
     sound.open(audioInputStream);
     gainControl = (FloatControl) sound.getControl(FloatControl.Type.MASTER_GAIN);
     gainControl.setValue(gain);
   } catch (Exception e) {
     System.out.println("The sound from file " + String.valueOf(soundPath) + "was not found!");
     System.out.println(e);
   } // Satisfy the catch
 }
Example #19
0
  /**
   * Attempt to set the balance between the two speakers. -1.0 is full left speak, 1.0 if full right
   * speaker. Anywhere in between moves between the two speakers. If the control is not supported
   * this method has no effect
   *
   * @param balance The balance value
   */
  public void setBalance(float balance) {
    this.balance = balance;

    if (outputLine == null) {
      return;
    }

    try {
      FloatControl control = (FloatControl) outputLine.getControl(FloatControl.Type.BALANCE);
      control.setValue(balance);
    } catch (IllegalArgumentException e) {
      // balance not supported
    }
  }
 /**
  * Sets Gain value. Line should be opened before calling this method. Linear scale 0.0 <--> 1.0
  * Threshold Coef. : 1/2 to avoid saturation.
  */
 public void setGain(double fGain) throws BasicPlayerException {
   if (hasGainControl()) {
     double minGainDB = getMinimumGain();
     double ampGainDB = ((10.0f / 20.0f) * getMaximumGain()) - getMinimumGain();
     double cste = Math.log(10.0) / 20;
     double valueDB =
         minGainDB + (1 / cste) * Math.log(1 + (Math.exp(cste * ampGainDB) - 1) * fGain);
     log.finest("Gain : " + valueDB);
     m_gainControl.setValue((float) valueDB);
     notifyEvent(BasicPlayerEvent.GAIN, getEncodedStreamPosition(), fGain, null);
   } else {
     throw new BasicPlayerException(BasicPlayerException.GAINCONTROLNOTSUPPORTED);
   }
 }
Example #21
0
  public String playSoundFile(String filename) {
    soundFile = new File("X\\" + filename);
    try {
      audioStream = AudioSystem.getAudioInputStream(soundFile);
      AudioFormat format = audioStream.getFormat();
      DataLine.Info info = new DataLine.Info(Clip.class, format);
      clip = (Clip) AudioSystem.getLine(info);
      clip.open(audioStream);

      volCtrl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
      volCtrl.setValue(volumn);
      clip.start();
    } catch (Exception e) {
      return e.getLocalizedMessage();
    }
    if (filename != lowBatteryAlertSoundFile
        && filename != powerOffAlertSoundFile
        && filename != loudVolumeAlertSoundFile
        && filename != lowVolumeAlertSoundFile
        && filename != mediumVolumeAlertSoundFile) lastPlayedSoundFile = filename;
    return "PLAYED " + filename;
  }
Example #22
0
  private void fade(FloatControl control, double to) {
    //noinspection SynchronizationOnLocalVariableOrMethodParameter
    synchronized (control) {
      int steps = FADE_DURATION / FADE_STEP_DURATION;

      to = (to <= 0.0) ? 0.0001 : ((to > 1.0) ? 1.0 : to);

      float currDB = control.getValue();
      float toDB = getDb(to);

      float diffStep = (toDB - currDB) / (float) steps;
      for (int i = 0; i < steps; i++) {
        currDB += diffStep;
        currDB = (Math.abs(currDB - toDB) < Math.abs(diffStep * 1.5)) ? toDB : currDB;
        control.setValue(currDB);
        try {
          Thread.sleep(FADE_STEP_DURATION);
        } catch (Exception ignored) {
          ignored.printStackTrace();
        }
      }
    }
  }
 /**
  * Set the balance from -1.0 for left, 0.0 for center and 1.0 for right
  *
  * @param range
  */
 public void setBalance(double range) {
   FloatControl gainControl = (FloatControl) audioClip.getControl(FloatControl.Type.BALANCE);
   gainControl.setValue((float) range);
 }
Example #24
0
 public void setVolumn(float num) {
   volumn = num;
   volCtrl.setValue(num);
 }
Example #25
0
 public void changeGain(float gain) {
   gainControl.setValue(gain);
 }
Example #26
0
 public void setVolume(float f) {
   float val = (float) (3 * f - 40);
   FloatControl control = (FloatControl) c.getControl(FloatControl.Type.MASTER_GAIN);
   control.setValue(val);
 }
Example #27
0
 private void setVolume(double value) {
   for (SourceDataLine line : new SourceDataLine[] {mainLine, bufferLine}) {
     FloatControl gainControl = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
     gainControl.setValue(getDb(value));
   }
 }
 /**
  * Set the volume from 0 (muted) to 11 (loud)
  *
  * @param volume
  */
 public void setVolume(double volume) {
   FloatControl gainControl = (FloatControl) audioClip.getControl(FloatControl.Type.MASTER_GAIN);
   double gain = volume / 11.0;
   double dB = (double) (Math.log(gain) / Math.log(10.0) * 20.0);
   gainControl.setValue((float) dB);
 }
Example #29
0
 public void setVolume(float volume) {
   gain = (FloatControl) audio.getControl(FloatControl.Type.MASTER_GAIN);
   gain.setValue(volume);
 }
 /**
  * Set the pan from -1.0 for left, 0.0 for center and 1.0 for right
  *
  * @param range
  */
 public void setPan(double range) {
   FloatControl gainControl = (FloatControl) audioClip.getControl(FloatControl.Type.PAN);
   gainControl.setValue((float) range);
 }