Showing the changes to libraries/script-engine/src/RecordingScriptingInterface.cpp (1 of 5 files). Show all
Unified · Split
| @@ -0,0 +1,270 @@ | |||
| 1 | // | ||
| 2 | // Created by Bradley Austin Davis on 2015/11/13 | ||
| 3 | // Copyright 2015 High Fidelity, Inc. | ||
| 4 | // | ||
| 5 | // Distributed under the Apache License, Version 2.0. | ||
| 6 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | ||
| 7 | // | ||
| 8 | |||
| 9 | #include "RecordingScriptingInterface.h" | ||
| 10 | |||
| 11 | #include <QThread> | ||
| 12 | |||
| 13 | #include <recording/Deck.h> | ||
| 14 | #include <recording/Recorder.h> | ||
| 15 | #include <recording/Clip.h> | ||
| 16 | #include <recording/Frame.h> | ||
| 17 | #include <NumericalConstants.h> | ||
| 18 | //#include <AudioClient.h> | ||
| 19 | #include <AudioConstants.h> | ||
| 20 | #include <Transform.h> | ||
| 21 | //#include "avatar/AvatarManager.h" | ||
| 22 | //#include "Application.h" | ||
| 23 | #include "ScriptEngineLogging.h" | ||
| 24 | |||
| 25 | typedef int16_t AudioSample; | ||
| 26 | |||
| 27 | |||
| 28 | using namespace recording; | ||
| 29 | |||
| 30 | // FIXME move to somewhere audio related? | ||
| 31 | static const QString AUDIO_FRAME_NAME = "com.highfidelity.recording.Audio"; | ||
| 32 | |||
| 33 | RecordingScriptingInterface::RecordingScriptingInterface() { | ||
| 34 | static const recording::FrameType AVATAR_FRAME_TYPE = recording::Frame::registerFrameType(AvatarData::FRAME_NAME); | ||
| 35 | // FIXME how to deal with driving multiple avatars locally? | ||
| 36 | Frame::registerFrameHandler(AVATAR_FRAME_TYPE, [this](Frame::ConstPointer frame) { | ||
| 37 | processAvatarFrame(frame); | ||
| 38 | }); | ||
| 39 | |||
| 40 | static const recording::FrameType AUDIO_FRAME_TYPE = recording::Frame::registerFrameType(AUDIO_FRAME_NAME); | ||
| 41 | Frame::registerFrameHandler(AUDIO_FRAME_TYPE, [this](Frame::ConstPointer frame) { | ||
| 42 | processAudioFrame(frame); | ||
| 43 | }); | ||
| 44 | |||
| 45 | _player = DependencyManager::get<Deck>(); | ||
| 46 | _recorder = DependencyManager::get<Recorder>(); | ||
| 47 | |||
| 48 | // auto audioClient = DependencyManager::get<AudioClient>(); | ||
| 49 | // connect(audioClient.data(), &AudioClient::inputReceived, this, &RecordingScriptingInterface::processAudioInput); | ||
| 50 | } | ||
| 51 | |||
| 52 | void RecordingScriptingInterface::setControlledAvatar(AvatarData* avatar) { | ||
| 53 | _controlledAvatar = avatar; | ||
| 54 | } | ||
| 55 | |||
| 56 | bool RecordingScriptingInterface::isPlaying() { | ||
| 57 | return _player->isPlaying(); | ||
| 58 | } | ||
| 59 | |||
| 60 | bool RecordingScriptingInterface::isPaused() { | ||
| 61 | return _player->isPaused(); | ||
| 62 | } | ||
| 63 | |||
| 64 | float RecordingScriptingInterface::playerElapsed() { | ||
| 65 | return _player->position(); | ||
| 66 | } | ||
| 67 | |||
| 68 | float RecordingScriptingInterface::playerLength() { | ||
| 69 | return _player->length(); | ||
| 70 | } | ||
| 71 | |||
| 72 | void RecordingScriptingInterface::loadRecording(const QString& filename) { | ||
| 73 | using namespace recording; | ||
| 74 | |||
| 75 | if (QThread::currentThread() != thread()) { | ||
| 76 | QMetaObject::invokeMethod(this, "loadRecording", Qt::BlockingQueuedConnection, | ||
| 77 | Q_ARG(QString, filename)); | ||
| 78 | return; | ||
| 79 | } | ||
| 80 | |||
| 81 | ClipPointer clip = Clip::fromFile(filename); | ||
| 82 | if (!clip) { | ||
| 83 | qWarning() << "Unable to load clip data from " << filename; | ||
| 84 | } | ||
| 85 | _player->queueClip(clip); | ||
| 86 | } | ||
| 87 | |||
| 88 | void RecordingScriptingInterface::startPlaying() { | ||
| 89 | if (QThread::currentThread() != thread()) { | ||
| 90 | QMetaObject::invokeMethod(this, "startPlaying", Qt::BlockingQueuedConnection); | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | //auto myAvatar = DependencyManager::get<AvatarManager>()->getMyAvatar(); | ||
| 94 | auto myAvatar = _controlledAvatar; | ||
| 95 | // Playback from the current position | ||
| 96 | if (_playFromCurrentLocation) { | ||
| 97 | _dummyAvatar.setRecordingBasis(std::make_shared<Transform>(myAvatar->getTransform())); | ||
| 98 | } else { | ||
| 99 | _dummyAvatar.clearRecordingBasis(); | ||
| 100 | } | ||
| 101 | _player->play(); | ||
| 102 | } | ||
| 103 | |||
| 104 | void RecordingScriptingInterface::setPlayerVolume(float volume) { | ||
| 105 | // FIXME | ||
| 106 | } | ||
| 107 | |||
| 108 | void RecordingScriptingInterface::setPlayerAudioOffset(float audioOffset) { | ||
| 109 | // FIXME | ||
| 110 | } | ||
| 111 | |||
| 112 | void RecordingScriptingInterface::setPlayerTime(float time) { | ||
| 113 | _player->seek(time); | ||
| 114 | } | ||
| 115 | |||
| 116 | void RecordingScriptingInterface::setPlayFromCurrentLocation(bool playFromCurrentLocation) { | ||
| 117 | _playFromCurrentLocation = playFromCurrentLocation; | ||
| 118 | } | ||
| 119 | |||
| 120 | void RecordingScriptingInterface::setPlayerLoop(bool loop) { | ||
| 121 | _player->loop(loop); | ||
| 122 | } | ||
| 123 | |||
| 124 | void RecordingScriptingInterface::setPlayerUseDisplayName(bool useDisplayName) { | ||
| 125 | _useDisplayName = useDisplayName; | ||
| 126 | } | ||
| 127 | |||
| 128 | void RecordingScriptingInterface::setPlayerUseAttachments(bool useAttachments) { | ||
| 129 | _useAttachments = useAttachments; | ||
| 130 | } | ||
| 131 | |||
| 132 | void RecordingScriptingInterface::setPlayerUseHeadModel(bool useHeadModel) { | ||
| 133 | _useHeadModel = useHeadModel; | ||
| 134 | } | ||
| 135 | |||
| 136 | void RecordingScriptingInterface::setPlayerUseSkeletonModel(bool useSkeletonModel) { | ||
| 137 | _useSkeletonModel = useSkeletonModel; | ||
| 138 | } | ||
| 139 | |||
| 140 | void RecordingScriptingInterface::play() { | ||
| 141 | _player->play(); | ||
| 142 | } | ||
| 143 | |||
| 144 | void RecordingScriptingInterface::pausePlayer() { | ||
| 145 | _player->pause(); | ||
| 146 | } | ||
| 147 | |||
| 148 | void RecordingScriptingInterface::stopPlaying() { | ||
| 149 | _player->stop(); | ||
| 150 | } | ||
| 151 | |||
| 152 | bool RecordingScriptingInterface::isRecording() { | ||
| 153 | return _recorder->isRecording(); | ||
| 154 | } | ||
| 155 | |||
| 156 | float RecordingScriptingInterface::recorderElapsed() { | ||
| 157 | return _recorder->position(); | ||
| 158 | } | ||
| 159 | |||
| 160 | void RecordingScriptingInterface::startRecording() { | ||
| 161 | if (_recorder->isRecording()) { | ||
| 162 | qCWarning(scriptengine) << "Recorder is already running"; | ||
| 163 | return; | ||
| 164 | } | ||
| 165 | |||
| 166 | if (QThread::currentThread() != thread()) { | ||
| 167 | QMetaObject::invokeMethod(this, "startRecording", Qt::BlockingQueuedConnection); | ||
| 168 | return; | ||
| 169 | } | ||
| 170 | |||
| 171 | _recordingEpoch = Frame::epochForFrameTime(0); | ||
| 172 | |||
| 173 | //auto myAvatar = DependencyManager::get<AvatarManager>()->getMyAvatar(); | ||
| 174 | //auto myAvatar = DependencyManager::get<AvatarManager>()->getMyAvatar(); | ||
| 175 | auto myAvatar = _controlledAvatar; | ||
| 176 | myAvatar->setRecordingBasis(); | ||
| 177 | _recorder->start(); | ||
| 178 | } | ||
| 179 | |||
| 180 | void RecordingScriptingInterface::stopRecording() { | ||
| 181 | _recorder->stop(); | ||
| 182 | |||
| 183 | _lastClip = _recorder->getClip(); | ||
| 184 | // post-process the audio into discreet chunks based on times of received samples | ||
| 185 | _lastClip->seek(0); | ||
| 186 | Frame::ConstPointer frame; | ||
| 187 | while (frame = _lastClip->nextFrame()) { | ||
| 188 | qDebug() << "Frame time " << frame->timeOffset << " size " << frame->data.size(); | ||
| 189 | } | ||
| 190 | _lastClip->seek(0); | ||
| 191 | |||
| 192 | //auto myAvatar = DependencyManager::get<AvatarManager>()->getMyAvatar(); | ||
| 193 | //auto myAvatar = DependencyManager::get<AvatarManager>()->getMyAvatar(); | ||
| 194 | auto myAvatar = _controlledAvatar; | ||
| 195 | myAvatar->clearRecordingBasis(); | ||
| 196 | } | ||
| 197 | |||
| 198 | void RecordingScriptingInterface::saveRecording(const QString& filename) { | ||
| 199 | if (QThread::currentThread() != thread()) { | ||
| 200 | QMetaObject::invokeMethod(this, "saveRecording", Qt::BlockingQueuedConnection, | ||
| 201 | Q_ARG(QString, filename)); | ||
| 202 | return; | ||
| 203 | } | ||
| 204 | |||
| 205 | if (!_lastClip) { | ||
| 206 | qWarning() << "There is no recording to save"; | ||
| 207 | return; | ||
| 208 | } | ||
| 209 | |||
| 210 | recording::Clip::toFile(filename, _lastClip); | ||
| 211 | } | ||
| 212 | |||
| 213 | void RecordingScriptingInterface::loadLastRecording() { | ||
| 214 | if (QThread::currentThread() != thread()) { | ||
| 215 | QMetaObject::invokeMethod(this, "loadLastRecording", Qt::BlockingQueuedConnection); | ||
| 216 | return; | ||
| 217 | } | ||
| 218 | |||
| 219 | if (!_lastClip) { | ||
| 220 | qCDebug(scriptengine) << "There is no recording to load"; | ||
| 221 | return; | ||
| 222 | } | ||
| 223 | |||
| 224 | _player->queueClip(_lastClip); | ||
| 225 | _player->play(); | ||
| 226 | } | ||
| 227 | |||
| 228 | void RecordingScriptingInterface::processAvatarFrame(const Frame::ConstPointer& frame) { | ||
| 229 | Q_ASSERT(QThread::currentThread() == thread()); | ||
| 230 | |||
| 231 | AvatarData::fromFrame(frame->data, _dummyAvatar); | ||
| 232 | |||
| 233 | //auto myAvatar = DependencyManager::get<AvatarManager>()->getMyAvatar(); | ||
| 234 | auto myAvatar = _controlledAvatar; | ||
| 235 | if (_useHeadModel && _dummyAvatar.getFaceModelURL().isValid() && | ||
| 236 | (_dummyAvatar.getFaceModelURL() != myAvatar->getFaceModelURL())) { | ||
| 237 | // FIXME | ||
| 238 | //myAvatar->setFaceModelURL(_dummyAvatar.getFaceModelURL()); | ||
| 239 | } | ||
| 240 | |||
| 241 | if (_useSkeletonModel && _dummyAvatar.getSkeletonModelURL().isValid() && | ||
| 242 | (_dummyAvatar.getSkeletonModelURL() != myAvatar->getSkeletonModelURL())) { | ||
| 243 | // FIXME | ||
| 244 | //myAvatar->useFullAvatarURL() | ||
| 245 | } | ||
| 246 | |||
| 247 | if (_useDisplayName && _dummyAvatar.getDisplayName() != myAvatar->getDisplayName()) { | ||
| 248 | myAvatar->setDisplayName(_dummyAvatar.getDisplayName()); | ||
| 249 | } | ||
| 250 | |||
| 251 | myAvatar->setPosition(_dummyAvatar.getPosition()); | ||
| 252 | myAvatar->setOrientation(_dummyAvatar.getOrientation()); | ||
| 253 | |||
| 254 | // FIXME attachments | ||
| 255 | // FIXME joints | ||
| 256 | // FIXME head lean | ||
| 257 | // FIXME head orientation | ||
| 258 | } | ||
| 259 | |||
| 260 | void RecordingScriptingInterface::processAudioInput(const QByteArray& audio) { | ||
| 261 | if (_recorder->isRecording()) { | ||
| 262 | static const recording::FrameType AUDIO_FRAME_TYPE = recording::Frame::registerFrameType(AUDIO_FRAME_NAME); | ||
| 263 | _recorder->recordFrame(AUDIO_FRAME_TYPE, audio); | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | void RecordingScriptingInterface::processAudioFrame(const recording::FrameConstPointer& frame) { | ||
| 268 | // auto audioClient = DependencyManager::get<AudioClient>(); | ||
| 269 | // audioClient->handleRecordedAudioInput(frame->data); | ||
| 270 | } | ||