Skip to content

Commit

Permalink
Fix BeatMap::scale() -- which was totally broken.
Browse files Browse the repository at this point in the history
Add tests for BeatGrid::scale() and BeatMap::scale(). Potentially a fix
for Bug #1341181.
  • Loading branch information
rryan committed Nov 23, 2014
1 parent 1cfc3cf commit 8774366
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
22 changes: 22 additions & 0 deletions src/test/beatgridtest.cpp
Expand Up @@ -18,6 +18,28 @@ class BeatGridTest : public testing::Test {
}
};

TEST_F(BeatGridTest, Scale) {
TrackPointer pTrack(new TrackInfoObject(), &QObject::deleteLater);

int sampleRate = 44100;
double bpm = 60.0;
pTrack->setBpm(bpm);
pTrack->setSampleRate(sampleRate);

BeatGrid* pGrid = new BeatGrid(pTrack.data(), 0);
pGrid->setBpm(bpm);

EXPECT_DOUBLE_EQ(bpm, pGrid->getBpm());
pGrid->scale(2);
EXPECT_DOUBLE_EQ(2 * bpm, pGrid->getBpm());

pGrid->scale(0.5);
EXPECT_DOUBLE_EQ(bpm, pGrid->getBpm());

pGrid->scale(0.25);
EXPECT_DOUBLE_EQ(0.25 * bpm, pGrid->getBpm());
}

TEST_F(BeatGridTest, TestNthBeatWhenOnBeat) {
TrackPointer pTrack(new TrackInfoObject(), &QObject::deleteLater);

Expand Down
21 changes: 21 additions & 0 deletions src/test/beatmaptest.cpp
Expand Up @@ -44,6 +44,27 @@ class BeatMapTest : public testing::Test {
int m_iFrameSize;
};

TEST_F(BeatMapTest, Scale) {
const double bpm = 60.0;
m_pTrack->setBpm(bpm);
m_pTrack->setSampleRate(m_iSampleRate);
double beatLengthFrames = getBeatLengthFrames(bpm);
double startOffsetFrames = 7;
const int numBeats = 100;
// Note beats must be in frames, not samples.
QVector<double> beats = createBeatVector(startOffsetFrames, numBeats, beatLengthFrames);
BeatMap* pMap = new BeatMap(m_pTrack, 0, beats);

// Check edge cases

EXPECT_DOUBLE_EQ(bpm, pMap->getBpm());
pMap->scale(2);
EXPECT_DOUBLE_EQ(2 * bpm, pMap->getBpm());

pMap->scale(0.5);
EXPECT_DOUBLE_EQ(bpm, pMap->getBpm());
}

TEST_F(BeatMapTest, TestNthBeat) {
const double bpm = 60.0;
m_pTrack->setBpm(bpm);
Expand Down
17 changes: 10 additions & 7 deletions src/track/beatmap.cpp
Expand Up @@ -407,17 +407,20 @@ void BeatMap::translate(double dNumSamples) {

void BeatMap::scale(double dScalePercentage) {
QMutexLocker locker(&m_mutex);
if (!isValid() || dScalePercentage <= 0.0) {
if (!isValid() || dScalePercentage <= 0.0 || m_beats.isEmpty()) {
return;
}
// Scale every beat relative to the first one.
Beat firstBeat = m_beats.first();
for (BeatList::iterator it = m_beats.begin();
it != m_beats.end(); ++it) {

// Scale the distance between every beat by 1/dScalePercentage to scale the
// BPM by dScalePercentage.
const double kScaleBeatDistance = 1.0 / dScalePercentage;
Beat prevBeat = m_beats.first();
BeatList::iterator it = m_beats.begin() + 1;
for (; it != m_beats.end(); ++it) {
// Need to not accrue fractional frames.
double newFrame = floor(
(1 - dScalePercentage) * firstBeat.frame_position() +
dScalePercentage * it->frame_position());
(1 - kScaleBeatDistance) * prevBeat.frame_position() +
kScaleBeatDistance * it->frame_position());
it->set_frame_position(newFrame);
}
onBeatlistChanged();
Expand Down

0 comments on commit 8774366

Please sign in to comment.