使用PortAudio生成正弦波以播放中间C [英] Generate sine wave to play middle C using PortAudio

查看:111
本文介绍了使用PortAudio生成正弦波以播放中间C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PortAudio中生成特定频率时遇到麻烦,每当我尝试更改sin(n * FREQ * 2 * PI / SAMPLE_RATE)内部的频率时,频率都保持不变,但是声音似乎确实发生了音色变化,我输入的频率值越高声音较难看,但频率相同.这是我在patestCallback循环中所拥有的:

I am having trouble generating specific frequencies in PortAudio, whenever I try and change the frequency inside of the sin(n * FREQ * 2 * PI / SAMPLE_RATE) the frequency remains the same however the sound does seem to change in timbre, the higher the frequency value I put in there the uglier the sound, yet the same frequency. This is what I have in my patestCallback loop:

static int patestCallback( const void *inputBuffer, void *outputBuffer,
                           unsigned long framesPerBuffer,
                           const PaStreamCallbackTimeInfo *timeInfo,
                           PaStreamCallbackFlags statusFlags,
                           void *userData )
{
   paTestData *data = (paTestData*)userData;
   float *out = (float*)outputBuffer;

   (void) timeInfo;
   (void) statusFlags;
   (void) inputBuffer;
   unsigned long n = 0;
   for(unsigned long i = 0; i<framesPerBuffer;i++,n++){
      float v = sin ( 261.626 * 2 * M_PI * ((float) n / (float) TABLE_SIZE) );
      *out++ = v;
      *out++ = v;
   }

   return paContinue;
}

推荐答案

简单解决方案:

static unsigned long n = 0;

您当前在每个函数调用中重置n,这会导致在每个新缓冲区的开头都发出喀哒声,并听到您听到的那些难看的声音.正弦的周期与缓冲区长度的差异越大,听起来越难看.

You currently reset n in every function call, which leads to clicks at the begin of each new buffer and to those ugly sounds you hear. The more the period of the sine differs from the buffer length the uglier it sounds.

这篇关于使用PortAudio生成正弦波以播放中间C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