Problem with QIODevice::writeData()



  • Hey,

    Currently i am trying to use Qt to record audio from microphone, encode it using ffmpeg and then save the encoded file as e.g. mp3.

    First step is to get data from the mic with Qt. Therefore i use the audioinput-Example (http://doc.qt.nokia.com/4.7-snapshot/multimedia-audioinput.html) and try to midify it. The QAudioInput class reference shows how to save the mic-input as *.raw-file, but i want to encode the signal before i save it.

    I think the writeData()-function (http://doc.qt.nokia.com/4.7-snapshot/qiodevice.html#writeData) is what i need, but i have trouble working with it.
    After reading the description i thought the functionis called by the device and delivers me the audiosignal in the const char* data, which has the lenght of "maxSize".

    This is my writeData-function, on first call it opens the FILE* f, the next 1000calls i write len blocks with the size of one byte from data into my file. Then i close the file.
    @qint64 AudioInfo::writeData(const char *data, qint64 len)
    {
    if(counter==0)
    {
    f = fopen ("testfile","w");
    }
    if(counter>0&&counter<1000)
    {
    fwrite(data, 1, len, f);
    }
    if(counter==1000)
    {
    fclose(f);
    }
    counter++;
    return len;
    }@

    I thought this file should be an raw-Audio-FILE with the format that i have set up for my AudioInfo-object. But if i open the created testfile with Audacity and the correct format-settings it is only random noise. If the mic isnt getting a signal the recorded file remains silent, but as soon as i talk to the mic i get random noise in my recorded file.

    Can Anyone help me out? If i only knew how to work with the two parameters i get with writeData (*data and len) i could go ahead and encode that data.

    thanks in advance



  • The code you show us now is not Qt code, and looks very inefficient. To write to files in Qt, you should look into using QFile (which inherits QIODevice). What were your issues with using that exactly?



  • Hey Andre,

    I know, this file was just a test to see how i can handle data and len. I thought saving len elements with the size of 1 Byte from data to a file would save the audio input. The File i generate this way is just random noise.
    I do not want a File afterall, i only need to find the right way of how to handle the two paramters writeData() delivers, because i want to hand the audio signal over to ffmpeg.



  • The two parameters are simply a pointer to the raw data, and the length of the block of data that can be found at that address.



  • that's what i thought. But now if i directly write that into my File and try to replay the data i only get random noise. Any idea?

    Would you agree, that my file should SHOULD include the audiodata if i write it this way:
    @fwrite(data, 1, len, f);@
    ?

    thanks in advance



  • There is no way to judge that from the code you show.



  • Yeah you are probably right, sorry.
    This is all based on the audioinput-example, important are probably these two functions:
    @void InputTest::initializeAudio()
    {
    m_pullMode = true;
    m_format.setFrequency(44100);
    m_format.setChannels(2);
    m_format.setSampleSize(16);
    m_format.setSampleType(QAudioFormat::SignedInt);
    m_format.setByteOrder(QAudioFormat::LittleEndian);
    m_format.setCodec("audio/pcm");

    QAudioDeviceInfo info(QAudioDeviceInfo::defaultInputDevice());
    if (!info.isFormatSupported(m_format)) {
        qWarning() << "Default format not supported - trying to use nearest";
        m_format = info.nearestFormat(m_format);
    }
    
    m_audioInfo  = new AudioInfo(m_format, this);
    connect(m_audioInfo, SIGNAL(update()), SLOT(refreshDisplay()));
    
    createAudioInput();
    

    }

    void InputTest::createAudioInput()
    {
    m_audioInput = new QAudioInput(m_device, m_format, this);
    connect(m_audioInput, SIGNAL(notify()), SLOT(notified()));
    connect(m_audioInput, SIGNAL(stateChanged(QAudio::State)), SLOT(stateChanged(QAudio::State)));
    m_audioInfo->start();
    m_audioInput->start(m_audioInfo);
    }@

    The class AudioInfo is a subclass of QIODevice, the reimplemented writeData() is in the first post.

    thanks in advance


  • Moderators

    Is your endianness correct? Each sample is 2 bytes long. What happens if you swap the byte order round?



  • [quote author="JKSH" date="1346757318"]Is your endianness correct? Each sample is 2 bytes long. What happens if you swap the byte order round?[/quote]

    I just tried to change Endianess from Little to Big, but i didnt get better results :/


  • Moderators

    Very odd. I wonder if your system is digitizing sound correctly -- try writing the data from QAudioInput directly to a QIODevice supplied by QAudioOutput::start(), and see if it sounds right.


Log in to reply
 

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