0

I'm writing a generalized utility for converting audio files to WAV. Works ok for WAV to WAV (I'm also changing some of the attributes), but I can't convert MP3 files. I have mp3spi in my classpath, so it seems to be able to read the MP3, but the WAV file that gets written doesn't seem to work.

In this example, I'm not trying to change any properties. Just reading the MP3 and writing to a WAV file

My code looks something like this

    File inputFileObj = new File(input);
    AudioInputStream audioInputStream = null;

    try {
        audioInputStream = AudioSystem.getAudioInputStream(inputFileObj);
    } catch (Exception e) {
        e.printStackTrace();
    }

    System.out.println("Input file format:");
    System.out.println(AudioSystem.getAudioFileFormat(inputFileObj));

    try {
        AudioSystem.write(audioInputStream, outputType, new File(output));
    } catch (Exception e) {
        e.printStackTrace();
    }

    System.out.println("Output file format:");
    System.out.println(AudioSystem.getAudioFileFormat(new File(output)));

Here's the output. As you can see, it appears to write the output file, but when I try to retrieve the format of the output file, it can't handle it. And if I try to play the output file, the player doesn't recognize it.

Input file: c:\testfiles\sample-b-converted.mp3
Output file: c:\testfiles\foo.wav
Output type: wav

Input file format:
MP3 (.mp3) file, byte length: 13227300, data format: MPEG2L3 16000.0 Hz, unknown bits per sample, mono, unknown frame size, 27.777779 frames/second, , frame length: 122475
Bytes written: 13227344
Output file format:
Exception in thread "main" javax.sound.sampled.UnsupportedAudioFileException: file is not a supported file type
    at javax.sound.sampled.AudioSystem.getAudioFileFormat(AudioSystem.java:1078)
    at org.torchai.AudioFileConvert01.main(AudioFileConvert01.java:60)

Is there something else I need to get this working?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Peter Kronenberg
  • 878
  • 10
  • 32
  • I saw this other issue and it's similar, but I have the pre-reqs and still can't get it to work – Peter Kronenberg Jan 26 '21 at 01:57
  • Now thay I look at it more carefully, there might be aspects of it that aren't explained fully. It looks like it converts it to a known format first before proceeding to convert to the format I want. I'll have to try again. – Peter Kronenberg Jan 26 '21 at 02:14

1 Answers1

0

Someone posted a comment referring me to mp3 to wav conversion in java. I had seen this issue, but didn't quite see the main aspect of the answer, since it wasn't really explained well.

An MP3 file apparently needs to go through a 2-step conversion. I don't fully understand why, but it seems you must first convert it to PCM_SIGNED with a sample size of 16 and a framesize of 2*# of channels. At that point, you can convert it again to the final format that you want.

Would still love to have a better explanation, but this at least gets me past my issue.

Peter Kronenberg
  • 878
  • 10
  • 32
  • I'm not sure what you are asking. PCM (as opposed to PCM deconstructed into byte) allows for algorithms that make use of the harmonic content of the sound stream be part of the compression algorithm. But also, for a reasonable fidelity, the values in signed bytes simply don't have enough range. With two bytes chained to produce one value, we have +/- 32767 as opposed to +/- 127. Floats ranging from -1 to 1 are usually considered to be quite adequate for sound representation. And if there is stereo, the values will have to stream via two channels. – Phil Freihofner Jan 26 '21 at 18:09
  • 1
    digital audio has two fundamental attributes : bit depth and sample rate ... to decode mp3 into PCM format will give you values for these two settings by definition ... once in PCM format you are free to directly output to WAV format or whatever ... you can also change modify the raw audio curve while in PCM format into different values for those two attributes ... typical CD quality audio has a bit depth of 16 bits meaning each audio sample which is a point on the raw audio curve requires 2 bytes of storage ... 8, 16, 24 or 32 bits are also available not simply 16 bits – Scott Stensland Jan 27 '21 at 14:18
  • I guess the part I don't understand is why can't I convert to PCM and change other values all in one step, instead of converting to PCM first -- keeping everything else the same, and only then, change other values (in my case, I'm trying to normalize everything to 16,000 sample rate, 1 channel) – Peter Kronenberg Jan 27 '21 at 20:05