JLayer Mono Mp3 to PCM decoding

前端 未结 2 1120
-上瘾入骨i
-上瘾入骨i 2021-01-16 08:24

I am currently working on mp3 decoding with javalayer 1.1.

So I want to receive the raw PCM data from my 44100 Hz, 16bit, Mp3s. It is perfectly working fine with ste

相关标签:
2条回答
  • 2021-01-16 09:04

    Hm... My answer comes a bit late.. Sorry for that.

    I completly solved the issue.

    JLayer did some weird stuff. If the input mp3 is Stereo, the values in the pcmbuffer are encoded like this: leftchannel, rightchannel, leftchannel, ... This is how it should be. But if the input mp3 is Mono the I get the same amount of samples in the pcmbuffer. But its not like: monochannel, 0, monochannel, 0 The whole data is in the first half of the pcmbuffer and the second half is all 0. So you just need to cut off the second half.

    0 讨论(0)
  • 2021-01-16 09:09

    I got it working, converting to byte[], using this code:

    ByteArrayOutputStream outStream = new ByteArrayOutputStream(1024);
    int divider = 1;
    if (SAMPLE_RATE < 44100) divider *= 2;
    if (CHANNELS == 1) divider *= 2;
    
    [...]
    
    short[] pcmBuffer = buffer.getBuffer();    
    for (int i=0; i<pcm.length/divider; i++) {
        outStream.write(pcm[i] & 0xff);
        outStream.write((pcm[i] >> 8 ) & 0xff);
    }
    

    The key was the divider parameter, that is 1 in stereo-44, 2 in mono-44 and 4 in mono-22. Didn't try yet other combinations.

    0 讨论(0)
提交回复
热议问题