LabSound / LabSound

:microscope: :speaker: graph-based audio engine
http://labsound.io
Other
730 stars 71 forks source link

Replaying a SampledAudioNode / AudioScheduledSourceNode #90

Closed ramirezd42 closed 6 years ago

ramirezd42 commented 6 years ago

Perhaps I'm misinterpreting the purpose of the "reset" function on the AudioScheduledSourceNode. I have a SampledAudioNode that I'm hoping to re-use after playing once. Something like

auto bus = lab::MakeBusFromFile(filePath.data(), false);
auto node = std::make_shared<lab::SampledAudioNode>();
node->setBus(r, bus);
node->start(0);

// ... some amount of time later
node->stop();
node->reset();

// .. some amount of time later
node->play(0);

But this doesn't work because of the following block of code:

// AudioScheduledSourceNode.cpp: Line 47
// ...
    // quantumStartFrame     : Start frame of the current time quantum.
    // quantumEndFrame       : End frame of the current time quantum.
    // startFrame            : Start frame for this source.
    // endFrame              : End frame for this source.
    size_t quantumStartFrame = context->currentSampleFrame();
    size_t quantumEndFrame = quantumStartFrame + quantumFrameSize;
    size_t startFrame = AudioUtilities::timeToSampleFrame(m_startTime, sampleRate);
    size_t endFrame = m_endTime == UnknownTime ? 0 : AudioUtilities::timeToSampleFrame(m_endTime, sampleRate);

    // If we know the end time and it's already passed, then don't bother doing any more rendering this cycle.
    if (m_endTime != UnknownTime && endFrame <= quantumStartFrame)
        finish(r);
// ...

Should the conditional not actually be if (m_endTime != UnknownTime && quantumEndFrame <= quantumStartFrame) here?

ramirezd42 commented 6 years ago

Actually the solution I posted would break "stop" functionality.

Just resetting m_endTime back to UnknownTime in AudioScheduledSourceNode::reset() seems to do the trick

void AudioScheduledSourceNode::reset(lab::ContextRenderLock &) {
    m_playbackState = UNSCHEDULED_STATE;;
    m_endTime = UnknownTime;
}
ramirezd42 commented 6 years ago

https://github.com/LabSound/LabSound/pull/92 for reference