javaswingjavasoundjslideraudioinputstream

How do I change the decibels of a clip


The problem I have is that the decibals of the audiofile only changes from the initial value of programVolume (the variable) but I have a JSlider that alters the the value of the variable;however, it does not alter the decibals of the file. What do I add/change to make sure the decibal changes based off the values set to the variable from the JSlider.

  File fileSong = new File (filePath); 
  AudioInputStream input = AudioSystem.getAudioInputStream(fileSong); 
  clip = AudioSystem.getClip(); 
  clip.open(input); 
  FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
  gainControl.setValue(programVolume);
  clip.start();

EDIT:

I have added the following code:

  File fileSong = new File (filePath); 
  AudioInputStream input = AudioSystem.getAudioInputStream(fileSong);
  clip = AudioSystem.getClip(); 
  clip.open(input); 

  FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
  float range = gainControl.getMaximum() - gainControl.getMinimum();
  float gain = (range * programVolume) + gainControl.getMinimum();
  gainControl.setValue(gain);

For my JSlider (which is in the method ChangeEvent e)

    programVolume =  (float)volume.getValue() / 100; 

I am still facing the same problem. The new values recieved from the JSlider do not change the volume. Only the initial value of programVolume alters the sound.


Solution

  • I figured it out! Whenever the variable was altered using the JSlider the clip had to be stopped and the point at which it was stopped was to be stored onto a variable. I chose a long and then the clip had to be restarted and when it restarts it uses the new volume the user selected and it begins at the same time it left off at giving the illusion to the user that the volume was altered while the audio-file was playing when it stopped, changed volume, and resumed. The Math.Log stuff is used to calculate/convert the decibels to the volume system we know (0%-100%) or so I think.

      File fileSong = new File (filePath);
      AudioInputStream input = AudioSystem.getAudioInputStream(fileSong); 
      clip = AudioSystem.getClip(); 
      clip.open(input); 
      clip.setMicrosecondPosition(position); 
    
      FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); 
      float range = (float) (Math.log(userInput) / Math.log(10) * 20); 
      gainControl.setValue(range); 
      clip.start();