Skip to content

Commit

Permalink
Merge pull request #4077 from Holzhaus/frame-refactor-engineprime
Browse files Browse the repository at this point in the history
EnginePrimeExportJob: Use mixxx::audio::FramePos internally
  • Loading branch information
uklotzde authored Jul 8, 2021
2 parents 637802d + 5fdb296 commit af8f905
Showing 1 changed file with 15 additions and 26 deletions.
41 changes: 15 additions & 26 deletions src/library/export/engineprimeexportjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,6 @@ std::optional<djinterop::musical_key> toDjinteropKey(
return keyMap[key];
}

inline double playPosToSampleOffset(double playPos) {
// Play positions are twice the sample offset expected by libdjinterop.
return playPos / 2;
}

inline double sampleOffsetToPlayPos(double sampleOffset) {
// Play positions are twice the sample offset expected by libdjinterop.
return sampleOffset * 2;
}

QString exportFile(const QSharedPointer<EnginePrimeExportRequest> pRequest,
TrackPointer pTrack) {
if (!pRequest->engineLibraryDbDir.exists()) {
Expand Down Expand Up @@ -164,9 +154,9 @@ void exportMetadata(djinterop::database* pDatabase,
snapshot.rating = pTrack->getRating() * 20; // note rating is in range 0-100

// Frames used interchangeably with "samples" here.
const auto sampleCount = static_cast<int64_t>(pTrack->getDuration() * pTrack->getSampleRate());
const auto frameCount = static_cast<int64_t>(pTrack->getDuration() * pTrack->getSampleRate());
snapshot.sampling = djinterop::sampling_info{
static_cast<double>(pTrack->getSampleRate()), sampleCount};
static_cast<double>(pTrack->getSampleRate()), frameCount};

// Set track loudness.
// Note that the djinterop API method for setting loudness may be revised
Expand All @@ -177,10 +167,10 @@ void exportMetadata(djinterop::database* pDatabase,
snapshot.average_loudness = pTrack->getReplayGain().getRatio();

// Set main cue-point.
double cuePlayPos = pTrack->getCuePoint().getPosition();
double cueSampleOffset = playPosToSampleOffset(cuePlayPos);
snapshot.default_main_cue = cueSampleOffset;
snapshot.adjusted_main_cue = cueSampleOffset;
mixxx::audio::FramePos cuePlayPos = pTrack->getMainCuePosition();
// FIXME: What if the cuePlayPos is invalid?
snapshot.default_main_cue = cuePlayPos.value();
snapshot.adjusted_main_cue = cuePlayPos.value();

// Fill in beat grid. For now, assume a constant average BPM across
// the whole track. Note that points in the track are specified as
Expand All @@ -194,24 +184,21 @@ void exportMetadata(djinterop::database* pDatabase,
// beginning of the track in 4-beat decrements to find the first beat
// in the track that also aligns with the start of a bar.
const auto firstBeatPlayPos = beats->firstBeat();
const auto cueBeatPlayPos = beats->findClosestBeat(
mixxx::audio::FramePos::fromEngineSamplePos(cuePlayPos));
const auto cueBeatPlayPos = beats->findClosestBeat(cuePlayPos);
int numBeatsToCue = beats->numBeatsInRange(firstBeatPlayPos, cueBeatPlayPos);
const auto firstBarAlignedBeatPlayPos = beats->findNBeatsFromPosition(
cueBeatPlayPos, numBeatsToCue & ~0x3);

// We will treat the first bar-aligned beat as beat zero. Find the
// number of beats from there until the end of the track in order to
// correctly assign an index for the last beat.
const auto lastBeatPlayPos =
beats->findPrevBeat(mixxx::audio::FramePos::fromEngineSamplePos(
sampleOffsetToPlayPos(sampleCount)));
const auto lastBeatPlayPos = beats->findPrevBeat(mixxx::audio::kStartFramePos + frameCount);
int numBeats = beats->numBeatsInRange(firstBarAlignedBeatPlayPos, lastBeatPlayPos);
if (numBeats > 0) {
std::vector<djinterop::beatgrid_marker> beatgrid{
{0, playPosToSampleOffset(firstBarAlignedBeatPlayPos.toEngineSamplePos())},
{numBeats, playPosToSampleOffset(lastBeatPlayPos.toEngineSamplePos())}};
beatgrid = el::normalize_beatgrid(std::move(beatgrid), sampleCount);
{0, firstBarAlignedBeatPlayPos.value()},
{numBeats, lastBeatPlayPos.value()}};
beatgrid = el::normalize_beatgrid(std::move(beatgrid), frameCount);
snapshot.default_beatgrid = beatgrid;
snapshot.adjusted_beatgrid = beatgrid;
} else {
Expand Down Expand Up @@ -248,7 +235,7 @@ void exportMetadata(djinterop::database* pDatabase,

djinterop::hot_cue hotCue{};
hotCue.label = label.toStdString();
hotCue.sample_offset = playPosToSampleOffset(pCue->getPosition().toEngineSamplePos());
hotCue.sample_offset = pCue->getPosition().value();

auto color = mixxx::RgbColor::toQColor(pCue->getColor());
hotCue.color = djinterop::pad_color{
Expand All @@ -266,6 +253,8 @@ void exportMetadata(djinterop::database* pDatabase,
// from a set of stored loops (and is easily overwritten), we do not export
// it to the external database here.
//
// TODO: This comment above is wrong, it does support saved loops now.
//
// Note also that the loops on any existing track are not modified here.

// Write waveform.
Expand All @@ -274,7 +263,7 @@ void exportMetadata(djinterop::database* pDatabase,
if (pWaveform) {
int64_t samplesPerEntry =
el::required_waveform_samples_per_entry(pTrack->getSampleRate());
int64_t externalWaveformSize = (sampleCount + samplesPerEntry - 1) / samplesPerEntry;
int64_t externalWaveformSize = (frameCount + samplesPerEntry - 1) / samplesPerEntry;
std::vector<djinterop::waveform_entry> externalWaveform;
externalWaveform.reserve(externalWaveformSize);
for (int64_t i = 0; i < externalWaveformSize; ++i) {
Expand Down

0 comments on commit af8f905

Please sign in to comment.