Try not to suggest same audio tracks

This commit is contained in:
emeric
2026-06-05 13:28:39 +02:00
parent 09468e2c2d
commit a4ab9829d5
2 changed files with 72 additions and 0 deletions
@@ -49,6 +49,7 @@
#include "math/PrincipalComponents.hpp"
#include "math/StatsAccumulator.hpp"
#include "NearDuplicateEmbeddingConstraint.hpp"
#include "track-selection-constraints/DuplicateTrackConstraint.hpp"
#include "track-selection-constraints/InterpolationFitConstraint.hpp"
#include "track-selection-constraints/MaxDistanceConstraint.hpp"
@@ -115,9 +116,11 @@ namespace lms::recommendation
constexpr float smoothTransitionWeight{ 0.2F };
constexpr float sameReleaseWeight{ 0.5F };
constexpr float sameArtistWeight{ 0.5F };
constexpr float nearDuplicateThreshold{ 0.01F };
_similarityEvaluator = {};
_similarityEvaluator.addHardConstraint(std::make_unique<DuplicateTrackConstraint>());
_similarityEvaluator.addHardConstraint(std::make_unique<NearDuplicateEmbeddingConstraint<ReducedDimCount>>(_trackVectors, nearDuplicateThreshold));
_similarityEvaluator.addHardConstraint(std::make_unique<MaxDistanceConstraint>(_trackDistanceThreshold));
_similarityEvaluator.addSoftConstraint(std::make_unique<InterpolationFitConstraint>(), interpolationFitWeight);
_similarityEvaluator.addSoftConstraint(std::make_unique<SmoothTransitionConstraint>(), smoothTransitionWeight);
@@ -126,6 +129,7 @@ namespace lms::recommendation
_pathEvaluator = {};
_pathEvaluator.addHardConstraint(std::make_unique<DuplicateTrackConstraint>());
_pathEvaluator.addHardConstraint(std::make_unique<NearDuplicateEmbeddingConstraint<ReducedDimCount>>(_trackVectors, nearDuplicateThreshold));
_pathEvaluator.addSoftConstraint(std::make_unique<InterpolationFitConstraint>(), interpolationFitWeight);
_pathEvaluator.addSoftConstraint(std::make_unique<SmoothTransitionConstraint>(), smoothTransitionWeight);
_pathEvaluator.addSoftConstraint(std::make_unique<SameReleaseConstraint>(_trackMetadata), sameReleaseWeight);
@@ -0,0 +1,68 @@
/*
* Copyright (C) 2026 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <unordered_map>
#include "database/objects/TrackId.hpp"
#include "math/NormalizedCosineDistance.hpp"
#include "math/Vector.hpp"
#include "track-selection-constraints/ITrackCandidateHardConstraint.hpp"
#include "Types.hpp"
namespace lms::recommendation
{
// Rejects any candidate whose embedding is closer than 'threshold' to any already-selected
// track's embedding — catches the same recording appearing in multiple compilations.
template<std::size_t ReducedDimCount>
class NearDuplicateEmbeddingConstraint : public ITrackCandidateHardConstraint
{
public:
using ReducedVector = math::Vector<ReducedDimCount, FloatType>;
using TrackVectorMap = std::unordered_map<db::TrackId, const ReducedVector*>;
NearDuplicateEmbeddingConstraint(const TrackVectorMap& trackVectors, float threshold)
: _trackVectors{ trackVectors }
, _threshold{ threshold }
{
}
bool rejects(const TrackCandidateContext& context) const override
{
const auto itCandidate{ _trackVectors.find(context.candidateTrackId) };
if (itCandidate == _trackVectors.cend())
return false;
const math::NormalizedCosineDistance distFunc{ *itCandidate->second };
for (const db::TrackId selectedId : context.selectedTracks)
{
const auto it{ _trackVectors.find(selectedId) };
if (it != _trackVectors.cend() && distFunc(*it->second) < _threshold)
return true;
}
return false;
}
private:
const TrackVectorMap& _trackVectors;
float _threshold;
};
} // namespace lms::recommendation