How to do realtime audioprocessing with Qt6
-
Hi everyone,
I am new to Qt and I think I have enough knowledge in C++ to start a project with Qt.
I would like to know, if apart from the documentation of Qt, there are other sites that you can advise me, and that go into more detail in the explanations.
For example, for audio processing I would like to know how the classes interact with each other as well as those that are not documented on Qt. To be more concrete, I have a project in which I want to open an audio file, extract the amplitude, apply operations on the amplitudes (filtering for example) and listen to the result in real time. I read on some forum that I have to inherit the QIODevice class but not more. My idea is to proceed as follows- create a subclass from IODevice with an attribute QAudioDecoder and QAudioBuffer
- Decode the audio file with the QAubiDecoder atribute and keep the amplitudes in the QAudioBuffer atribute of my subclass
- Replace the pure virtual function readData of my subclass and inside the function apply my filtering operations.
- send the results of the operations to my subclass of QIODevice
- Initialize an Object of type QAudioSink with my subclass.
can you please tell me if this way of proceeding is correct? if not, what is the right way to do it.
At the moment I'm at point 2 and try with the following code to decode an Audio file but it is not work. The QAudioDecoder object emits the error QAudioDecoder::ResourceError.
Can you have an idea of the source of the error?
I use Qt6.Thanks in advance
m_audioDecoder = new QAudioDecoder(this); m_audioBuffer = new QAudioBuffer; m_audioFormat = new QAudioFormat; m_audioFormat->setSampleRate(44100); m_audioFormat->setChannelCount(1); m_audioFormat->setSampleFormat(QAudioFormat::Float); qDebug() << m_audioFormat->isValid(); m_audioDecoder->setSource(QUrl(tr("C:/Users/DesktoP/Audio.wav"))); m_audioDecoder->setAudioFormat(*m_audioFormat); connect(m_audioDecoder, &QAudioDecoder::bufferReady, this, &MainWindow::slot_bufReady); m_audioDecoder->start(); connect(m_audioDecoder, QOverload<QAudioDecoder::Error>::of(&QAudioDecoder::error), [=](QAudioDecoder::Error error){ qDebug()<< error; });//<= ERROR QAudioDecoder::ResourceError
-
Hi and welcome to devnet,
QUrl(tr("C:/Users/DesktoP/Audio.wav"))
Three things are looking wrong here:
- use QUrl::fromLocalFile
- why are you using tr ? Are you going to translate that path ?
- Desktop casing is suspicious.
-
@SGaist
Hello,
Thank you Sir for your intervention,
I used QUrl::fromLocalFile and I changed the spelling of Desktop. Now it works.I would also like to have your opinion on my way to filter data and listen in real time. I was able to extract data from the QAudioBuffer object, but I think they are not suitable for filtering operations. I made another post on it which you can find here: https://forum.qt.io/topic/141536/how-to-get-usable-data-between-1-and-1-from-qaudiobuffer-qt6 . I would be grateful, if you take a look at it.
If you also know of a document that explains in more detail how the classes needed for audio processing interact with each other, then I would be grateful if you could point it out to me. An example so that you understand why I am looking for such documentation is the pure virtual function quint64 readData(char *data, quint64 Len) from QIODevice. For my project, I will have to reimplement it, but I would like to know what function calls it and how to determine the Len parameter.
Excuse me if my English is not correct. I use a translator
-
I think the best would be to take a look at the sources if you want to fully understand how it works.