/* 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) {
    }
  }
Exemplo n.º 2
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();
    }
  }
Exemplo n.º 3
0
 /** Returns true if Pan control is supported. */
 public boolean hasPanControl() {
   if (m_panControl == null) {
     // Try to get Pan control again (to support J2SE 1.5)
     if ((m_line != null) && (m_line.isControlSupported(FloatControl.Type.PAN))) {
       m_panControl = (FloatControl) m_line.getControl(FloatControl.Type.PAN);
     }
   }
   return m_panControl != null;
 }
Exemplo n.º 4
0
 /** Returns true if Gain control is supported. */
 public boolean hasGainControl() {
   if (m_gainControl == null) {
     // Try to get Gain control again (to support J2SE 1.5)
     if ((m_line != null) && (m_line.isControlSupported(FloatControl.Type.MASTER_GAIN))) {
       m_gainControl = (FloatControl) m_line.getControl(FloatControl.Type.MASTER_GAIN);
     }
   }
   return m_gainControl != null;
 }
Exemplo n.º 5
0
 public void unMute() {
   if (audioLine == null) {
     isMuted = false;
   } else {
     isMuted = false;
     if (!audioLine.isControlSupported(BooleanControl.Type.MUTE)) return;
     BooleanControl control = (BooleanControl) audioLine.getControl(BooleanControl.Type.MUTE);
     control.setValue(false);
   }
 }
Exemplo n.º 6
0
 public void mute() {
   if (audioLine == null) {
     isMuted = true;
   } else {
     // when requesting the same line you sometimes get different features
     isMuted = true;
     if (!audioLine.isControlSupported(BooleanControl.Type.MUTE)) return;
     BooleanControl control = (BooleanControl) audioLine.getControl(BooleanControl.Type.MUTE);
     control.setValue(true);
   }
 }
Exemplo n.º 7
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());
     }
   }
 }
Exemplo n.º 8
0
  @Override
  public void onActive() {
    if (audioLine != null) audioLine.close();

    try {
      if (player.getMixer() != null) {
        log.debug("Custom mixer " + player.getMixer().getName());
        audioLine = AudioSystem.getSourceDataLine(PCM, player.getMixer());
      } else {
        audioLine = AudioSystem.getSourceDataLine(PCM);
      }
      audioLine.open(PCM, 1048576);
      onVolumeChanged(player.getVolume());
      if (isMuted && audioLine.isControlSupported(BooleanControl.Type.MUTE))
        ((BooleanControl) audioLine.getControl(BooleanControl.Type.MUTE)).setValue(true);
    } catch (LineUnavailableException e) {
      log.error("onActive error", e);
    }
  }
Exemplo n.º 9
0
 public boolean isControlSupported(Control.Type control) {
   return dataLine.isControlSupported(control);
 }
Exemplo n.º 10
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);
   }
 }