/* 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); }
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<>(); }
@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); }
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); }
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()); }
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); }
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; } }
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 }
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(); } } } }
public void setVolume(float volume) { gain = (FloatControl) audio.getControl(FloatControl.Type.MASTER_GAIN); gain.setValue(volume); }
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)); } }
// Возвращает текущую громкость (число от 0 до 1) public float getVolume() { float v = volumeC.getValue(); float min = volumeC.getMinimum(); float max = volumeC.getMaximum(); return (v - min) / (max - min); }
public void changeGain(float gain) { gainControl.setValue(gain); }
public int getValue() { return (int) (control.getValue() * getScaleFactor()); }
public void setValue(int nValue) { super.setValue(nValue); control.setValue((float) nValue / getScaleFactor()); }
/** * Returns whether the type of a FloatControl is BALANCE or PAN. * * @param control FloatControl control * @return boolean is Balance or Pan */ private static boolean isBalanceOrPan(FloatControl control) { Control.Type type = control.getType(); return type.equals(FloatControl.Type.PAN) || type.equals(FloatControl.Type.BALANCE); }