Pierre
Pierre

Reputation: 31

Speex AEC remove only a part of the echo

I try to use Speex Accoustic Echo Cancellation with Qt 5.2.0, but only a part of the echo is removed, it still remain.
Here is my test (not optimized for memory management). It records voice, remove echo and play it 400ms later with a really small echo cancellation.

Any idea where my code is wrong ?

//************
// declared in MainWindows.h
QAudioInput*    m_audioInput;
QAudioOutput*   m_audioOutput;
QIODevice*      m_ioIn;
QIODevice*      m_ioOut;
QList<QByteArray>   m_listBA;
//************

void MainWindow::InitSpeex()
{
    int loc_samplingRate=16000;
    int loc_frameSize=320;
    m_echo_state = speex_echo_state_init(loc_frameSize, 10*loc_frameSize);
    speex_echo_ctl(m_echo_state, SPEEX_ECHO_SET_SAMPLING_RATE, &loc_samplingRate);
    m_preprocess = speex_preprocess_state_init(loc_frameSize, loc_samplingRate);
}

void MainWindow::DestroySpeex()
{
    speex_echo_state_destroy(m_echo_state);
    speex_preprocess_state_destroy(m_preprocess);
    m_echo_state = NULL;
    m_preprocess = NULL;
}

void MainWindow::Start()
{
    QAudioFormat format;
    // Set up the desired format, for example:
    format.setSampleRate(SAMPLING_RATE);
    format.setChannelCount(1);
    format.setSampleSize(16);
    format.setCodec("audio/pcm");
    format.setByteOrder(QAudioFormat::LittleEndian);
    format.setSampleType(QAudioFormat::UnSignedInt);

    QAudioDeviceInfo loc_infoInput = QAudioDeviceInfo::defaultInputDevice();
    if (!loc_infoInput.isFormatSupported(format)) {
        qWarning() << "Default format not supported, trying to use the nearest.";
        format = loc_infoInput.nearestFormat(format);
   }

   m_audioInput = new QAudioInput(format, this);


   QAudioDeviceInfo loc_infoOutput(QAudioDeviceInfo::defaultOutputDevice());
    if (!loc_infoOutput.isFormatSupported(format)) {
    qWarning() << "Raw audio format not supported by backend, cannot play audio.";
    return;
    }

    m_audioOutput = new QAudioOutput(format, this);

    m_ioIn = m_audioInput->start();
    m_ioOut = m_audioOutput->start();

    connect(m_ioIn, SIGNAL(readyRead()), this, SLOT(on_data_input()));
}

void MainWindow::on_data_input()
{
    // push data on the list
    m_listBA.push_back( m_ioIn->readAll() );


    // il list is about 400ms of sample
    if( m_listBA.size() > 20)
    {
        // send oldest sample to speex and play it
        speex_echo_playback(m_echo_state, (spx_int16_t*)m_listBA[1].constData());
        m_ioOut->write( m_listBA.takeFirst() );

        // get most recent sample, remove echo and repush it on the list to be played    later
        speex_echo_capture(m_echo_state, (spx_int16_t*)m_listBA[m_listBA.size()-1].data(), m_AECBufferOut);
         loc_vad = speex_preprocess_run(m_preprocess, m_AECBufferOut);
        m_listBA.takeLast();
        m_listBA.push_back( QByteArray((const char*)m_AECBufferOut, sizeof( m_AECBufferOut)) );
    }
}

Upvotes: 2

Views: 1379

Answers (1)

MSalters
MSalters

Reputation: 180235

I'm wondering what you're trying to do with m_listBA. Apparently it's a FIFO, but you're not using [0].

Furthermore, why are you adding the raw input to the list just to remove it a few lines later?

Finally, m_AECBufferOut might be a pointer. In that case, sizeof(m_AECBufferOut) probably is 4 or 8. It's NOT how many bytes of output you have.

Basically, get your setup working without echo cancellation, and verify that it just adds a delay. Only then should you try to get the echo cancellation working. If the first part works, you don't need to post it here.

Upvotes: 0

Related Questions