1

我的应用程序使用 LAME 将原始 pcm 数据编码为 mp3。但我有一个问题 - 输出 mp3 在持续时间内包含“点击”。像这样的东西:

声音...“点击”声音..“点击”声音..“点击”等...

我尝试了不同版本的 LAME 并尝试更改许多 LAME 设置但没有成功。我的应用程序还可以将 pcm 转换为 wav 和 ogg,但此转换器不会导致“点击”。

有一个编码代码(pcm输入包含一个通道数据,pcm已经重采样到44100频率):

初始化:

lame = lame_init();
lame_set_in_samplerate(lame, 44100);
lame_set_VBR(lame, vbr_abr);//vbr_default
lame_init_params(lame);

//Samples count (do not have any effect on output)
dwSamples=1024;

//mp3 buffer size, if it do not equals dwSamples*2 than output sound getting scaled
dwMP3Buffer=dwSamples*2;

pMP3Buffer = new BYTE[dwMP3Buffer];

写入 pcm 数据

int Mp3Stream :: Write(short * _data, int _size)
{
    if (_size > 0)
    {
        for(int curPos = 0; curPos < _size; curPos += dwMP3Buffer)
        {
            //int size = dwMP3Buffer;

            //if (_size - curPos < dwMP3Buffer)
            //  size = _size - curPos;

            int bytes = lame_encode_buffer(lame, (short *)((char *)_data + curPos), (short *)((char *)_data + curPos), dwSamples, pMP3Buffer, 0);

            IPF_TRACE(1, "MP3 encoder wrote "<<bytes<<" bytes");

            if (bytes<0)
            {
                IPF_TRACE(1, "MP3 encoding failed with code "<<bytes);
                return bytes;
            }

            BOOL bResult = WriteFile(hFile, pMP3Buffer, bytes, &bw, NULL);

            if (!bResult || bytes != bw)
            {
                IPF_TRACE(1, "MP3 write to file failed with code "<<bytes);
            }
        };
    }
}

敲定

int bytes = lame_encode_flush(lame, pMP3Buffer, 0);
    if (bytes<0)
{
    IPF_TRACE(1, "MP3 flush failed with code "<<bytes);
}
BOOL bResult = WriteFile(hFile, pMP3Buffer, bytes, &bw, NULL);
if (!bResult || bytes != bw)
{
    IPF_TRACE(1, "MP3 write to file failed with code "<<bytes);
}
int ret = lame_close(lame);
if (ret < 0) 
{
    IPF_TRACE(1, "MP3 lame close failed with code "<<ret);
}
delete []pMP3Buffer;
4

3 回答 3

1

尝试这个,

void AQRecorder::MyInputBufferHandler(  void *                              inUserData,
                                AudioQueueRef                       inAQ,
                                AudioQueueBufferRef                 inBuffer,
                                const AudioTimeStamp *              inStartTime,
                                UInt32                              inNumPackets,
                                const AudioStreamPacketDescription* inPacketDesc)
{
AQRecorder *aqr = (AQRecorder *)inUserData;
//    NSLog(@"%f",inStartTime->mSampleTime);
try
{
    if (inNumPackets > 0)
    {
        AudioFileWritePackets(aqr->mRecordFile, FALSE, inBuffer->mAudioDataByteSize, inPacketDesc, aqr->mRecordPacket, &inNumPackets, inBuffer->mAudioData);

        aqr->mRecordPacket += inNumPackets;

        int MP3_SIZE =inBuffer->mAudioDataByteSize * 4;
        unsigned char mp3_buffer[MP3_SIZE];
        AppDelegate *delegate =[[UIApplication sharedApplication]delegate];
        lame_t lame = lame_init();
        lame_set_in_samplerate(lame, 44100);
        lame_set_VBR(lame, vbr_default);
        lame_init_params(lame);

  //                int encodedBytes=lame_encode_buffer_interleaved(lame, (short int *)inBuffer->mAudioData , inNumPackets, mp3_buffer, MP3_SIZE);


        int encodedBytes = lame_encode_buffer(lame, (short*)inBuffer->mAudioData,  (short*)inBuffer->mAudioData, inNumPackets, mp3_buffer, MP3_SIZE);

        [delegate.mp3AudioData appendBytes:mp3_buffer length:encodedBytes];

        if (inBuffer->mAudioDataByteSize != 0) {
        }
        else
        {
            int encode=lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
            [delegate.mp3AudioData appendBytes:mp3_buffer length:encode];
        }
        lame_close(lame);
    }

    if (aqr->IsRunning())
    {
        AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);
    }
 } catch (CAXException e)
 {
 char buf[256];
 fprintf(stderr, "Error: %s (%s)\n", e.mOperation, e.FormatError(buf));
}
}
于 2013-08-22T04:04:56.633 回答
1

Sound -> Click -> Sound -> Click 模式很可能意味着您一次只写入一半缓冲区,而另一半在 Write 调用中的每个循环中保持为零。

原因是您的循环增量器基于 dwSamples*2 的 dwMP3Buffer 值。

 for(int curPos = 0; curPos < _size; curPos += dwMP3Buffer){

这意味着您实际上并未对所有输入值进行编码,因为 dwMP3Buffer 是您实际传递给 lame_encode_buffer 调用 (dwSamples) 的样本数量的两倍。

lame_encode_buffer(lame, (short *)((char *)_data + curPos), 
                   (short *)((char *)_data + curPos), dwSamples, pMP3Buffer, 0);

尝试将循环增量器更改为以下内容:

 for(int curPos = 0; curPos < _size; curPos += dwSamples){
于 2012-04-27T05:34:53.570 回答
0

我会尝试设置

dwSamples = 1152

1152 - 是 MPEG1Audio 编码器的音频帧大小(以样本为单位)。看起来 LAME 按帧多块处理数据

于 2012-04-28T05:21:52.240 回答