Search code examples
pythonmusic21

music21 write() function giving AttributeError: 'Rest' object has no attribute 'pitches' on python 3.9.1 and music21 version 7


I'm using the code found on this gist: https://gist.github.com/aldous-rey/68c6c43450517aa47474#file-transposer-py and when I run the script on MIDIs I have, including one from the author, I get the error

AttributeError: 'Rest' object has no attribute 'pitches'

It's quite old code, I had to modify a bit to fit python 3.9.1. I'm using the latest at this time music21 version 7. Was there any changes to the write() function that breaks this code?

EDIT: Here is the entire traceback:

Traceback (most recent call last):
  File "C:\Users\username\path\to\transposer.py", line 28, in <module>
    newscore.write('midi',newFileName)
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\music21\stream\base.py", line 328, in write
    return super().write(fmt=fmt, fp=fp, **keywords)
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\music21\base.py", line 2665, in write
    return formatWriter.write(self,
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\music21\converter\subConverters.py", line 1147, in write
    mf = midiTranslate.music21ObjectToMidiFile(obj, **midiTranslateKeywords)
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\music21\midi\translate.py", line 260, in music21ObjectToMidiFile
    return streamToMidiFile(music21Object, addStartDelay=addStartDelay)
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\music21\midi\translate.py", line 2559, in streamToMidiFile
    midiTracks = streamHierarchyToMidiTracks(s,
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\music21\midi\translate.py", line 2443, in streamHierarchyToMidiTracks
    subs.stripTies(inPlace=True, matchByPitch=False)
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\music21\stream\base.py", line 7283, in stripTies
    endMatch = updateEndMatch(n)
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\music21\stream\base.py", line 7166, in updateEndMatch
    and nLast is not None and len(nLast.pitches) == len(nInner.pitches)):
AttributeError: 'Rest' object has no attribute 'pitches'

Solution

  • Yes, this is a bug in version 7, but thanks to this report, I've opened a pull request to fix the issue.

    There might be a bug-fix release this fall, or you could also just pull from the development version once the PR is merged, e.g. pip uninstall music21 and pip install git+https://github.com/cuthbertLab/music21.git. Thanks again for reporting!