Ives
Ives

Reputation: 555

Supported file format in Java Sound

Here is the code I use to determine sound files can be played as Clip or not(written in Kotlin).
It returns false on 24bit file on Windows but returns true on Mac, the results are different. Why doesn't it return the same results on each system?

    fun isValidFile(file: File): Boolean{

     try {
         val format = AudioSystem.getAudioFileFormat(file).format
         val info = DataLine.Info(Clip::class.java, format)
         val audioSystem = AudioSystem.getLine(info)

         return audioSystem is Clip
     }
     catch (e : Exception){
         return false
     }

    }

The 24bit file is playable on Mac but if I try to adjust its volume by FloatControl I get Unsupported control type: Master Gainerror while the 16bit file just works fine.

clip.getControl(FloatControl.Type.MASTER_GAIN) as FloatControl

Upvotes: 0

Views: 230

Answers (1)

Phil Freihofner
Phil Freihofner

Reputation: 7910

"Why doesn't it return the same results on each system?"

Operating Systems and the various computers and cards that they configure are all external to Java. They differ in what standards they use and what controls they provide. The lack of a volume control for a specific format is most probably determined by what that particular OS has available.

For my purposes, I found that volume controls and other controls (panning, various effects) are so hit-and-miss that for what I want to do, that I bit the bullet and coded these functions within Java. This can be done by using a SourceDataLine instead of Clip, as SourceDataLine exposes the data to the coder as it is being played.

I also made myself content with the standard "CD-Quality" format: 16-bit, stereo, 44100 fps, as this is currently the highest standard that has been implemented on all the Java systems I've come across. (I haven't any experience with Raspberry PI -- IDK if it works there.)

Libraries can help and code can be inspected for examples.

Upvotes: 1

Related Questions