Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MuseSampler: fixed potential compatibility issues #25887

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions src/framework/musesampler/internal/musesamplerresolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
}
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -313,7 +307,7 @@ std::vector<Instrument> 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();
Expand Down Expand Up @@ -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"\\");
}
84 changes: 59 additions & 25 deletions src/playback/view/internal/inputresourceitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<AudioResourceType, QString> 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)
{
Expand Down Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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()));

Expand Down Expand Up @@ -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);
}
Expand All @@ -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()));

Expand Down
18 changes: 18 additions & 0 deletions src/project/internal/projectaudiosettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ static const std::map<AudioResourceType, QString> 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;
Expand Down Expand Up @@ -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));
}
Expand Down