// // Audio.h // interface/src/scripting // // Created by Zach Pomerantz on 28/5/2017. // Copyright 2017 High Fidelity, Inc. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // #ifndef hifi_scripting_Audio_h #define hifi_scripting_Audio_h #include "AudioScriptingInterface.h" #include "AudioDevices.h" #include "AudioEffectOptions.h" #include "SettingHandle.h" #include "AudioFileWav.h" #include namespace scripting { class Audio : public AudioScriptingInterface, protected ReadWriteLockable { Q_OBJECT SINGLETON_DEPENDENCY /**jsdoc * The Audio API provides facilities to interact with audio inputs and outputs and to play sounds. * * @namespace Audio * * @hifi-interface * @hifi-client-entity * @hifi-server-entity * @hifi-assignment-client * * @property {boolean} muted - true if the audio input is muted, otherwise false. * @property {boolean} noiseReduction - true if noise reduction is enabled, otherwise false. When * enabled, the input audio signal is blocked (fully attenuated) when it falls below an adaptive threshold set just * above the noise floor. * @property {number} inputLevel - The loudness of the audio input, range 0.0 (no sound) – * 1.0 (the onset of clipping). Read-only. * @property {boolean} clipping - true if the audio input is clipping, otherwise false. * @property {number} inputVolume - Adjusts the volume of the input audio; range 0.01.0. * If set to a value, the resulting value depends on the input device: for example, the volume can't be changed on some * devices, and others might only support values of 0.0 and 1.0. * @property {boolean} isStereoInput - true if the input audio is being used in stereo, otherwise * false. Some devices do not support stereo, in which case the value is always false. * @property {string} context - The current context of the audio: either "Desktop" or "HMD". * Read-only. * @property {object} devices Read-only. Deprecated: This property is deprecated and will be * removed. * @property {boolean} isSoloing Read-only. true if any nodes are soloed. * @property {Uuid[]} soloList Read-only. Get the list of currently soloed node UUIDs. */ Q_PROPERTY(bool muted READ isMuted WRITE setMuted NOTIFY mutedChanged) Q_PROPERTY(bool noiseReduction READ noiseReductionEnabled WRITE enableNoiseReduction NOTIFY noiseReductionChanged) Q_PROPERTY(float inputVolume READ getInputVolume WRITE setInputVolume NOTIFY inputVolumeChanged) Q_PROPERTY(float inputLevel READ getInputLevel NOTIFY inputLevelChanged) Q_PROPERTY(bool clipping READ isClipping NOTIFY clippingChanged) Q_PROPERTY(QString context READ getContext NOTIFY contextChanged) Q_PROPERTY(AudioDevices* devices READ getDevices NOTIFY nop) public: static QString AUDIO; static QString HMD; static QString DESKTOP; static float loudnessToLevel(float loudness); virtual ~Audio() {} bool isMuted() const; bool noiseReductionEnabled() const; float getInputVolume() const; float getInputLevel() const; bool isClipping() const; QString getContext() const; void showMicMeter(bool show); /**jsdoc * @function Audio.setInputDevice * @param {object} device * @param {boolean} isHMD * @deprecated This function is deprecated and will be removed. */ Q_INVOKABLE void setInputDevice(const QAudioDeviceInfo& device, bool isHMD); /**jsdoc * @function Audio.setOutputDevice * @param {object} device * @param {boolean} isHMD * @deprecated This function is deprecated and will be removed. */ Q_INVOKABLE void setOutputDevice(const QAudioDeviceInfo& device, bool isHMD); /**jsdoc * Enable or disable reverberation. Reverberation is done by the client, on the post-mix audio. The reverberation options * come from either the domain's audio zone if used — configured on the server — or as scripted by * {@link Audio.setReverbOptions|setReverbOptions}. * @function Audio.setReverb * @param {boolean} enable - true to enable reverberation, false to disable. * @example Enable reverberation for a short while. * var sound = SoundCache.getSound(Script.resourcesPath() + "sounds/sample.wav"); * var injector; * var injectorOptions = { * position: MyAvatar.position * }; * * Script.setTimeout(function () { * print("Reverb OFF"); * Audio.setReverb(false); * injector = Audio.playSound(sound, injectorOptions); * }, 1000); * * Script.setTimeout(function () { * var reverbOptions = new AudioEffectOptions(); * reverbOptions.roomSize = 100; * Audio.setReverbOptions(reverbOptions); * print("Reverb ON"); * Audio.setReverb(true); * }, 4000); * * Script.setTimeout(function () { * print("Reverb OFF"); * Audio.setReverb(false); * }, 8000); */ Q_INVOKABLE void setReverb(bool enable); /**jsdoc * Configure reverberation options. Use {@link Audio.setReverb|setReverb} to enable or disable reverberation. * @function Audio.setReverbOptions * @param {AudioEffectOptions} options - The reverberation options. */ Q_INVOKABLE void setReverbOptions(const AudioEffectOptions* options); /**jsdoc * Starts making an audio recording of the audio being played in-world (i.e., not local-only audio) to a file in WAV format. * @function Audio.startRecording * @param {string} filename - The path and name of the file to make the recording in. Should have a .wav * extension. The file is overwritten if it already exists. * @returns {boolean} true if the specified file could be opened and audio recording has started, otherwise * false. * @example Make a 10 second audio recording. * var filename = File.getTempDir() + "/audio.wav"; * if (Audio.startRecording(filename)) { * Script.setTimeout(function () { * Audio.stopRecording(); * print("Audio recording made in: " + filename); * }, 10000); * * } else { * print("Could not make an audio recording in: " + filename); * } */ Q_INVOKABLE bool startRecording(const QString& filename); /**jsdoc * Finish making an audio recording started with {@link Audio.startRecording|startRecording}. * @function Audio.stopRecording */ Q_INVOKABLE void stopRecording(); /**jsdoc * Check whether an audio recording is currently being made. * @function Audio.getRecording * @returns {boolean} true if an audio recording is currently being made, otherwise false. */ Q_INVOKABLE bool getRecording(); signals: /**jsdoc * @function Audio.nop * @returns {Signal} * @deprecated This signal is deprecated and will be removed. */ void nop(); /**jsdoc * Triggered when the audio input is muted or unmuted. * @function Audio.mutedChanged * @param {boolean} isMuted - true if the audio input is muted, otherwise false. * @returns {Signal} * @example Report when audio input is muted or unmuted * Audio.mutedChanged.connect(function (isMuted) { * print("Audio muted: " + isMuted); * }); */ void mutedChanged(bool isMuted); /**jsdoc * Triggered when the audio input noise reduction is enabled or disabled. * @function Audio.noiseReductionChanged * @param {boolean} isEnabled - true if audio input noise reduction is enabled, otherwise false. * @returns {Signal} */ void noiseReductionChanged(bool isEnabled); /**jsdoc * Triggered when the input audio volume changes. * @function Audio.inputVolumeChanged * @param {number} volume - The requested volume to be applied to the audio input, range 0.0 – * 1.0. The resulting value of Audio.inputVolume depends on the capabilities of the device: * for example, the volume can't be changed on some devices, and others might only support values of 0.0 * and 1.0. * @returns {Signal} */ void inputVolumeChanged(float volume); /**jsdoc * Triggered when the input audio level changes. * @function Audio.inputLevelChanged * @param {number} level - The loudness of the input audio, range 0.0 (no sound) – 1.0 (the * onset of clipping). * @returns {Signal} */ void inputLevelChanged(float level); /**jsdoc * Triggered when the clipping state of the input audio changes. * @function Audio.clippingChanged * @param {boolean} isClipping - true if the audio input is clipping, otherwise false. * @returns {Signal} */ void clippingChanged(bool isClipping); /**jsdoc * Triggered when the current context of the audio changes. * @function Audio.contextChanged * @param {string} context - The current context of the audio: either "Desktop" or "HMD". * @returns {Signal} */ void contextChanged(const QString& context); public slots: /**jsdoc * @function Audio.onContextChanged * @deprecated This function is deprecated and will be removed. */ void onContextChanged(); private slots: void setMuted(bool muted); void enableNoiseReduction(bool enable); void setInputVolume(float volume); void onInputLoudnessChanged(float loudness, bool isClipping); protected: // Audio must live on a separate thread from AudioClient to avoid deadlocks Audio(); private: float _inputVolume { 1.0f }; float _inputLevel { 0.0f }; bool _isClipping { false }; bool _isMuted { false }; bool _enableNoiseReduction { true }; // Match default value of AudioClient::_isNoiseGateEnabled. bool _contextIsHMD { false }; AudioDevices* getDevices() { return &_devices; } AudioDevices _devices; }; }; #endif // hifi_scripting_Audio_h