JLayer Mono Mp3到PCM解码 [英] JLayer Mono Mp3 to PCM decoding

查看:117
本文介绍了JLayer Mono Mp3到PCM解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用javalayer 1.1进行mp3解码.

I am currently working on mp3 decoding with javalayer 1.1.

所以我想从我的44100 Hz,16位,Mp3s接收原始PCM数据. 它可以很好地与立体声mp3配合使用,但是单声道mp3却存在一些奇怪的问题.

So I want to receive the raw PCM data from my 44100 Hz, 16bit, Mp3s. It is perfectly working fine with stereo mp3s, but i have strange issues with mono mp3s.

这里有一些代码.

InputStream data = c.getResources().openRawResource(resId);
Bitstream bitstream = new Bitstream(data);
Decoder decoder = new Decoder();
while(thereIsData) {
  Head frameHeader = bitstream.readFrame();
  SampleBuffer buffer = (SampleBuffer) decoder.decodeFrame(frameHeader, bitstream);
  short[] pcmBuffer = buffer.getBuffer();

  // Do some stuff with pcm (For example creating a wav file )

  bitstream.closeFrame();
}

buffer.getChannelCount()== 1 buffer.getFrequency()== 41000

buffer.getChannelCount() == 1, buffer.getFrequency() == 41000

所以...问题是.如果我创建一个44100 Hz,单声道,16位WaveFile并将其放在Audacity中以查看这些波形.声音周期性地为0,例如:(200ms声音)...(200ms NoSound)...(200ms声音)...(200ms NoSound)

So... The Problem is. If I create a 44100 Hz, mono Channel, 16-bit WaveFile and put it in Audacity to see the waves. The sound is periodically 0,like: (200ms Sound)...(200ms NoSound)...(200ms Sound)...(200ms NoSound)

在写入.wav ...(Yeahi syso所有内容)之前,pcm数据也是如此

This goes also for the pcm data before writing to .wav... (Yeahi syso all the stuff)

所以人们可能会认为,必须有零帧或某物.在那...所以我剪掉所有只有0个值的帧.这会导致wav文件中的零中断稍微短一些.对我来说,必须有部分零帧. 因此,我从pcm数据中剪切了所有零值.文件听起来没问题.

So one may think, well there got to be zero-frames or sth. in there... So I cut off all frames with only 0 values in it. This results in slighty shorter zero breaks in the wav file. Means to me, there must be partial zero frames. So I cut ALL zero values from the pcm data... And as weird as it seems, this worked. The file sounds OK.

但这不是解决方案.我仍然不知道为什么会有这些错误的零值.而且我还需要保持mp3的静音状态.

But this cant be the solution. I still dont know why there are these false zero values. And I need silence in my mp3's as well.

我很感谢每一个解释提示.谢谢

I'd appreciate every explanation hint. Thanks

推荐答案

我使用以下代码将其转换为byte []:

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);
}

关键是divider参数,即立体声44中的1,单声道44中的2和单声道22中的4.尚未尝试其他组合.

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.

这篇关于JLayer Mono Mp3到PCM解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