Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ogg to wav conversion

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.)

like image 556
Peter Jaloveczki Avatar asked Mar 13 '13 13:03

Peter Jaloveczki


1 Answers

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.

like image 173
longhua Avatar answered Sep 26 '22 18:09

longhua