Skip to content

Commit

Permalink
fix SamplePlayerTest's async file load when using BufferPlayerNode
Browse files Browse the repository at this point in the history
  • Loading branch information
richardeakin committed Oct 31, 2024
1 parent d6e6f21 commit 48f916a
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions test/_audio/AudioTests/src/SamplePlayerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ void SamplePlayerTest::setupBufferPlayerNode()

auto loadFn = [bufferPlayer, this] {
bufferPlayer->loadBuffer( mSourceFile->clone() );
mWaveformPlot.load( bufferPlayer->getBuffer(), app::getWindowBounds() );
CI_LOG_V( "loaded source buffer, frames: " << bufferPlayer->getBuffer()->getNumFrames() );

CI_LOG_I( "loaded source buffer, frames: " << bufferPlayer->getBuffer()->getNumFrames() );
};

auto connectFn = [bufferPlayer, this] {
Expand All @@ -98,12 +96,14 @@ void SamplePlayerTest::setupBufferPlayerNode()
mAsyncLoadFuture = std::async( [=] {
loadFn();
app::App::get()->dispatchAsync( [=] {
mWaveformPlot.load( bufferPlayer->getBuffer(), app::getWindowBounds() );
connectFn();
} );
} );
}
else {
loadFn();
mWaveformPlot.load( bufferPlayer->getBuffer(), app::getWindowBounds() );
connectFn();
};
}
Expand Down Expand Up @@ -202,9 +202,6 @@ void SamplePlayerTest::printSupportedExtensions()
app::console() << endl;
}

// TODO: if mLoadAsync is true then use std::async like in setupBufferPlayerNode()
// - for setSourceFile()
// - for bufferPlayer->loadBuffer()
void SamplePlayerTest::openFile( const ci::fs::path &fullPath )
{
try {
Expand All @@ -221,8 +218,18 @@ void SamplePlayerTest::openFile( const ci::fs::path &fullPath )

auto bufferPlayer = dynamic_pointer_cast<audio::BufferPlayerNode>( mSamplePlayerNode );
if( bufferPlayer ) {
bufferPlayer->loadBuffer( mSourceFile->clone() );
mWaveformPlot.load( bufferPlayer->getBuffer(), app::getWindowBounds() );
if( mLoadAsync ) {
mAsyncLoadFuture = std::async( [=] {
bufferPlayer->loadBuffer( mSourceFile->clone() );
app::App::get()->dispatchAsync( [=] {
mWaveformPlot.load( bufferPlayer->getBuffer(), app::getWindowBounds() );
} );
} );
}
else {
bufferPlayer->loadBuffer( mSourceFile->clone() );
mWaveformPlot.load( bufferPlayer->getBuffer(), app::getWindowBounds() );
}
}
else {
auto filePlayer = dynamic_pointer_cast<audio::FilePlayerNode>( mSamplePlayerNode );
Expand Down Expand Up @@ -258,7 +265,7 @@ void SamplePlayerTest::update()
}

// testing delayed trigger: print SamplePlayerNode start / stop times
if( mSamplePlayerNodeEnabledState != mSamplePlayerNode->isEnabled() ) {
if( mSamplePlayerNode && mSamplePlayerNodeEnabledState != mSamplePlayerNode->isEnabled() ) {
mSamplePlayerNodeEnabledState = mSamplePlayerNode->isEnabled();
string stateStr = mSamplePlayerNodeEnabledState ? "started" : "stopped";
CI_LOG_I( "mSamplePlayerNode " << stateStr << " at " << app::getElapsedSeconds() << ", isEof: " << boolalpha << mSamplePlayerNode->isEof() << dec );
Expand Down Expand Up @@ -291,7 +298,7 @@ void SamplePlayerTest::draw()
audio::BufferRef recordedBuffer = mRecorder->getRecordedCopy();
drawAudioBuffer( *recordedBuffer, app::getWindowBounds() );
}
else {
else if( mSamplePlayerNode ) {
auto bufferPlayer = dynamic_pointer_cast<audio::BufferPlayerNode>( mSamplePlayerNode );
if( bufferPlayer )
mWaveformPlot.draw();
Expand Down

0 comments on commit 48f916a

Please sign in to comment.