PDA

View Full Version : Real time audio processing



Andrea993
1st September 2011, 16:48
I want to do the arithmetic mean of values of a buffer which I wanna to fill with QAudioInput.

I declare these objects:

QAudioFormat audioformat;
QAudioInput *input;
QIODevice *intermediario;
QByteArray buffer;


I wrote this function for formatting audioformat:

void formatta_audio (QAudioFormat *formato)
{
formato->setFrequency(1000);
formato->setSampleSize(8);
formato->setCodec("audio/pcm");
formato->setByteOrder(QAudioFormat::LittleEndian);
formato->setSampleType(QAudioFormat::UnSignedInt);
formato->setChannels(1);
}

In the MainWindow costructor I wrote this:

QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();

formatta_audio(&audioformat);

if (!info.isFormatSupported(audioformat))
audioformat = info.nearestFormat(audioformat);

input= new QAudioInput (audioformat);

ui->setupUi(this);

I start the QaudioInput in this way:

intermediario = input->start();
connect (intermediario,SIGNAL(readyRead()),this,SLOT(cattu ra()));

And stop the QaudioInput in this way:

input->stop();

"cattura()" function is this:

void MainWindow::cattura()
{

qint64 Nbytesletti;
qint64 i;
qint64 media=0;
qint64 bytescatturati;


bytescatturati= intermediario->bytesAvailable();

if (bytescatturati>0)
{

Nbytesletti= intermediario->read(buffer.data(),bytescatturati);

for (i=0;i<Nbytesletti;i++)
{
media+=(qint64)buffer.at(i);
}

media/=Nbytesletti;

ui->label->setText(QString("%1").arg(media));
}


}

I've tried a lot of times but "intermediario->bytesAvailable()" return ever "0"

Why? What is the problem?

Andrea993
3rd September 2011, 13:20
nobody can hellp me?

SeeLook
28th November 2011, 10:20
In fact QAudioInput is little unpredictable.
bytesAvaiable() returns 0, bytesRead() will give You some "positive" value.
but only Nbytesletti returns by read() gives true bytes number, usualy much biger than byteaRead
and completely ignores buffer size and second parameter in read() method.

Don't give up :-)