Skip to content
Merged
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
21 changes: 19 additions & 2 deletions music21/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# ------------------------------------------------------------------------------
'''
Expand Down Expand Up @@ -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:
Expand All @@ -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())
Expand Down
Loading