diff --git a/src/framework/musesampler/internal/musesamplerresolver.cpp b/src/framework/musesampler/internal/musesamplerresolver.cpp index 14ad3bae831f5..d9487c85fff92 100644 --- a/src/framework/musesampler/internal/musesamplerresolver.cpp +++ b/src/framework/musesampler/internal/musesamplerresolver.cpp @@ -68,14 +68,8 @@ InstrumentInfo findInstrument(MuseSamplerLibHandlerPtr libHandler, const AudioRe while (auto instrument = libHandler->getNextInstrument(instrumentList)) { int instrumentId = libHandler->getInstrumentId(instrument); - String internalName = String::fromUtf8(libHandler->getInstrumentName(instrument)); - String internalCategory = String::fromUtf8(libHandler->getInstrumentCategory(instrument)); - String instrumentSoundId = String::fromUtf8(libHandler->getMpeSoundId(instrument)); - - if (resourceMeta.attributeVal(u"playbackSetupData") == instrumentSoundId - && resourceMeta.attributeVal(u"museCategory") == internalCategory - && resourceMeta.attributeVal(u"museName") == internalName - && resourceMeta.attributeVal(u"museUID") == String::fromStdString(std::to_string(instrumentId))) { + + if (resourceMeta.attributeVal(u"museUID") == String::number(instrumentId)) { return { instrumentId, instrument }; } } @@ -168,7 +162,7 @@ AudioResourceMetaList MuseSamplerResolver::resolveResources() const } AudioResourceMeta meta; - meta.id = buildMuseInstrumentId(internalCategory, internalName, instrumentId).toStdString(); + meta.id = std::to_string(instrumentId); meta.type = AudioResourceType::MuseSamplerSoundPack; meta.vendor = "MuseSounds"; meta.attributes = { @@ -313,7 +307,7 @@ std::vector MuseSamplerResolver::instruments() const JsonObject obj = doc.rootObject(); Instrument instrument; - instrument.id = buildMuseInstrumentId(instrument.category, instrument.name, id); + instrument.id = String::number(id); instrument.soundId = String::fromUtf8(m_libHandler->getMpeSoundId(msInstrument)); instrument.musicXmlId = String::fromUtf8(m_libHandler->getMusicXmlSoundId(msInstrument)); instrument.name = obj.value("FriendlyName").toString(); @@ -342,13 +336,3 @@ void MuseSamplerResolver::loadSoundPresetAttributes(SoundPresetAttributes& attri } } } - -String MuseSamplerResolver::buildMuseInstrumentId(const String& category, const String& name, int uniqueId) const -{ - StringList list; - list.append(category); - list.append(name); - list.append(String::fromStdString(std::to_string(uniqueId))); - - return list.join(u"\\"); -} diff --git a/src/playback/view/internal/inputresourceitem.cpp b/src/playback/view/internal/inputresourceitem.cpp index 707d0d9f9064b..d26776448d8f1 100644 --- a/src/playback/view/internal/inputresourceitem.cpp +++ b/src/playback/view/internal/inputresourceitem.cpp @@ -43,12 +43,39 @@ using namespace muse::audio; using namespace muse::audio::synth; static const QString VST_MENU_ITEM_ID("VST3"); -static const QString SOUNDFONTS_MENU_ITEM_ID = muse::qtrc("playback", "SoundFonts"); +static const QString SOUNDFONTS_MENU_ITEM_ID("SoundFonts"); static const QString MUSE_MENU_ITEM_ID("Muse Sounds"); static const QString GET_MORE_SOUNDS_ID("getMoreSounds"); static const muse::String MS_BASIC_SOUNDFONT_NAME(u"MS Basic"); +static std::unordered_map AUDIO_RESOURCE_TYPE_TO_STR { + { AudioResourceType::FluidSoundfont, SOUNDFONTS_MENU_ITEM_ID }, + { AudioResourceType::VstPlugin, VST_MENU_ITEM_ID }, + { AudioResourceType::MuseSamplerSoundPack, MUSE_MENU_ITEM_ID }, +}; + +static QString makeMenuResourceItemId(AudioResourceType type, const QString& resourceId) +{ + QString str = muse::value(AUDIO_RESOURCE_TYPE_TO_STR, type); + if (str.isEmpty()) { + return QString(); + } + + return str + "\\" + resourceId; +} + +static void parseAudioResourceTypeAndId(const QString& menuItemId, AudioResourceType& type, AudioResourceId& resourceId) +{ + QStringList parts = menuItemId.split("\\"); + if (parts.size() < 2) { + return; + } + + type = muse::key(AUDIO_RESOURCE_TYPE_TO_STR, parts.at(0)); + resourceId = parts.at(1).toStdString(); +} + InputResourceItem::InputResourceItem(QObject* parent) : AbstractAudioResourceItem(parent) { @@ -112,21 +139,28 @@ void InputResourceItem::handleMenuItem(const QString& menuItemId) return; } - const AudioResourceId& newSelectedResourceId = menuItemId.toStdString(); + AudioResourceType newResourceType = AudioResourceType::Undefined; + AudioResourceId newResourceId; + parseAudioResourceTypeAndId(menuItemId, newResourceType, newResourceId); - for (const auto& pairByType : m_availableResourceMap) { - for (const auto& pairByVendor : pairByType.second) { - for (const AudioResourceMeta& resourceMeta : pairByVendor.second) { - if (newSelectedResourceId != resourceMeta.id) { - continue; - } + auto it = m_availableResourceMap.find(newResourceType); + if (it == m_availableResourceMap.cend()) { + LOGE() << "Resource not found: " << menuItemId; + return; + } - if (m_currentInputParams.resourceMeta == resourceMeta) { - continue; - } + for (const auto& pairByVendor : it->second) { + for (const AudioResourceMeta& resourceMeta : pairByVendor.second) { + if (newResourceId != resourceMeta.id) { + continue; + } - emit inputParamsChangeRequested(resourceMeta); + if (m_currentInputParams.resourceMeta == resourceMeta) { + continue; } + + emit inputParamsChangeRequested(resourceMeta); + return; } } } @@ -213,11 +247,11 @@ QVariantMap InputResourceItem::buildMuseMenuItem(const ResourceByVendorMap& reso for (const auto& inst : category.second) { const std::string& instId = inst.first; - QString instName = inst.second.toQString(); bool isCurrentInstrument = m_currentInputParams.resourceMeta.id == instId; - subItemsByCategory << buildMenuItem(QString::fromStdString(instId), - instName, - isCurrentInstrument); + QString instName = inst.second.toQString(); + QString itemId = makeMenuResourceItemId(AudioResourceType::MuseSamplerSoundPack, QString::fromStdString(instId)); + + subItemsByCategory << buildMenuItem(itemId, instName, isCurrentInstrument); isCurrentCategory = isCurrentCategory || isCurrentInstrument; } @@ -273,17 +307,16 @@ QVariantMap InputResourceItem::buildVstMenuItem(const ResourceByVendorMap& resou QVariantList subItemsByType; for (const auto& pair : resourcesByVendor) { - const QString& vendor = QString::fromStdString(pair.first); - QVariantList subItemsByVendor; for (const AudioResourceMeta& resourceMeta : pair.second) { - const QString& resourceId = QString::fromStdString(resourceMeta.id); - subItemsByVendor << buildMenuItem(resourceId, + QString resourceId = QString::fromStdString(resourceMeta.id); + subItemsByVendor << buildMenuItem(makeMenuResourceItemId(resourceMeta.type, resourceId), resourceId, m_currentInputParams.resourceMeta.id == resourceMeta.id); } + QString vendor = QString::fromStdString(pair.first); subItemsByType << buildMenuItem(vendor, vendor, m_currentInputParams.resourceMeta.vendor == pair.first, @@ -346,7 +379,7 @@ QVariantMap InputResourceItem::buildSoundFontsMenuItem(const ResourceByVendorMap } return buildMenuItem(SOUNDFONTS_MENU_ITEM_ID, - SOUNDFONTS_MENU_ITEM_ID, + muse::qtrc("playback", "SoundFonts"), m_currentInputParams.resourceMeta.type == AudioResourceType::FluidSoundfont, soundFontItems); } @@ -391,7 +424,7 @@ QVariantMap InputResourceItem::buildMsBasicMenuItem(const AudioResourceMetaList& presetName = muse::qtrc("playback", "Bank %1, preset %2").arg(item.preset.bank).arg(item.preset.program); } - return buildMenuItem(QString::fromStdString(resourceMeta.id), + return buildMenuItem(makeMenuResourceItemId(resourceMeta.type, QString::fromStdString(resourceMeta.id)), presetName, isCurrent); } @@ -441,7 +474,7 @@ QVariantMap InputResourceItem::buildMsBasicMenuItem(const AudioResourceMetaList& // Prepend the "Choose automatically" item categoryItems.prepend(buildSeparator()); - categoryItems.prepend(buildMenuItem(QString::fromStdString(chooseAutomaticMeta.id), + categoryItems.prepend(buildMenuItem(makeMenuResourceItemId(chooseAutomaticMeta.type, QString::fromStdString(chooseAutomaticMeta.id)), muse::qtrc("playback", "Choose automatically"), isCurrentSoundFont && !currentPreset.has_value())); @@ -487,7 +520,8 @@ QVariantMap InputResourceItem::buildSoundFontMenuItem(const muse::String& soundF presetName = muse::qtrc("playback", "Preset %1").arg(presetPair.first); } - presetItems << buildMenuItem(QString::fromStdString(presetPair.second.id), + QString itemId = makeMenuResourceItemId(AudioResourceType::FluidSoundfont, QString::fromStdString(presetPair.second.id)); + presetItems << buildMenuItem(itemId, presetName, isCurrentPreset); } @@ -500,7 +534,7 @@ QVariantMap InputResourceItem::buildSoundFontMenuItem(const muse::String& soundF // Prepend the "Choose automatically" item bankItems.prepend(buildSeparator()); - bankItems.prepend(buildMenuItem(QString::fromStdString(chooseAutomaticMeta.id), + bankItems.prepend(buildMenuItem(makeMenuResourceItemId(chooseAutomaticMeta.type, QString::fromStdString(chooseAutomaticMeta.id)), muse::qtrc("playback", "Choose automatically"), isCurrentSoundFont && !currentPreset.has_value())); diff --git a/src/project/internal/projectaudiosettings.cpp b/src/project/internal/projectaudiosettings.cpp index 0574648e3d0ab..70902fbc030f2 100644 --- a/src/project/internal/projectaudiosettings.cpp +++ b/src/project/internal/projectaudiosettings.cpp @@ -49,6 +49,22 @@ static const std::map RESOURCE_TYPE_MAP = { { AudioResourceType::MusePlugin, "muse_plugin" }, }; +static void doCompatibilityConversions(AudioResourceMeta& meta) +{ + if (meta.type == AudioResourceType::MuseSamplerSoundPack) { + // MS 4.5: resource name and category have been excluded from ID + // Old format: category\\name\\uid + if (meta.id.find("\\") == std::string::npos) { + return; + } + + const String& museUID = meta.attributeVal(u"museUID"); + if (!museUID.empty()) { + meta.id = museUID.toStdString(); + } + } +} + const AudioOutputParams& ProjectAudioSettings::masterAudioOutputParams() const { return m_masterOutputParams; @@ -271,6 +287,8 @@ Ret ProjectAudioSettings::read(const engraving::MscReader& reader) AudioInputParams inParams = inputParamsFromJson(trackObject.value("in").toObject()); AudioOutputParams outParams = outputParamsFromJson(trackObject.value("out").toObject()); + doCompatibilityConversions(inParams.resourceMeta); + m_trackInputParamsMap.emplace(id, std::move(inParams)); m_trackOutputParamsMap.emplace(id, std::move(outParams)); }