/* 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) {
    }
  }
Пример #2
0
 /** Gets min Gain value. */
 public float getMinimumGain() {
   if (hasGainControl()) {
     return m_gainControl.getMinimum();
   } else {
     return 0.0F;
   }
 }
Пример #3
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<>();
 }
Пример #4
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();
    }
  }
Пример #5
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);
 }
Пример #6
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();
    }
  }
Пример #7
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);
  }
Пример #8
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());
 }
Пример #9
0
 public FloatControlBoundedRangeModel(FloatControl control) {
   this.control = control;
   float range = 100;
   float steps = range / 100;
   factor = range / steps;
   int min = (int) (control.getMinimum() * factor);
   int max = (int) (control.getMaximum() * factor);
   int value = (int) (control.getValue() * factor);
   setRangeProperties(value, 0, min, max, false);
 }
Пример #10
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;
 }
Пример #11
0
 /** Opens the line. */
 protected void openLine() throws LineUnavailableException {
   if (m_line != null) {
     AudioFormat audioFormat = m_audioInputStream.getFormat();
     int buffersize = lineBufferSize;
     if (buffersize <= 0) {
       buffersize = m_line.getBufferSize();
     }
     m_lineCurrentBufferSize = buffersize;
     m_line.open(audioFormat, buffersize);
     log.info("Open Line : BufferSize=" + buffersize);
     /*-- Display supported controls --*/
     Control[] c = m_line.getControls();
     for (int p = 0; p < c.length; p++) {
       log.info("Controls : " + c[p].toString());
     }
     /*-- Is Gain Control supported ? --*/
     if (m_line.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
       m_gainControl = (FloatControl) m_line.getControl(FloatControl.Type.MASTER_GAIN);
       log.info(
           "Master Gain Control : ["
               + m_gainControl.getMinimum()
               + ","
               + m_gainControl.getMaximum()
               + "] "
               + m_gainControl.getPrecision());
     }
     /*-- Is Pan control supported ? --*/
     if (m_line.isControlSupported(FloatControl.Type.PAN)) {
       m_panControl = (FloatControl) m_line.getControl(FloatControl.Type.PAN);
       log.info(
           "Pan Control : ["
               + m_panControl.getMinimum()
               + ","
               + m_panControl.getMaximum()
               + "] "
               + m_panControl.getPrecision());
     }
   }
 }
Пример #12
0
  private static float convert(FloatControl control, float level) {
    float min = control.getMinimum() / 2;
    float max = control.getMaximum();

    float range;
    float breakpoint;
    float meanLevel;

    if (min < 0) {
      min = min * -1;
    }
    if (max < 0) {
      max = max * -1;
    }
    range = min + max;
    breakpoint = min * 100 / range;
    meanLevel = level * range / 100;

    if (meanLevel > min) {
      return meanLevel - min;
    } else {
      return meanLevel - range + max;
    }
  }
Пример #13
0
 // Возвращает текущую громкость (число от 0 до 1)
 public float getVolume() {
   float v = volumeC.getValue();
   float min = volumeC.getMinimum();
   float max = volumeC.getMaximum();
   return (v - min) / (max - min);
 }
Пример #14
0
 private void setVolume(SourceDataLine line, int vol) {
   FloatControl.Type type =
       (line.isControlSupported(FloatControl.Type.VOLUME))
           ? FloatControl.Type.VOLUME
           : (line.isControlSupported(FloatControl.Type.MASTER_GAIN))
               ? FloatControl.Type.MASTER_GAIN
               : null;
   if (type == null) {
     logger.warning("No volume or master gain controls.");
     return;
   }
   FloatControl control;
   try {
     control = (FloatControl) line.getControl(type);
   } catch (IllegalArgumentException e) {
     return; // Should not happen
   }
   //
   // The units of MASTER_GAIN seem to consistently be dB, but
   // in the case of VOLUME this is unclear (there is even a query
   // to that effect in the source).  getUnits() says "pulseaudio
   // units" on my boxen, and the PulseAudio doco talks about dB
   // so for now we are assuming that the controls we are using
   // are both logarithmic:
   //
   //   gain = A.log_10(k.vol)
   // So scale vol <= 1 to gain_min and vol >= 100 to gain_max
   //   gain_min = A.log_10(k.1)
   //   gain_max = A.log_10(k.100)
   // Solving for A,k yields:
   //   A = (gain_max - gain_min)/2
   //   k = 10^(gain_min/A)
   // =>
   //   gain = gain_min + (gain_max - gain_min)/2 * log_10(vol)
   //
   float min = control.getMinimum();
   float max = control.getMaximum();
   float gain =
       (vol <= 0)
           ? min
           : (vol >= 100) ? max : min + 0.5f * (max - min) * (float) Math.log10(vol);
   try {
     control.setValue(gain);
     logger.finest(
         "Using volume " + vol + "%, " + control.getUnits() + "=" + gain + " control=" + type);
   } catch (Exception e) {
     logger.log(
         Level.WARNING,
         "Could not set volume "
             + " (control="
             + type
             + " in ["
             + min
             + ","
             + max
             + "])"
             + " to "
             + gain
             + control.getUnits(),
         e);
   }
 }