Showing the changes to libraries/script-engine/src/AudioScriptingInterface.cpp (1 of 6 files). Show all
Unified · Split
| @@ -0,0 +1,74 @@ | ||
| 1 | +// | |
| 2 | +// AudioScriptingInterface.cpp | |
| 3 | +// libraries/audio/src | |
| 4 | +// | |
| 5 | +// Created by Stephen Birarda on 1/2/2014. | |
| 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 | +#include "AudioScriptingInterface.h" | |
| 13 | + | |
| 14 | +void registerAudioMetaTypes(QScriptEngine* engine) { | |
| 15 | + qScriptRegisterMetaType(engine, injectorOptionsToScriptValue, injectorOptionsFromScriptValue); | |
| 16 | + qScriptRegisterMetaType(engine, soundSharedPointerToScriptValue, soundSharedPointerFromScriptValue); | |
| 17 | + qScriptRegisterMetaType(engine, soundPointerToScriptValue, soundPointerFromScriptValue); | |
| 18 | +} | |
| 19 | + | |
| 20 | +AudioScriptingInterface& AudioScriptingInterface::getInstance() { | |
| 21 | + static AudioScriptingInterface staticInstance; | |
| 22 | + return staticInstance; | |
| 23 | +} | |
| 24 | + | |
| 25 | +AudioScriptingInterface::AudioScriptingInterface() : | |
| 26 | + _localAudioInterface(NULL) | |
| 27 | +{ | |
| 28 | + | |
| 29 | +} | |
| 30 | + | |
| 31 | +AudioInjector* AudioScriptingInterface::playSound(Sound* sound, const AudioInjectorOptions& injectorOptions) { | |
| 32 | + AudioInjector* injector = NULL; | |
| 33 | + QMetaObject::invokeMethod(this, "invokedPlaySound", Qt::BlockingQueuedConnection, | |
| 34 | + Q_RETURN_ARG(AudioInjector*, injector), | |
| 35 | + Q_ARG(Sound*, sound), Q_ARG(const AudioInjectorOptions&, injectorOptions)); | |
| 36 | + return injector; | |
| 37 | +} | |
| 38 | + | |
| 39 | +AudioInjector* AudioScriptingInterface::invokedPlaySound(Sound* sound, const AudioInjectorOptions& injectorOptions) { | |
| 40 | + if (sound) { | |
| 41 | + // stereo option isn't set from script, this comes from sound metadata or filename | |
| 42 | + AudioInjectorOptions optionsCopy = injectorOptions; | |
| 43 | + optionsCopy.stereo = sound->isStereo(); | |
| 44 | + | |
| 45 | + QThread* injectorThread = new QThread(); | |
| 46 | + injectorThread->setObjectName("Audio Injector Thread"); | |
| 47 | + | |
| 48 | + AudioInjector* injector = new AudioInjector(sound, optionsCopy); | |
| 49 | + injector->setLocalAudioInterface(_localAudioInterface); | |
| 50 | + | |
| 51 | + injector->moveToThread(injectorThread); | |
| 52 | + | |
| 53 | + // start injecting when the injector thread starts | |
| 54 | + connect(injectorThread, &QThread::started, injector, &AudioInjector::injectAudio); | |
| 55 | + | |
| 56 | + | |
| 57 | + // connect the right slots and signals for AudioInjector and thread cleanup | |
| 58 | + connect(injector, &AudioInjector::destroyed, injectorThread, &QThread::quit); | |
| 59 | + connect(injectorThread, &QThread::finished, injectorThread, &QThread::deleteLater); | |
| 60 | + connect(injectorThread, &QThread::destroyed, this, &AudioScriptingInterface::threadDead); | |
| 61 | + | |
| 62 | + injectorThread->start(); | |
| 63 | + | |
| 64 | + return injector; | |
| 65 | + | |
| 66 | + } else { | |
| 67 | + qDebug() << "AudioScriptingInterface::playSound called with null Sound object."; | |
| 68 | + return NULL; | |
| 69 | + } | |
| 70 | +} | |
| 71 | + | |
| 72 | +void AudioScriptingInterface::threadDead() { | |
| 73 | + qDebug() << "The audio thread has gone down"; | |
| 74 | +} | |