Skip to content

Commit

Permalink
Get rid of Track object dependency (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
Holzhaus committed Feb 16, 2020
1 parent 327ea3a commit 0e87d94
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 142 deletions.
73 changes: 19 additions & 54 deletions src/track/serato/markers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "engine/engine.h"
#include "track/cue.h"
#include "track/serato/tags.h"
#include "track/track.h"
#include "util/color/rgbcolor.h"

Expand All @@ -13,7 +14,6 @@ const int kNumEntries = 14;
const int kLoopEntryStartIndex = 5;
const int kEntrySize = 22;
const quint16 kVersion = 0x0205;
constexpr mixxx::RgbColor kDefaultTrackColor = mixxx::RgbColor(0xFF9999);

// These functions conversion between the 4-byte "Serato Markers_" color format
// and RgbColor (3-Byte RGB, transparency disabled).
Expand Down Expand Up @@ -265,23 +265,33 @@ QByteArray SeratoMarkers::dump() const {
SeratoMarkersEntryPointer pEntry = m_entries.at(i);
stream.writeRawData(pEntry->dump(), kEntrySize);
}
stream << seratoColorFromRgb(m_trackColor.value_or(kDefaultTrackColor));
stream << seratoColorFromRgb(m_trackColor.value_or(SeratoTags::kDefaultTrackColor));
return data;
}

void SeratoMarkers::syncToTrackObject(Track* track, bool clearOtherValues) const {
qDebug() << "Syncing 'Serato Markers_' tag data to Track object..."
<< "(other values will be"
<< (clearOtherValues ? "cleared)" : "kept)");
QList<CuePointer> SeratoMarkers::getCues(TrackId trackId, int sampleRate) const {
qDebug() << "Reading cues from 'Serato Markers_' tag data...";

QMap<int, mixxx::SeratoMarkersEntryPointer> cueMap;
const double sampleRateKhz = sampleRate / 1000.0;
const double samples = sampleRateKhz * mixxx::kEngineChannelCount;
VERIFY_OR_DEBUG_ASSERT(samples != 0) {
qWarning() << "Failed to calculate samples factor, cannot read cues!";
return QList<CuePointer>();
}

QList<CuePointer> cues;
int cueIndex = 0;
for (const auto& pEntry : m_entries) {
DEBUG_ASSERT(pEntry);
switch (pEntry->typeId()) {
case SeratoMarkersEntry::TypeId::Cue: {
cueMap.insert(cueIndex, pEntry);
CuePointer pCue(new Cue());

pCue->setType(Cue::Type::HotCue);
pCue->setHotCue(cueIndex);
pCue->setStartPosition(static_cast<double>(pEntry->getStartPosition()) * samples);
// TODO: Add support for hotcue colors

cueIndex++;
break;
}
Expand All @@ -291,52 +301,7 @@ void SeratoMarkers::syncToTrackObject(Track* track, bool clearOtherValues) const
}
}

const double sampleRateKhz = track->getSampleRate() / 1000.0;
const double samples = sampleRateKhz * mixxx::kEngineChannelCount;
VERIFY_OR_DEBUG_ASSERT(samples != 0) {
qWarning() << "Failed to calculate samples factor, cannot import cues!";
return;
}

// Update existing hotcues
for (const CuePointer& pCue : track->getCuePoints()) {
if (pCue->getType() != Cue::Type::HotCue) {
continue;
}

const mixxx::SeratoMarkersEntryPointer cueEntry = cueMap.take(pCue->getHotCue());
if (!cueEntry || !cueEntry->hasStartPosition()) {
if ((cueEntry && !cueEntry->hasStartPosition()) || clearOtherValues) {
// Delete cue if
// a) entry is present but has no position, or
// b) entry is not present and clearOtherValues is true
qDebug() << "Removed existing hotcue" << pCue->getHotCue() << pCue->getLabel() << "at" << pCue->getPosition();
track->removeCue(pCue);
}
continue;
}

pCue->setType(Cue::Type::HotCue);
pCue->setStartPosition(static_cast<double>(cueEntry->getStartPosition()) * samples);
if (clearOtherValues) {
// Reset label if clearOtherValues is set
pCue->setLabel("");
}
// TODO: Add support for hotcue colors
qDebug() << "Updated existing hotcue" << pCue->getHotCue() << pCue->getLabel() << "at" << pCue->getPosition();
}

// Add new cues
for (auto i = cueMap.constBegin(); i != cueMap.constEnd(); i++) {
CuePointer pCue = CuePointer(track->createAndAddCue());

pCue->setType(Cue::Type::HotCue);
pCue->setHotCue(i.key());
pCue->setStartPosition(static_cast<double>(i.value()->getStartPosition()) * samples);
// TODO: Add support for hotcue colors
qDebug() << "Added new hotcue" << pCue->getHotCue() << "at" << pCue->getPosition();
}
qDebug() << "Syncing 'Serato Markers_' tag data to Track object completed";
return cues;
}

} //namespace mixxx
6 changes: 2 additions & 4 deletions src/track/serato/markers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
#include <QList>
#include <memory>

