gpt4 book ai didi

macos - FFT来自端口音频流的样本

转载 作者:行者123 更新时间:2023-12-03 02:07:32 28 4
gpt4 key购买 nike

这里的初学者(OSX 10.9.5,Xcode 6)

我有一个发出声音的portAudio流。现在,我想获得在回调中生成的那些随机值,并通过fftw计划运行它们。据我所知,fftw需要在主体中执行。那么如何显示从回调到主目录的数字呢?我觉得它与指针有关,但这是一个没有根据的猜测...

加入两个不同的库时遇到一些困难。小小的帮助将不胜感激,谢谢!

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "portaudio.h"
#include "fftw3.h"



#define NUM_SECONDS (1)
#define SAMPLE_RATE (44100)

typedef struct
{
float left_phase;
float right_phase;
}
paTestData;

static int patestCallback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData )
{
/* Cast data passed through stream to our structure. */
paTestData *data = (paTestData*)userData;
float *out = (float*)outputBuffer;
unsigned int i;
(void) inputBuffer; /* Prevent unused variable warning. */

for( i=0; i<framesPerBuffer; i++ )
{
*out++ = data->left_phase; /* left */
*out++ = data->right_phase; /* right */

/* Generate random value that ranges between -1.0 and 1.0. */
data->left_phase = (((float)rand()/(float)(RAND_MAX)) * 2) - 1 ;

data->right_phase = (((float)rand()/(float)(RAND_MAX)) * 2) - 1 ;


printf("%f, %f\n", data->left_phase, data->right_phase);
}
return 0;
}

/*******************************************************************/
static paTestData data;
int main(void);
int main(void)
{


PaStream *stream;
PaError err;


printf("PortAudio Test: output noise.\n");
/* Initialize our data for use by callback. */
data.left_phase = data.right_phase = 0.0;
/* Initialize library before making any other calls. */
err = Pa_Initialize();
if( err != paNoError ) goto error;

/* Open an audio I/O stream. */
err = Pa_OpenDefaultStream( &stream,
0, /* no input channels */
2, /* stereo output */
paFloat32, /* 32 bit floating point output */
SAMPLE_RATE,
512, /* frames per buffer */
patestCallback,
&data );
if( err != paNoError ) goto error;


err = Pa_StartStream( stream );
if( err != paNoError ) goto error;



/* Sleep for several seconds. */
Pa_Sleep(NUM_SECONDS*1000);


err = Pa_StopStream( stream );
if( err != paNoError ) goto error;
err = Pa_CloseStream( stream );
if( err != paNoError ) goto error;
Pa_Terminate();
printf("Test finished.\n");
return err;
error:
Pa_Terminate();
fprintf( stderr, "An error occured while using the portaudio stream\n" );
fprintf( stderr, "Error number: %d\n", err );
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
return err;
}

最佳答案

您可以尝试以“阻止写入”模式运行流,而不使用回调。要使用此模式,请为streamCallbackPa_OpenDefaultStream参数传递NULL,然后连续循环调用Pa_WriteStream。该 call 将在必要时阻止。像这样的伪代码:

Pa_OpenStream(&stream, 0, 2, paFloat32, SAMPLE_RATE, 512, NULL, NULL);
Pa_StartStream(stream);
float interleavedSamples[2*512];
for (int i = 0 ; i < SAMPLE_RATE/512 ; i++) // approx 1 second
{
GenerateNoise(&interleavedSamples, 2, 512, &data);
RunFft(interleavedSamples, ...);
PaWriteStream(stream, interleavedSamples, 512);
}

关于macos - FFT来自端口音频流的样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26777247/

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