How to play sound continuously using QTConcurrent
-
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.
-
Hi,
What exactly are you trying to achieve with QtConcurrent ?
-
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?
-
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.