Qt real time audio processing



  • I want to do the arithmetic mean of values of a buffer which I wanna to fill with QAudioInput.

    I have declare these objects:
    @QAudioFormat audioformat;
    QAudioInput *input;
    QIODevice *intermediario;
    QByteArray buffer;@

    I've write 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 have write 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(cattura()));
    @
    And stop the QaudioInput in this way:
    @
    input->stop();
    @

    catturafunction is this:
    @
    void MainWindow::cattura()
    {
    qint64 bytescatturati=1000; //I don't know what I should write
    qint64 Nbytesletti;
    qint64 i;
    qint64 media=0;

    Nbytesletti= intermediario->read(buffer.data(),bytescatturati);
    
    for (i=0;i<Nbytesletti;i++)
    {
        media=media+(qint64)buffer.at(i);
    }
    
    media/=Nbytesletti;
    
    ui->label->setText(QString("%1").arg(media));
    

    }
    @

    I've tried several times but the function "cattura" never was called and I don't know the right value of bytescatturati.

    Can someone help me please?


  • Moderators

    Welcome to the forum!

    Are your classes with signals and slots derived directly or indirectly from QObject?

    [quote author="andrea993.93" date="1314450880"]
    @
    intermediario = input->start();
    connect (intermediario,SIGNAL(readyRead()),this,SLOT(cattura()));
    @
    [/quote]

    Did you make sure that the connect has been successful?
    Connect does return a bool for testing the success. Alternatively there is also some output indicating which element cannot be found.



  • I've tried, the connect return "true".
    intermediario is a QIODevice.

    The prototype of cattura() is in "mainwindow.h"
    @
    private slots:
    void cattura();
    @


  • Moderators

    Ok, so the signal is connected to the slot successfully.

    [quote author="andrea993.93" date="1314450880"]
    I have declare these objects:
    @QAudioFormat audioformat;
    QAudioInput *input;
    QIODevice *intermediario;
    QByteArray buffer;@
    [/quote]
    The only explanation I would have now is that these pointer are destroyed when you are exiting one of your methods. You might want to check this. Furthermore, you can check the "state":http://doc.qt.nokia.com/4.7/qaudio.html#State-enum of your audio input.



  • these objects
    @
    QAudioFormat audioformat;
    QAudioInput *input;
    QIODevice *intermediario;
    QByteArray buffer;
    @
    are declared out of methods; every method can access them


  • Moderators

    Did you check the state of your audio stream ?

    [quote author="koahnig" date="1314458762"]Furthermore, you can check the "state":http://doc.qt.nokia.com/4.7/qaudio.html#State-enum of your audio input. [/quote]

    You can do with "state()":http://doc.qt.nokia.com/4.7/qaudioinput.html#state . Also you can use "bytesReady()":http://doc.qt.nokia.com/4.7/qaudioinput.html#bytesReady for checking.

    Otherwise hopefully somebody with QAudio experience is picking up this thread.



  • I' ve looked the state.
    Just I set intermediario->start the state is actived immediately after the state begins stopped and then never return active


  • Moderators

    Sounds a bit that whatever you are opening to deliver the audio information does not provide any data. If you are reading a file, it may various reasons like file empty or wrong format.
    If you are using a device supplying the data, it is probably stopped or disconnected for any reason.



  • You might take a look at "this thread":http://developer.qt.nokia.com/forums/viewthread/7679/ , i posted an example for doing audio processing there.



  • I had already read that thread, thank you...

    I tried to compile for windows and the connect work correctly but I need to compile for symbian.

    The slot "cattura()" is called all times when "intermediario" is "readyread()"...is it right?
    Then which is the correct value of "bytescatturati"?
    @
    qint64 bytescatturati=1000; //????
    @



  • Well, let's take a look at the API Documentation of QIODevice ... ;-)

    bq. void QIODevice::bytesWritten ( qint64 bytes ) [signal]
    This signal is emitted every time a payload of data has been written to the device. The bytes argument is set to the number of bytes that were written in this payload.
    bytesWritten() is not emitted recursively; if you reenter the event loop or call waitForBytesWritten() inside a slot connected to the bytesWritten() signal, the signal will not be reemitted (although waitForBytesWritten() may still return true).
    See also readyRead().

    and

    bq. qint64 QIODevice::bytesAvailable () const [virtual]
    Returns the number of bytes that are available for reading. This function is commonly used with sequential devices to determine the number of bytes to allocate in a buffer before reading.
    Subclasses that reimplement this function must call the base implementation in order to include the size of QIODevices' buffer. Example:
    qint64 CustomDevice::bytesAvailable() const
    {
    return buffer.size() + QIODevice::bytesAvailable();
    }
    See also bytesToWrite(), readyRead(), and isSequential().



  • I've changed the cattura() function:
    @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));
    }
    

    }
    @
    But "intermediario->bytesAvailable()" return ever "0".
    Why?



  • I've try to monitor the QAudio::State and after "input->start()" the state is Idle.

    Why?



  • have you read this?

    bq. Symbian Platform Security Requirements
    On Symbian, processes which use this class must have the UserEnvironment platform security capability. If the client process lacks this capability, calls to either overload of start() will fail. This failure is indicated by the QAudioInput object setting its error() value to QAudio::OpenError and then emitting a stateChanged(QAudio::StoppedState) signal.
    Platform security capabilities are added via the TARGET.CAPABILITY qmake variable.



  • thank you very much.

    But it does'n work with windows :(
    First I want it work with Windows



  • nobody can help me?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.