如何解决PortAudio 不输出清晰的音频
我使用 libav
提取音频帧并将非 S16
格式的帧转换为 S16
,以便可以使用端口音频播放。
但是当我播放音频时,它不清楚或有时由于一些调整它根本不好。来自 wav
文件的音频比来自 mp4
文件的音频更清晰一些,但总体而言它不能产生良好的输出。
回调函数:
int patestCallback(const void* inputBuffer,void* outputBuffer,unsigned long framesPerBuffer,const PaStreamCallbackTimeInfo* timeInfo,PaStreamCallbackFlags statusFlags,void* _userData)
{
uint8_t* out = (uint8_t*)outputBuffer;
uint8_t* data = (uint8_t*)audioFile.get_converted();
PaUserData* userData = (PaUserData*)_userData;
int nb = framesPerBuffer;
for (int i = 0; i < nb; i+=1) {
*out++ = data[i];
}
return 0;
}
主要功能:
int main() {
Pa_Initialize();
PaStream* stream;
PaUserData data;
PaStreamParameters outputParameters;
int channels = audioFile.pCodecCtx->channels;
int sr = audioFile.pCodecCtx->sample_rate;
data.channels = channels;
printf("channels: %i\nsr: %i\n",channels,sr);
outputParameters.device = Pa_GetDefaultOutputDevice();
if (outputParameters.device == paNoDevice) {
fprintf(stderr,"Error: No default output device.\n");
}
outputParameters.channelCount = channels;
outputParameters.hostApiSpecificStreamInfo = NULL;
outputParameters.sampleFormat = paUInt8;
outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
Pa_OpenStream(&stream,NULL,&outputParameters,sr,2048 * channels,paDitherOff,patestCallback,&data);
Pa_StartStream(stream);
Pa_Sleep(8 * 1000);
Pa_Terminate();
system("pause");
return 0;
}
其他全局变量:
char* filename = (char*)"wavFile.wav";
static FileOpener audioFile(filename,(char*)"audio");
struct PaUserData {
int channels;
};
转换代码:
uint8_t* FileOpener::get_converted()
{
this->get_next_audio_frame();
if (this->pCodecCtx->sample_fmt != AV_SAMPLE_FMT_S16) {
SwrContext* swr = swr_alloc();
swr_alloc_set_opts(swr,this->pCodecCtx->channel_layout,AV_SAMPLE_FMT_S16,44100,this->pCodecCtx->sample_fmt,this->pCodecCtx->sample_rate,nullptr);
swr_init(swr);
int nb = 2048 * this->pCodecCtx->channels;
buffer = new uint8_t[nb];
swr_convert(swr,&buffer,nb,(const uint8_t**)pFrame->extended_data,pFrame->nb_samples);
return this->buffer;
}
else {
return *this->pFrame->extended_data;
}
}
如何播放清晰的音频?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。