Skip to content

Commit

Permalink
Audio Output: Fix Mute
Browse files Browse the repository at this point in the history
  • Loading branch information
HTRamsey committed Apr 17, 2024
1 parent 9be4498 commit 19be931
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
43 changes: 23 additions & 20 deletions src/Audio/AudioOutput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
****************************************************************************/

#include "AudioOutput.h"
#include "QGCLoggingCategory.h"

#include <QtCore/QRegularExpression>

#define MAX_TEXT_QUEUE_SIZE 20U

Q_LOGGING_CATEGORY( AudioOutputLog, "qgc.audio.audiooutput" );
QGC_LOGGING_CATEGORY( AudioOutputLog, "qgc.audio.audiooutput" );
// qt.speech.tts.flite
// qt.speech.tts.android

Expand Down Expand Up @@ -47,48 +48,50 @@ AudioOutput* AudioOutput::instance()
AudioOutput::AudioOutput( QObject* parent )
: QTextToSpeech( QStringLiteral("none"), parent )
{
#ifdef QT_DEBUG
( void ) connect( this, &QTextToSpeech::stateChanged, []( QTextToSpeech::State state ) {
qCInfo( AudioOutputLog ) << Q_FUNC_INFO << "State:" << state;
});
( void ) connect( this, &QTextToSpeech::errorOccurred, []( QTextToSpeech::ErrorReason reason, const QString &errorString ) {
( void ) reason;
qCInfo( AudioOutputLog ) << Q_FUNC_INFO << "Error:" << errorString;
});
#endif
( void ) connect( this, &QTextToSpeech::stateChanged, []( QTextToSpeech::State state ) {
qCDebug( AudioOutputLog ) << Q_FUNC_INFO << "State:" << state;
});
( void ) connect( this, &QTextToSpeech::errorOccurred, []( QTextToSpeech::ErrorReason reason, const QString &errorString ) {
qCDebug( AudioOutputLog ) << Q_FUNC_INFO << "Error: (" << reason << ") " << errorString;
});
( void ) connect( this, &QTextToSpeech::volumeChanged, []( double volume ) {
qCDebug( AudioOutputLog ) << Q_FUNC_INFO << "volume:" << volume;
});

if ( !QTextToSpeech::availableEngines().isEmpty() ) {
if ( setEngine( QString() ) ) { // Autoselect engine by priority
qCDebug( AudioOutputLog ) << Q_FUNC_INFO << "engine:" << engine();
if ( availableLocales().contains( QLocale( "en_US" ) ) ) {
setLocale( QLocale( "en_US" ) );
}

( void ) connect( this, &QTextToSpeech::volumeChanged, [ this ]( double volume ) {
( void ) volume;
const bool muted = isMuted();
if ( muted != m_lastMuted ) {
emit mutedChanged( muted );
m_lastMuted = muted;
}
( void ) connect( this, &AudioOutput::mutedChanged, [ this ]( bool muted ) {
qCDebug( AudioOutputLog ) << Q_FUNC_INFO << "muted:" << muted;
( void ) QMetaObject::invokeMethod( this, "setVolume", Qt::AutoConnection, muted ? 0. : 1. );
});
}
}
}

bool AudioOutput::isMuted() const
{
return qFuzzyIsNull( volume() );
return m_muted;
}

void AudioOutput::setMuted( bool enable )
{
if ( enable != isMuted() ) {
( void ) QMetaObject::invokeMethod( this, "setVolume", Qt::AutoConnection, enable ? 0. : 100. );
m_muted = enable;
emit mutedChanged( m_muted );
}
}

void AudioOutput::say( const QString& text, AudioOutput::TextMods textMods )
void AudioOutput::read( const QString& text, AudioOutput::TextMods textMods )
{
if( m_muted ) {
return;
}

if ( !engineCapabilities().testFlag( QTextToSpeech::Capability::Speak ) ) {
qCWarning( AudioOutputLog ) << Q_FUNC_INFO << "Speech Not Supported:" << text;
return;
Expand Down
4 changes: 2 additions & 2 deletions src/Audio/AudioOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class AudioOutput : public QTextToSpeech
bool isMuted() const;
void setMuted( bool enable );

void say( const QString& text, AudioOutput::TextMods textMods = TextMod::None );
void read( const QString& text, AudioOutput::TextMods textMods = TextMod::None );

static AudioOutput* instance();
static bool getMillisecondString( const QString& string, QString& match, int& number );
Expand All @@ -47,7 +47,7 @@ class AudioOutput : public QTextToSpeech

private:
qsizetype m_textQueueSize = 0;
bool m_lastMuted = false;
bool m_muted = false;
static const QHash<QString, QString> s_textHash;
};
Q_DECLARE_OPERATORS_FOR_FLAGS( AudioOutput::TextMods )
1 change: 1 addition & 0 deletions src/QGCApplication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ bool QGCApplication::_initForNormalAppBoot()
{
AudioOutput::instance()->setMuted( value.toBool() );
});
AudioOutput::instance()->setMuted( toolbox()->settingsManager()->appSettings()->audioMuted()->rawValue().toBool() );

_qmlAppEngine = toolbox()->corePlugin()->createQmlApplicationEngine(this);
toolbox()->corePlugin()->createRootWindow(_qmlAppEngine);
Expand Down
2 changes: 1 addition & 1 deletion src/Vehicle/Vehicle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2511,7 +2511,7 @@ void Vehicle::virtualTabletJoystickValue(double roll, double pitch, double yaw,

void Vehicle::_say(const QString& text)
{
AudioOutput::instance()->say(text.toLower());
AudioOutput::instance()->read(text.toLower());
}

bool Vehicle::airship() const
Expand Down

0 comments on commit 19be931

Please sign in to comment.