Search code examples
pythonmusic21

Silence music21 warnings


I am getting an abundance of music21 warnings about zero-duration chords:

WARNING: midi chord with zero duration will be treated as grace

I tried overwriting the warn method on music21.environment.Environment() with the following:

def silence(*args, **kwargs): pass
e = music21.environment.Environment()
setattr(e, 'warn', silence)

I also tried muting all warnings:

import warnings
warnings.filterwarnings('ignore')

Neither has done the trick. How can I mute these (or ideally, all) warnings from music21? (I validate my data post-processing.)


Solution

  • Try:

    >>> from music21 import midi  # or your standard import
    >>> def noop(input):
    ...   pass
    ... 
    >>> midi.translate.environLocal.warn = noop
    

    We're tracking a modernization request to move the warnings system over to the python warnings module at https://github.com/cuthbertLab/music21/issues/254 so that you can filter idiomatically. The case you've presented here is a bit more pressing than the original case on the ticket IMO, so I'll leave a comment.