gpt4 book ai didi

objective-c - Speex 编码/解码导致嘶嘶声(Objective-c)

转载 作者:搜寻专家 更新时间:2023-10-30 20:25:32 25 4
gpt4 key购买 nike

当我绕过 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”是从我的录音/回放回调中返回的。有人可以验证我是否正确填充了缓冲区吗?我看到一个类似的问题报告here ,但看不到我的代码中哪里可能做错了:

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

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

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

最佳答案

所以这是编码/解码函数中的问题,我需要跨多个帧调用 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 库配置为使用定点计算而不是浮点计算之后,它仍然比我的音频循环运行得慢(导致一种新的断断续续)。关于如何让它运行得更快的任何线索?

关于objective-c - Speex 编码/解码导致嘶嘶声(Objective-c),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14489779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com