18#include "playbackmanager.h"
21#include <QElapsedTimer>
26#include "layersound.h"
27#include "layermanager.h"
29#include "toolmanager.h"
32PlaybackManager::PlaybackManager(
Editor* editor) :
BaseManager(editor, __FUNCTION__)
36PlaybackManager::~PlaybackManager()
41bool PlaybackManager::init()
46 mFlipTimer =
new QTimer(
this);
49 mScrubTimer =
new QTimer(
this);
51 mSoundclipsToPLay.
clear();
54 mFps = settings.value(SETTING_FPS).toInt();
55 mMsecSoundScrub = settings.value(SETTING_SOUND_SCRUB_MSEC).toInt();
56 if (mMsecSoundScrub == 0) { mMsecSoundScrub = 100; }
57 mSoundScrub = settings.value(SETTING_SOUND_SCRUB_ACTIVE).toBool();
69 mIsLooping = data->isLooping();
70 mIsRangedPlayback = data->isRangedPlayback();
71 mMarkInFrame = data->getMarkInFrameNumber();
72 mMarkOutFrame = data->getMarkOutFrameNumber();
73 mFps = data->getFrameRate();
84 data->setLooping(mIsLooping);
85 data->setRangedPlayback(mIsRangedPlayback);
86 data->setMarkInFrameNumber(mMarkInFrame);
87 data->setMarkOutFrameNumber(mMarkOutFrame);
88 data->setFrameRate(mFps);
89 data->setCurrentFrame(editor()->currentFrame());
93bool PlaybackManager::isPlaying()
98void PlaybackManager::play()
103 int frame = editor()->currentFrame();
104 if (frame >= mEndFrame || frame < mStartFrame)
106 editor()->scrubTo(mStartFrame);
107 frame = editor()->currentFrame();
110 mListOfActiveSoundFrames.
clear();
112 mCheckForSoundsHalfway =
true;
115 mTimer->
setInterval(
static_cast<int>(1000.f / mFps));
119 mPlayingFrameCounter = 1;
120 mElapsedTimer->
start();
122 emit playStateChanged(
true);
125void PlaybackManager::stop()
129 emit playStateChanged(
false);
132void PlaybackManager::playFlipRoll()
134 if (isPlaying()) {
return; }
136 int start = editor()->currentFrame();
140 mFlipRollMax = settings.value(SETTING_FLIP_ROLL_DRAWINGS).toInt();
141 for (
int i = 0; i < mFlipRollMax; i++)
143 int prev = editor()->layers()->currentLayer()->getPreviousKeyFramePosition(tmp);
150 if (mFlipList.
isEmpty()) {
return; }
153 mFlipRollInterval = settings.value(SETTING_FLIP_ROLL_MSEC).toInt();
157 editor()->scrubTo(mFlipList[0]);
159 emit playStateChanged(
true);
162void PlaybackManager::playFlipInBetween()
164 if (isPlaying()) {
return; }
167 int start = editor()->currentFrame();
169 int prev = layerMgr->currentLayer()->getPreviousKeyFramePosition(start);
170 int next = layerMgr->currentLayer()->getNextKeyFramePosition(start);
172 if (prev < start && next > start &&
173 layerMgr->currentLayer()->keyExists(prev) &&
174 layerMgr->currentLayer()->keyExists(next))
190 mFlipInbetweenInterval = settings.value(SETTING_FLIP_INBETWEEN_MSEC).toInt();
193 editor()->scrubTo(mFlipList[0]);
195 emit playStateChanged(
true);
198void PlaybackManager::playScrub(
int frame)
200 if (!mSoundScrub || !mSoundclipsToPLay.
isEmpty()) {
return; }
202 auto layerMan = editor()->layers();
203 for (
int i = 0; i < layerMan->count(); i++)
205 Layer* layer = layerMan->getLayer(i);
206 if (layer->type() == Layer::SOUND && layer->visible())
208 KeyFrame* key = layer->getKeyFrameWhichCovers(frame);
212 mSoundclipsToPLay.
append(clip);
217 if (mSoundclipsToPLay.
isEmpty()) {
return; }
219 mScrubTimer->
singleShot(mMsecSoundScrub,
this, &PlaybackManager::stopScrubPlayback);
220 for (
int i = 0; i < mSoundclipsToPLay.
count(); i++)
222 mSoundclipsToPLay.
at(i)->playFromPosition(frame, mFps);
226void PlaybackManager::setFps(
int fps)
232 settings.setValue(SETTING_FPS, fps);
233 emit fpsChanged(mFps);
237 for (
int i = 0; i < object()->getLayerCount(); ++i)
239 Layer* layer = object()->getLayer(i);
240 if (layer->type() == Layer::SOUND)
242 auto soundLayer =
dynamic_cast<LayerSound *
>(layer);
243 soundLayer->updateFrameLengths(mFps);
249void PlaybackManager::playSounds(
int frame)
257 std::vector< LayerSound* > kSoundLayers;
258 for (
int i = 0; i < object()->getLayerCount(); ++i)
260 Layer* layer = object()->getLayer(i);
261 if (layer->type() == Layer::SOUND)
263 kSoundLayers.push_back(
static_cast<LayerSound*
>(layer));
269 KeyFrame* key = layer->getLastKeyFrameAtPosition(frame);
271 if (!layer->visible())
279 if (key->pos() + key->length() >= frame)
281 if (!mListOfActiveSoundFrames.
contains(key->pos()))
283 mListOfActiveSoundFrames.
append(key->pos());
288 if (mCheckForSoundsHalfway)
291 for (
int i = 0; i < mListOfActiveSoundFrames.
count(); i++)
293 int listPosition = mListOfActiveSoundFrames.
at(i);
294 if (layer->keyExistsWhichCovers(listPosition))
296 key = layer->getKeyFrameWhichCovers(listPosition);
298 clip->playFromPosition(frame, mFps);
302 else if (layer->keyExists(frame))
304 key = layer->getKeyFrameAt(frame);
310 mActiveSoundFrame = frame;
313 if (frame >= mEndFrame)
315 if (layer->keyExists(mActiveSoundFrame))
317 key = layer->getKeyFrameWhichCovers(mActiveSoundFrame);
322 if (!mListOfActiveSoundFrames.
isEmpty())
323 mListOfActiveSoundFrames.
clear();
330 mCheckForSoundsHalfway =
false;
345 int t = qRound((mPlayingFrameCounter - 1) * (1000.f / mFps));
346 if (mElapsedTimer->
elapsed() < t)
352 ++mPlayingFrameCounter;
356void PlaybackManager::stopSounds()
358 std::vector<LayerSound*> kSoundLayers;
360 for (
int i = 0; i < object()->getLayerCount(); ++i)
362 Layer* layer = object()->getLayer(i);
363 if (layer->type() == Layer::SOUND)
365 kSoundLayers.push_back(
static_cast<LayerSound*
>(layer));
371 layer->foreachKeyFrame([](
KeyFrame* key)
379void PlaybackManager::stopScrubPlayback()
381 for (
int i = 0; i < mSoundclipsToPLay.
count(); i++)
383 mSoundclipsToPLay.
at(i)->pause();
385 mSoundclipsToPLay.
clear();
388void PlaybackManager::timerTick()
390 int currentFrame = editor()->currentFrame();
393 if (currentFrame >= mEndFrame)
397 editor()->scrubTo(mStartFrame);
398 mCheckForSoundsHalfway =
true;
411 editor()->scrubForward();
413 int newFrame = editor()->currentFrame();
414 playSounds(newFrame);
417void PlaybackManager::flipTimerTick()
419 if (mFlipList.
count() < 2 || editor()->currentFrame() != mFlipList[0])
422 editor()->scrubTo(mFlipList.
last());
423 emit playStateChanged(
false);
427 editor()->scrubTo(mFlipList[1]);
432void PlaybackManager::setLooping(
bool isLoop)
434 if (mIsLooping != isLoop)
437 emit loopStateChanged(mIsLooping);
441void PlaybackManager::enableRangedPlayback(
bool b)
443 if (mIsRangedPlayback != b)
445 mIsRangedPlayback = b;
450 emit rangedPlaybackStateChanged(mIsRangedPlayback);
454void PlaybackManager::setRangedStartFrame(
int frame)
456 mMarkInFrame = frame;
460void PlaybackManager::setRangedEndFrame(
int frame)
462 mMarkOutFrame = frame;
466void PlaybackManager::updateStartFrame()
468 mStartFrame = (mIsRangedPlayback) ? mMarkInFrame : 1;
471void PlaybackManager::updateEndFrame()
474 mEndFrame = (mIsRangedPlayback) ? mMarkOutFrame : projectLength;
477void PlaybackManager::enableSound(
bool b)
487 mCheckForSoundsHalfway =
true;
int animationLength(bool includeSounds=true)
Get the length of current project.
bool skipFrame()
PlaybackManager::skipFrame() Small errors accumulate while playing animation If the error is greater ...
qint64 elapsed() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setInterval(int msec)
bool isActive() const const
void setTimerType(Qt::TimerType atype)
void append(const T &value)
const T & at(int i) const const
bool contains(const T &value) const const
int count(const T &value) const const
bool isEmpty() const const