Skip to content

Commit

Permalink
Move PlaybackComponent autoplay handling to update()
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanbeek committed Jan 20, 2025
1 parent fd5b252 commit 9e1ddbb
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion demos/audiovisualfft/data/objects.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
],
"Gain": 1.0,
"StereoPanning": 0.5,
"AutoPlay": false,
"AutoPlay": true,
"StartPosition": 17000.0,
"Duration": 0.0,
"FadeInTime": 0.0,
Expand Down
9 changes: 0 additions & 9 deletions demos/audiovisualfft/src/audiovisualfftapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,6 @@ namespace nap
// Connect reload slot
mResourceManager->mPostResourcesLoadedSignal.connect(mReloadSlot);

// Start audio file playback
auto audioEntity = mScene->findEntity("AudioEntity");
if (!errorState.check(audioEntity != nullptr, "unable to find entity with name: %s", "AudioEntity"))
return false;
auto playbackComponent = audioEntity->findComponent<audio::PlaybackComponentInstance>();
if (!errorState.check(playbackComponent != nullptr, "unable to find PlaybackComponentInstance"))
return false;
playbackComponent->start();

// All done!
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace nap
mFadeInTime = mResource->mFadeInTime;
mFadeOutTime = mResource->mFadeOutTime;
mPitch = mResource->mPitch;
mAutoPlay = mResource->mAutoPlay;

mAudioService = getEntityInstance()->getCore()->getService<AudioService>();
mNodeManager = &mAudioService->getNodeManager();
Expand Down Expand Up @@ -97,15 +98,18 @@ namespace nap
mGainControls.emplace_back(std::move(gainControl));
}

if (mResource->mAutoPlay)
start(mResource->mStartPosition, mResource->mDuration);

return true;
}


void PlaybackComponentInstance::update(double deltaTime)
{
if (mAutoPlay)
{
start(mResource->mStartPosition, mResource->mDuration);
mAutoPlay = false;
}

if (mPlaying)
{
mCurrentPlayingTime += deltaTime * 1000.f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,10 @@ namespace nap
TimeValue mDuration = 0;
TimeValue mCurrentPlayingTime = 0;
std::vector<int> mChannelRouting;

bool mAutoPlay = false;

bool mPlaying = false; // Indicates wether the component is currently playing

PlaybackComponent* mResource = nullptr; // The component's resource
NodeManager* mNodeManager = nullptr; // The audio node manager this component's audio nodes are managed by
AudioService* mAudioService = nullptr;
Expand Down

0 comments on commit 9e1ddbb

Please sign in to comment.