Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with QIODevice::writeData()
Forum Updated to NodeBB v4.3 + New Features

Problem with QIODevice::writeData()

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 5.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    noway
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • N Offline
        N Offline
        noway
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            noway
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • N Offline
                N Offline
                noway
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

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

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    noway
                    wrote on last edited by
                    #9

                    [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 :/

                    1 Reply Last reply
                    0
                    • JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #10

                      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.

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved