2

I am facing a problem regarding audio decoding. I have the SPIMP3 library for mp3 decoding and I am trying to decode mp3's and storing them to an array of bytes.

Here is the thing, when I try to decode a 2 minute mp3 song it gives me, for example, the following bytes:

[ -1, 0, 42, -115, -45, 0, 14 ... etc].

But when I cut that mp3 in half and try to decode the first half I get the following bytes:

[ 1, 0, 0, 65, -97, 135, -64, 32 ...etc]

The weird thing is that they don't match. The only thing that differs here is the audio length, but I am decoding the first part of both of the mp3 samples which is the same.

Here is my code:

public void testPlay(String mp3) {
    try {
        File file = new File(mp3);
        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);

        play(decodedFormat, din);
        spi(decodedFormat, in);
        in.close();
    } catch (Exception e) {
        System.out.println("MP3");
    }

}

private void play(AudioFormat targetFormat, AudioInputStream din) throws IOException, LineUnavailableException {

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] data = new byte[4096];
    SourceDataLine line = getLine(targetFormat);

        int nBytesRead = 0, nBytesWritten = 0;
        while (nBytesRead != -1) {
            nBytesRead = din.read(data, 0, data.length);
            if (nBytesRead != -1) {
                nBytesWritten = line.write(data, 0, nBytesRead);
                out.write(data, 0, 4096);
            }

        }

       byte[] audio = out.toByteArray();

}

Is this something to be expected or is there a problem on my code???

How can I change my code to get the same bytes for the matching part of my mp3?

Thank you.

1 Answer 1

4

this line should be:

out.write(data, 0, nBytesRead);
Sign up to request clarification or add additional context in comments.

4 Comments

Hello jtahlborn and thank you for your response. I tried your suggestion but with no luck. The data still have a little of deviation from the initial once. Let me explain what I am doing,
Lets say I have a 2 minute song, I try to decode it with my code above and I get for some specific positions on my byte array the following: [2, 44, -71, -99, 134]. Then I use audacity to cut 1 (one) second from the ending of my mp3. Then I try to decode it. I get the following data on the same position as the last once: [9, 35, -66, -33, 124]. And the giving positions are close at the beginning of my array. Shouldn't the data remain the same as the last once? Thank you
@DaReDeViL - when you use audacity to cut the 1 second from the end, are you sure that the beginning is unchanged? audacity may reencode the file, causing it to be slightly different from before. one easy way to check is to see if the mp3 binary data matches up until the removed part (before you decode).
OMG you were absolutely right! Audacity re-encoded my MP3 to MP3 after cutting that part. I thought the generated mp3 would be a new mp3 with that part deleted! :) jtahlborn, Thank you very much and appreciate every help you gave me, you helped me a lot! I will leave my question alive just to let you know your comments were helpful and I will remove it after. Again, thank you very much my friend. Take care. DaReDeViL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.