gpt4 book ai didi

c - PortAudio - 单声道工作,立体声故障?

转载 作者:行者123 更新时间:2023-12-04 04:40:39 26 4
gpt4 key购买 nike

这让我很难过;这在单声道中效果很好,但是当我将其更改为立体声时,它听起来断断续续和静止。这是一个交错问题还是什么(数据是交错的,顺便说一句)

编辑:将交错方法更改为更合乎逻辑的方法,但仍然无济于事......

    #include <pthread.h>
#include <portaudio/portaudio.h>
#include <sndfile.h>

#define STEREO 2
#define SAMPLE_RATE 44100

typedef struct{
M_float* data, *data_pos;
sf_count_t frames;
} StreamData;
StreamData stream_data;

int ProcessAudio(const void* input, void* output, ulong frames_per_buff,
const PaStreamCallbackTimeInfo* time_info, PaStreamCallbackFlags flags, void* strm_data){

M_StreamData* strm = (M_StreamData*)(strm_data);
M_float* strm_out = (M_float*)(output);

if(strm -> frames > frames_per_buff){

for(sf_count_t frames_iter = 0; frames_iter < frames_per_buff;){
M_float sample = (strm -> data_pos++)[0];
strm_out[frames_iter] = sample;
strm_out[++frames_iter] = sample;
}

}else{
return (paComplete);
}

return (paContinue);
}

void* StartAudio(void* params){
SF_INFO info;
info.channels = M_STEREO;
info.samplerate = M_SAMPLE_RATE;
info.format = SF_FORMAT_RAW | SF_FORMAT_FLOAT;

SNDFILE* file = sf_open(u8"californication.raw", SFM_READ, &info);

stream_data.data = malloc(info.frames * info.channels * sizeof(float));
stream_data.data_pos = stream_data.data;
stream_data.frames = sf_readf_float(file, stream_data.data, info.frames);

sf_close(file);

if(Pa_Initialize() != paNoError)exit(0);

PaStream* pa_stream = NULLPTR_T;
Pa_OpenDefaultStream(&pa_stream, 0, STEREO, paFloat32, SAMPLE_RATE, paFramesPerBufferUnspecified,
&ProcessAudio, &stream_data);

Pa_StartStream(pa_stream);
Pa_Sleep(9001);
Pa_StopStream(pa_stream);
Pa_CloseStream(pa_stream);

sf_close(file);
free(stream_data.data);

Pa_Terminate();

return (NULLPTR_T);
}

int main(void){
static pthread_t thrd;
pthread_create(&thrd, NULLPTR_T, &M_StartAudio, NULLPTR_T);

return (false);
}

最佳答案

如果不编译您的代码,我无法确定,但看起来您的交错算法是将两个样本写入输出缓冲区中的同一索引,而不是将一个样本写入输出缓冲区两次。

这段代码:

  strm_out[frames_iter] = (strm -> data_pos++)[0];
strm_out[frames_iter] = (strm -> data_pos++)[1];

应该看起来像:
M_float* sample =  (strm -> data_pos++)[0]; 
strm_out[frames_iter] = sample;
strm_out[++frames_iter] = sample;

同样,这可能不完全正确,但希望它说明了我的观点。

关于c - PortAudio - 单声道工作,立体声故障?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18940408/

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