Skip to content

Commit cb9d85a

Browse files
committed
Fix pitch parameter mapping in LibreSVIP conversions
1 parent 927639b commit cb9d85a

4 files changed

Lines changed: 110 additions & 1 deletion

File tree

‎src/plugins/libresvipformatconverter/internal/LibreSVIPFileExporter.cpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <libresvipformatconverter/internal/LibreSVIPConversionWizard.h>
2121
#include <libresvipformatconverter/internal/LibreSVIPManager.h>
22+
#include <libresvipformatconverter/internal/LibreSVIPPitchMapper.h>
2223
#include <libresvipformatconverter/internal/prebuiltinformats.h>
2324

2425
namespace LibreSVIPFormatConverter::Internal {
@@ -50,9 +51,11 @@ namespace LibreSVIPFormatConverter::Internal {
5051
}
5152

5253
bool LibreSVIPFileExporter::execExport(const QString &path, const opendspx::Model &model, QWindow *window) {
54+
auto convertedModel = model;
55+
LibreSVIPPitchMapper::mapModel(convertedModel, LibreSVIPPitchMapper::Direction::ToLibreSVIP);
5356
opendspx::SerializationErrorList serializationErrors;
5457
std::stringstream stream(std::ios::out | std::ios::binary);
55-
opendspx::Serializer::serialize(stream, model, serializationErrors, opendspx::Serializer::CheckError, opendspx::Serializer::Compress | opendspx::Serializer::WriteSchema);
58+
opendspx::Serializer::serialize(stream, convertedModel, serializationErrors, opendspx::Serializer::CheckError, opendspx::Serializer::Compress | opendspx::Serializer::WriteSchema);
5659
if (serializationErrors.containsFatal() || serializationErrors.containsError()) {
5760
qCCritical(lcLibreSVIPFileExporter) << "Failed to serialize DSPX data for LibreSVIP";
5861
SVS::MessageBox::critical(Core::RuntimeInterface::qmlEngine(), window,

‎src/plugins/libresvipformatconverter/internal/LibreSVIPFileImporter.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <libresvipformatconverter/internal/LibreSVIPConversionWizard.h>
2121
#include <libresvipformatconverter/internal/LibreSVIPManager.h>
22+
#include <libresvipformatconverter/internal/LibreSVIPPitchMapper.h>
2223
#include <libresvipformatconverter/internal/prebuiltinformats.h>
2324

2425
namespace LibreSVIPFormatConverter::Internal {
@@ -93,6 +94,7 @@ namespace LibreSVIPFormatConverter::Internal {
9394
tr("The DSPX data returned by LibreSVIP could not be opened."));
9495
return false;
9596
}
97+
LibreSVIPPitchMapper::mapModel(convertedModel, LibreSVIPPitchMapper::Direction::FromLibreSVIP);
9698
model = std::move(convertedModel);
9799
return true;
98100
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// SPDX-FileCopyrightText: Team OpenVPI
2+
// SPDX-License-Identifier: GPL-3.0-only
3+
4+
#include "LibreSVIPPitchMapper.h"
5+
6+
#include <cmath>
7+
#include <memory>
8+
#include <vector>
9+
10+
#include <opendspx/model.h>
11+
#include <opendspx/paramcurveanchor.h>
12+
#include <opendspx/paramcurvefree.h>
13+
#include <opendspx/singingclip.h>
14+
15+
#include <coreplugin/ArchitectureInfo.h>
16+
17+
namespace LibreSVIPFormatConverter::Internal {
18+
19+
namespace {
20+
21+
constexpr double maximumPitchValue = 12800.0;
22+
23+
void mapCurves(std::vector<opendspx::ParamCurveRef> &curves, LibreSVIPPitchMapper::Direction direction) {
24+
const auto mapValue = [direction](int value) {
25+
if (direction == LibreSVIPPitchMapper::Direction::FromLibreSVIP) {
26+
return Core::ParameterInfo::toDspxModelValue(value / maximumPitchValue);
27+
}
28+
return static_cast<int>(std::llround(Core::ParameterInfo::fromDspxModelValue(value) * maximumPitchValue));
29+
};
30+
for (auto &curve : curves) {
31+
if (!curve) {
32+
continue;
33+
}
34+
switch (curve->type) {
35+
case opendspx::ParamCurve::Free: {
36+
auto mappedCurve = std::make_shared<opendspx::ParamCurveFree>(*std::static_pointer_cast<opendspx::ParamCurveFree>(curve));
37+
for (auto &value : mappedCurve->values) {
38+
value = mapValue(value);
39+
}
40+
curve = mappedCurve;
41+
break;
42+
}
43+
case opendspx::ParamCurve::Anchor: {
44+
auto mappedCurve = std::make_shared<opendspx::ParamCurveAnchor>(*std::static_pointer_cast<opendspx::ParamCurveAnchor>(curve));
45+
for (auto &node : mappedCurve->nodes) {
46+
node.y = mapValue(node.y);
47+
}
48+
curve = mappedCurve;
49+
break;
50+
}
51+
}
52+
}
53+
}
54+
55+
}
56+
57+
void LibreSVIPPitchMapper::mapModel(opendspx::Model &model, Direction direction) {
58+
for (auto &track : model.content.tracks) {
59+
for (auto &clip : track.clips) {
60+
if (!clip || clip->type != opendspx::Clip::Type::Singing) {
61+
continue;
62+
}
63+
const auto &singingClip = *std::static_pointer_cast<opendspx::SingingClip>(clip);
64+
if (singingClip.params.find("pitch") == singingClip.params.end()) {
65+
continue;
66+
}
67+
auto mappedClip = std::make_shared<opendspx::SingingClip>(singingClip);
68+
auto &pitch = mappedClip->params.at("pitch");
69+
mapCurves(pitch.original, direction);
70+
mapCurves(pitch.edited, direction);
71+
mapCurves(pitch.transform, direction);
72+
clip = mappedClip;
73+
}
74+
}
75+
}
76+
77+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-FileCopyrightText: Team OpenVPI
2+
// SPDX-License-Identifier: GPL-3.0-only
3+
4+
#ifndef DIFFSCOPE_LIBRESVIP_FORMAT_CONVERTER_LIBRESVIPPITCHMAPPER_H
5+
#define DIFFSCOPE_LIBRESVIP_FORMAT_CONVERTER_LIBRESVIPPITCHMAPPER_H
6+
7+
namespace opendspx {
8+
struct Model;
9+
}
10+
11+
namespace LibreSVIPFormatConverter::Internal {
12+
13+
class LibreSVIPPitchMapper {
14+
public:
15+
enum class Direction {
16+
FromLibreSVIP,
17+
ToLibreSVIP,
18+
};
19+
20+
// Maps pitch curves without modifying shared clips or curves in other models.
21+
// Applies to all LibreSVIP versions until a version with normalized DSPX pitch values is identified.
22+
static void mapModel(opendspx::Model &model, Direction direction);
23+
};
24+
25+
}
26+
27+
#endif // DIFFSCOPE_LIBRESVIP_FORMAT_CONVERTER_LIBRESVIPPITCHMAPPER_H

0 commit comments

Comments
 (0)