Overte C++ Documentation
ScriptAudioInjector.h
1 //
2 // ScriptAudioInjector.h
3 // libraries/audio/src
4 //
5 // Created by Stephen Birarda on 2015-02-11.
6 // Copyright 2015 High Fidelity, Inc.
7 // Copyright 2023 Overte e.V.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 // SPDX-License-Identifier: Apache-2.0
12 //
13 
16 
17 #ifndef hifi_ScriptAudioInjector_h
18 #define hifi_ScriptAudioInjector_h
19 
20 #include <QtCore/QObject>
21 #include <ScriptValue.h>
22 
23 #include "AudioInjectorManager.h"
24 
25 class ScriptEngine;
26 
27 /*@jsdoc
28  * Plays or "injects" the content of an audio file.
29  *
30  * <p>Create using {@link Audio} API methods.</p>
31  *
32  * @class AudioInjector
33  *
34  * @hifi-interface
35  * @hifi-client-entity
36  * @hifi-avatar
37  * @hifi-server-entity
38  * @hifi-assignment-client
39  *
40  * @property {boolean} playing - <code>true</code> if the audio is currently playing, otherwise <code>false</code>.
41  * <em>Read-only.</em>
42  * @property {number} loudness - The loudness in the last frame of audio, range <code>0.0</code> &ndash; <code>1.0</code>.
43  * <em>Read-only.</em>
44  * @property {AudioInjector.AudioInjectorOptions} options - Configures how the injector plays the audio.
45  */
47 class ScriptAudioInjector : public QObject {
48  Q_OBJECT
49 
50  Q_PROPERTY(bool playing READ isPlaying)
51  Q_PROPERTY(float loudness READ getLoudness)
52  Q_PROPERTY(AudioInjectorOptions options WRITE setOptions READ getOptions)
53 public:
54  ScriptAudioInjector(const AudioInjectorPointer& injector);
56 public slots:
57 
58  /*@jsdoc
59  * Stops current playback, if any, and starts playing from the beginning.
60  * @function AudioInjector.restart
61  */
62  void restart() { DependencyManager::get<AudioInjectorManager>()->restart(_injector); }
63 
64  /*@jsdoc
65  * Stops audio playback.
66  * @function AudioInjector.stop
67  * @example <caption>Stop playing a sound before it finishes.</caption>
68  * const sound = SoundCache.getSound(Script.resourcesPath() + "sounds/sample.wav");
69  * let injector;
70  * const injectorOptions = {
71  * position: MyAvatar.position
72  * };
73  *
74  * Script.setTimeout(function () { // Give the sound time to load.
75  * injector = Audio.playSound(sound, injectorOptions);
76  * }, 1000);
77  *
78  * Script.setTimeout(function () {
79  * injector.stop();
80  * }, 2000);
81  */
82  void stop() { DependencyManager::get<AudioInjectorManager>()->stop(_injector); }
83 
84  /*@jsdoc
85  * Gets the current configuration of the audio injector.
86  * @function AudioInjector.getOptions
87  * @returns {AudioInjector.AudioInjectorOptions} Configuration of how the injector plays the audio.
88  * @example <caption>Prints injector options to log</caption>
89  * let sound = SoundCache.getSound(Script.resourcesPath() + "sounds/sample.wav");
90  * let injector;
91  * let injectorOptions = {
92  * position: MyAvatar.position,
93  * volume: 0.8,
94  * };
95  *
96  * Script.setTimeout(function () { // Give the sound time to load.
97  * injector = Audio.playSound(sound, injectorOptions);
98  * }, 1000);
99  *
100  * Script.setTimeout(function () {
101  * const options = injector.getOptions();
102  *
103  * print("Current AudioInjectorOptions: ",JSON.stringify(options)); // Print all options to log
104  *
105  * print("Volume is set to ", options.volume);
106  * }, 2000);
107  */
108  AudioInjectorOptions getOptions() const { return DependencyManager::get<AudioInjectorManager>()->getOptions(_injector); }
109 
110  /*@jsdoc
111  * Configures how the injector plays the audio.
112  * This will replace all current AudioInjectorOptions.
113  * @function AudioInjector.setOptions
114  * @param {AudioInjector.AudioInjectorOptions} options - Configuration of how the injector plays the audio.
115  * @example <caption>Reduce the volume of the sound without changing other existing options.</caption>
116  * const sound = SoundCache.getSound(Script.resourcesPath() + "sounds/sample.wav");
117  * let injector;
118  * const injectorOptions = {
119  * position: MyAvatar.position,
120  * volume: 1.0,
121  * };
122  *
123  * Script.setTimeout(function () { // Give the sound time to load.
124  * injector = Audio.playSound(sound, injectorOptions);
125  * }, 1000);
126  *
127  * Script.setTimeout(function () {
128  * const options = injector.getOptions();
129  * options.volume = 0.2 // Reduce volume
130  * injector.setOptions(options); // replace existing options
131  * }, 5000);
132  */
133  void setOptions(const AudioInjectorOptions& options) { DependencyManager::get<AudioInjectorManager>()->setOptions(_injector, options); }
134 
135  /*@jsdoc
136  * Gets the loudness of the most recent frame of audio played.
137  * @function AudioInjector.getLoudness
138  * @returns {number} The loudness of the most recent frame of audio played, range <code>0.0</code> &ndash; <code>1.0</code>.
139  */
140  float getLoudness() const { return DependencyManager::get<AudioInjectorManager>()->getLoudness(_injector); }
141 
142  /*@jsdoc
143  * Gets whether or not the audio is currently playing.
144  * @function AudioInjector.isPlaying
145  * @returns {boolean} <code>true</code> if the audio is currently playing, otherwise <code>false</code>.
146  * @example <caption>See if a sound is playing.</caption>
147  * const sound = SoundCache.getSound(Script.resourcesPath() + "sounds/sample.wav");
148  * let injector;
149  * const injectorOptions = {
150  * position: MyAvatar.position
151  * };
152  *
153  * Script.setTimeout(function () { // Give the sound time to load.
154  * injector = Audio.playSound(sound, injectorOptions);
155  * }, 1000);
156  *
157  * Script.setTimeout(function () {
158  * print("Sound is playing: " + injector.isPlaying());
159  * }, 2000);
160  */
161  bool isPlaying() const { return DependencyManager::get<AudioInjectorManager>()->isPlaying(_injector); }
162 
163 signals:
164 
165  /*@jsdoc
166  * Triggered when the audio has finished playing.
167  * @function AudioInjector.finished
168  * @returns {Signal}
169  * @example <caption>Report when a sound has finished playing.</caption>
170  * const sound = SoundCache.getSound(Script.resourcesPath() + "sounds/sample.wav");
171  * let injector;
172  * const injectorOptions = {
173  * position: MyAvatar.position
174  * };
175  *
176  * Script.setTimeout(function () { // Give the sound time to load.
177  * injector = Audio.playSound(sound, injectorOptions);
178  * injector.finished.connect(function () {
179  * print("Finished playing sound");
180  * });
181  * }, 1000);
182  */
183  void finished();
184 
185 private:
186  QWeakPointer<AudioInjector> _injector;
187 
188  friend ScriptValue injectorToScriptValue(ScriptEngine* engine, ScriptAudioInjector* const& in);
189 };
190 
191 Q_DECLARE_METATYPE(ScriptAudioInjector*)
192 
193 ScriptValue injectorToScriptValue(ScriptEngine* engine, ScriptAudioInjector* const& in);
194 bool injectorFromScriptValue(const ScriptValue& object, ScriptAudioInjector*& out);
195 
196 #endif // hifi_ScriptAudioInjector_h
197 
Provides the AudioInjector scripting interface.
Definition: ScriptAudioInjector.h:47
Provides an engine-independent interface for a scripting engine.
Definition: ScriptEngine.h:93
[ScriptInterface] Provides an engine-independent interface for QScriptValue
Definition: ScriptValue.h:40