How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?
-
@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
o. May there be anything wrong in the following line?
Why not check out by yourself. QFile::exits() for example.
@Christian-Ehrlicher
The file definitely exists. Even when I adress here the absolute path, no ausio can be heard. -
I am currently porting a quite complex project from qt5 to qt6, and cannot figure out how to correctly port this line:
QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice());
With qt5, this very simple code works pretty well to play a smal wav file:
#include <QSound> #include <QAudioOutput> #include <QDir> #include <QCoreApplication> { QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); QString homePath = QDir::homePath(); if (play_) { QSound::play(binPath + "/sounds/1.wav"); } }
With qt6, I tried the following, ...
#include <QSoundEffect> #include <QAudioOutput> #include <QMediaDevices> #include <QDir> #include <QCoreApplication> { QAudioDevice info(QMediaDevices::defaultAudioOutput()); QString homePath = QDir::homePath(); if (play_) { QSoundEffect effect; effect.setSource(QUrl::fromLocalFile(binPath + "/sounds/1.wav")); effect.play(); } }
... but I am getting the following two compilation errors about thís line:
error: variable 'QAudioDevice info' has initializer but incomplete type
error: invalid use of incomplete type 'class QAudioDevice'Any idea?
@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
{
QAudioDevice info(QMediaDevices::defaultAudioOutput());
QString homePath = QDir::homePath();
if (play_) {
QSoundEffect effect;
effect.setSource(QUrl::fromLocalFile(binPath + "/sounds/1.wav"));
effect.play();
}
}Then the next c++ question - how long does QSoundEffect live?
-
@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
{
QAudioDevice info(QMediaDevices::defaultAudioOutput());
QString homePath = QDir::homePath();
if (play_) {
QSoundEffect effect;
effect.setSource(QUrl::fromLocalFile(binPath + "/sounds/1.wav"));
effect.play();
}
}Then the next c++ question - how long does QSoundEffect live?
@Christian-Ehrlicher
Unfortunately, still no success. I tried also the following code, however, again no audio (checked all sinks).#include <QSoundEffect> #include <QAudioOutput> #include <QMediaDevices> #include <QAudioDevice> #include <QAudioSink> #include <QDir> #include <QCoreApplication> { QAudioDevice device(QMediaDevices::defaultAudioOutput()); QString homePath = QDir::homePath(); QAudioFormat format; format.setSampleRate(8000); format.setChannelCount(1); format.setSampleFormat(QAudioFormat::UInt8); QAudioSink* audio = new QAudioSink(format, this); connect(audio, SIGNAL(stateChanged(QAudio::State)), this, SLOT(handleStateChanged(QAudio::State))); if (play_) { QFile effect; effect.setFileName(homePath + "/sounds/1.wav"); effect.open(QIODevice::ReadOnly); audio->start(&effect); } }
-
@Christian-Ehrlicher
Unfortunately, still no success. I tried also the following code, however, again no audio (checked all sinks).#include <QSoundEffect> #include <QAudioOutput> #include <QMediaDevices> #include <QAudioDevice> #include <QAudioSink> #include <QDir> #include <QCoreApplication> { QAudioDevice device(QMediaDevices::defaultAudioOutput()); QString homePath = QDir::homePath(); QAudioFormat format; format.setSampleRate(8000); format.setChannelCount(1); format.setSampleFormat(QAudioFormat::UInt8); QAudioSink* audio = new QAudioSink(format, this); connect(audio, SIGNAL(stateChanged(QAudio::State)), this, SLOT(handleStateChanged(QAudio::State))); if (play_) { QFile effect; effect.setFileName(homePath + "/sounds/1.wav"); effect.open(QIODevice::ReadOnly); audio->start(&effect); } }
@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
if (play_) { QFile effect; effect.setFileName(homePath + "/sounds/1.wav"); effect.open(QIODevice::ReadOnly); audio->start(&effect); }
lifetime?
-
@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
if (play_) { QFile effect; effect.setFileName(homePath + "/sounds/1.wav"); effect.open(QIODevice::ReadOnly); audio->start(&effect); }
lifetime?
@Christian-Ehrlicher
Give me the answer. I'n not a professional programmer ... -
@Christian-Ehrlicher
Give me the answer. I'n not a professional programmer ...@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
I'n not a professional programmer ...
But you should understand the basic stuff of the programming language you're using. And I asked you to think about the lifetime of the objects in the code I copied from you - so maybe im some more words: how long does
QFile effect;
live? Not that I asked the exact same question or another object some hours ago. -
@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
I'n not a professional programmer ...
But you should understand the basic stuff of the programming language you're using. And I asked you to think about the lifetime of the objects in the code I copied from you - so maybe im some more words: how long does
QFile effect;
live? Not that I asked the exact same question or another object some hours ago.@Christian-Ehrlicher Christian, sorry, I am looking for a solution, not for playing games. If you knew who I am and what I have already manage to program you wouldn't treat me that way. Sorry, if this just a from some nerds... Good bye.
-
@Christian-Ehrlicher Christian, sorry, I am looking for a solution, not for playing games. If you knew who I am and what I have already manage to program you wouldn't treat me that way. Sorry, if this just a from some nerds... Good bye.
@Urbi said in How to port QAudioOutput device(QAudioDeviceInfo::defaultOutputDevice()); to Qt6?:
. Sorry, if this just a from some nerds... Good bye.
No nerds - basic c++ understanding.
QFile effect
lives until the closing bracket after audio->start() so how showaudio
be able to read anything from this QFile afterwards? -
@Christian-Ehrlicher Christian, sorry, I am looking for a solution, not for playing games. If you knew who I am and what I have already manage to program you wouldn't treat me that way. Sorry, if this just a from some nerds... Good bye.
@Christian-Ehrlicher is right, you need to know at least basic C++ to create your projects. @Urbi in your case your local
QFile effect;
variable gets out of scope (gets deleted) after the bracket, so no audio will be outputed. You must have it as a pointer and keep in mind to remove it later after you no longer need it to prevent a memory leak. You can usedelete
ordeleteLater()
. Please, check out my test code below.Code:
if (play) { QFile *effectFile = new QFile(this); effectFile->setFileName(QString("%1/%2").arg(homePath, "sounds/1.wav")); qDebug() << "Is file exists: " << effectFile->exists() << " | " << effectFile->fileName(); effectFile->open(QIODevice::ReadOnly); audio->start(effectFile); effectFile->close(); effectFile->deleteLater(); //removes the QFile object, or call - delete effectFile }
Also, you can use the
qDebug()
to check the console output for example to see if the file exists and other useful stuff. By the way, whatQt 6
version are you using for this project? -
@Christian-Ehrlicher is right, you need to know at least basic C++ to create your projects. @Urbi in your case your local
QFile effect;
variable gets out of scope (gets deleted) after the bracket, so no audio will be outputed. You must have it as a pointer and keep in mind to remove it later after you no longer need it to prevent a memory leak. You can usedelete
ordeleteLater()
. Please, check out my test code below.Code:
if (play) { QFile *effectFile = new QFile(this); effectFile->setFileName(QString("%1/%2").arg(homePath, "sounds/1.wav")); qDebug() << "Is file exists: " << effectFile->exists() << " | " << effectFile->fileName(); effectFile->open(QIODevice::ReadOnly); audio->start(effectFile); effectFile->close(); effectFile->deleteLater(); //removes the QFile object, or call - delete effectFile }
Also, you can use the
qDebug()
to check the console output for example to see if the file exists and other useful stuff. By the way, whatQt 6
version are you using for this project?@Cobra91151 It gives this compilation error: 'audio' was not declared in this scope; did you mean 'QAudio'?
-
@Christian-Ehrlicher is right, you need to know at least basic C++ to create your projects. @Urbi in your case your local
QFile effect;
variable gets out of scope (gets deleted) after the bracket, so no audio will be outputed. You must have it as a pointer and keep in mind to remove it later after you no longer need it to prevent a memory leak. You can usedelete
ordeleteLater()
. Please, check out my test code below.Code:
if (play) { QFile *effectFile = new QFile(this); effectFile->setFileName(QString("%1/%2").arg(homePath, "sounds/1.wav")); qDebug() << "Is file exists: " << effectFile->exists() << " | " << effectFile->fileName(); effectFile->open(QIODevice::ReadOnly); audio->start(effectFile); effectFile->close(); effectFile->deleteLater(); //removes the QFile object, or call - delete effectFile }
Also, you can use the
qDebug()
to check the console output for example to see if the file exists and other useful stuff. By the way, whatQt 6
version are you using for this project?@Cobra91151 Sorry, I forgot that I had already deleted the lines
QAudioFormat format; format.setSampleRate(8000); format.setChannelCount(1); format.setSampleFormat(QAudioFormat::UInt8); QAudioSink* audio = new QAudioSink(format, this);
Now it compiles and sends "something" to the default audio device. However, I just hear a crackle, and the program crashes afterwards. But at least one step ahead ...
-
@Cobra91151 It gives this compilation error: 'audio' was not declared in this scope; did you mean 'QAudio'?
It's
audio
object from your code above:QAudioSink *audio = new QAudioSink(format, this);
WhatQt 6
version do you use? Because I gotqt.multimedia.audiooutput: Failed to set up resampler
issue, which is well known bug: https://bugreports.qt.io/browse/QTBUG-108383. This issue has been fixed inQt 6.4.3
.
So, I recommend check out the console output for the same issue? -
@Cobra91151 Sorry, I forgot that I had already deleted the lines
QAudioFormat format; format.setSampleRate(8000); format.setChannelCount(1); format.setSampleFormat(QAudioFormat::UInt8); QAudioSink* audio = new QAudioSink(format, this);
Now it compiles and sends "something" to the default audio device. However, I just hear a crackle, and the program crashes afterwards. But at least one step ahead ...
Then check out your program using the debugger to find out what causes the crash (debug mode only). It should point you to the right code section.
By the way, include this code in main function:
#ifdef QT_DEBUG qputenv("QT_FATAL_WARNINGS", "1"); qputenv("QT_MESSAGE_PATTERN", "Type: %{type}\nProduct Name: %{appname}\nFile: %{file}\nLine: %{line}\nMethod: %{function}\nThreadID: %{threadid}\nThreadPtr: %{qthreadptr}\nMessage: %{message}"); #endif
Then run the debugger. It should provide more details about this issue.
-
Then check out your program using the debugger to find out what causes the crash (debug mode only). It should point you to the right code section.
By the way, include this code in main function:
#ifdef QT_DEBUG qputenv("QT_FATAL_WARNINGS", "1"); qputenv("QT_MESSAGE_PATTERN", "Type: %{type}\nProduct Name: %{appname}\nFile: %{file}\nLine: %{line}\nMethod: %{function}\nThreadID: %{threadid}\nThreadPtr: %{qthreadptr}\nMessage: %{message}"); #endif
Then run the debugger. It should provide more details about this issue.
@Cobra91151 With these parameters, the wav file is correctly played. However, the program still crashes immediately after playing is finished.
#include <QSoundEffect> #include <QAudioOutput> #include <QMediaDevices> #include <QAudioDevice> #include <QAudioSink> #include <QCoreApplication> #include <QDir> { QAudioDevice info(QMediaDevices::defaultAudioOutput()); QString homePath = QDir::homePath(); QAudioFormat format; format.setSampleRate(48000); format.setChannelCount(1); format.setSampleFormat(QAudioFormat::Int16); QAudioSink* audio = new QAudioSink(format, this); connect(audio, SIGNAL(stateChanged(QAudio::State)), this, SLOT(handleStateChanged(QAudio::State))); if (play_) { QFile *effectFile = new QFile(this); effectFile->setFileName(QString("%1/%2").arg(homePath, "/sounds/1.wav")); qDebug() << "Is file exists: " << effectFile->exists() << " | " << effectFile->fileName(); effectFile->open(QIODevice::ReadOnly); audio->start(effectFile); effectFile->close(); effectFile->deleteLater(); //removes the QFile object, or call - delete effectFile } }
-
@Cobra91151 With these parameters, the wav file is correctly played. However, the program still crashes immediately after playing is finished.
#include <QSoundEffect> #include <QAudioOutput> #include <QMediaDevices> #include <QAudioDevice> #include <QAudioSink> #include <QCoreApplication> #include <QDir> { QAudioDevice info(QMediaDevices::defaultAudioOutput()); QString homePath = QDir::homePath(); QAudioFormat format; format.setSampleRate(48000); format.setChannelCount(1); format.setSampleFormat(QAudioFormat::Int16); QAudioSink* audio = new QAudioSink(format, this); connect(audio, SIGNAL(stateChanged(QAudio::State)), this, SLOT(handleStateChanged(QAudio::State))); if (play_) { QFile *effectFile = new QFile(this); effectFile->setFileName(QString("%1/%2").arg(homePath, "/sounds/1.wav")); qDebug() << "Is file exists: " << effectFile->exists() << " | " << effectFile->fileName(); effectFile->open(QIODevice::ReadOnly); audio->start(effectFile); effectFile->close(); effectFile->deleteLater(); //removes the QFile object, or call - delete effectFile } }
I see your
QAudioSink *audio
is initialized but never deleted, this could be a memory leak which could lead to crash.Code:
if (play_) { QFile *effectFile = new QFile(this); effectFile->setFileName(QString("%1/%2").arg(homePath, "/sounds/1.wav")); qDebug() << "Is file exists: " << effectFile->exists() << " | " << effectFile->fileName(); effectFile->open(QIODevice::ReadOnly); audio->start(effectFile); effectFile->close(); effectFile->deleteLater(); //removes the QFile object, or call - delete effectFile } audio->deleteLater(); // removes QAudioSink object
Please, check out my post above regarding using the debugger.
-
I see your
QAudioSink *audio
is initialized but never deleted, this could be a memory leak which could lead to crash.Code:
if (play_) { QFile *effectFile = new QFile(this); effectFile->setFileName(QString("%1/%2").arg(homePath, "/sounds/1.wav")); qDebug() << "Is file exists: " << effectFile->exists() << " | " << effectFile->fileName(); effectFile->open(QIODevice::ReadOnly); audio->start(effectFile); effectFile->close(); effectFile->deleteLater(); //removes the QFile object, or call - delete effectFile } audio->deleteLater(); // removes QAudioSink object
Please, check out my post above regarding using the debugger.
@Cobra91151 Ok, the last two lines are breaking it.
effectFile->close();
leads to an abrupt end of the played sound. And this line causes the program crashs:
effectFile->deleteLater(); //removes the QFile object, or call - delete effectFile
I will now try to consolidate my code. (It was just a simplified example I presented here.) Let's see if I can put the entire definition of audio device, format and so on somewhere else where it is only called once after program start.
Anyway, many thanks for your help, Cobra91151!
-
@Cobra91151 Ok, the last two lines are breaking it.
effectFile->close();
leads to an abrupt end of the played sound. And this line causes the program crashs:
effectFile->deleteLater(); //removes the QFile object, or call - delete effectFile
I will now try to consolidate my code. (It was just a simplified example I presented here.) Let's see if I can put the entire definition of audio device, format and so on somewhere else where it is only called once after program start.
Anyway, many thanks for your help, Cobra91151!
It could close the file when audio is still running. It was just an example how to work with
QFile
. So, I recommend you to close and delete the file after the audio is finished. Happy coding! -
It could close the file when audio is still running. It was just an example how to work with
QFile
. So, I recommend you to close and delete the file after the audio is finished. Happy coding!@Cobra91151 Yes, that seems to be necessary. Because in reality there are up to 12 such wav files played in time cascade under certain conditions. I've set a command to close the audio sink at the end of this cascade to prevent a memory leak. Meanwhile, this is working pretty well.
However, I noticed, that the virtual memory slightly increases each time a wav file is loaded. Please tell me: what is the right command to close and delete such a played file? Because as written before, the two lineseffectFile->close(); effectFile->deleteLater();
do not work in my case.
-
@Cobra91151 Yes, that seems to be necessary. Because in reality there are up to 12 such wav files played in time cascade under certain conditions. I've set a command to close the audio sink at the end of this cascade to prevent a memory leak. Meanwhile, this is working pretty well.
However, I noticed, that the virtual memory slightly increases each time a wav file is loaded. Please tell me: what is the right command to close and delete such a played file? Because as written before, the two lineseffectFile->close(); effectFile->deleteLater();
do not work in my case.
@Urbi I have now put all the close and delete commands at the end of the cascade and all the
QFile *effect123 = new QFile(this);
definitions at the beginning. This way the program doesn't crash anymore with the two lines from above. BUT: The observed memory leak effect is still the same. Why?
audio->deleteLater();
is set, and also for each effect file
effect123->close(); effect123->deleteLater();
I have found a number of similar reports on the Internet, but so far I've not seen any solution. Any idea?
-
@Urbi I have now put all the close and delete commands at the end of the cascade and all the
QFile *effect123 = new QFile(this);
definitions at the beginning. This way the program doesn't crash anymore with the two lines from above. BUT: The observed memory leak effect is still the same. Why?
audio->deleteLater();
is set, and also for each effect file
effect123->close(); effect123->deleteLater();
I have found a number of similar reports on the Internet, but so far I've not seen any solution. Any idea?
Hello!
The issue with memory leak could be that you create multiple
QFile
objects on the heap and remove only some of them. Or you have other objects which are still not deleted. You need a better object management depending on your current project or use the smart pointers (https://wiki.qt.io/Smart_Pointers). As fordeleteLater()
method it does not remove it immediately, it's only schedules this object for deletion, which is recommended byQt
to usedeleteLater()
instead ofdelete
. You can use for example:delete someFileObj;
to clear the resources immediately. Also, you can make some objects as class member variables and remove them in the destructor, you can reuse one object for several files etc. Additionally, please, check out the docs for more details aboutdeleteLater()
method: https://doc.qt.io/qt-6/qobject.html#deleteLater