我想知道如何知道portaudio测试示例的关系以及如何获得频率。我想知道如何将特定频率发送到声卡? 1.如何知道代码发送到声卡的具体频率 2.我如何发送罪(2 * pi * f * t)和sin((2 * pi * f * t)-3.14)这是一个延迟版本。这是代码。任何帮助都非常感谢
#include <stdio.h>
#include <math.h>
#include "portaudio.h"
#define NUM_SECONDS (20)
#define SAMPLE_RATE (44100)
#define AMPLITUDE (0.9)
#define FRAMES_PER_BUFFER (64)
#define OUTPUT_DEVICE Pa_GetDefaultOutputDeviceID()
//#define OUTPUT_DEVICE (2)
#ifndef M_PI
#define M_PI (3.14159265)
#endif
#define TABLE_SIZE (200)
typedef struct
{
float sine[TABLE_SIZE];
int left_phase;
int right_phase;
}
paTestData;
static int patestCallback( void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
PaTimestamp outTime, void *userData )
{
paTestData *data = (paTestData*)userData;
float *out = (float*)outputBuffer;
unsigned long i;
int finished = 0;
(void) outTime; /* Prevent unused variable warnings. */
(void) inputBuffer;
for( i=0; i<framesPerBuffer; i++ )
{
*out++ = data->sine[data->left_phase]; /* left */
*out++ = data->sine[data->right_phase]; /* right */
data->left_phase += 1;
if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
data->right_phase +=2.57; /* higher pitch so we can distinguish left and right. */
if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
}
return finished;
}
/*******************************************************************/
int main(void);
int main(void)
{
PortAudioStream *stream;
PaError err;
paTestData data;
int i;
printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d, devID = %d\n",
SAMPLE_RATE, FRAMES_PER_BUFFER, OUTPUT_DEVICE);
/* initialise sinusoidal wavetable */
for( i=0; i<TABLE_SIZE; i++ )
{
data.sine[i] = (float) (AMPLITUDE * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ));
}
data.left_phase = data.right_phase = 0;
err = Pa_Initialize();
if( err != paNoError ) goto error;
err = Pa_OpenStream(
&stream,
paNoDevice,/* default input device */
0, /* no input */
paFloat32, /* 32 bit floating point input */
NULL,
OUTPUT_DEVICE,
2, /* stereo output */
paFloat32, /* 32 bit floating point output */
NULL,
SAMPLE_RATE,
FRAMES_PER_BUFFER,
0, /* number of buffers, if zero then use default minimum */
paClipOff, /* we won't output out of range samples so don't bother clipping them */
patestCallback,
&data );
if( err != paNoError ) goto error;
err = Pa_StartStream( stream );
if( err != paNoError ) goto error;
printf("Play for %d seconds.\n", NUM_SECONDS ); fflush(stdout);
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;
错误: Pa_Terminate(); fprintf(stderr,“使用portaudio流时发生错误\ n”); fprintf(stderr,“错误号:%d \ n”,错误); fprintf(stderr,“错误消息:%s \ n”,Pa_GetErrorText(错误)); 返回错误; }
答案 0 :(得分:2)
快速查看建议SAMPLE_RATE / TABLE_SIZE将为您提供正弦波的每秒周期,这似乎是~220Hz,这是一个A3音符。虽然从代码我可以看到左声道正在获得这个波,而右声道正以2.75倍基本速率踩过表,我想这将是606Hz,它没有达到音符,我希望这个样本听起来相当可怕!
要发送正弦和延迟版本,您只需使用相位参数的偏移量查找延迟版本,注意正确环绕,并将波加在一起。您可能希望降低AMPLITUDE以避免削波。