Skip to content

Commit

Permalink
Merge pull request #518 from cuthbertLab/fix_midi_lints
Browse files Browse the repository at this point in the history
Fix midi lint errors
  • Loading branch information
mscuthbert committed Mar 23, 2020
2 parents 97e8171 + 95e35eb commit 2e1a8e7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 25 deletions.
4 changes: 2 additions & 2 deletions documentation/source/usersGuide/usersGuide_29_spanners.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"source": [
"# User's Guide, Chapter 29: Spanners 1 (Slurs)\n",
"\n",
"In `music21`, a \"Spanner\" is a :class:`~music21.base.Music21Object` that denotes a relationship among other elements, such as Notes, Chords, or even Streams, which may or may not be separated in a hierarchy, such as `Note` objects in different measures. They are used to encode things such as Slurs or lines or Ottava (8va) marks and even things like staff groupings. Music would be dull if we could not cross the barline, and Spanners are the key to doing so successfully.\n",
"In `music21`, a \":class:`~music21.spanner.Spanner`\" is a :class:`~music21.base.Music21Object` that denotes a relationship among other elements, such as Notes, Chords, or even Streams, which may or may not be separated in a hierarchy, such as `Note` objects in different measures. They are used to encode things such as Slurs or lines or Ottava (8va) marks and even things like staff groupings. Music would be dull if we could not cross the barline, and Spanners are the key to doing so successfully.\n",
"\n",
"## Slurs\n",
"\n",
Expand Down Expand Up @@ -984,7 +984,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
"version": "3.8.1"
}
},
"nbformat": 4,
Expand Down
4 changes: 2 additions & 2 deletions music21/graph/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ def getColor(color):
except KeyError:
raise GraphException('invalid color abbreviation: %s' % color)
try:
return webcolors.css3_names_to_hex[color]
except KeyError: # no color match
return webcolors.name_to_hex(color)
except ValueError: # no color match
raise GraphException('invalid color name: %s' % color)

elif common.isListLike(color):
Expand Down
11 changes: 6 additions & 5 deletions music21/midi/realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ def x_testBusyCallback(self):
import random

def busyCounter(timeList):
timeCounter = timeList[0]
timeCounter.times += timeCounter.updateTime
print('hi! waited %d milliseconds' % (timeCounter.times))
timeCounter_inner = timeList[0]
timeCounter_inner.times += timeCounter_inner.updateTime
print('hi! waited %d milliseconds' % (timeCounter_inner.times))

class Mock:
times = 0
Expand Down Expand Up @@ -229,11 +229,12 @@ def getRandomStream():
s.append(lastN)
return s

# noinspection PyShadowingNames
def restoreList(timeList):
timeCounter = timeList[0]
streamPlayer = timeList[1]
currentPos = streamPlayer.pygame.mixer.music.get_pos()
if currentPos < 500 and timeCounter.lastPos >= 500:
if currentPos < 500 <= timeCounter.lastPos:
timeCounter.times -= 1
if timeCounter.times > 0:
streamPlayer.streamIn = getRandomStream()
Expand All @@ -244,7 +245,7 @@ def restoreList(timeList):
else:
timeCounter.lastPos = currentPos

class TimePlayer():
class TimePlayer:
ready = False
times = 3
lastPos = 1000
Expand Down
31 changes: 17 additions & 14 deletions music21/midi/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ def noteToMidiEvents(inputM21, includeDeltaTime=True, channel=1):
me1.centShift = n.pitch.getCentShiftFromMidi()

# TODO: not yet using dynamics or velocity
# volScalar = n.volume.getRealized(useDynamicContext=False,
# useVelocity=True, useArticulations=False)
# volScalar = n.volume.getRealized(useDynamicContext=False,
# useVelocity=True, useArticulations=False)

# use cached realized, as realized values should have already been set
me1.velocity = int(round(n.volume.cachedRealized * 127))
Expand Down Expand Up @@ -855,7 +855,7 @@ def midiEventsToKey(eventList):
return k


