|
| 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 | +} |
0 commit comments