Java ogg to wav conversion
Asked Answered
L

3

7

I have the following code snippet:

File file = new File(sourceFile);
AudioInputStream in = AudioSystem.getAudioInputStream(file);
AudioInputStream din = null;
AudioFormat baseFormat = in.getFormat();

AudioFormat decodedFormat = new AudioFormat(
                AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(),
                16, baseFormat.getChannels(), baseFormat.getChannels() * 2,
                baseFormat.getSampleRate(), false);

din = AudioSystem.getAudioInputStream(decodedFormat, in);
AudioSystem.write(din, AudioFileFormat.Type.WAVE, new File(targetFile));

I wrote this code to transfrom MP3 and OGG files to WAV files. These formats are not supported by java by default as I understand, so I had to add different jars to my classpath as described here:

http://www.javazoom.net/mp3spi/documents.html

http://www.javazoom.net/vorbisspi/documents.html

The code runs fine when I'm converting MP3 files. But when I try to convert OGG files I get the following error:

java.lang.IllegalArgumentException: Unsupported conversion: PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian from VORBISENC 44100.0 Hz, unknown bits per sample, stereo, 1 bytes/frame, 16000.0 frames/second

Now this is thrown only when getting the decoded inputstream itself, which means that the ogg file was parsed successfuly.

AudioSystem.getTargetEncodings(sourceFormat)

The code above will not return any values, when processing neither MP3 or OGG files. The only difference is that MP3 works fine.

I added all jars referenced by the previous link needed for the ogg conversion except the latest version of the jogg-0.0.7.jar because I could not find it.

I tried different solutions like JAVE but I need it run on MAC as well, and JAVE will not run on MAC without some special implementation.

Does anyone have any suggestion what may cause the problem? May there be any workaround to convert OGG file to WAV?

EDIT:

Ow wow I realized what the problem is... After reading everthing on the manual, I learned that using two different kind of SPIs (like in this case MP3 and OGG) can cause problems. I removed the MP3 spi maven dependency and the error disappeared. Thought the result wav is basically empty. Any suggestions on that? I will be happy if I can get OGG to WAV transformation to work, I can transform MP3 in another way.

My pom dependency looks like this:

<dependency>
    <groupId>com.googlecode.soundlibs</groupId>
    <artifactId>vorbisspi</artifactId>
    <version>1.0.3-1</version>
</dependency>

This downloads I guess all jars I need to make OGG transformation work. (Looks like not.)

Leatheroid answered 13/3, 2013 at 13:57 Comment(3)
Is this what you want? poi1.googlecode.com/files/jogg-0.0.7.jarCraniometer
I found that jar i just could not find the maven dependency for it so i didnt want to use it. I tried to donload it add to classpath from a lib folder but no bueno. Sry I have been wasting a lot of time with this issue I forgot to mention this.Leatheroid
Add tritonus_jorbis-0.3.6.jar (tritonus.org/tritonus_share-0.3.6.jar) can convert OGG to WAV correctly. Not sure why.Craniometer
C
3

Use tritonus_jorbis-0.3.6.jar to replace vorbisspi1.0.2.jar.

When decoding original AudioInputStream, the AudioSystem get all SPI's FormatConversionProvider and check one by one until it finds one that can work. So the order of classpath is important.

Check your classpath to make sure "org.tritonus.sampled.convert.jorbis.JorbisFormatConversionProvider" is used. Check every JAR in the classpath; search the following file in it. Or just place tritonus_jorbis-0.3.6.jar at the very beginning of the classpath.

META-INF\services\javax.sound.sampled.spi.FormatConversionProvider

For how to specify classpath order in maven, check this post: Maven classpath order issues.

Craniometer answered 13/3, 2013 at 15:19 Comment(6)
I tried this it did not help, or just couldnt get it to work. : (Leatheroid
What JAR's do you have in your library?Craniometer
I will edit my question you can see my maven dependency there. I basicly cleared everything else out so nothing can conflict with anything. (like mp3 spi did)Leatheroid
It seems that you still have vorbisspi1.0.2.jar in your classpath. It contains a javax.sound.sampled.spi.FormatConversionProvider and this FormatConversionProvider doesn't work.Craniometer
I got it through my thick skull finally. I removed the original dependency all together. From there on I got classnotfoundexception errors until i got all the depencies and jar references correct. Thanks a lot your help!Leatheroid
Peter, I have been struggling with the same issue. Can you please share your POM? I'm curious to see how you finally resolved this. I have one project with the following jar files: - tritonus_share-0.3.6.jar - tritonus_jorbis-0.3.6.jar - jogg-0.0.7.jar - jorbis-0.0.15.jar But that does not seem to work in my mavenized projects!Concretize
C
2

Actually, you can use both mp3spi and vorbisspi together. You just have to make sure that Maven will use the right version of tritonus-share. If you rely on Maven's transitive dependency mechanism, you will end up with tritonus-share 0.3.7-1 which is lacking the class org.tritonus.share.sampled.convert.TMatrixFormatConversionProvider. That's what breaks vorbisspi.

I just had to explicitly add a dependency on tritonus-share 0.3.7-2 to my pom.xml and now both ogg and mp3 playback works via Java sound API.

See my blog post for details: http://odoepner.wordpress.com/2013/07/19/play-mp3-or-ogg-using-javax-sound-sampled-mp3spi-vorbisspi/

Clarey answered 25/7, 2013 at 15:7 Comment(0)
J
0

You might want to try FFsampledSP (part of SampledSP).

It's an FFmpeg based implementation for Win and OSX that implements the javax.sound.sampled interfaces. OGG, FLAC etc. are supported. It's basically the same decoder as the ones used in other FFmpeg applications (like the VLC player and so many others).

Jarboe answered 10/11, 2013 at 18:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.