From e594efdbbc9dd0e0e88eb4eb9d5a49a622a2fc83 Mon Sep 17 00:00:00 2001 From: Phil Taylor Date: Fri, 17 Feb 2023 14:30:22 +0000 Subject: [PATCH] Try to stop crash if there is no default audio device --- cwsender.cpp | 14 ++++------ cwsidetone.cpp | 74 ++++++++++++++++++++++++++------------------------ 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/cwsender.cpp b/cwsender.cpp index bfbd024..ea03702 100644 --- a/cwsender.cpp +++ b/cwsender.cpp @@ -38,10 +38,6 @@ cwSender::cwSender(QWidget *parent) : [=](const unsigned char& pitch) { tone->setFrequency(pitch); }); connect(this, &cwSender::setLevel, [=](const unsigned char& level) { tone->setLevel(level); }); - - connect( this, SIGNAL( pitchChanged(int) ), ui->pitchSpin, SLOT( setValue(int) ) ); - connect( this, SIGNAL( dashChanged(int) ), ui->dashSpin, SLOT( setValue(int) ) ); - connect( this, SIGNAL( wpmChanged(int) ), ui->wpmSpin, SLOT( setValue(int) ) ); } cwSender::~cwSender() @@ -70,7 +66,7 @@ void cwSender::handleKeySpeed(unsigned char wpm) if (wpm != ui->wpmSpin->value() && (wpm >= ui->wpmSpin->minimum()) && (wpm <= ui->wpmSpin->maximum())) { ui->wpmSpin->blockSignals(true); - emit wpmChanged((int)wpm); + QMetaObject::invokeMethod(ui->wpmSpin, "setValue", Qt::QueuedConnection, Q_ARG(int, wpm)); ui->wpmSpin->blockSignals(false); #if (QT_VERSION >= QT_VERSION_CHECK(5,10,0)) QMetaObject::invokeMethod(tone, [=]() { @@ -88,7 +84,7 @@ void cwSender::handleDashRatio(unsigned char ratio) if (calc != ui->dashSpin->value() && (calc >= ui->dashSpin->minimum()) && (ratio <= ui->dashSpin->maximum())) { ui->dashSpin->blockSignals(true); - emit dashChanged(calc); + QMetaObject::invokeMethod(ui->dashSpin, "setValue", Qt::QueuedConnection, Q_ARG(double, calc)); ui->dashSpin->blockSignals(false); #if (QT_VERSION >= QT_VERSION_CHECK(5,10,0)) QMetaObject::invokeMethod(tone, [=]() { @@ -105,7 +101,7 @@ void cwSender::handlePitch(unsigned char pitch) { if (cwPitch != ui->pitchSpin->value() && cwPitch >= ui->pitchSpin->minimum() && cwPitch <= ui->pitchSpin->maximum()) { ui->pitchSpin->blockSignals(true); - emit pitchChanged(cwPitch); + QMetaObject::invokeMethod(ui->pitchSpin, "setValue", Qt::QueuedConnection, Q_ARG(int, cwPitch)); ui->pitchSpin->blockSignals(false); #if (QT_VERSION >= QT_VERSION_CHECK(5,10,0)) QMetaObject::invokeMethod(tone, [=]() { @@ -313,7 +309,7 @@ void cwSender::runMacroButton(int buttonNumber) outText = macroText[buttonNumber].arg(sequenceNumber, 3, 10, QChar('0')); sequenceNumber++; ui->sequenceSpin->blockSignals(true); - ui->sequenceSpin->setValue(sequenceNumber); + QMetaObject::invokeMethod(ui->sequenceSpin, "setValue", Qt::QueuedConnection, Q_ARG(int, sequenceNumber)); ui->sequenceSpin->blockSignals(false); } else { outText = macroText[buttonNumber]; @@ -435,7 +431,7 @@ void cwSender::setSidetoneEnable(bool val) void cwSender::setSidetoneLevel(int val) { - ui->sidetoneLevelSlider->setValue(val); + QMetaObject::invokeMethod(ui->sidetoneLevelSlider, "setValue", Qt::QueuedConnection, Q_ARG(int, val)); } QStringList cwSender::getMacroText() diff --git a/cwsidetone.cpp b/cwsidetone.cpp index c778e92..815c61b 100644 --- a/cwsidetone.cpp +++ b/cwsidetone.cpp @@ -92,51 +92,55 @@ void cwSidetone::init() format.setSampleFormat(QAudioFormat::Int16); QAudioDevice device = QMediaDevices::defaultAudioOutput(); #endif - - if (!device.isFormatSupported(format)) { - qWarning(logCW()) << "Default format not supported, using preferred"; - format = device.preferredFormat(); - } + if (device.isDefault()) { + if (!device.isFormatSupported(format)) { + qWarning(logCW()) << "Default format not supported, using preferred"; + format = device.preferredFormat(); + } #if (QT_VERSION < QT_VERSION_CHECK(6,0,0)) - output = new QAudioOutput(device,format); + output = new QAudioOutput(device,format); #else - output = new QAudioSink(device,format); + output = new QAudioSink(device,format); #endif - output->setVolume((qreal)volume/100.0); + + output->setVolume((qreal)volume/100.0); + } } void cwSidetone::send(QString text) { - text=text.simplified(); - buffer.clear(); - QString currentChar; - int pos = 0; - while (pos < text.size()) - { - QChar ch = text.at(pos).toUpper(); - if (ch == NULL) + if (output != Q_NULLPTR) { + text=text.simplified(); + buffer.clear(); + QString currentChar; + int pos = 0; + while (pos < text.size()) { - currentChar = cwTable[' ']; + QChar ch = text.at(pos).toUpper(); + if (ch == NULL) + { + currentChar = cwTable[' ']; + } + else if (this->cwTable.contains(ch)) + { + currentChar = cwTable[ch]; + } + else + { + currentChar=cwTable['?']; + } + generateMorse(currentChar); + pos++; } - else if (this->cwTable.contains(ch)) - { - currentChar = cwTable[ch]; - } - else - { - currentChar=cwTable['?']; - } - generateMorse(currentChar); - pos++; - } - outputDevice = output->start(); - if (outputDevice) { - qint64 written = outputDevice->write(buffer); - while (written < buffer.size()) - { - written += outputDevice->write(buffer.data()+written, buffer.size() - written); - QApplication::processEvents(); + outputDevice = output->start(); + if (outputDevice != Q_NULLPTR) { + qint64 written = outputDevice->write(buffer); + while (written < buffer.size()) + { + written += outputDevice->write(buffer.data()+written, buffer.size() - written); + QApplication::processEvents(); + } } } //qInfo(logCW()) << "Sending" << this->currentChar;