mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-05-05 17:55:45 +08:00
Ref T730, improved threaded tone player
* allow to re-init and get device info * mutable mutex
This commit is contained in:
committed by
Mat Sutcliffe
parent
90e87835fc
commit
5d9ea83b93
@@ -36,18 +36,39 @@ namespace BlackSound
|
|||||||
QTimer::singleShot(0, this, &CThreadedTonePairPlayer::playBuffer);
|
QTimer::singleShot(0, this, &CThreadedTonePairPlayer::playBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CThreadedTonePairPlayer::reinitializeAudio(const CAudioDeviceInfo &device)
|
||||||
|
{
|
||||||
|
if (this->getAudioDevice() == device) { return false; }
|
||||||
|
{
|
||||||
|
QMutexLocker ml(&m_mutex);
|
||||||
|
m_deviceInfo = device;
|
||||||
|
}
|
||||||
|
this->initialize();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
CAudioDeviceInfo CThreadedTonePairPlayer::getAudioDevice() const
|
||||||
|
{
|
||||||
|
QMutexLocker ml(&m_mutex);
|
||||||
|
return m_deviceInfo;
|
||||||
|
}
|
||||||
|
|
||||||
void CThreadedTonePairPlayer::initialize()
|
void CThreadedTonePairPlayer::initialize()
|
||||||
{
|
{
|
||||||
|
QMutexLocker ml(&m_mutex);
|
||||||
CLogMessage(this).info(u"CThreadedTonePairPlayer for device '%1'") << m_deviceInfo.getName();
|
CLogMessage(this).info(u"CThreadedTonePairPlayer for device '%1'") << m_deviceInfo.getName();
|
||||||
|
|
||||||
m_audioFormat.setSampleRate(44100);
|
QAudioFormat format;
|
||||||
m_audioFormat.setChannelCount(1);
|
format.setSampleRate(44100);
|
||||||
m_audioFormat.setSampleSize(16); // 8 or 16 works
|
format.setChannelCount(1);
|
||||||
m_audioFormat.setCodec("audio/pcm");
|
format.setSampleSize(16); // 8 or 16 works
|
||||||
m_audioFormat.setByteOrder(QAudioFormat::LittleEndian);
|
format.setCodec("audio/pcm");
|
||||||
m_audioFormat.setSampleType(QAudioFormat::SignedInt);
|
format.setByteOrder(QAudioFormat::LittleEndian);
|
||||||
|
format.setSampleType(QAudioFormat::SignedInt);
|
||||||
|
|
||||||
QAudioDeviceInfo selectedDevice = getHighestCompatibleOutputDevice(m_deviceInfo, m_audioFormat);
|
// find best device
|
||||||
|
const QAudioDeviceInfo selectedDevice = getHighestCompatibleOutputDevice(m_deviceInfo, format);
|
||||||
|
m_audioFormat = format;
|
||||||
m_audioOutput = new QAudioOutput(selectedDevice, m_audioFormat, this);
|
m_audioOutput = new QAudioOutput(selectedDevice, m_audioFormat, this);
|
||||||
connect(m_audioOutput, &QAudioOutput::stateChanged, this, &CThreadedTonePairPlayer::handleStateChanged);
|
connect(m_audioOutput, &QAudioOutput::stateChanged, this, &CThreadedTonePairPlayer::handleStateChanged);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,12 +39,17 @@ namespace BlackSound
|
|||||||
//! Destructor
|
//! Destructor
|
||||||
virtual ~CThreadedTonePairPlayer() override;
|
virtual ~CThreadedTonePairPlayer() override;
|
||||||
|
|
||||||
public slots:
|
|
||||||
//! Play the list of tones.
|
//! Play the list of tones.
|
||||||
//! If the player is currently active, this call will be ignored.
|
//! If the player is currently active, this call will be ignored.
|
||||||
void play(int volume, const QList<BlackSound::CTonePair> &tonePairs);
|
void play(int volume, const QList<BlackSound::CTonePair> &tonePairs);
|
||||||
|
|
||||||
protected slots:
|
//! Reinitialize audio
|
||||||
|
bool reinitializeAudio(const BlackMisc::Audio::CAudioDeviceInfo &device);
|
||||||
|
|
||||||
|
//! Used audio device
|
||||||
|
BlackMisc::Audio::CAudioDeviceInfo getAudioDevice() const;
|
||||||
|
|
||||||
|
protected:
|
||||||
//! \copydoc BlackMisc::CContinuousWorker::initialize
|
//! \copydoc BlackMisc::CContinuousWorker::initialize
|
||||||
virtual void initialize() override;
|
virtual void initialize() override;
|
||||||
|
|
||||||
@@ -65,10 +70,10 @@ namespace BlackSound
|
|||||||
QAudioOutput *m_audioOutput = nullptr;
|
QAudioOutput *m_audioOutput = nullptr;
|
||||||
QByteArray m_bufferData;
|
QByteArray m_bufferData;
|
||||||
QBuffer m_buffer;
|
QBuffer m_buffer;
|
||||||
QMutex m_mutex { QMutex::Recursive };
|
|
||||||
QAudioFormat m_audioFormat;
|
QAudioFormat m_audioFormat;
|
||||||
QMap<CTonePair, QByteArray> m_tonePairCache;
|
QMap<CTonePair, QByteArray> m_tonePairCache;
|
||||||
|
mutable QMutex m_mutex { QMutex::Recursive };
|
||||||
};
|
};
|
||||||
}
|
} // ns
|
||||||
|
|
||||||
#endif // guard
|
#endif // guard
|
||||||
|
|||||||
Reference in New Issue
Block a user