如何使用lame lib iOS将音频float数组转换为mp3缓冲区?

时间:2018-02-27 09:01:14

标签: ios objective-c lame ezaudio audiobufferlist

我正在使用EZAudio Lib在播放时获取音频数据。现在我想将这些数据作为mp3缓冲区广播。我使用蹩脚的lib来编码数据并进行广播。但我无法正确转换它。

从EZAudio lib函数下面获取音频浮动缓冲区

- (void)  audioPlayer:(EZAudioPlayer *)audioPlayer
          playedAudio:(float **)buffer
       withBufferSize:(UInt32)bufferSize
 withNumberOfChannels:(UInt32)numberOfChannels
          inAudioFile:(EZAudioFile *)audioFile

在函数下方使用缓冲区后转换为AudioBufferList

- (AudioBufferList *)writeList:(long)numFrames fromArray:(short **)data{

if (!data)        return nil;
if (!numFrames)    return nil;

AudioBufferList *abl = AllocateAudioBufferList(mStreamFormat.mChannelsPerFrame, numFrames*sizeof(short));
if (!abl)        return nil;

for (long c = 0; c < mStreamFormat.mChannelsPerFrame; c++) {
    abl->mBuffers[c].mNumberChannels = 2;
    abl->mBuffers[c].mDataByteSize = numFrames*sizeof(short);
    if (data[c]) {
        short *buffer = (short*)abl->mBuffers[c].mData;
        for (long v = 0; v < numFrames; v++) {
            buffer[v] = data[c][v];
           /* if (data[c][v] > 0.999)        data[c][v] = 0.999;
            else if (data[c][v] < -1.)    data[c][v] = -1.;
            buffer[v] = (short)(data[c][v]*32768.f);*/
        }
    }
    else     memset(abl->mBuffers[c].mData, 0, numFrames*sizeof(short));
}
return abl;
}

当我得到AudioBufferList并传递给下面的函数进行编码

- (void)newEncodingCodeWithFilePath:(AudioBufferList *)bufferListInOut {

int inNumPackets = 2048;
int MP3_SIZE = inNumPackets*2;
unsigned char mp3_buffer[MP3_SIZE];

memset(mp3_buffer, 0, sizeof(mp3_buffer));

int encodedBytes = lame_encode_buffer(lame,bufferListInOut->mBuffers[0].mData,  bufferListInOut->mBuffers[1].mData, inNumPackets, mp3_buffer, MP3_SIZE);

[self sendBuffer:[NSData dataWithBytes:mp3_buffer length:encodedBytes]];

 lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
}

我想将playbackAudio(缓冲区)转换为编码数据(使用lame),以便我可以发送到服务器。

请帮帮我。

0 个答案:

没有答案