diff --git a/music21/instrument.py b/music21/instrument.py index 16becc2d6..11f0cb59b 100644 --- a/music21/instrument.py +++ b/music21/instrument.py @@ -9,7 +9,7 @@ # Ben Houge # Mark Gotham # -# Copyright: Copyright © 2009-2024 Michael Scott Asato Cuthbert +# Copyright: Copyright © 2009-2026 Michael Scott Asato Cuthbert # License: BSD, see license.txt # ------------------------------------------------------------------------------ ''' @@ -2544,10 +2544,15 @@ def getAllNamesForInstrument(instrumentClass: Instrument, Note that the language string is not case-sensitive, so 'German' is also fine. ''' + from music21.languageExcerpts import instrumentLookup + language = language.lower() instrumentNameDict = {} - instrumentClassName = instrumentClass.instrumentName or '' + namedClasses = set(instrumentLookup.allToClassName.values()) + instrumentClassName = next((cls.__name__ for cls in type(instrumentClass).__mro__ + if cls.__name__ in namedClasses), + instrumentClass.instrumentName or '') if language == SearchLanguage.ALL: for lang in SearchLanguage: @@ -2569,6 +2574,18 @@ class TestExternal(unittest.TestCase): class Test(unittest.TestCase): + def testGetAllNamesForInstrumentByClass(self): + acousticBass = getAllNamesForInstrument(AcousticBass(), SearchLanguage.ENGLISH) + self.assertEqual(acousticBass, {'english': ['acoustic bass']}) + + firstViolins = Violin() + firstViolins.instrumentName = 'Violin I' + self.assertEqual(getAllNamesForInstrument(firstViolins, SearchLanguage.ENGLISH), + {'english': ['violin', 'violins']}) + + self.assertEqual(getAllNamesForInstrument(Instrument('Violin'), SearchLanguage.ENGLISH), + {'english': ['violin', 'violins']}) + def testCopyAndDeepcopy(self): from music21.test.commonTest import testCopyAll testCopyAll(self, globals())