swift
threadedtonepairplayer.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2016 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #ifndef SWIFT_SOUND_THREADEDTONEPAIRPLAYER_H
7 #define SWIFT_SOUND_THREADEDTONEPAIRPLAYER_H
8 
9 #include <QAudioFormat>
10 #include <QAudioSink>
11 #include <QBuffer>
12 #include <QMap>
13 #include <QReadWriteLock>
14 #include <QtEndian>
15 #include <QtGlobal>
16 
18 #include "misc/worker.h"
19 #include "sound/swiftsoundexport.h"
20 #include "sound/tonepair.h"
21 
22 class QTimer;
23 
24 namespace swift::sound
25 {
28  {
29  Q_OBJECT
30 
31  public:
33  CThreadedTonePairPlayer(QObject *owner, const QString &name,
35 
37  virtual ~CThreadedTonePairPlayer() override = default;
38 
41  void play(int volume, const QList<swift::sound::CTonePair> &tonePairs);
42 
44  bool reinitializeAudio(const swift::misc::audio::CAudioDeviceInfo &device);
45 
47  swift::misc::audio::CAudioDeviceInfo getAudioDevice() const;
48 
49  protected:
51  virtual void initialize() override;
52 
54  virtual void beforeQuit() noexcept override;
55 
56  private:
57  void handleStateChanged(QAudio::State newState);
58  void playBuffer();
59  QByteArray getAudioByTonePairs(const QList<CTonePair> &tonePairs);
60  QByteArray generateAudioFromTonePairs(const CTonePair &tonePair);
61 
67  void writeAmplitudeToBuffer(double amplitude, unsigned char *bufferPointer);
68 
69  swift::misc::audio::CAudioDeviceInfo m_deviceInfo;
70  QAudioSink *m_audioOutput = nullptr;
71  QByteArray m_bufferData;
72  QBuffer m_buffer;
73  QAudioFormat m_audioFormat;
74  QMap<CTonePair, QByteArray> m_tonePairCache;
75  mutable QRecursiveMutex m_mutex;
76  };
77 } // namespace swift::sound
78 
79 #endif // SWIFT_SOUND_THREADEDTONEPAIRPLAYER_H
Base class for a long-lived worker object which lives in its own thread.
Definition: worker.h:275
Value object encapsulating information of a audio device.
Threaded tone player. Don't use it directly but use.
virtual ~CThreadedTonePairPlayer()=default
Destructor.
Tone pair to be played.
Definition: tonepair.h:20
#define SWIFT_SOUND_EXPORT
Export a class or function from the library.