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

Prevent redundant accidentals #1667

Closed
Daniel63656 opened this issue Nov 21, 2023 · 1 comment
Closed

Prevent redundant accidentals #1667

Daniel63656 opened this issue Nov 21, 2023 · 1 comment
Labels

Comments

@Daniel63656
Copy link

Daniel63656 commented Nov 21, 2023

In my application I create a musicxml from another token language that only states visible accidentals. For that reason, I rely on keySignature.getAccidentalByStep() to get the accidental of the key if no accidental was stated and there is no prior accidental in the measure for that step+octave.
This works fine except that music21 seems to add redundant naturals like in this example where the D5 gets a natural (because prior measure contained D#4). Even setting the accidental to None does not help. How to stop this auto creation of redundant naturals?

from music21 import *

score = stream.Score()
part = stream.Part()

score.append(part)

measure = stream.Measure()
part.append(measure)
keyS = key.KeySignature(1)
measure.append(keyS)

n = note.Note('D#4')
measure.append(n)

n2 = note.Note()
n2.step = 'F'
n2.octave = 4
# if acc is explicitly stated: use it
# elif (n2.step, n2.octave) in measureAcc.keys(): use value of dictionary
# else:
n2.pitch.accidental = keyS.accidentalByStep(n2.step)
measure.append(n2)

measure = stream.Measure()
part.append(measure)
measure.append(keyS)

n = note.Note('D5')
n.pitch.accidental = keyS.accidentalByStep(n.step)
#n.pitch.accidental = None     # no effect
measure.append(n)

score.show()
@jacobtylerwalls
Copy link
Member

Hi @Daniel63656. You'll want to get familiar with music21's displayType attribute on Accidental objects. If you don't set the displayType yourself, you're delegating to music21's makeAccidentals() method, called with default arguments, one of which (cautionaryPitchClass=True) is creating the natural you don't want.

So, your options are to:

  • manually set the displayType yourself, e.g.:
n.pitch.accidental = Accidental()
n.pitch.accidental.displayType = 'never'
  • or, call makeAccidentals() with the specific arguments you want, e.g. cautionaryPitchClass=False
  • or, and possibly additionally if you find it necessary, call .show(makeNotation=False) so that you opt-out of the methods that clean up notation. (This last option is for power users who aren't depending on music21 cleaning up fine details of rests/bars/tuplet brackets etc)

@jacobtylerwalls jacobtylerwalls closed this as not planned Won't fix, can't repro, duplicate, stale Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants