我又来了。这次我正在处理音频,之前我遇到了一些内存泄漏问题,但现在它们已经解决了,这次我遇到了一个新问题,当我将 portaudio 配置为立体声(通道 = 2)时,音频输出质量不好。
它只在单声道中输出良好,谷歌几乎没有解决方案(或者我是一个糟糕的“谷歌人”),这里是代码:
播放音频的线程:
int16_t* audioBuffer=(int16_t*)av_malloc(FRAME_SZ_AV);
int sz = MEDIA->DecodeAudioFrame(audioBuffer,0);
if(sz==1)
Pa_WriteStream(MEDIA->output_stream,audioBuffer,MEDIA->_audio_ccontext->frame_size);
if(sz!=1)
MessageBox(0,"error","error",MB_OK);
ZeroMemory(audioBuffer,FRAME_SZ_AV);
av_freep(&audioBuffer);
解码音频帧函数
int WbMedia::DecodeAudioFrame(int16_t *audio_buf, int buf_size){
int return_status=0;
AVPacket t_pack;
while(!audio_packets.empty()){
// Get new packet
WaitForSingleObject(Queue_Audio_Mutex,INFINITE);
t_pack = audio_packets.front();
audio_packets.pop();
ReleaseMutex(Queue_Audio_Mutex);
int obt_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
int consm = avcodec_decode_audio3(_audio_ccontext,audio_buf,&obt_size,&t_pack);
if(consm > 0 && obt_size > 0){
return_status=1;
break;
}
return_status=-1;
break;
}
av_free_packet(&t_pack);
return return_status;
}
端口音频设置
output_params.device = Pa_GetDefaultOutputDevice(); //choosen_device.dev_index;
output_params.sampleFormat=paInt16;
output_params.channelCount=channel_count;
output_params.suggestedLatency=choosen_device.dev_inf->defaultLowOutputLatency;
output_params.hostApiSpecificStreamInfo=NULL;
// Start with PA opening
PaError pa_opening_err = Pa_OpenStream(&output_stream,
NULL,
&output_params,
sample_fr,
_audio_ccontext->frame_size,
paNoFlag,
NULL,
NULL
);
为什么音频输出质量差的立体声而不是单声道?我该如何解决?