Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void AudioScheduledSourceNode::updatePlaybackInfo(
auto sampleRate = context_->getSampleRate();

size_t firstFrame = context_->getCurrentSampleFrame();
size_t lastFrame = firstFrame + framesToProcess;
size_t lastFrame = firstFrame + framesToProcess - 1;

size_t startFrame =
std::max(dsp::timeToSampleFrame(startTime_, sampleRate), firstFrame);
Expand Down Expand Up @@ -105,7 +105,7 @@ void AudioScheduledSourceNode::updatePlaybackInfo(
? std::max(startFrame, firstFrame) - firstFrame
: 0;
nonSilentFramesToProcess =
std::max(std::min(lastFrame, stopFrame), startFrame) - startFrame;
std::max(std::min(lastFrame, stopFrame) + 1, startFrame) - startFrame;

assert(startOffset <= framesToProcess);
assert(nonSilentFramesToProcess <= framesToProcess);
Expand All @@ -124,7 +124,7 @@ void AudioScheduledSourceNode::updatePlaybackInfo(

// stop will happen in this render quantum
// zero remaining frames after stop frame
if (stopFrame < lastFrame && stopFrame >= firstFrame) {
if (stopFrame <= lastFrame && stopFrame >= firstFrame) {
playbackState_ = PlaybackState::STOP_SCHEDULED;
startOffset = 0;
nonSilentFramesToProcess = stopFrame - firstFrame;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using namespace audioapi;

class AudioParamTest : public ::testing::Test {
protected:
std::shared_ptr<IAudioEventHandlerRegistry> eventRegistry;
std::shared_ptr<MockAudioEventHandlerRegistry> eventRegistry;
std::unique_ptr<OfflineAudioContext> context;
static constexpr int sampleRate = 44100;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#include <audioapi/core/OfflineAudioContext.h>
#include <audioapi/core/destinations/AudioDestinationNode.h>
#include <audioapi/core/sources/AudioScheduledSourceNode.h>
#include <audioapi/core/utils/worklets/SafeIncludes.h>
#include <audioapi/utils/AudioBus.h>
#include <gtest/gtest.h>
#include <test/src/MockAudioEventHandlerRegistry.h>

using namespace audioapi;
static constexpr int SAMPLE_RATE = 44100;
static constexpr int RENDER_QUANTUM = 128;
static constexpr double RENDER_QUANTUM_TIME =
static_cast<double>(RENDER_QUANTUM) / SAMPLE_RATE;

class AudioScheduledSourceTest : public ::testing::Test {
protected:
std::shared_ptr<MockAudioEventHandlerRegistry> eventRegistry;
std::unique_ptr<OfflineAudioContext> context;

void SetUp() override {
eventRegistry = std::make_shared<MockAudioEventHandlerRegistry>();
context = std::make_unique<OfflineAudioContext>(
2, 5 * SAMPLE_RATE, SAMPLE_RATE, eventRegistry, RuntimeRegistry{});
}
};

class TestableAudioScheduledSourceNode : public AudioScheduledSourceNode {
public:
explicit TestableAudioScheduledSourceNode(BaseAudioContext *context)
: AudioScheduledSourceNode(context) {
isInitialized_ = true;
}

void updatePlaybackInfo(
const std::shared_ptr<AudioBus> &processingBus,
int framesToProcess,
size_t &startOffset,
size_t &nonSilentFramesToProcess) {
AudioScheduledSourceNode::updatePlaybackInfo(
processingBus, framesToProcess, startOffset, nonSilentFramesToProcess);
}

std::shared_ptr<AudioBus> processNode(const std::shared_ptr<AudioBus> &, int)
override {
return nullptr;
}

PlaybackState getPlaybackState() const {
return playbackState_;
}

void playFrames(int frames) {
size_t startOffset = 0;
size_t nonSilentFramesToProcess = 0;
auto processingBus =
std::make_shared<AudioBus>(128, 2, static_cast<float>(SAMPLE_RATE));
updatePlaybackInfo(
processingBus, frames, startOffset, nonSilentFramesToProcess);
context_->getDestination()->renderAudio(processingBus, frames);
}
};

TEST_F(AudioScheduledSourceTest, IsUnscheduledStateSetCorrectly) {
auto sourceNode = TestableAudioScheduledSourceNode(context.get());
EXPECT_EQ(
sourceNode.getPlaybackState(),
AudioScheduledSourceNode::PlaybackState::UNSCHEDULED);

sourceNode.start(RENDER_QUANTUM_TIME);
EXPECT_NE(
sourceNode.getPlaybackState(),
AudioScheduledSourceNode::PlaybackState::UNSCHEDULED);
}

TEST_F(AudioScheduledSourceTest, IsScheduledStateSetCorrectly) {
auto sourceNode = TestableAudioScheduledSourceNode(context.get());
sourceNode.start(RENDER_QUANTUM_TIME);
EXPECT_EQ(
sourceNode.getPlaybackState(),
AudioScheduledSourceNode::PlaybackState::SCHEDULED);

sourceNode.playFrames(RENDER_QUANTUM);
EXPECT_EQ(
sourceNode.getPlaybackState(),
AudioScheduledSourceNode::PlaybackState::SCHEDULED);

sourceNode.playFrames(1);
EXPECT_NE(
sourceNode.getPlaybackState(),
AudioScheduledSourceNode::PlaybackState::SCHEDULED);
}

TEST_F(AudioScheduledSourceTest, IsPlayingStateSetCorrectly) {
auto sourceNode = TestableAudioScheduledSourceNode(context.get());
sourceNode.start(0);
sourceNode.stop(RENDER_QUANTUM_TIME);

sourceNode.playFrames(RENDER_QUANTUM);
EXPECT_EQ(
sourceNode.getPlaybackState(),
AudioScheduledSourceNode::PlaybackState::PLAYING);

sourceNode.playFrames(1);
EXPECT_NE(
sourceNode.getPlaybackState(),
AudioScheduledSourceNode::PlaybackState::PLAYING);
}

TEST_F(AudioScheduledSourceTest, IsStopScheduledStateSetCorrectly) {
auto sourceNode = TestableAudioScheduledSourceNode(context.get());
sourceNode.start(0);
sourceNode.stop(RENDER_QUANTUM_TIME);
sourceNode.playFrames(1); // start playing
sourceNode.playFrames(RENDER_QUANTUM);
EXPECT_EQ(
sourceNode.getPlaybackState(),
AudioScheduledSourceNode::PlaybackState::STOP_SCHEDULED);

sourceNode.playFrames(1);
EXPECT_NE(
sourceNode.getPlaybackState(),
AudioScheduledSourceNode::PlaybackState::STOP_SCHEDULED);
}

TEST_F(AudioScheduledSourceTest, IsFinishedStateSetCorrectly) {
auto sourceNode = TestableAudioScheduledSourceNode(context.get());
sourceNode.start(0);
sourceNode.stop(RENDER_QUANTUM_TIME);
sourceNode.playFrames(1); // start playing

EXPECT_CALL(
*eventRegistry,
invokeHandlerWithEventBody("ended", testing::_, testing::_))
.Times(1);
sourceNode.playFrames(RENDER_QUANTUM);
sourceNode.playFrames(1);
EXPECT_TRUE(sourceNode.isFinished());
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using namespace audioapi;

class ConstantSourceTest : public ::testing::Test {
protected:
std::shared_ptr<IAudioEventHandlerRegistry> eventRegistry;
std::shared_ptr<MockAudioEventHandlerRegistry> eventRegistry;
std::unique_ptr<OfflineAudioContext> context;
static constexpr int sampleRate = 44100;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using namespace audioapi;

class GainTest : public ::testing::Test {
protected:
std::shared_ptr<IAudioEventHandlerRegistry> eventRegistry;
std::shared_ptr<MockAudioEventHandlerRegistry> eventRegistry;
std::unique_ptr<OfflineAudioContext> context;
static constexpr int sampleRate = 44100;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class MockAudioEventHandlerRegistry : public IAudioEventHandlerRegistry {
MOCK_METHOD(void, unregisterHandler,
(const std::string &eventName, uint64_t listenerId), (override));

MOCK_METHOD(void, invokeHandlerWithEventBody,
(const std::string &eventName, const EventMap &body), (override));
MOCK_METHOD(void, invokeHandlerWithEventBody,
(const std::string &eventName, uint64_t listenerId, const EventMap &body), (override));
MOCK_METHOD2(invokeHandlerWithEventBody, void
(const std::string &eventName, const EventMap &body));
MOCK_METHOD3(invokeHandlerWithEventBody, void
(const std::string &eventName, uint64_t listenerId, const EventMap &body));
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using namespace audioapi;

class OscillatorTest : public ::testing::Test {
protected:
std::shared_ptr<IAudioEventHandlerRegistry> eventRegistry;
std::shared_ptr<MockAudioEventHandlerRegistry> eventRegistry;
std::unique_ptr<OfflineAudioContext> context;
static constexpr int sampleRate = 44100;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using namespace audioapi;

class StereoPannerTest : public ::testing::Test {
protected:
std::shared_ptr<IAudioEventHandlerRegistry> eventRegistry;
std::shared_ptr<MockAudioEventHandlerRegistry> eventRegistry;
std::unique_ptr<OfflineAudioContext> context;
static constexpr int sampleRate = 44100;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static constexpr float tolerance = 0.0001f;
namespace audioapi {
class BiquadFilterTest : public ::testing::Test {
protected:
std::shared_ptr<IAudioEventHandlerRegistry> eventRegistry;
std::shared_ptr<MockAudioEventHandlerRegistry> eventRegistry;
std::unique_ptr<OfflineAudioContext> context;

void SetUp() override {
Expand Down