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 play sound continuously using QTConcurrent
Qt 6.11 is out! See what's new in the release blog

How to play sound continuously using QTConcurrent

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 463 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.
  • H Offline
    H Offline
    harish__rajora
    wrote on last edited by
    #1

    The scenario is that I have to process some bytes of data continuously to the QBuffer and play it. I have the following code with QtConcurrent:

        {
    
            file_name = QFileDialog::getOpenFileName(this, "Open a file");
            if (!file_name.isEmpty()){
                QLabel *fileName = findChild<QLabel*>("fileName");
                fileName->setText(file_name);
                cout<<file_name.toStdString();
    
                QFile audio_file(file_name);
    
                if(audio_file.open(QIODevice::ReadOnly)) {
                   readheader();
                    audio_data = audio_file.readAll();
                    audio_file.close();
                
                    QDataStream audio_ds(audio_data);
                    int val = 0;
                    audio_ds>>val;
                    audio_buffer = new QBuffer(&temporary_audio_array);
    
    
                
                    int initial_byte = 0;
    
    
    
                         temporary_audio_array.clear();
                         audio_buffer->reset();
                         for(int i = initial_byte; i < initial_byte + 80000; i++)
                               temporary_audio_array.append(audio_data[i]);
    
                 
    
                    qDebug()<<"The size of audio_buffer is"<<audio_data.size();
                    audio_buffer->seek(0);
                    QAudioFormat format = QAudioFormat();
                 
    
                    cout << "Filled buffer" << endl;
                    QAudioOutput *outputstream;
                    outputstream = new QAudioOutput(this);
                    format.setSampleFormat(QAudioFormat::Int16);
                  
                    myplayer->setAudioOutput(outputstream);
                    if(!audio_buffer->open(QIODevice::ReadWrite)) qDebug() << "Buffer not opened";
                        else
                        {
                            qDebug() << "Buffer size:" << audio_buffer->size();
                            audio_buffer->seek(0);
                
                            myplayer->setSourceDevice(audio_buffer, QUrl(file_name));
    
                        }
                  }
              }
          }
    

    The following is the play function

        {
                QFuture <void> res = QtConcurrent::run(&MainWindow::printCounter, this);
    
            if(current_playState == false){
    
               
                myplayer->play();
                current_playState = true;
            } else {
                myplayer->pause();
                current_playState = false;
            }
    
          }
    

    This is the thread code

    void MainWindow::printCounter(){
            qInfo()<<QThread::currentThread();
            for(int i = 0; i < 80000; i++)
                temporary_audio_array.append(audio_data[i]);
        }
    
    

    The audio plays only till the first 80k bytes. I am new to this. Any help appreciated.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What exactly are you trying to achieve with QtConcurrent ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • H Offline
        H Offline
        harish__rajora
        wrote on last edited by
        #3

        I have a library that can decode a fixed number of bytes at a time. I need to parallel process bytes through the library and keep playing the audio. I am using QConcurrent to process the bytes in a thread while the batch that has already been processed plays.

        Can you help me in any way? How to achieve this?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I would rather write a dedicated class that works with that processing library and once a batch is done emit a signal with a QByteArray containing these data that you can then directly write into the sink.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          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