Overte C++ Documentation
AudioDevices.h
1 //
2 // AudioDevices.h
3 // interface/src/scripting
4 //
5 // Created by Zach Pomerantz on 28/5/2017.
6 // Copyright 2017 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_scripting_AudioDevices_h
13 #define hifi_scripting_AudioDevices_h
14 
15 #include <memory>
16 #include <mutex>
17 
18 #include <QObject>
19 #include <QAbstractListModel>
20 #include <QAudioDeviceInfo>
21 
22 #include <HifiAudioDeviceInfo.h>
23 
24 namespace scripting {
25 
26 class AudioDevice {
27 public:
28  HifiAudioDeviceInfo info;
29  QString display;
30  bool selectedDesktop { false };
31  bool selectedHMD { false };
32  QString type;
33 };
34 
35 class AudioDeviceList : public QAbstractListModel {
36  Q_OBJECT
37 
38 public:
39  AudioDeviceList(QAudio::Mode mode = QAudio::AudioOutput);
40  virtual ~AudioDeviceList();
41 
42  virtual std::shared_ptr<AudioDevice> newDevice(const AudioDevice& device)
43  { return std::make_shared<AudioDevice>(device); }
44 
45  int rowCount(const QModelIndex& parent = QModelIndex()) const override { Q_UNUSED(parent); return _devices.size(); }
46  QHash<int, QByteArray> roleNames() const override { return _roles; }
47  Qt::ItemFlags flags(const QModelIndex& index) const override { return _flags; }
48 
49  // get/set devices through a QML ListView
50  QVariant data(const QModelIndex& index, int role) const override;
51 
52  // reset device to the last selected device in this context, or the default
53  void resetDevice(bool contextIsHMD);
54 
55 signals:
56  void deviceChanged(const HifiAudioDeviceInfo& device);
57  void selectedDevicePlugged(const HifiAudioDeviceInfo& device, bool isHMD);
58 
59 protected slots:
60  void onDeviceChanged(const HifiAudioDeviceInfo& device, bool isHMD);
61  void onDevicesChanged(QAudio::Mode mode, const QList<HifiAudioDeviceInfo>& devices);
62 
63 protected:
64  friend class AudioDevices;
65 
66  static QHash<int, QByteArray> _roles;
67  static Qt::ItemFlags _flags;
68  const QAudio::Mode _mode;
69  HifiAudioDeviceInfo _selectedDesktopDevice;
70  HifiAudioDeviceInfo _selectedHMDDevice;
71  QString _backupSelectedDesktopDeviceName;
72  QString _backupSelectedHMDDeviceName;
73  QList<std::shared_ptr<AudioDevice>> _devices;
74  QString _hmdSavedDeviceName;
75  QString _desktopSavedDeviceName;
76 };
77 
78 class AudioInputDevice : public AudioDevice {
79 public:
80  AudioInputDevice(const AudioDevice& device) : AudioDevice(device) {}
81  float peak { 0.0f };
82 };
83 
84 class AudioInputDeviceList : public AudioDeviceList {
85  Q_OBJECT
86  Q_PROPERTY(bool peakValuesAvailable READ peakValuesAvailable CONSTANT)
87  Q_PROPERTY(bool peakValuesEnabled READ peakValuesEnabled WRITE setPeakValuesEnabled NOTIFY peakValuesEnabledChanged)
88 
89 public:
90  AudioInputDeviceList() : AudioDeviceList(QAudio::AudioInput) {}
91  virtual ~AudioInputDeviceList() = default;
92 
93  virtual std::shared_ptr<AudioDevice> newDevice(const AudioDevice& device) override
94  { return std::make_shared<AudioInputDevice>(device); }
95 
96  QVariant data(const QModelIndex& index, int role) const override;
97 
98 signals:
99  void peakValuesEnabledChanged(bool enabled);
100 
101 protected slots:
102  void onPeakValueListChanged(const QList<float>& peakValueList);
103 
104 protected:
105  friend class AudioDevices;
106 
107  bool peakValuesAvailable();
108  std::once_flag _peakFlag;
109  bool _peakValuesAvailable;
110 
111  bool peakValuesEnabled() const { return _peakValuesEnabled; }
112  void setPeakValuesEnabled(bool enable);
113  bool _peakValuesEnabled { false };
114 };
115 class Audio;
116 
117 class AudioDevices : public QObject {
118  Q_OBJECT
119  Q_PROPERTY(AudioInputDeviceList* input READ getInputList NOTIFY nop)
120  Q_PROPERTY(AudioDeviceList* output READ getOutputList NOTIFY nop)
121 
122 public:
123  AudioDevices(bool& contextIsHMD);
124  virtual ~AudioDevices();
125 
126 signals:
127  void nop();
128 
129 private slots:
130  void chooseInputDevice(const HifiAudioDeviceInfo& device, bool isHMD);
131  void chooseOutputDevice(const HifiAudioDeviceInfo& device, bool isHMD);
132 
133  void onContextChanged(const QString& context);
134  void onDeviceSelected(QAudio::Mode mode, const HifiAudioDeviceInfo& device,
135  const HifiAudioDeviceInfo& previousDevice, bool isHMD);
136  void onDeviceChanged(QAudio::Mode mode, const HifiAudioDeviceInfo& device);
137  void onDevicesChanged(QAudio::Mode mode, const QList<HifiAudioDeviceInfo>& devices);
138 
139 private:
140  friend class Audio;
141 
142  AudioInputDeviceList* getInputList() { return &_inputs; }
143  AudioDeviceList* getOutputList() { return &_outputs; }
144 
145  AudioInputDeviceList _inputs;
146  AudioDeviceList _outputs { QAudio::AudioOutput };
147  HifiAudioDeviceInfo _requestedOutputDevice;
148  HifiAudioDeviceInfo _requestedInputDevice;
149 
150  const bool& _contextIsHMD;
151 };
152 
153 };
154 
155 #endif // hifi_scripting_AudioDevices_h