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;
QAudioFormat audioformat;
QAudioInput *input;
QIODevice *intermediario;
QByteArray buffer;
To copy to clipboard, switch view to plain text mode
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);
}
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);
}
To copy to clipboard, switch view to plain text mode
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);
QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
formatta_audio(&audioformat);
if (!info.isFormatSupported(audioformat))
audioformat = info.nearestFormat(audioformat);
input= new QAudioInput (audioformat);
ui->setupUi(this);
To copy to clipboard, switch view to plain text mode
I start the QaudioInput in this way:
intermediario = input->start();
connect (intermediario,SIGNAL(readyRead()),this,SLOT(cattura()));
intermediario = input->start();
connect (intermediario,SIGNAL(readyRead()),this,SLOT(cattura()));
To copy to clipboard, switch view to plain text mode
And stop the QaudioInput in this way:
input->stop();
input->stop();
To copy to clipboard, switch view to plain text mode
"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
));
}
}
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));
}
}
To copy to clipboard, switch view to plain text mode
I've tried a lot of times but "intermediario->bytesAvailable()" return ever "0"
Why? What is the problem?
Bookmarks