summaryrefslogtreecommitdiffstats
path: root/src/test/readaheadmanager_test.cpp
blob: afdb5dd90c23995c0322faddb4ea9395913fc636 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <gtest/gtest.h>

#include <QtDebug>
#include <QScopedPointer>

#include "engine/cachingreader.h"
#include "control/controlobject.h"
#include "engine/loopingcontrol.h"
#include "engine/readaheadmanager.h"
#include "test/mixxxtest.h"
#include "util/assert.h"
#include "util/defs.h"
#include "util/sample.h"

class StubReader : public CachingReader {
  public:
    StubReader()
            : CachingReader("[test]", UserSettingsPointer()) { }

    SINT read(SINT startSample, SINT numSamples, bool reverse,
             CSAMPLE* buffer) override {
        Q_UNUSED(startSample);
        Q_UNUSED(reverse);
        SampleUtil::clear(buffer, numSamples);
        return numSamples;
    }
};

class StubLoopControl : public LoopingControl {
  public:
    StubLoopControl()
            : LoopingControl("[test]", UserSettingsPointer()) { }

    void pushTriggerReturnValue(double value) {
        m_triggerReturnValues.push_back(value);
    }

    void pushProcessReturnValue(double value) {
        m_processReturnValues.push_back(value);
    }

    double nextTrigger(const double dRate,
                       const double currentSample,
                       const double totalSamples,
                       const int iBufferSize) override {
        Q_UNUSED(dRate);
        Q_UNUSED(currentSample);
        Q_UNUSED(totalSamples);
        Q_UNUSED(iBufferSize);
        RELEASE_ASSERT(!m_triggerReturnValues.isEmpty());
        return m_triggerReturnValues.takeFirst();
    }

    double process(const double dRate,
                   const double dCurrentSample,
                   const double dTotalSamples,
                   const int iBufferSize) override {
        Q_UNUSED(dRate);
        Q_UNUSED(dCurrentSample);
        Q_UNUSED(dTotalSamples);
        Q_UNUSED(iBufferSize);
        RELEASE_ASSERT(!m_processReturnValues.isEmpty());
        return m_processReturnValues.takeFirst();
    }

    // getTrigger returns the sample that the engine will next be triggered to
    // loop to, given the value of currentSample and dRate.
    double getTrigger(const double dRate,
                      const double currentSample,
                      const double totalSamples,
                      const int iBufferSize) override {
        Q_UNUSED(dRate);
        Q_UNUSED(currentSample);
        Q_UNUSED(totalSamples);
        Q_UNUSED(iBufferSize);
        return kNoTrigger;
    }

    // hintReader has no effect in this stubbed class
    void hintReader(HintVector* pHintList) override {
        Q_UNUSED(pHintList);
    }

    void notifySeek(double dNewPlaypos) {
        Q_UNUSED(dNewPlaypos);
    }

  public slots:
    void trackLoaded(TrackPointer pTrack, TrackPointer pOldTrack) override {
        Q_UNUSED(pTrack);
        Q_UNUSED(pOldTrack);
    }

  protected:
    QList<double> m_triggerReturnValues;
    QList<double> m_processReturnValues;
};

class ReadAheadManagerTest : public MixxxTest {
  public:
    ReadAheadManagerTest() : m_pBuffer(SampleUtil::alloc(MAX_BUFFER_LEN)) { }
  protected:
    void SetUp() override {
        SampleUtil::clear(m_pBuffer, MAX_BUFFER_LEN);
        m_pReader.reset(new StubReader());
        m_pLoopControl.reset(new StubLoopControl());
        m_pReadAheadManager.reset(new ReadAheadManager(m_pReader.data(),
                                                       m_pLoopControl.data()));
    }

    QScopedPointer<StubReader> m_pReader;
    QScopedPointer<StubLoopControl> m_pLoopControl;
    QScopedPointer<ReadAheadManager> m_pReadAheadManager;
    CSAMPLE* m_pBuffer;
};

TEST_F(ReadAheadManagerTest, LoopEnableSeekBackward) {
    // If a loop is enabled and the current playposition is ahead of the loop,
    // we should seek to the beginning of the loop.
    m_pReadAheadManager->notifySeek(110);
    // Trigger value means, the sample that triggers the loop (loop out)
    m_pLoopControl->pushTriggerReturnValue(100);
    // Process value is the sample we should seek to
    m_pLoopControl->pushProcessReturnValue(10);
    EXPECT_EQ(0, m_pReadAheadManager->getNextSamples(1.0, m_pBuffer, 100));
    EXPECT_EQ(10, m_pReadAheadManager->getPlaypos());
}

TEST_F(ReadAheadManagerTest, InReverseLoopEnableSeekForward) {
    // If we are in reverse, a loop is enabled, and the current playposition
    // is before of the loop, we should seek to the out point of the loop.
    m_pReadAheadManager->notifySeek(1);
    // Trigger value means, the sample that triggers the loop (loop in)
    m_pLoopControl->pushTriggerReturnValue(10);
    // Process value is the sample we should seek to.
    m_pLoopControl->pushProcessReturnValue(100);
    EXPECT_EQ(0, m_pReadAheadManager->getNextSamples(-1.0, m_pBuffer, 100));
    EXPECT_EQ(100, m_pReadAheadManager->getPlaypos());
}

TEST_F(ReadAheadManagerTest, FractionalFrameLoop) {
    // If we are in reverse, a loop is enabled, and the current playposition
    // is before of the loop, we should seek to the out point of the loop.
    m_pReadAheadManager->notifySeek(0.5);
    // Trigger value means, the sample that triggers the loop (loop in)
    m_pLoopControl->pushTriggerReturnValue(20.2);
    m_pLoopControl->pushTriggerReturnValue(20.2);
    m_pLoopControl->pushTriggerReturnValue(20.2);
    m_pLoopControl->pushTriggerReturnValue(20.2);
    m_pLoopControl->pushTriggerReturnValue(20.2);
    m_pLoopControl->pushTriggerReturnValue(20.2);
    // Process value is the sample we should seek to.
    m_pLoopControl->pushProcessReturnValue(3.3);
    m_pLoopControl->pushProcessReturnValue(3.3);
    m_pLoopControl->pushProcessReturnValue(3.3);
    m_pLoopControl->pushProcessReturnValue(3.3);
    m_pLoopControl->pushProcessReturnValue(3.3);
    m_pLoopControl->pushProcessReturnValue(kNoTrigger);
    // read from start to loop trigger, overshoot 0.3
    EXPECT_EQ(20, m_pReadAheadManager->getNextSamples(1.0, m_pBuffer, 100));
    // read loop
    EXPECT_EQ(18, m_pReadAheadManager->getNextSamples(1.0, m_pBuffer, 80));
    // read loop
    EXPECT_EQ(16, m_pReadAheadManager->getNextSamples(1.0, m_pBuffer, 62));
    // read loop
    EXPECT_EQ(18, m_pReadAheadManager->getNextSamples(1.0, m_pBuffer, 46));
    // read loop
    EXPECT_EQ(16, m_pReadAheadManager->getNextSamples(1.0, m_pBuffer, 28));
    // read loop
    EXPECT_EQ(12, m_pReadAheadManager->getNextSamples(1.0, m_pBuffer, 12));

    // start 0.5 to 20.2 = 19.7
    // loop 3.3 to 20.2 = 16.9
    // 100 - 19,7 - 4 * 16,9 = 12,7
    // 12.7 + 3.3 = 16

    // The rounding error must not exceed a half frame (one samples in stereo)
    EXPECT_NEAR(16, m_pReadAheadManager->getPlaypos(), 1);
}