def corpusMelodicIntervalSearch(show = True): # this version compares china to netherlands from music21 import corpus from music21.analysis import discrete mid = discrete.MelodicIntervalDiversity() groupEast = corpus.search('shanxi', field='locale') groupWest = corpus.search('niederlande', field='locale') msg = [] for name, group in [('shanxi', groupEast), ('niederlande', groupWest)]: intervalDict = {} workCount = 0 intervalCount = 0 seventhCount = 0 for fp, n in group: workCount += 1 s = converter.parse(fp, number=n) intervalDict = mid.countMelodicIntervals(s, found=intervalDict) for key in sorted(intervalDict.keys()): intervalCount += intervalDict[key][1] # second value is count if key in ['m7', 'M7']: seventhCount += intervalDict[key][1] pcentSevenths = round(((seventhCount / float(intervalCount)) * 100), 4) msg.append('locale: %s: found %s percent melodic sevenths, out of %s intervals in %s works' % (name, pcentSevenths, intervalCount, workCount)) # for key in sorted(intervalDict.keys()): # print intervalDict[key] for sub in msg: if show == True: print (sub)
def corpusFindMelodicSevenths(show=True): # find and display melodic sevenths import os from music21 import corpus from music21.analysis import discrete mid = discrete.MelodicIntervalDiversity() groupEast = corpus.search('shanxi', 'locale') groupWest = corpus.search('niederlande', 'locale') found = [] for name, group in [('shanxi', groupEast), ('niederlande', groupWest)]: for fp, n in group: s = converter.parse(fp, number=n) intervalDict = mid.countMelodicIntervals(s) for key in sorted(intervalDict.keys()): if key in ['m7', 'M7']: found.append([fp, n, s]) results = stream.Stream() for fp, num, s in found: environLocal.printDebug(['working with found', fp, num]) # this assumes these are all monophonic noteStream = s.flat.getElementsByClass('Note') for i, n in enumerate(noteStream): if i <= len(noteStream) - 2: nNext = noteStream[i + 1] else: nNext = None if nNext is not None: #environLocal.printDebug(['creating interval from notes:', n, nNext, i]) i = interval.notesToInterval(n, nNext) environLocal.printDebug(['got interval', i.name]) if i.name in ['m7', 'M7']: #n.addLyric(s.metadata.title) junk, fn = os.path.split(fp) n.addLyric('%s: %s' % (fn, num)) m = noteStream.extractContext( n, 1, 2, forceOutputClass=stream.Measure) m.makeAccidentals() m.timeSignature = m.bestTimeSignature() results.append(m) if show == True: results.show()