Click here to Skip to main content
11,496,146 members (407 online)
Click here to Skip to main content

Cross Platform Microphone Audio Processing Utility In Qt

, 25 Jul 2012 CPOL 30.9K 1.9K 26
Simple signal processing implementation to microphone audio
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.

Introduction

This article explains how to record audio from microphone and process the audio sample using DSP algorithms. In this article, I am using the 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 anycomplex 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 audio input and audio output examples to develop this utility.

Background

In my recent project, I met with some issues to record audio and playback at a time. I searched about it in the web and saw a lot of forums discussing about to capture audio and play back to the speaker at a time. No one given any proper answer to do this using Qt. After some research work finally I succeeded to record and playback audio at a time.

Using the code

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

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:

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 the codec() 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 audio sample 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 microphone 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 lots of complex audio filtering algorithms are available to produce a perfect audio filtering Low Pass is the simplest algorithm in that list. Shows how to change the volume in low level audio processing.

Reference

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Arun Kumar K S
Software Developer
India India
I am an OSX\Qt\VC++\.net software developer from India. The main technologies I used are VC++,MFC,C#.NET,Qt,COM,Windows media Format etc...


I'm looking to advance my Software Engineering career by learning new technologies and extending my programming experience.


EMail : arunkumaraymuo@gmail.com
Skype : arunkumarinskype

Comments and Discussions

 
Questionrunning project Pin
Member 1126024225-Nov-14 2:09
memberMember 1126024225-Nov-14 2:09 
GeneralMy vote of 5 Pin
Leonenko Igor4-Oct-14 3:48
memberLeonenko Igor4-Oct-14 3:48 
Suggestiontransferring data into Buffer Pin
Alain Weiler25-Jan-14 3:32
memberAlain Weiler25-Jan-14 3:32 
Questionremark on BufferSize and more... [modified] Pin
Alain Weiler28-Dec-13 13:47
memberAlain Weiler28-Dec-13 13:47 
Questionrange of for loops is not correct [modified] Pin
Alain Weiler23-Dec-13 13:03
memberAlain Weiler23-Dec-13 13:03 
Questionwrong limits of data samples Pin
Alain Weiler21-Dec-13 15:09
memberAlain Weiler21-Dec-13 15:09 
GeneralMy vote of 5 Pin
sreekumar12347-Jun-13 6:11
membersreekumar12347-Jun-13 6:11 
QuestionQtMultimedia Pin
nguyentuanebook14-Feb-13 23:22
membernguyentuanebook14-Feb-13 23:22 
AnswerRe: QtMultimedia Pin
Arun Kumar K S24-Feb-13 18:49
memberArun Kumar K S24-Feb-13 18:49 
GeneralRe: QtMultimedia Pin
nguyentuanebook3-Mar-13 17:51
membernguyentuanebook3-Mar-13 17:51 
QuestionToo much noise Pin
Member 814714818-Dec-12 5:32
memberMember 814714818-Dec-12 5:32 
AnswerRe: Too much noise Pin
Arun Kumar K S18-Dec-12 18:59
memberArun Kumar K S18-Dec-12 18:59 
AnswerRe: Too much noise Pin
Alain Weiler15-Feb-14 14:44
memberAlain Weiler15-Feb-14 14:44 
GeneralMy vote of 5 [modified] Pin
Member 944012425-Sep-12 2:12
memberMember 944012425-Sep-12 2:12 
GeneralRe: My vote of 5 Pin
Arun Kumar K S25-Sep-12 14:25
memberArun Kumar K S25-Sep-12 14:25 
GeneralMy vote of 2 Pin
topocc23-Sep-12 7:03
membertopocc23-Sep-12 7:03 
GeneralRe: My vote of 2 [modified] Pin
Arun Kumar K S23-Sep-12 18:51
memberArun Kumar K S23-Sep-12 18:51 
QuestionVote excellent and a suggestion Pin
X3m2-Aug-12 5:52
memberX3m2-Aug-12 5:52 
GeneralMy vote of 5 Pin
xComaWhitex24-Jul-12 19:12
memberxComaWhitex24-Jul-12 19:12 
GeneralRe: My vote of 5 Pin
Arun Kumar K S25-Jul-12 23:50
memberArun Kumar K S25-Jul-12 23:50 
GeneralMy vote of 5 Pin
gndnet17-Jul-12 12:51
membergndnet17-Jul-12 12:51 
GeneralMy vote of 5 Pin
lintovadakkekudy14-Jul-12 3:41
memberlintovadakkekudy14-Jul-12 3:41 
GeneralRe: My vote of 5 Pin
Arun Kumar K S16-Jul-12 1:04
memberArun Kumar K S16-Jul-12 1:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 25 Jul 2012
Article Copyright 2012 by Arun Kumar K S
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid