Audio volume control (increase or decrease) in Java

81,782

Solution 1

If you're using the Java Sound API, you can set the volume with the MASTER_GAIN control.

import javax.sound.sampled.*;

AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(
    new File("some_file.wav"));
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
FloatControl gainControl = 
    (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(-10.0f); // Reduce volume by 10 decibels.
clip.start();

Solution 2

You can adjust volume using a GainControl, try something like this after you have opened the line

FloatControl volume= (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN); 

Solution 3

public final class VolumeControl
{

    private VolumeControl(){}

    private static LinkedList<Line> speakers = new LinkedList<Line>();

    private final static void findSpeakers()
    {
        Mixer.Info[] mixers = AudioSystem.getMixerInfo();

        for (Mixer.Info mixerInfo : mixers)
        {
            if(!mixerInfo.getName().equals("Java Sound Audio Engine")) continue;

            Mixer mixer         = AudioSystem.getMixer(mixerInfo);
            Line.Info[] lines   = mixer.getSourceLineInfo();

            for (Line.Info info : lines)
            {

                try 
                {
                    Line line = mixer.getLine(info);
                    speakers.add(line);

                }
                catch (LineUnavailableException e)      { e.printStackTrace();                                                                                  } 
                catch (IllegalArgumentException iaEx)   {                                                                                                       }
            }
        }
    }

    static
    {
        findSpeakers();
    }

    public static void setVolume(float level)
    {
        System.out.println("setting volume to "+level);
        for(Line line : speakers)
        {
            try
            {
                line.open();
                FloatControl control = (FloatControl)line.getControl(FloatControl.Type.MASTER_GAIN);
                control.setValue(limit(control,level));
            }
            catch (LineUnavailableException e) { continue; }
            catch(java.lang.IllegalArgumentException e) { continue; }



        }
    }

    private static float limit(FloatControl control,float level)
    { return Math.min(control.getMaximum(), Math.max(control.getMinimum(), level)); }

}
Share:
81,782
Cliff
Author by

Cliff

Originating in the dark ages, one future Emperor would rise to claim victory over all knights of Java development. Trained in the ancient arts of RPG our Emperor fought many a battle with sys admins and project managers in order to spread the truth of clean, readable design code. Alas, our hero was tragically scarred in the line of battle while fighting with an IT manager that deemed it more important to patch an already failing system by appending an overly complex front end system. While ultimately triumphing in victory our hero gained a new perspective on software design. It was during this period that he turned to the dark side, enslaving those who did his bidding and bringing turmoil to those who stood in his way.

Updated on July 20, 2022

Comments

  • Cliff
    Cliff almost 2 years

    How do I increase the volume of an outgoing wav audio stream using Java? I'm having issues with various Java TTS engines and the output volume of the synthesized speech. Is there an API call or a doo-hickey.jar I can use to pump up the volume?