def keySignatureToMidiEvents(ks, includeDeltaTime=True):
def keySignatureToMidiEvents(ks: 'music21.key.KeySignature', includeDeltaTime=True):
r'''
Convert a single :class:`~music21.key.Key` or
:class:`~music21.key.KeySignature` object to
Expand Down Expand Up @@ -1064,12 +1064,15 @@ def elementToMidiEventList(
return # dynamics have already been applied to notes
elif 'TimeSignature' in classes:
# return a pair of events
el: 'music21.meter.TimeSignature'
sub = timeSignatureToMidiEvents(el, includeDeltaTime=False)
elif 'KeySignature' in classes:
el: 'music21.key.KeySignature'
sub = keySignatureToMidiEvents(el, includeDeltaTime=False)
elif 'TempoIndication' in classes:
# any tempo indication will work
# note: tempo indications need to be in channel one for most playback
el: 'music21.tempo.TempoIndication'
sub = tempoToMidiEvents(el, includeDeltaTime=False)
elif 'Instrument' in classes:
# first instrument will have been gathered above with get start elements
Expand Down Expand Up @@ -1348,9 +1351,9 @@ def assignPacketsToChannels(
for start, stop, usedChannel in list(uniqueChannelEvents): # a list
if usedChannel not in foundChannels:
foundChannels.append(usedChannel)
# for ch in chList:
# if ch not in foundChannels:
# foundChannels.append(ch)
# for ch in chList:
# if ch not in foundChannels:
# foundChannels.append(ch)
# environLocal.printDebug(['foundChannels', foundChannels])
# environLocal.printDebug(['usedTracks', usedTracks])

Expand All @@ -1374,7 +1377,7 @@ def assignPacketsToChannels(
# environLocal.printDebug(['adding pitch bend for found channels', me])
# this sort is necessary
post.sort(
key=lambda x: (x['offset'], x['midiEvent'].sortOrder)
key=lambda x_event: (x_event['offset'], x_event['midiEvent'].sortOrder)
)

# TODO: for each track, add an additional silent event to make sure
Expand Down Expand Up @@ -2014,8 +2017,8 @@ def midiTracksToStreams(midiTracks, ticksPerQuarter=None, quantizePost=True,
quantizePost,
inputM21=streamPart,
**keywords)
# streamPart._setMidiTracksPart(mt,
# ticksPerQuarter=ticksPerQuarter, quantizePost=quantizePost)
# streamPart._setMidiTracksPart(mt,
# ticksPerQuarter=ticksPerQuarter, quantizePost=quantizePost)
s.insert(0, streamPart)
else:
# note: in some cases a track such as this might have metadata
Expand Down Expand Up @@ -3066,13 +3069,13 @@ def testMidiEventsImported(self):
self.maxDiff = None
from music21 import corpus

def procCompare(mf, match):
def procCompare(mf_inner, match_inner):
triples = []
for i in range(0, len(mf.tracks[0].events), 2):
d = mf.tracks[0].events[i] # delta
e = mf.tracks[0].events[i + 1] # events
for i in range(0, len(mf_inner.tracks[0].events), 2):
d = mf_inner.tracks[0].events[i] # delta
e = mf_inner.tracks[0].events[i + 1] # events
triples.append((d.time, e.type.name, e.pitch))
self.assertEqual(triples, match)
self.assertEqual(triples, match_inner)

s = corpus.parse('bach/bwv66.6')
part = s.parts[0].measures(6, 9) # last measures
Expand Down
2 changes: 1 addition & 1 deletion music21/musicxml/m21ToXml.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def normalizeColor(color):
if color in (None, ''):
return color
if '#' not in color:
return (webcolors.css3_names_to_hex[color]).upper()
return webcolors.name_to_hex(color).upper()
else:
return color.upper()

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ jsonpickle
matplotlib
more_itertools
numpy
webcolors
webcolors>=1.11

0 comments on commit 2e1a8e7

Please sign in to comment.