Simple Microphone Audio Processing Utility Using Qt
Rate this article
4.33 out of 5
This article explains how to record audio from microphone and process the audio sample using DSP algorithms to produce altered audio output. In this article, I am using QAudioInput and QAudioOutput classes to record and play back to the input audio, and used Low pass filter algorithm to reduce noise in input audio. Here, I have tried to explain the low level audio processing using Qt. Here I am not explaining any complex DSP algorithms. It’s just a starting point to a better cross platform audio processing utility. You can implement your own algorithms or any complex algorithms to extend your ideas. I used Qt's audioinput and audio output examples to develop this utility.



Simple Audio Processing Utility Image

Download Source Code
In Qt Creator start a new Qt Widget project. The wizard will create all the necessary files to start a desktop application. To link against the multimedia module, add  QT += multimedia line to your AppNamae .pro file:
 TARGET  specify the output file name  (TARGET  = ApplicationName)
TEMPLATE specify its compiled output type. Assign it to an application (TEMPLATE = app)
Add all the source and header files are using in your project.
Add Form design resources to FORMS += mainwindow.ui.This will create user interface to your application

QT       += core gui

             multimedia

 

TARGET = MyAudio

TEMPLATE = app

 

SOURCES += main.cpp

        mainwindow.cpp

 

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui


Before starting the audio processing initialize Audio Format Frequency, Channel, Sample Rate etc.Use QAudioFormat to initialize audio. 

An audio format specifies how data in an audio stream is arranged, i.e, how the stream is to be interpreted. The encoding itself is specified by thecodec() used for the stream.

In addition to the encoding, QAudioFormat contains other parameters that further specify how the audio data is arranged. These are the frequency, the number of channels, the sample size, the sample type, and the byte order. The following table describes these in more detail. 


m_format.setFrequency(8000); //set frequency to 8000

    m_format.setChannels(1); //set channels to mono

    m_format.setSampleSize(16); //set sample sze to 16 bit

    m_format.setSampleType(QAudioFormat::UnSignedInt ); //Sample type as usigned integer sample

    m_format.setByteOrder(QAudioFormat::LittleEndian); //Byte order

    m_format.setCodec("audio/pcm"); //set codec as simple audio/pcm

 

    QAudioDeviceInfo infoIn(QAudioDeviceInfo::defaultInputDevice());

    if (!infoIn.isFormatSupported(m_format))

    {

        //Default format not supported - trying to use nearest

        m_format = infoIn.nearestFormat(m_format);

    }

 

    QAudioDeviceInfo infoOut(QAudioDeviceInfo::defaultOutputDevice());

 

    if (!infoOut.isFormatSupported(m_format))

    {

       //Default format not supported - trying to use nearest

        m_format = infoOut.nearestFormat(m_format);

    }


Create audio input and output objects with IO device and audio formats.

The QAudioInput class provides an interface for receiving audio data from an audio input device.

The QAudioOutput class provides an interface for sending audio data to an audio output device. 

m_audioInput = new QAudioInput(m_Inputdevice, m_format, this);

 

m_audioOutput = new QAudioOutput(m_Outputdevice, m_format, this);

 

Start audio input and audio output and connect readyRead() SIGNAL to readMore() SLOT.

The readyRead () signal is emitted once every time new data is available for reading from the device. It will only be emitted again once new data is available, such as when a new payload of network data has arrived on your network socket, or when a new block of data has been appended to your device.


 

//Audio output device

    m_output= m_audioOutput->start();

     //Audio input device

    m_input = m_audioInput->start();

    //connect readyRead signal to readMre slot.

    //Call readmore when audio samples fill in inputbuffer

    connect(m_input, SIGNAL(readyRead()), SLOT(readMore()));

 

Read sound samples from input device to buffer

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

 

Implemented Low Pass filter algorithm to reduce noise in the input audio to produce smooth sound.

int iIndex;

         if(ui->chkRemoveNoise->checkState() == Qt::Checked)

         {

                //Remove noise using Low Pass filter algortm[Simple algorithm used to remove noise]

                for ( iIndex=1; iIndex < len; iIndex++ )

                {

                    outdata[ iIndex ] = 0.333 * resultingData[iIndex ] + ( 1.0 - 0.333 ) * outdata[ iIndex-1 ];

                }

          }

 

Apply volume to audiosample for adjusting the output volume.Multiply an integer number to audio sample to adjust its amplitude.  

for ( iIndex=0; iIndex < len; iIndex++ )

         {

             //Cange volume to each integer data in a sample

             outdata[ iIndex ] = ApplyVolumeToSample( outdata[ iIndex ]);

         }

        

         int MainWindow::ApplyVolumeToSample(short iSample)

{

    //Calculate volume, Volume limited to  max 35535 and min -35535

    return std::max(std::min(((iSample * m_iVolume) / 50) ,35535), -35535);

 

}

 

Finally play back the modified audio samples to speaker. This will play modified audio from microphne to speaker.

//write modified sond sample to outputdevice for playback audio

          m_output->write((char*)outdata, len);

 

You can implement your own algorithms in readMore() function. Here I implemented Low Pass filter algorithms to reduce noise in input audio. There are lts of complex audio filtering algorithms are available to produce a perfect audi filtering Low Pass is the simplest algorthm in that list. Shows how to change the volume in low level audio processing.

 

Reference

http://doc.qt.nokia.com/4.7-snapshot/multimedia-audioinput.html 

http://harmattan-dev.nokia.com/docs/library/html/qt4/



About The Author
Arun Kumar
Number Of articles Posted
1
Number Of Messages Posted
12
Joined Date
26/05/2012
   
Author
Messages
arunkumaraymuo
Posted: 08/09/2016
This article explains how to record audio from microphone and process the audio sample using DSP algorithms. The altered audio can save or output to a speaker. here I written to the output device(speaker).

After click on play button speak through the microphone of your system.  you can hear your sounds from speaker.
Check your systems default audio devices. I used default audio devices for record and play  sound samples  
TJ
Posted: 08/09/2016
Thanks Arun, yes it works now, what a nice code you wrote. 
Is it possible to stream audio from mic on one computer to another computer in the same network? do you have any sample code written in QT for that?

arunkumaraymuo
Posted: 08/09/2016
Please check this link
http://www.developer.nokia.com/Community/Wiki/Streaming_Audio_with_Qt
TJ
Posted: 08/09/2016
Hi Arun,
Thanks for the code and information.
I am truing to run your code, and what I get is just the window that you have a plot of it in this document. When I press "play" nothing happens, can you please explain how the program works? What does it play? 
Thanks.
Please Login to Post Your Comments
Name (Max 50 Chars)
Comments

 
TekTipsDownload
GateExam
Academic Projects
TekTipsExperts
Android