gpt4 book ai didi

c++ - PortAudio:声音开始和结束时的短噪音

转载 作者:行者123 更新时间:2023-11-28 04:53:03 24 4
gpt4 key购买 nike

我有类似钢琴的应用程序,当我按下钢琴键时,它会播放声音。在我的应用程序中,我编写了自己的正弦波发生器。应用程序是用 Qt 编写的。我认为问题出在 portAudio 上,但我找不到任何解决方案。

我已经为您记录了我的问题听起来如何:https://vocaroo.com/i/s1yiWjaJffTU

这是我的生成器类:

声音引擎.h

#ifndef SOUNDENGINE_H
#define SOUNDENGINE_H

#include <QThread>
#include <math.h>
#include "portaudio.h"

#define SAMPLE_RATE (44100)
#define FRAMES_PER_BUFFER (64)
#define FREQUENCY 220

#ifndef M_PI
#define M_PI (3.14159265)
#endif

#define TABLE_SIZE (200)

typedef struct
{
float sine[TABLE_SIZE];
int phase;
}
paTestData;

class SoundEngine : public QThread
{
Q_OBJECT
public:
bool turnOFF;
void run();
static int patestCallback( const void *inputBuffer, void *outputBuffer,unsigned long framesPerBuffer,const PaStreamCallbackTimeInfo* timeInfo,PaStreamCallbackFlags statusFlags,void *userData );
void generateSine();
void removeSine();
private:
paTestData data;
PaStream *stream;
PaError err;
bool isPressed;
};

#endif // SOUNDENGINE_H

声音引擎.cpp

#include "soundengine.h"
#include <QDebug>

void SoundEngine::run()
{
PaStreamParameters outputParameters;
int i;
double t;
turnOFF = false;
isPressed = false;

static unsigned long n=0;
for( i=0; i<TABLE_SIZE; i++, n++ )
{
t = (double)i/(double)SAMPLE_RATE;
data.sine[i] = 0;
//data.sine[i] = 0.3*sin(2 * M_PI * FREQUENCY * t);
/*data.sine[i] *= 1.0/2;
data.sine[i] += 0.5*sin(2 * M_PI * (FREQUENCY+110) * t);
data.sine[i] *= 2.0/3;
data.sine[i] += (1.0/3)*sin(2 * M_PI * (FREQUENCY+60) * t);
data.sine[i] *= 3.0/4;
data.sine[i] += (1.0/4)*sin(2 * M_PI * (FREQUENCY+160) * t);*/
}
data.phase = 0;

err = Pa_Initialize();
if(err != paNoError) qDebug()<<"Błąd przy inicjalizacji strumienia:"<<Pa_GetErrorText(err);

outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
if (outputParameters.device == paNoDevice) qDebug()<<"Błąd: Brak domyślnego urządzenia wyjścia!";

outputParameters.channelCount = 2; /* stereo output */
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;

err = Pa_OpenStream(
&stream,
NULL, /* no input */
&outputParameters,
SAMPLE_RATE,
FRAMES_PER_BUFFER,
paClipOff, /*paNoFlag we won't output out of range samples so don't bother clipping them */
patestCallback,
&data );
if(err != paNoError) qDebug()<<"Błąd przy otwieraniu strumienia:"<<Pa_GetErrorText(err);
//err = Pa_StartStream( stream );
if(err != paNoError) qDebug()<<"Błąd przy starcie strumienia:"<<Pa_GetErrorText(err);

while (turnOFF == false) {
Pa_Sleep(500);
}

//err = Pa_StopStream( stream );
if(err != paNoError) qDebug()<<"Błąd przy zatrzymywaniu strumienia:"<<Pa_GetErrorText(err);
err = Pa_CloseStream( stream );
if(err != paNoError) qDebug()<<"Błąd przy zamykaniu strumienia:"<<Pa_GetErrorText(err);
Pa_Terminate();
}

int SoundEngine::patestCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
{
paTestData *callData = (paTestData*)userData;
float *out = (float*)outputBuffer;
float sample;
unsigned long i;

(void) timeInfo; /* Prevent unused variable warnings. */
(void) statusFlags;
(void) inputBuffer;

for( i=0; i<framesPerBuffer; i++ )
{
sample = callData->sine[callData->phase++];
*out++ = sample; /* left */
*out++ = sample; /* right */
if( callData->phase >= TABLE_SIZE ) callData->phase -= TABLE_SIZE;
}

return paContinue;
}

void SoundEngine::generateSine()
{
if(isPressed == false)
{
for(int i=0; i<TABLE_SIZE; i++)
{
data.sine[i] += 0.3*sin(2 * M_PI * 440 * ((double)i/(double)SAMPLE_RATE));
}
isPressed = true;
err = Pa_StartStream( stream );
}
}

void SoundEngine::removeSine()
{
err = Pa_StopStream( stream );
for(int i=0; i<TABLE_SIZE; i++)
{
data.sine[i] -= 0.3*sin(2 * M_PI * 440 * ((double)i/(double)SAMPLE_RATE));
}
isPressed = false;

}

当我按下按钮时,函数

void SoundEngine::generateSine()

正在运行 - 它会发出声音。当我释放按钮时,方法

void SoundEngine::removeSine()

移除声音。

最佳答案

版主注意:这个问题似乎更属于 dsp.stackexchange 而不是这个论坛。

您的声音或 PortAudio 都没有问题。您最后听到的声音只是音频突然停止的结果。看看下面的声音图像,它在整个持续时间内具有恒定的振幅。该声音最后会发出爆裂声。

Constant amplitude throughout the sound file

相反,如果我们通过修改波形的包络(与图像 #1 中的声音相同)来衰减振幅,使其类似于图像 #2 中的声音,我们将不会听到声音的任何突然变化在最后。

enter image description here

总之,如果您的目标是完全消除您听到的砰砰声,请淡出(或淡入)您的声音。

关于c++ - PortAudio:声音开始和结束时的短噪音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47753325/

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