Contrôle du volume audio (augmentation ou diminution) en Java


Comment augmenter le volume d'un flux audio wav sortant en utilisant Java? J'ai des problèmes avec divers moteurs Java TTS et le volume de sortie du discours synthétisé. Y a-t-il un appel API ou un doo-hickey.jar je peux utiliser pour pomper le volume?

Author: markusk, 2009-06-05

3 answers

Si vous utilisez l'API Java Sound, vous pouvez définir le volume avec le contrôle MASTER_GAIN.

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();
 35
Author: markusk, 2009-06-05 00:23:16

Vous pouvez régler le volume à l'aide d'un GainControl, essayez quelque chose comme ça après avoir ouvert la ligne

FloatControl volume= (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN); 
 9
Author: objects, 2010-11-22 15:41:19
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)); }

}
 1
Author: Jan Cajthaml, 2016-09-13 09:29:01