PDA

View Full Version : How to record and play sound simultaneously



sayem.bd
17th October 2011, 08:28
Hi, I have posted this problem in two other places but found no solution. So I am posting it here.

How can I implement full duplex audio communication in Qt? That is, I want to record sound from the microphone and at the same time I want to play the sound
on the headphone/speaker.

The code that I have tried so far is as below -


void AudioRecorder::on_btnStart_clicked()
{
// Get audio format and search for nearest matching format of the device
QAudioFormat format = this->getFormat();

// Star recording using the specified format
this->audioInput = new QAudioInput(format, this);
connect(this->audioInput,SIGNAL(stateChanged(QAudio::State)),
SLOT(inputStateChanged(QAudio::State)));
QIODevice *myBuffer = this->audioInput->start();

this->audioOutput = new QAudioOutput(format, this);
connect(this->audioOutput,SIGNAL(stateChanged(QAudio::State)),
SLOT(outputStateChanged(QAudio::State)));
this->audioOutput->start(myBuffer);
}

QAudioFormat AudioRecorder::getFormat()
{
QAudioFormat format;
format.setFrequency(8000);
format.setChannels(1);
format.setSampleSize(8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);

QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
if (!info.isFormatSupported(format))
{
this->log("Default format not supported. Trying to use the nearest format.....");
format = info.nearestFormat(format);
}

return format;
}

The only problem is that in linux this code runs for some time but after that the output device faces an Underrun Error (I have checked that in the stateChanged slot) and in windows this program crashes just after it's run.

How can I solve this problem? Is there any other method for full duplex audio communication?

arunkumaraymuo1
24th July 2012, 09:44
start the input and output device like this



m_output= m_audioOutput->start();
m_input = m_audioInput->start();
connect(m_input, SIGNAL(readyRead()), SLOT(readMore()));

and write the input sample to output in readMore()

m_output->write(outdata, len);

Check this article in code project that record from microphone and play back to speaker simultaneously

http://www.codeproject.com/Articles/421287/Cross-Platform-Microphone-Audio-Processing-Utility