为Audio创建回声效果 [英] Create echo effect to Audio

查看:59
本文介绍了为Audio创建回声效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在用Qt(C ++)开发一个音频处理工具,现在当我通过迈克讲话时,我可以听到扬声器发出的声音,现在我需要将回声效果应用于此音频输出,我将如何做到这一点,我将获得一个整数数组中的16位音频样本,此数组的更改将产生回声效果输出声音,任何示例或代码将对我非常有帮助



提前致谢









这是我的代码,我使用Qt作为开发工具



Hi,

I am developing one audio processing utility in Qt(C++), Now I can hear audio from speaker when I speak through mike, Now I need to apply echo effect to this audio output, How I can do this,I will get 16 bit audio samples in an integer array,what changes to this array will produce echo effect to output sound, Any examples or code will very helpful to me

Thanks in advance




Here is my code, I am using Qt as development tool

void MainWindow::readMore()
{

    if(!m_audioInput)
        return;

    qint64 len = m_audioInput->bytesReady();
    if(len > 4096)
        len = 4096;

    qint64 l = m_input->read(m_buffer.data(), len);
    if(l > 0)
    {


        Q_ASSERT(m_format.sampleSize() % 8 == 0);
        const int channelBytes = m_format.sampleSize() / 8;
        const int sampleBytes = m_format.channels() * channelBytes;
        Q_ASSERT(len % sampleBytes == 0);
        const int numSamples = len / sampleBytes;

 short *data = (short*)m_buffer.data();
        addecho(data,numSamples,4000,0.9);
       // m_output->write(m_buffer);


    }



}

void MainWindow::addecho(short* pcm_databuffer,unsigned int pcm_datalength,unsigned int phase,double damping)
{
     std::vector<short> myvector;

  //int* pcm_databuffer_with_echo = new int[](pcm_datalength+phase);
  unsigned int i,j;
  for(i=0;i<phase;i++)
  {
      myvector.push_back(pcm_databuffer[i]);
   // pcm_databuffer_with_echo[i] = pcm_databuffer[i];
  }
  for(j=0;i<pcm_datalength;i++,j++)
  { myvector.push_back(pcm_databuffer[i] +(int)((double)pcm_databuffer[j]*damping));
   // pcm_databuffer_with_echo[i] = pcm_databuffer[i] +
    // (int)((double)pcm_databuffer[j]*damping);
  }
  for(;j<pcm_datalength;i++,j++)
  {
       myvector.push_back((int)((double)pcm_databuffer[j]*damping));
    //pcm_databuffer_with_echo[i] = (int)((double)pcm_databuffer[j]*damping);
  }

  // output to device
  short *data=(short *)myvector.data();
  m_output->write((char*)data, pcm_datalength+phase);
  //PumpToDevice(pcm_databuffer_with_echo,pcm_datalength+phase);

  //delete [] pcm_databuffer_with_echo;
   myvector.empty();
}

推荐答案

给你的例子很少。必须计算回声延迟的相位(sps *延迟,即每秒44000个样本* 0.5秒延迟 - >相位= 22000)。

Little example for you. phase has to be calculated for the delay of your echo (sps*delay, i.e. 44000 samples per second * 0.5 seconds delay -> phase = 22000).
void addecho(int* pcm_databuffer,unsigned int pcm_datalength,unsigned int phase,double damping)
{
  int* pcm_databuffer_with_echo = new int[](pcm_datalength+phase);
  unsigned int i,j;
  for(i=0;i<phase;i++)>
  {
    pcm_databuffer_with_echo[i] = pcm_databuffer[i];
  }
  for(j=0;i<pcm_datalength;i++,j++)>
  {
    pcm_databuffer_with_echo[i] = pcm_databuffer[i] +
     (int)((double)pcm_databuffer[j]*damping);
  }
  for(;j<pcm_datalength;i++,j++)>
  {
    pcm_databuffer_with_echo[i] = (int)((double)pcm_databuffer[j]*damping);
  }

  // output to device
  PumpToDevice(pcm_databuffer_with_echo,pcm_datalength+phase);

  delete [] pcm_databuffer_with_echo;
}



祝你好运。


Good luck.


它找到了一个简单的解决方案,所以我写了自己的决定发布此信息以供将来参考,以及让人们在没有麻烦的情况下快速使用。



快速而肮脏的解决方案,但它确实有效。



快乐影响:)



It was hell finding a simple solution to this so i wrote my own and decided to post this here for future reference and for people to grab and be able to use quickly without all the hassle.

quick and dirty solution but it works.

happy effecting :)

#define ECHO 1024*10
float echo_buffer[ECHO];
int echooffset=1024*2; // chenge to delay more example int echooffset=1024*6;
int echooffsetplay;
int EFFECT=1;


if (EFFECT)
{

    if (EFFECT==1) // reverb
{
            echo_buffer[echooffset]+=sample*0.5;// original *  ( amount of original )
            if (echooffset++>=ECHO-1)echooffset=0;
            sample+=echo_buffer[echooffsetplay];echo_buffer[echooffsetplay]*=0.45;// decay
            if (echooffsetplay++>=ECHO-1)echooffsetplay=0;
}
    if (EFFECT==2) // echo
{
            echo_buffer[echooffset]=sample*0.5;// original *  ( amount of original )
            if (echooffset++>=ECHO-1)echooffset=0;
            sample+=echo_buffer[echooffsetplay]*0.4;// amount to mix into dry
            if (echooffsetplay++>=ECHO-1)echooffsetplay=0;
}
    if (EFFECT==3) // spacial
{
            echo_buffer[echooffset]+=sample*0.5;// original *  ( amount of original )
            if (echooffset++>=ECHO-1)echooffset=0;
            sample-=echo_buffer[echooffsetplay];echo_buffer[echooffsetplay]*=0.45;// amount of spacial
            if (echooffsetplay++>=ECHO-1)echooffsetplay=0;
}


}


这篇关于为Audio创建回声效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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