Overte C++ Documentation
AudioScope.h
1 //
2 // AudioScope.h
3 // interace/src/audio
4 //
5 // Created by Stephen Birarda on 2014-12-16.
6 // Copyright 2014 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_AudioScope_h
13 #define hifi_AudioScope_h
14 
15 #include <glm/glm.hpp>
16 
17 #include <QByteArray>
18 #include <QObject>
19 
20 #include <DependencyManager.h>
21 #include <gpu/Batch.h>
22 
23 
24 class AudioScope : public QObject, public Dependency {
25  Q_OBJECT
26  SINGLETON_DEPENDENCY
27 
28  /*@jsdoc
29  * The <code>AudioScope</code> API provides facilities for an audio scope.
30  *
31  * @namespace AudioScope
32  *
33  * @deprecated This API doesn't work properly. It is deprecated and will be removed.
34  *
35  * @hifi-interface
36  * @hifi-client-entity
37  * @hifi-avatar
38  *
39  * @property {number[]} scopeInput - Scope input. <em>Read-only.</em>
40  * @property {number[]} scopeOutputLeft - Scope left output. <em>Read-only.</em>
41  * @property {number[]} scopeOutputRight - Scope right output. <em>Read-only.</em>
42  * @property {number[]} triggerInput - Trigger input. <em>Read-only.</em>
43  * @property {number[]} triggerOutputLeft - Trigger left output. <em>Read-only.</em>
44  * @property {number[]} triggerOutputRight - Trigger right output. <em>Read-only.</em>
45  */
46 
47  Q_PROPERTY(QVector<int> scopeInput READ getScopeInput)
48  Q_PROPERTY(QVector<int> scopeOutputLeft READ getScopeOutputLeft)
49  Q_PROPERTY(QVector<int> scopeOutputRight READ getScopeOutputRight)
50 
51  Q_PROPERTY(QVector<int> triggerInput READ getTriggerInput)
52  Q_PROPERTY(QVector<int> triggerOutputLeft READ getTriggerOutputLeft)
53  Q_PROPERTY(QVector<int> triggerOutputRight READ getTriggerOutputRight)
54 
55 public:
56  // Audio scope methods for allocation/deallocation
57  void allocateScope();
58  void freeScope();
59  void reallocateScope(int frames);
60 
61 public slots:
62 
63  /*@jsdoc
64  * Toggle.
65  * @function AudioScope.toggle
66  */
67  void toggle() { setVisible(!_isEnabled); }
68 
69  /*@jsdoc
70  * Set visible.
71  * @function AudioScope.setVisible
72  * @param {boolean} visible - Visible.
73  */
74  void setVisible(bool visible);
75 
76  /*@jsdoc
77  * Get visible.
78  * @function AudioScope.getVisible
79  * @returns {boolean} Visible.
80  */
81  bool getVisible() const { return _isEnabled; }
82 
83  /*@jsdoc
84  * Toggle pause.
85  * @function AudioScope.togglePause
86  */
87  void togglePause() { setPause(!_isPaused); }
88 
89  /*@jsdoc
90  * Set pause.
91  * @function AudioScope.setPause
92  * @param {boolean} pause - Pause.
93  */
94  void setPause(bool paused) { _isPaused = paused; emit pauseChanged(); }
95 
96  /*@jsdoc
97  * Get pause.
98  * @function AudioScope.getPause
99  * @returns {boolean} Pause.
100  */
101  bool getPause() { return _isPaused; }
102 
103  /*@jsdoc
104  * Toggle trigger.
105  * @function AudioScope.toggleTrigger
106  */
107  void toggleTrigger() { _autoTrigger = !_autoTrigger; }
108 
109  /*@jsdoc
110  * Get auto trigger.
111  * @function AudioScope.getAutoTrigger
112  * @returns {boolean} Auto trigger.
113  */
114  bool getAutoTrigger() { return _autoTrigger; }
115 
116  /*@jsdoc
117  * Set auto trigger.
118  * @function AudioScope.setAutoTrigger
119  * @param {boolean} autoTrigger - Auto trigger.
120  */
121  void setAutoTrigger(bool autoTrigger) { _isTriggered = false; _autoTrigger = autoTrigger; }
122 
123  /*@jsdoc
124  * Set trigger values.
125  * @function AudioScope.setTriggerValues
126  * @param {number} x - X.
127  * @param {number} y - Y.
128  */
129  void setTriggerValues(int x, int y) { _triggerValues.x = x; _triggerValues.y = y; }
130 
131  /*@jsdoc
132  * Set triggered.
133  * @function AudioScope.setTriggered
134  * @param {boolean} triggered - Triggered.
135  */
136  void setTriggered(bool triggered) { _isTriggered = triggered; }
137 
138  /*@jsdoc
139  * Get triggered.
140  * @function AudioScope.getTriggered
141  * @returns {boolean} Triggered.
142  */
143  bool getTriggered() { return _isTriggered; }
144 
145  /*@jsdoc
146  * Get frames per second.
147  * @function AudioScope.getFramesPerSecond
148  * @returns {number} Frames per second.
149  */
150  float getFramesPerSecond();
151 
152  /*@jsdoc
153  * Get frames per scope.
154  * @function AudioScope.getFramesPerScope
155  * @returns {number} Frames per scope.
156  */
157  int getFramesPerScope() { return _framesPerScope; }
158 
159  /*@jsdoc
160  * Select five frames audio scope.
161  * @function AudioScope.selectAudioScopeFiveFrames
162  */
163  void selectAudioScopeFiveFrames();
164 
165  /*@jsdoc
166  * Select twenty frames audio scope.
167  * @function AudioScope.selectAudioScopeTwentyFrames
168  */
169  void selectAudioScopeTwentyFrames();
170 
171  /*@jsdoc
172  * Select fifty frames audio scope.
173  * @function AudioScope.selectAudioScopeFiftyFrames
174  */
175  void selectAudioScopeFiftyFrames();
176 
177  /*@jsdoc
178  * Get scope input.
179  * @function AudioScope.getScopeInput
180  * @returns {number[]} Scope input.
181  */
182  QVector<int> getScopeInput() { return _scopeInputData; };
183 
184  /*@jsdoc
185  * Get scope left output.
186  * @function AudioScope.getScopeOutputLeft
187  * @returns {number[]} Scope left output.
188  */
189  QVector<int> getScopeOutputLeft() { return _scopeOutputLeftData; };
190 
191  /*@jsdoc
192  * Get scope right output.
193  * @function AudioScope.getScopeOutputRight
194  * @returns {number[]} Scope right output.
195  */
196  QVector<int> getScopeOutputRight() { return _scopeOutputRightData; };
197 
198  /*@jsdoc
199  * Get trigger input.
200  * @function AudioScope.getTriggerInput
201  * @returns {number[]} Trigger input.
202  */
203  QVector<int> getTriggerInput() { return _triggerInputData; };
204 
205  /*@jsdoc
206  * Get left trigger output.
207  * @function AudioScope.getTriggerOutputLeft
208  * @returns {number[]} Left trigger output.
209  */
210  QVector<int> getTriggerOutputLeft() { return _triggerOutputLeftData; };
211 
212  /*@jsdoc
213  * Get right trigger output.
214  * @function AudioScope.getTriggerOutputRight
215  * @returns {number[]} Right trigger output.
216  */
217  QVector<int> getTriggerOutputRight() { return _triggerOutputRightData; };
218 
219  /*@jsdoc
220  * Set local echo.
221  * @function AudioScope.setLocalEcho
222  * @parm {boolean} localEcho - Local echo.
223  */
224  void setLocalEcho(bool localEcho);
225 
226  /*@jsdoc
227  * Set server echo.
228  * @function AudioScope.setServerEcho
229  * @parm {boolean} serverEcho - Server echo.
230  */
231  void setServerEcho(bool serverEcho);
232 
233 signals:
234 
235  /*@jsdoc
236  * Triggered when pause changes.
237  * @function AudioScope.pauseChanged
238  * @returns {Signal}
239  */
240  void pauseChanged();
241 
242  /*@jsdoc
243  * Triggered when scope is triggered.
244  * @function AudioScope.triggered
245  * @returns {Signal}
246  */
247  void triggered();
248 
249 protected:
250  AudioScope();
251 
252 private slots:
253  void addStereoSilenceToScope(int silentSamplesPerChannel);
254  void addLastFrameRepeatedWithFadeToScope(int samplesPerChannel);
255  void addStereoSamplesToScope(const QByteArray& samples);
256  void addInputToScope(const QByteArray& inputSamples);
257 
258 private:
259 
260  // Audio scope methods for data acquisition
261  int addBufferToScope(QByteArray* byteArray, int frameOffset, const int16_t* source, int sourceSamples,
262  unsigned int sourceChannel, unsigned int sourceNumberOfChannels, float fade = 1.0f);
263  int addSilenceToScope(QByteArray* byteArray, int frameOffset, int silentSamples);
264 
265  QVector<int> getScopeVector(const QByteArray* scope, int offset);
266 
267  bool shouldTrigger(const QVector<int>& scope);
268  void computeInputData();
269  void computeOutputData();
270 
271  void storeTriggerValues();
272 
273  bool _isEnabled;
274  bool _isPaused;
275  bool _isTriggered;
276  bool _autoTrigger;
277  int _scopeInputOffset;
278  int _scopeOutputOffset;
279  int _framesPerScope;
280  int _samplesPerScope;
281 
282  QByteArray* _scopeInput;
283  QByteArray* _scopeOutputLeft;
284  QByteArray* _scopeOutputRight;
285  QByteArray _scopeLastFrame;
286 
287  QVector<int> _scopeInputData;
288  QVector<int> _scopeOutputLeftData;
289  QVector<int> _scopeOutputRightData;
290 
291  QVector<int> _triggerInputData;
292  QVector<int> _triggerOutputLeftData;
293  QVector<int> _triggerOutputRightData;
294 
295 
296  glm::ivec2 _triggerValues;
297 
298  int _audioScopeBackground;
299  int _audioScopeGrid;
300  int _inputID;
301  int _outputLeftID;
302  int _outputRightD;
303 };
304 
305 #endif // hifi_AudioScope_h