def testAll(self): from music21 import corpus, dynamics a = corpus.parse('bach/bwv57.8') a.parts[0].insert(0, dynamics.Dynamic('mf')) a.parts[0].insert(10, dynamics.Dynamic('f')) if self.show: plotStream(a, 'all')
def testPlotChordsC(self): from music21 import dynamics from music21 import note from music21 import stream from music21 import scale sc = scale.MajorScale('c4') s = stream.Stream() s.append(dynamics.Dynamic('f')) s.append(note.Note('c4')) s.append(sc.getChord('e3', 'a3', quarterLength=0.5)) # s.append(note.Note('c3', quarterLength=2)) s.append(dynamics.Dynamic('mf')) s.append(sc.getChord('b3', 'e4', quarterLength=1.5)) s.append(dynamics.Dynamic('pp')) s.append(sc.getChord('f4', 'g5', quarterLength=3)) s.append(sc.getChord('f4', 'g5', quarterLength=3)) s.append(note.Note('c5', quarterLength=3)) for args in [ ('histogram', 'pitch'), ('histogram', 'pitchclass'), ('histogram', 'quarterlength'), ('scatter', 'pitch', 'quarterlength'), ('scatter', 'pitchspace', 'offset'), ('scatter', 'pitch', 'offset'), ('scatter', 'dynamics'), ('bar', 'pitch'), ('bar', 'pc'), ('weighted', 'pc', 'duration'), ('weighted', 'dynamics'), ]: # s.plot(*args, doneAction='write') s.plot(*args, doneAction=None)
def testRealizeVolumeA(self): from music21 import stream, dynamics, note, volume s = stream.Stream() s.repeatAppend(note.Note('g3'), 16) # before insertion of dynamics match = [n.volume.cachedRealizedStr for n in s.notes] self.assertEqual(match, [ '0.71', '0.71', '0.71', '0.71', '0.71', '0.71', '0.71', '0.71', '0.71', '0.71', '0.71', '0.71', '0.71', '0.71', '0.71', '0.71' ]) for i, d in enumerate(['pp', 'p', 'mp', 'f', 'mf', 'ff', 'ppp', 'mf']): s.insert(i * 2, dynamics.Dynamic(d)) # cached will be out of date in regard to new dynamics match = [n.volume.cachedRealizedStr for n in s.notes] self.assertEqual(match, [ '0.71', '0.71', '0.71', '0.71', '0.71', '0.71', '0.71', '0.71', '0.71', '0.71', '0.71', '0.71', '0.71', '0.71', '0.71', '0.71' ]) # calling realize will set all to new cached values volume.realizeVolume(s) match = [n.volume.cachedRealizedStr for n in s.notes] self.assertEqual(match, [ '0.35', '0.35', '0.5', '0.5', '0.64', '0.64', '0.99', '0.99', '0.78', '0.78', '1.0', '1.0', '0.21', '0.21', '0.78', '0.78' ]) # we can get the same results without using realizeVolume, though # this uses slower context searches s = stream.Stream() s.repeatAppend(note.Note('g3'), 16) for i, d in enumerate(['pp', 'p', 'mp', 'f', 'mf', 'ff', 'ppp', 'mf']): s.insert(i * 2, dynamics.Dynamic(d)) match = [n.volume.cachedRealizedStr for n in s.notes] self.assertEqual(match, [ '0.35', '0.35', '0.5', '0.5', '0.64', '0.64', '0.99', '0.99', '0.78', '0.78', '1.0', '1.0', '0.21', '0.21', '0.78', '0.78' ]) # loooking at raw velocity values match = [n.volume.velocity for n in s.notes] self.assertEqual(match, [ None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None ]) # can set velocity with realized values volume.realizeVolume(s, setAbsoluteVelocity=True) match = [n.volume.velocity for n in s.notes] self.assertEqual(match, [ 45, 45, 63, 63, 81, 81, 126, 126, 99, 99, 127, 127, 27, 27, 99, 99 ])
def testGetContextSearchB(self): from music21 import stream, note, dynamics s = stream.Stream() d1 = dynamics.Dynamic('mf') s.insert(0, d1) d2 = dynamics.Dynamic('f') s.insert(2, d2) n1 = note.Note('g') s.insert(4, n1) # can get dynamics from volume object self.assertEqual(n1.volume.getDynamicContext(), d2)
def testGetContextSearchA(self): from music21 import stream, note, volume, dynamics s = stream.Stream() d1 = dynamics.Dynamic('mf') s.insert(0, d1) d2 = dynamics.Dynamic('f') s.insert(2, d2) n1 = note.Note('g') v1 = volume.Volume(client=n1) s.insert(4, n1) # can get dynamics from volume object self.assertEqual(v1.client.getContextByClass('Dynamic'), d2) self.assertEqual(v1.getDynamicContext(), d2)
def testRealizeVolumeB(self): from music21 import corpus, dynamics s = corpus.parse('bwv66.6') durUnit = s.highestTime // 8 # let floor dyns = ['pp', 'p', 'mp', 'f', 'mf', 'ff', 'f', 'mf'] for i, p in enumerate(s.parts): for j, d in enumerate(dyns): oTarget = j*durUnit # placing dynamics in Measure requires extra handling m = p.getElementsByOffset(oTarget, mustBeginInSpan=False).getElementsByClass('Measure')[0] oInsert = oTarget - m.getOffsetBySite(p) m.insert(oInsert, dynamics.Dynamic(d)) # shift 2 places each time dyns = dyns[2:] + dyns[:2] #s.show() #s.show('midi') #### TODO: BUG -- one note too loud... match = [n.volume.cachedRealizedStr for n in s.parts[0].flat.notes] self.assertEqual(match, ['0.35', '0.35', '0.35', '0.35', '0.35', '0.5', '0.5', '0.5', '0.5', '0.64', '0.64', '0.64', '0.64', '0.64', '0.99', '0.99', '0.99', '0.99', '0.78', '0.78', '0.78', '0.78', '1.0', '1.0', '1.0', '1.0', '0.99', '0.99', '0.99', '0.99', '0.78', '0.78', '0.78', '0.78', '0.78', '0.78', '0.78']) match = [n.volume.cachedRealizedStr for n in s.parts[1].flat.notes] self.assertEqual(match, ['0.64', '0.64', '0.64', '0.64', '0.99', '0.99', '0.99', '0.99', '0.99', '0.78', '0.78', '0.78', '0.78', '0.78', '1.0', '1.0', '1.0', '1.0', '0.99', '0.99', '0.99', '0.99', '0.99', '0.78', '0.78', '0.78', '0.78', '0.35', '0.35', '0.35', '0.35', '0.35', '0.35', '0.5', '0.5', '0.5', '0.5', '0.5', '0.5', '0.5', '0.5', '0.5']) match = [n.volume.cachedRealizedStr for n in s.parts[3].flat.notes] self.assertEqual(match, ['0.99', '0.99', '0.99', '0.99', '0.99', '0.78', '0.78', '0.78', '0.78', '0.78', '0.35', '0.35', '0.35', '0.35', '0.35', '0.5', '0.5', '0.5', '0.5', '0.5', '0.5', '0.5', '0.64', '0.64', '0.64', '0.64', '0.99', '0.99', '0.99', '0.99', '0.78', '0.78', '0.78', '0.78', '0.78', '1.0', '1.0', '1.0', '1.0', '1.0', '1.0'])
def testGetRealizedA(self): from music21 import volume, dynamics v1 = volume.Volume(velocity=64) self.assertEqual(v1.getRealizedStr(), '0.5') d1 = dynamics.Dynamic('p') self.assertEqual(v1.getRealizedStr(useDynamicContext=d1), '0.35') d1 = dynamics.Dynamic('ppp') self.assertEqual(v1.getRealizedStr(useDynamicContext=d1), '0.15') d1 = dynamics.Dynamic('fff') self.assertEqual(v1.getRealizedStr(useDynamicContext=d1), '0.91') # if vel is at max, can scale down with a dynamic v1 = volume.Volume(velocity=127) d1 = dynamics.Dynamic('fff') self.assertEqual(v1.getRealizedStr(useDynamicContext=d1), '1.0') d1 = dynamics.Dynamic('ppp') self.assertEqual(v1.getRealizedStr(useDynamicContext=d1), '0.3') d1 = dynamics.Dynamic('mp') self.assertEqual(v1.getRealizedStr(useDynamicContext=d1), '0.9') d1 = dynamics.Dynamic('p') self.assertEqual(v1.getRealizedStr(useDynamicContext=d1), '0.7')
def testHorizontalInstrumentationB(self): from music21 import corpus, dynamics s = corpus.parse('bwv66.6') dyn = ['p', 'mf', 'f', 'ff', 'mp', 'fff', 'ppp'] i = 0 for p in s.parts: for m in p.getElementsByClass('Measure'): m.insert(0, dynamics.Dynamic(dyn[i % len(dyn)])) i += 1 s.plot('dolan', fillByMeasure=True, segmentByTarget=True, doneAction=None)
def testDynamicsPositionA(self): from music21 import stream, note, dynamics s = stream.Stream() selections = ['pp', 'f', 'mf', 'fff'] #positions = [-20, 0, 20] for i in range(10): d = dynamics.Dynamic(selections[i % len(selections)]) #d.positionVertical = positions[i%len(positions)] s.append(d) s.append(note.Note('c1'))
def createDynamics(self, attributes): r''' Adding dynamics like "fff", "pp", ... to the measure. >>> nwt = noteworthy.translate.NoteworthyTranslator() >>> nwt.currentMeasure = stream.Measure() >>> nwt.createDynamics({"Style":"fff","Pos":"-8"}) >>> nwt.currentMeasure[0] <music21.dynamics.Dynamic fff > ''' # Dynamic case if 'Style' in attributes: g = dynamics.Dynamic(attributes['Style']) self.currentMeasure.append(g)
def testDynamicsPositionB(self): import random from music21 import stream, note, layout, dynamics s = stream.Stream() for i in range(6): m = stream.Measure(number=i + 1) m.append(layout.SystemLayout(isNew=True)) m.append(note.Rest(type='whole')) s.append(m) for m in s.getElementsByClass('Measure'): offsets = [x * .25 for x in range(16)] random.shuffle(offsets) offsets = offsets[:4] for o in offsets: d = dynamics.Dynamic('mf') d.positionVertical = 20 m.insert(o, d)
def testShowAllTypes(self): ''' show all known types to display tests fromMusic21Object() ''' from music21 import scale from music21 import chord from music21 import duration from music21 import dynamics from music21 import meter from music21 import pitch m = stream.Measure() n = note.Note("D#6") m.repeatAppend(n, 6) m.show() s = stream.Stream() s.repeatAppend(n, 6) s.show() s = stream.Score() s.repeatAppend(n, 6) s.show() s = stream.Score() p = stream.Part() p.repeatAppend(n, 6) p2 = stream.Part() p2.repeatAppend(n, 6) s.insert(0, p) s.insert(0, p2) s.show() #emptyStream s = stream.Stream() s.show() p2.show() n.show() c = chord.Chord(['C3', 'D4', 'E5']) c.show() r = note.Rest() r.show() p = pitch.Pitch() p.show() d = duration.Duration(2.0) d.show() #empty duration! shows blank 4/4 measure, maybe with default rest. d = duration.Duration() d.show() mf = dynamics.Dynamic('mf') mf.show() cm = scale.MajorScale('C') cm.show() o = scale.OctatonicScale("C#4") o.show() ts = meter.TimeSignature('3/4') ts.show()