OpenJDK Change Clp volume

YoungDev :

I'm importing code from Oracle JDK to OpenJDK (Java 8). The only problem left to solve is how to change the volume of Java Clip (java.sound.sampled.Clip) ? Even after hours of searching I couldn't find how to change the volume of Clip.

Using Oracle JDK I did it so:

FloatControl floatControl = (FloatControl) myClip.getControl(FloatControl.Type.MASTER_GAIN);
floatControl.setValue(myVolume);

I also tried FloatControl.Type.VOLUME but that isn't supported.

I would love just to change the volume of a Clip instead of rewriting my entire audio system using another class/package.


The idea I have is to multiply the incoming audio data with a float which will either lower or raise the volume. Downside is that it will probably be expensive as I need to change volume while playing the audio instead of doing it once when the program is launched.


EDIT: I guess that it's not that simple with Clip as there's not a single answer or a comment. What else I could use then aside Clip that is easy to set up and is lightweight (as I might have up top 30 sounds playing at once) ?


Kubuntu 19.04 on Linux 5.0.0-27
OpenJDK 1.8.0_222

Phil Freihofner :

I wrote AudioCue for this very purpose. It's free, and the license is BSD (open source). A number of game programmers at Java-Gaming.org have used it with good results.

It is set up to follow the API of Clip pretty closely, but with extra features. It has real time volume, panning, and frequency capabilities, and allows a user to play overlapping copies of the same cue.

There are only five class files, so it is pretty small. Of those, use of the AudioMixer class is optional, but if you have 30 sounds at once, it would probably be helpful, as it funnels everything out to a single SourceDataLine instead of running an output line for each sound.

I'm happy to help with setting it up, or answer any questions.

Yes, the Controls that Java provides for Clip are problematic. A lot depends on the computer system/hardware involved, as to whether they are implemented or not. And the controls tend to have poor granularity, only change the volume at buffer boundaries. AudioCue stores the internal audio as a "CD Quality" .wav and is granular down the single audio frame.

Another alternative to consider is the github project TinySound. It is a larger project than AudioCue, but still pretty small, and works well. I can't recall if it has real time faders for the volume. It might not. It's main advantage is that it can handle .ogg compressed files. But I might be able to help if you need to use compression.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325345&siteId=1