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. How to generate audio stream in realtime?

How to generate audio stream in realtime?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.5k 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.
  • _M_H__ Offline
    _M_H__ Offline
    _M_H_
    wrote on last edited by
    #1

    Hi,
    I'm working on an embedded device which is measureing a gas concentration. The value should be displayed as number and also the level should be audible. The higher the level the more klicks per second should be played (e.g. 10% -> 10 Klicks per second / 100% -> 100 Klicks per second). Similar to a Geiger counter.
    Currently I measure the level and generate a buffer with the audio data (wav-format).

    The following code shows only the significant parts of the code. It works, but with problems.

    //Headerfile
    QMediaPlayer *m_player;
    QBuffer *m_stream;
    QByteArray m_audioHeader;
    QByteArray m_audioData;
    
    void GasSensor::myFunction() {
    //...
    m_stream->setBuffer(&m_audioData);
    m_stream->open(QIODevice::ReadWrite|QIODevice::Append);
    m_player->setMedia(QMediaContent(), m_stream);
    connect(m_timerRefresh, &QTimer::timeout, this, &GasSensor::refresh, Qt::UniqueConnection);
    m_timerRefresh->start(1000);
    }
    
    void GasSensor::refresh() {
    	//... get current gas concentration level
    	//... generate newAudioData with Klick-Sound
    	// Samplerate = 8kHz / Samplesize = 16 bit
    
    	//Copy audiodata to stream buffer (double buffer principle)
    	if( m_currentBufferNum == 1 ) {
    		m_stream->seek(0);
    		m_currentBufferNum = 0;
    		m_stream->write(newAudioData);
    		m_player->setPosition(0);	//--> this leads to additional unwanted pause and klicks
    	} else {
    		m_stream->seek(8000*2);
    		m_currentBufferNum = 1;
    		m_stream->write(newAudioData);
    		m_player->setPosition(1000);   //--> this leads to additional unwanted pause and klicks
    	}
    }
    

    The audio data is played continously but every time I set a new play position (m_player->setPosition()) I get two problems. First: The play position is not updated fast enough and a small pause (some milliseconds) is audible. Second: The pulse-pause-ratio is changed because the refresh-function is not called exacly every 1000 ms. I understand that this is a normal behaviour and I'm searching for a workaround.
    I have the following idea but I couldn't find a solution to solve it:
    A stream should be generated and played continiously. The part with the new audio data (klick-sounds) should be added to the stream some milliseconds (e.g. 100-200 ms) before the play position. The buffer of the audioplayer should not be increased because of limited memory available. Similar to a webradio stream. But I don't need to store or buffer old data.

    Has anyone an idea how to generate and play audio data in realtime?
    Thanks in advance for your comments.
    Regards,
    Michael

    1 Reply Last reply
    0
    • tekojoT Offline
      tekojoT Offline
      tekojo
      wrote on last edited by tekojo
      #2

      Hi @_M_H_
      Just an idea, but would changing the playbackRate work for you? http://doc.qt.io/qt-5/qmediaplayer.html#playbackRate-prop
      Have one sound that you loop and change the playback rate of that.

      P.S. I haven't done anything with multimedia in a couple of years, so it's just an idea.

      1 Reply Last reply
      1
      • _M_H__ Offline
        _M_H__ Offline
        _M_H_
        wrote on last edited by
        #3

        Thanks for your idea. But this is no solution for me, because the pulse width should be alwas the same. Only the pause time shoud vary. If I increase the playback rate, both will be changed.

        1 Reply Last reply
        1

        • Login

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