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  * var sound = SoundCache.getSound(Script.resourcesPath() + "sounds/sample.wav");
69  * var injector;
70  * var 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  */
89  AudioInjectorOptions getOptions() const { return DependencyManager::get<AudioInjectorManager>()->getOptions(_injector); }
90 
91  /*@jsdoc
92  * Configures how the injector plays the audio.
93  * @function AudioInjector.setOptions
94  * @param {AudioInjector.AudioInjectorOptions} options - Configuration of how the injector plays the audio.
95  */
96  void setOptions(const AudioInjectorOptions& options) { DependencyManager::get<AudioInjectorManager>()->setOptions(_injector, options); }
97 
98  /*@jsdoc
99  * Gets the loudness of the most recent frame of audio played.
100  * @function AudioInjector.getLoudness
101  * @returns {number} The loudness of the most recent frame of audio played, range <code>0.0</code> &ndash; <code>1.0</code>.
102  */
103  float getLoudness() const { return DependencyManager::get<AudioInjectorManager>()->getLoudness(_injector); }
104 
105  /*@jsdoc
106  * Gets whether or not the audio is currently playing.
107  * @function AudioInjector.isPlaying
108  * @returns {boolean} <code>true</code> if the audio is currently playing, otherwise <code>false</code>.
109  * @example <caption>See if a sound is playing.</caption>
110  * var sound = SoundCache.getSound(Script.resourcesPath() + "sounds/sample.wav");
111  * var injector;
112  * var injectorOptions = {
113  * position: MyAvatar.position
114  * };
115  *
116  * Script.setTimeout(function () { // Give the sound time to load.
117  * injector = Audio.playSound(sound, injectorOptions);
118  * }, 1000);
119  *
120  * Script.setTimeout(function () {
121  * print("Sound is playing: " + injector.isPlaying());
122  * }, 2000);
123  */
124  bool isPlaying() const { return DependencyManager::get<AudioInjectorManager>()->isPlaying(_injector); }
125 
126 signals:
127 
128  /*@jsdoc
129  * Triggered when the audio has finished playing.
130  * @function AudioInjector.finished
131  * @returns {Signal}
132  * @example <caption>Report when a sound has finished playing.</caption>
133  * var sound = SoundCache.getSound(Script.resourcesPath() + "sounds/sample.wav");
134  * var injector;
135  * var injectorOptions = {
136  * position: MyAvatar.position
137  * };
138  *
139  * Script.setTimeout(function () { // Give the sound time to load.
140  * injector = Audio.playSound(sound, injectorOptions);
141  * injector.finished.connect(function () {
142  * print("Finished playing sound");
143  * });
144  * }, 1000);
145  */
146  void finished();
147 
148 private:
149  QWeakPointer<AudioInjector> _injector;
150 
151  friend ScriptValue injectorToScriptValue(ScriptEngine* engine, ScriptAudioInjector* const& in);
152 };
153 
154 Q_DECLARE_METATYPE(ScriptAudioInjector*)
155 
156 ScriptValue injectorToScriptValue(ScriptEngine* engine, ScriptAudioInjector* const& in);
157 bool injectorFromScriptValue(const ScriptValue& object, ScriptAudioInjector*& out);
158 
159 #endif // hifi_ScriptAudioInjector_h
160 
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