1

我正在使用 EZAudio Lib 在播放时获取音频数据。现在我想将此数据广播为 mp3 缓冲区。我正在使用 lame 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);
}

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

请帮帮我。

4

0 回答 0