简体   繁体   中英

Why does java AudioSystem.getTargetEncodings( AudioFormat ) return different Encoding than the encoding of the AudioFormat we provide?

I have an AudioInputStream audioInput which the object itself has encoding as ULAW

System.out.println( audioInput.getFormat().getEncoding() ); 

Which return ULAW encoding in the console.

ULAW

However, when I use AudioSystem.getTargetEncodings( audioInput ) it return a different set of Encoding

    Encoding availableEncoding[] =  AudioSystem.getTargetEncodings( audioIn.getFormat() );
    for ( Encoding encode : availableEncoding ) 
    {
        System.out.println( encode );
    }

which return :

PCM_SIGNED

The thing is, I have to work with a lot of these files, where the encoding of the object and and target encoding doesn't match. These audio files cannot be opened by AudioSystem clip and throw an exception

Clip audioClip = AudioSystem.getClip();
audioClip.open( audioInput ); 
// this throws error javax.sound.sampled.LineUnavailableException: line with format ULAW 8000.0 Hz, 8 bit, mono, 1 bytes/frame,  not supported.

However, if I convert the audio to one of the target encoding

audioInput = AudioSystem.getAudioInputStream( Encoding.PCM_SIGNED , audioInput);
audioClip.open( audioInput );  //it works !!

Although this approach seem to work for all the sound file to work with, I barely have sufficient knowledge of what I am doing. According to the java doc, the AudioSystem.getTargetEncodings description is ( in case it might help ) :

"Obtains the encodings that the system can obtain from an audio input stream with the specified format using the set of installed format converters."

Sorry for the long detail but here are my questions :

  1. Why does java AudioSystem.getTargetEncodings( AudioFormat ) return different result than the encoding of the AudioFormat we provide we provide in the parameter ?

  2. So does this mean, in my case, that the system can perceive and process the file only with PCM_SIGNED encoding even though the encoding of the file itself is ULAW ?

  3. Is my solution legit ? It really bug me a lot to have a solution that seems to work without a good reason

According to what I've learned so far:

  1. Java is looking at the default setup for playback for your system's configuration, not at the format of the audio file. Many (most?) computer systems are set up to play back PCM_SIGNED.

  2. Depends what you mean by "only." There are probably ways to change your system's playback preferences, but I have always just gone with the default, or one of the "supported" options.

  3. The solution is totally legit! The streaming conversions were written for just this circumstance: many audio file types, system that is set up to play a limited subset. They also help if you wish to programmatically convert and save files in a new format.

But for my audio uses (SF/X for game cues--I come across files of different types), I tend to convert them all, externally, using Audacity, prior to packaging them with the game, so as not to have to do any conversion during the game.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM