Skip to content

Commit

Permalink
Merge pull request #4233 from Holzhaus/beats-frombytearray
Browse files Browse the repository at this point in the history
Add Beats::fromByteArray() static method
  • Loading branch information
uklotzde authored Aug 19, 2021
2 parents 0efa809 + 8299a06 commit ccef574
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/library/dao/trackdao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ bool setTrackBeats(const QSqlRecord& record, const int column, Track* pTrack) {
QString beatsSubVersion = record.value(column + 2).toString();
QByteArray beatsBlob = record.value(column + 3).toByteArray();
bool bpmLocked = record.value(column + 4).toBool();
const mixxx::BeatsPointer pBeats = BeatFactory::loadBeatsFromByteArray(
const mixxx::BeatsPointer pBeats = mixxx::Beats::fromByteArray(
pTrack->getSampleRate(), beatsVersion, beatsSubVersion, beatsBlob);
if (pBeats) {
if (bpmLocked) {
Expand Down
19 changes: 0 additions & 19 deletions src/track/beatfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,6 @@ const QString kRoundingVersion = QStringLiteral("V4");

} // namespace

mixxx::BeatsPointer BeatFactory::loadBeatsFromByteArray(
mixxx::audio::SampleRate sampleRate,
const QString& beatsVersion,
const QString& beatsSubVersion,
const QByteArray& beatsSerialized) {
if (beatsVersion == BEAT_GRID_1_VERSION ||
beatsVersion == BEAT_GRID_2_VERSION) {
auto pGrid = mixxx::BeatGrid::makeBeatGrid(sampleRate, beatsSubVersion, beatsSerialized);
qDebug() << "Successfully deserialized BeatGrid";
return pGrid;
} else if (beatsVersion == BEAT_MAP_VERSION) {
auto pMap = mixxx::BeatMap::makeBeatMap(sampleRate, beatsSubVersion, beatsSerialized);
qDebug() << "Successfully deserialized BeatMap";
return pMap;
}
qDebug() << "BeatFactory::loadBeatsFromByteArray could not parse serialized beats.";
return mixxx::BeatsPointer();
}

mixxx::BeatsPointer BeatFactory::makeBeatGrid(
mixxx::audio::SampleRate sampleRate,
mixxx::Bpm bpm,
Expand Down
5 changes: 0 additions & 5 deletions src/track/beatfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ class Track;

class BeatFactory {
public:
static mixxx::BeatsPointer loadBeatsFromByteArray(
mixxx::audio::SampleRate sampleRate,
const QString& beatsVersion,
const QString& beatsSubVersion,
const QByteArray& beatsSerialized);
static mixxx::BeatsPointer makeBeatGrid(
mixxx::audio::SampleRate sampleRate,
mixxx::Bpm bpm,
Expand Down
2 changes: 1 addition & 1 deletion src/track/beatgrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ BeatsPointer BeatGrid::makeBeatGrid(
}

// static
BeatsPointer BeatGrid::makeBeatGrid(
BeatsPointer BeatGrid::fromByteArray(
audio::SampleRate sampleRate,
const QString& subVersion,
const QByteArray& byteArray) {
Expand Down
2 changes: 1 addition & 1 deletion src/track/beatgrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BeatGrid final : public Beats {
mixxx::Bpm bpm,
mixxx::audio::FramePos firstBeatPos);

static BeatsPointer makeBeatGrid(
static BeatsPointer fromByteArray(
audio::SampleRate sampleRate,
const QString& subVersion,
const QByteArray& byteArray);
Expand Down
2 changes: 1 addition & 1 deletion src/track/beatmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ BeatMap::BeatMap(const BeatMap& other)
}

// static
BeatsPointer BeatMap::makeBeatMap(
BeatsPointer BeatMap::fromByteArray(
audio::SampleRate sampleRate,
const QString& subVersion,
const QByteArray& byteArray) {
Expand Down
2 changes: 1 addition & 1 deletion src/track/beatmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BeatMap final : public Beats {

~BeatMap() override = default;

static BeatsPointer makeBeatMap(
static BeatsPointer fromByteArray(
audio::SampleRate sampleRate,
const QString& subVersion,
const QByteArray& byteArray);
Expand Down
30 changes: 30 additions & 0 deletions src/track/beats.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
#include "track/beats.h"

#include "track/beatgrid.h"
#include "track/beatmap.h"

namespace mixxx {

// static
mixxx::BeatsPointer Beats::fromByteArray(
mixxx::audio::SampleRate sampleRate,
const QString& beatsVersion,
const QString& beatsSubVersion,
const QByteArray& beatsSerialized) {
mixxx::BeatsPointer pBeats = nullptr;
if (beatsVersion == BEAT_GRID_1_VERSION || beatsVersion == BEAT_GRID_2_VERSION) {
pBeats = mixxx::BeatGrid::fromByteArray(sampleRate, beatsSubVersion, beatsSerialized);
} else if (beatsVersion == BEAT_MAP_VERSION) {
pBeats = mixxx::BeatMap::fromByteArray(sampleRate, beatsSubVersion, beatsSerialized);
} else {
qWarning().nospace() << "Failed to deserialize Beats (" << beatsVersion
<< "): Invalid beats version";
return nullptr;
}

if (!pBeats) {
qWarning().nospace() << "Failed to deserialize Beats (" << beatsVersion
<< "): Parsing failed";
return nullptr;
}

qDebug().nospace() << "Successfully deserialized Beats (" << beatsVersion << ")";
return pBeats;
}

int Beats::numBeatsInRange(audio::FramePos startPosition, audio::FramePos endPosition) const {
audio::FramePos lastPosition = audio::kStartFramePos;
int i = 1;
Expand Down
6 changes: 6 additions & 0 deletions src/track/beats.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class Beats {
public:
virtual ~Beats() = default;

static mixxx::BeatsPointer fromByteArray(
mixxx::audio::SampleRate sampleRate,
const QString& beatsVersion,
const QString& beatsSubVersion,
const QByteArray& beatsSerialized);

enum class BpmScale {
Double,
Halve,
Expand Down

0 comments on commit ccef574

Please sign in to comment.