#include "track/cue.h"
#include "util/color/rgbcolor.h"
#include "util/types.h"

// Forward declaration
class Track;

namespace mixxx {

class SeratoMarkersEntry;
Expand Down Expand Up @@ -158,7 +156,7 @@ class SeratoMarkers final {
m_trackColor = color;
}

void syncToTrackObject(Track* track, bool clearOtherValues = true) const;
QList<CuePointer> getCues(int sampleRate) const;

private:
QList<SeratoMarkersEntryPointer> m_entries;
Expand Down
84 changes: 42 additions & 42 deletions src/track/serato/markers2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <QtEndian>

#include "engine/engine.h"
#include "track/cue.h"
#include "track/track.h"

namespace {
Expand Down Expand Up @@ -430,21 +429,31 @@ QByteArray SeratoMarkers2::dump() const {
return outerData.leftJustified(size, '\0');
}

void SeratoMarkers2::syncToTrackObject(Track* track) const {
qDebug() << "Syncing 'Serato Markers2' tag data to Track object...";
QList<CuePointer> SeratoMarkers2::getCues(int sampleRate) const {
qDebug() << "Reading cues from 'Serato Markers2' tag data...";

QMap<int, const mixxx::SeratoMarkers2CueEntry*> cueMap;
const double sampleRateKhz = sampleRate / 1000.0;
const double samples = sampleRateKhz * mixxx::kEngineChannelCount;
VERIFY_OR_DEBUG_ASSERT(samples != 0) {
qWarning() << "Failed to calculate samples factor, cannot read cues!";
return;
}

QList<CuePointer> cues;
for (auto& pEntry : m_entries) {
DEBUG_ASSERT(pEntry);
switch (pEntry->typeId()) {
case SeratoMarkers2Entry::TypeId::Cue: {
const mixxx::SeratoMarkers2CueEntry* pCueEntry = static_cast<mixxx::SeratoMarkers2CueEntry*>(pEntry.get());
cueMap.insert(pCueEntry->getIndex(), pCueEntry);
break;
}
case SeratoMarkers2Entry::TypeId::Bpmlock: {
const mixxx::SeratoMarkers2BpmlockEntry* pBpmlockEntry = static_cast<mixxx::SeratoMarkers2BpmlockEntry*>(pEntry.get());
track->setBpmLocked(pBpmlockEntry->isLocked());

CuePointer pCue = CuePointer(new Cue());
pCue->setType(Cue::Type::HotCue);
pCue->setHotCue(cueEntry->getIndex());
pCue->setStartPosition(static_cast<double>(cueEntry->getPosition()) * samples);
pCue->setLabel(cueEntry->getLabel());
// TODO: Add support for hotcue colors
cues.push_back(pCue);

break;
}
// TODO: Add support for COLOR/LOOP/FLIP
Expand All @@ -453,46 +462,37 @@ void SeratoMarkers2::syncToTrackObject(Track* track) const {
}
}

const double sampleRateKhz = track->getSampleRate() / 1000.0;
const double samples = sampleRateKhz * mixxx::kEngineChannelCount;
VERIFY_OR_DEBUG_ASSERT(samples != 0) {
qWarning() << "Failed to calculate samples factor, cannot import cues!";
return;
}
return cues;
}

RgbColor::optional_t SeratoMarkers2::getTrackColor() const {
qDebug() << "Reading track color from 'Serato Markers2' tag data...";

// Update existing hotcues
for (const CuePointer& pCue : track->getCuePoints()) {
if (pCue->getType() != Cue::Type::HotCue) {
for (auto& pEntry : m_entries) {
DEBUG_ASSERT(pEntry);
if (pEntry->typeId() != SeratoMarkers2Entry::TypeId::Color) {
continue;
}
const mixxx::SeratoMarkers2ColorEntry* pColorEntry = static_cast<mixxx::SeratoMarkers2ColorEntry*>(pEntry.get());
return RgbColor::optional(pColorEntry->getColor());
}

return std::nullopt;
}

bool SeratoMarkers2::isBpmLocked() const {
qDebug() << "Reading bpmlock state from 'Serato Markers2' tag data...";

const mixxx::SeratoMarkers2CueEntry* cueEntry = cueMap.take(pCue->getHotCue());
if (!cueEntry) {
qWarning() << "Removed existing hotcue" << pCue->getHotCue() << pCue->getLabel() << "at" << pCue->getPosition();
track->removeCue(pCue);
for (auto& pEntry : m_entries) {
DEBUG_ASSERT(pEntry);
if (pEntry->typeId() != SeratoMarkers2Entry::TypeId::Bpmlock) {
continue;
}

pCue->setType(Cue::Type::HotCue);
pCue->setStartPosition(static_cast<double>(cueEntry->getPosition()) * samples);
pCue->setLabel(cueEntry->getLabel());
// TODO: Add support for hotcue colors
qWarning() << "Updated existing hotcue" << pCue->getHotCue() << pCue->getLabel() << "at" << pCue->getPosition();
const mixxx::SeratoMarkers2BpmlockEntry* pBpmlockEntry = static_cast<mixxx::SeratoMarkers2BpmlockEntry*>(pEntry.get());
return pBpmlockEntry->isLocked();
}

// Add new cues
for (auto cueEntry : cueMap.values()) {
DEBUG_ASSERT(cueEntry);
CuePointer pCue = CuePointer(track->createAndAddCue());

pCue->setType(Cue::Type::HotCue);
pCue->setHotCue(cueEntry->getIndex());
pCue->setStartPosition(static_cast<double>(cueEntry->getPosition()) * samples);
pCue->setLabel(cueEntry->getLabel());
// TODO: Add support for hotcue colors
qWarning() << "Added new hotcue" << pCue->getHotCue() << pCue->getLabel() << "at" << pCue->getPosition();
}
qDebug() << "Syncing 'Serato Markers2' tag data to Track object completed";
return false;
}

} //namespace mixxx
41 changes: 9 additions & 32 deletions src/track/serato/markers2.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@
#include <QList>
#include <memory>

#include "track/cue.h"
#include "util/color/rgbcolor.h"
#include "util/types.h"

// forward declaration(s)
class Track;

namespace {
constexpr mixxx::RgbColor kDefaultTrackColor = mixxx::RgbColor(0xFF9999);
constexpr mixxx::RgbColor kDefaultCueColor = mixxx::RgbColor(0xCC0000);
} // namespace

namespace mixxx {

// Enum values need to appear in the same order as the corresponding entries
Expand Down Expand Up @@ -64,6 +57,7 @@ class SeratoMarkers2UnknownEntry : public SeratoMarkers2Entry {
: m_type(std::move(type)),
m_data(std::move(data)) {
}
SeratoMarkers2UnknownEntry() = delete;
~SeratoMarkers2UnknownEntry() override = default;

QString type() const override {
Expand Down Expand Up @@ -92,10 +86,7 @@ class SeratoMarkers2BpmlockEntry : public SeratoMarkers2Entry {
SeratoMarkers2BpmlockEntry(bool locked)
: m_locked(locked) {
}

SeratoMarkers2BpmlockEntry()
: m_locked(false) {
}
SeratoMarkers2BpmlockEntry() = delete;

static SeratoMarkers2EntryPointer parse(const QByteArray& data);

Expand Down Expand Up @@ -142,10 +133,7 @@ class SeratoMarkers2ColorEntry : public SeratoMarkers2Entry {
SeratoMarkers2ColorEntry(RgbColor color)
: m_color(color) {
}

SeratoMarkers2ColorEntry()
: m_color(kDefaultTrackColor) {
}
SeratoMarkers2ColorEntry() = delete;

static SeratoMarkers2EntryPointer parse(const QByteArray& data);

Expand Down Expand Up @@ -195,13 +183,7 @@ class SeratoMarkers2CueEntry : public SeratoMarkers2Entry {
m_color(color),
m_label(label) {
}

SeratoMarkers2CueEntry()
: m_index(0),
m_position(0),
m_color(kDefaultCueColor),
m_label(QString("")) {
}
SeratoMarkers2CueEntry() = delete;

static SeratoMarkers2EntryPointer parse(const QByteArray& data);

Expand Down Expand Up @@ -285,14 +267,7 @@ class SeratoMarkers2LoopEntry : public SeratoMarkers2Entry {
m_locked(locked),
m_label(label) {
}

SeratoMarkers2LoopEntry()
: m_index(0),
m_startposition(0),
m_endposition(0),
m_locked(false),
m_label(QString("")) {
}
SeratoMarkers2LoopEntry() = delete;

static SeratoMarkers2EntryPointer parse(const QByteArray& data);

Expand Down Expand Up @@ -422,7 +397,9 @@ class SeratoMarkers2 final {
m_entries = std::move(entries);
}

void syncToTrackObject(Track* track) const;
QList<CuePointer> getCues(int sampleRate) const;
RgbColor::optional_t getTrackColor() const;
bool isBpmLocked() const;

private:
int m_allocatedSize;
Expand Down

0 comments on commit 0e87d94

Please sign in to comment.