Friday, December 30, 2016

Qt wrappers for codec2 library


Qt wrappers for codec2 library


About

As I wrote in previous post I was amazed by codec2 (w:codec2) library for speech encoding. You can find more documentation about how to build codec2 library on Windows in my previous post:

You can find more information about codec2 on home page of codec2 here:

Next I want to write wrappers for Qt and test this library with my speech.  See below and you will find more.


Sources + documentation

You can find all sources + demo application here:

Demo is an player which can record and play raw codec2 encoded data. No any containers (wav or something else supported), so you can use this wrappers even for network communicating.

Documentation can be found using Doxygen just from source files. Doxygen file can be found in main folder, it tuned to produce .html output. So, you will get actual documentation in .html format.

Usage for recording (encoding)


First of all you need open output file:

    outputFile = new QFile(fileName);
    outputFile->open(QIODevice::WriteOnly | QIODevice::Truncate);
 
Then you need create encoder and attach output file to the encoder:
    encoder = new QCodec2Encoder(outputFile, this);
    encoder->setCodecMode(ui->cbBitrate->currentData().toInt());
    encoder->setNaturalEncoding(true);
    encoder->start(QIODevice::Truncate);
 
Finally, you need initialize QAudioInput:

    QAudioFormat    format = QCodec2BaseClass::codec2AudioFormat();
    audioInput = new QAudioInput(deviceInfo, format, this);

    connect
    (
        audioInput,
        &QAudioInput::stateChanged,
        this,
        &MainWindow::audioDeviceChanged
    );

    audioInput->setBufferSize(format.sampleRate()*format.channelCount()*(format.sampleSize()/8));

    audioInput->start(m_Codec2Writer);

At the end do not forget flush buffers and close file.
    audioInput->stop();

    encoder->stop();

    outputFile->flush();
    outputFile->close();

Usage for playing (decoding)

First of all you need open output file:

    audioInput = new QFile(m_WorkingFileName);
    audioInput->open(QIODevice::ReadOnly);
 
Next you need create decoder:

    decoder = new QCodec2Decoder(audioInput, this);
    decoder->setCodecMode(ui->cbBitrate->currentData().toInt());
    decoder->setNaturalEncoding(ui->cbEncodingType->currentData().toBool());
    decoder->start();
 
Finally, you need create audio QAudioOutput for plyaing decoded data:

    QAudioFormat    format = QCodec2BaseClass::codec2AudioFormat();

    audioOutput = new QAudioOutput(deviceInfo, format, this);

    connect
    (
        audioOutput,
        &QAudioOutput::stateChanged,
        this,
        &MainWindow::audioDeviceChanged
    );

    audioOutput->setBufferSize(format.sampleRate()*format.channelCount()*(format.sampleSize()/8));

    audioOutput->start(decoder);
 
At the end do not forget flush buffers and close file.

    audioOutput->stop();

    decoder->stop();

    audioInput->flush();
    audioInput->close();

Enjoy!

No comments:

Post a Comment