3

当我绕过 speex 编码/解码步骤时,原始音频输出是正确的。我想要的是对从我的录制回调中捕获的整个缓冲区进行编码、解码并发送回播放循环。我不确定的几项是:

  1. 为 enc_buffer 和 dec_buffer 分配什么大小
  2. 在 speex_bits_read_from(SpeexBits* bits,char* bytes,int len) 中指定什么长度
  3. 在 int speex_bits_write(SpeexBits* bits,char* bytes,int max_len) 中指定的最大大小

这是我的 speex 编解码器初始化:

#define SAMPLE_RATE 8000
#define MAX_FRAMES 100
#define FRAME_SIZE 160

enc_state = speex_encoder_init(&speex_nb_mode);
dec_state = speex_decoder_init(&speex_nb_mode);

spx_int32_t tmp;
tmp=5;
speex_encoder_ctl(enc_state, SPEEX_SET_QUALITY, &tmp);
tmp=1;
speex_encoder_ctl(enc_state, SPEEX_SET_COMPLEXITY, &tmp);

speex_encoder_ctl(enc_state, SPEEX_GET_FRAME_SIZE, &enc_frame_size );
speex_decoder_ctl(dec_state, SPEEX_GET_FRAME_SIZE, &dec_frame_size );

tmp = SAMPLE_RATE;

speex_encoder_ctl(enc_state, SPEEX_SET_SAMPLING_RATE, &tmp);
speex_decoder_ctl(dec_state, SPEEX_SET_SAMPLING_RATE, &tmp);

speex_bits_init(&enc_bits);
speex_bits_init(&dec_bits);

//Unsure of this allocation size
enc_buffer = (char*)malloc(sizeof(char)*enc_frame_size*MAX_FRAMES);
dec_buffer = (spx_int16_t*)malloc(sizeof(spx_int16_t)*dec_frame_size*MAX_FRAMES);

我的编码/解码方法:

-(char*)encodeAudioBuffer:(spx_int16_t*)audioBuffer withByteSize:(int)numberOfFrames andWriteSizeTo:(int*)inSize{
    speex_bits_reset(&enc_bits);
    speex_encode_int(enc_state, audioBuffer, &enc_bits);

    //Unsure of this third argument. 'numberOfFrames' is the stored number of input frames from my recording callback.
    *inSize = speex_bits_write(&enc_bits, enc_buffer, numberOfFrames*enc_frame_size);

    return enc_buffer;
}
-(spx_int16_t*)decodeSpeexBits:(char*)encodedAudio  withEncodedSize:(int)encodedSize andDecodedSize:(int)decodedSize{

    //Unsure of this third argument.  'encodedSize' is the number written to *inSize in the encode method
    speex_bits_read_from(&dec_bits, encodedAudio, encodedSize*dec_frame_size);

    speex_decode_int(dec_state, &dec_bits, dec_buffer);
    return dec_buffer;
}

他们是这样称呼的:

- (void)encodeBufferList:(AudioBufferList*)bufferList withNumberOfFrames:(int)numberOfFrames{
    AudioBuffer sourceBuffer = bufferList->mBuffers[0];
    int speexSize = 0;
    char* encodedAudio = [speexCodec encodeAudioBuffer:(spx_int16_t*)sourceBuffer.mData withByteSize:numberOfFrames andWriteSizeTo:&speexSize];
    spx_int16_t* decodedAudio = [speexCodec decodeSpeexBits:encodedAudio withEncodedSize:speexSize andDecodedSize:sourceBuffer.mDataByteSize];
    memcpy(audioBuffer.mData, sourceBuffer.mData, numberOfFrames * sizeof(SInt32));
}

其中“bufferList”是从我的录制/播放回调返回的。有人可以验证我正在正确填充缓冲区吗?我在这里看到了一个类似的问题,但看不到我的代码中哪里可能做错了:

static OSStatus recordingCallback(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData)
{
    AudioBuffer buffer;
    OSStatus status;
    AudioStreamer *input = (__bridge AudioStreamer*) inRefCon;

    buffer.mDataByteSize = inNumberFrames * sizeof(SInt16);
    buffer.mNumberChannels = 1;
    buffer.mData = malloc( inNumberFrames * sizeof(SInt16));

    AudioBufferList bufferList;
    bufferList.mNumberBuffers = 1;
    bufferList.mBuffers[0] = buffer;

    status = AudioUnitRender([input rioAUInstance], ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, &bufferList);
    [input encodeBufferList:&bufferList withNumberOfFrames:inNumberFrames];
    return noErr;
}

static OSStatus playbackCallback(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData)
{
    AudioStreamer* input = (__bridge AudioStreamer*)inRefCon;
    UInt32 size = MIN(ioData->mBuffers[0].mDataByteSize, [input audioBuffer].mDataByteSize);
    memcpy(ioData->mBuffers[0].mData, input.audioBuffer.mData, size);
    return noErr;
}

编码/解码产生的噪音是颗粒状的静态嘶嘶声,但它不是完全随机的信息——当我对着麦克风吹气时,我可以在噪音后面听到它。

任何帮助解决这个问题将不胜感激。一旦我把所有事情都整理好,我可能最终会写博客,似乎很多人在设置这个编解码器时遇到了各种琐碎的问题。

4

2 回答 2

1

所以这是编码/解码函数中的一个问题,我需要跨多个帧调用 speex_encode_int,因为它似乎一次只处理 1 帧,然后将它们写入编码缓冲区,如下所示:

-(char*)encodeAudioBuffer:(spx_int16_t*)audioBuffer withNumberOfFrames:(int)numberOfFrames andWriteSizeTo:(int*)inSize{
    speex_bits_reset(&enc_bits);
    for(int i = 0; i < numberOfFrames; ++i){
        speex_encode_int(enc_state, audioBuffer+i, &enc_bits);
    }
    *inSize = speex_bits_write(&enc_bits, enc_buffer, numberOfFrames);
    return enc_buffer;
}

与解码类似,speex_bits_read_from 编码缓冲区,然后遍历每一帧的 dec_bits,写入解码缓冲区

-(spx_int16_t*)decodeSpeexBits:(char*)encodedAudio  withEncodedSize:(int)encodedSize andNumberOfFrames:(int)numberOfFrames{
    speex_bits_read_from(&dec_bits, encodedAudio, encodedSize);
    for(int i = 0; i < numberOfFrames; ++i){
        speex_decode_int(dec_state, &dec_bits, dec_buffer+i);
    }
    return dec_buffer;
}

这对我来说仍然运行得很慢。即使将 speex 库配置为使用定点计算而不是浮点计算,它的运行速度仍然比我的音频循环慢(导致一种新的断断续续)。关于如何让这个运行更快的任何线索?

于 2013-02-07T17:57:21.420 回答
0

在您的两个循环中,您都在传递音频缓冲区,但没有考虑帧大小:

for(int i = 0; i < numberOfFrames; ++i){
    speex_encode_int(enc_state, audioBuffer+i, &enc_bits);
}

它应该是:

for(int i = 0; i < numberOfFrames; ++i){
    speex_encode_int(enc_state, audioBuffer + (i * enc_frame_size), &enc_bits);
}

希望有帮助。

于 2013-02-09T16:28:42.703 回答