Frame computation + timerOffset member

When jumping inside a recording
This commit is contained in:
Atlante45 2014-09-22 14:01:02 -07:00
parent 0b89fc1f63
commit 8393fcc6eb
2 changed files with 47 additions and 3 deletions

View file

@ -18,6 +18,7 @@
Player::Player(AvatarData* avatar) :
_recording(new Recording()),
_timerOffset(0),
_avatar(avatar),
_audioThread(NULL),
_playFromCurrentPosition(true),
@ -38,7 +39,7 @@ bool Player::isPlaying() const {
qint64 Player::elapsed() const {
if (isPlaying()) {
return _timer.elapsed();
return _timerOffset + _timer.elapsed();
} else {
return 0;
}
@ -97,8 +98,9 @@ void Player::startPlaying() {
_avatar->setForceFaceshiftConnected(true);
qDebug() << "Recorder::startPlaying()";
_currentFrame = 0;
setupAudioThread();
_currentFrame = 0;
_timerOffset = 0;
_timer.start();
}
}
@ -156,6 +158,7 @@ void Player::loopRecording() {
cleanupAudioThread();
setupAudioThread();
_currentFrame = 0;
_timerOffset = 0;
_timer.restart();
}
@ -213,6 +216,43 @@ void Player::play() {
_injector->setOptions(_options);
}
void Player::setCurrentFrame(int currentFrame) {
if (_recording && currentFrame >= _recording->getFrameNumber()) {
stopPlaying();
return;
}
_currentFrame = currentFrame;
_timerOffset = _recording->getFrameTimestamp(_currentFrame);
}
void Player::setCurrentTime(qint64 currentTime) {
if (currentTime < 0 || currentTime >= _recording->getLength()) {
stopPlaying();
return;
}
_timerOffset = currentTime;
// Find correct frame
int bestGuess = 0;
int lowestBound = 0;
int highestBound = _recording->getFrameNumber() - 1;
while (_recording->getFrameTimestamp(bestGuess) <= _timerOffset &&
_recording->getFrameTimestamp(bestGuess + 1) > _timerOffset) {
if (_recording->getFrameTimestamp(bestGuess) < _timerOffset) {
lowestBound = bestGuess;
} else {
highestBound = bestGuess;
}
bestGuess = lowestBound +
(highestBound - lowestBound) *
(_timerOffset - _recording->getFrameTimestamp(lowestBound)) /
(_recording->getFrameTimestamp(highestBound) - _recording->getFrameTimestamp(lowestBound));
}
}
void Player::setPlayFromCurrentLocation(bool playFromCurrentLocation) {
_playFromCurrentPosition = playFromCurrentLocation;
}
@ -227,7 +267,7 @@ bool Player::computeCurrentFrame() {
}
while (_currentFrame < _recording->getFrameNumber() - 1 &&
_recording->getFrameTimestamp(_currentFrame) < _timer.elapsed()) {
_recording->getFrameTimestamp(_currentFrame) < elapsed()) {
++_currentFrame;
}

View file

@ -41,6 +41,9 @@ public slots:
void loadRecording(RecordingPointer recording);
void play();
void setCurrentFrame(int currentFrame);
void setCurrentTime(qint64 currentTime);
void setPlayFromCurrentLocation(bool playFromCurrentPosition);
void setLoop(bool loop) { _loop = loop; }
void useAttachements(bool useAttachments) { _useAttachments = useAttachments; }
@ -57,6 +60,7 @@ private:
QElapsedTimer _timer;
RecordingPointer _recording;
int _currentFrame;
qint64 _timerOffset;
QSharedPointer<AudioInjector> _injector;
AudioInjectorOptions _options;