Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def testBracketA(self):
from music21 import converter
from music21.musicxml import testPrimitive
s = converter.parse(testPrimitive.directions31a)
raw = fromMusic21Object(s)
self.assertEqual(raw.count('
def pitchQuarterLengthUsageWeightedScatter(show=True):
#from music21 import converter, graph
from music21.musicxml import testFiles as xml
from music21.humdrum import testFiles as kern
mozartStream = converter.parse(xml.mozartTrioK581Excerpt) # @UndefinedVariable
notes = mozartStream.flat.stripTies()
g = graph.plot.ScatterWeightedPitchSpaceQuarterLength(notes,
title='Mozart Trio K. 581 Excerpt')
g.run()
g = graph.plot.ScatterWeightedPitchClassQuarterLength(notes,
title='Mozart Trio K. 581 Excerpt')
g.run()
chopinStream = converter.parse(kern.mazurka6)
notes = chopinStream.flat.stripTies()
g = graph.plot.ScatterWeightedPitchSpaceQuarterLength(notes,
title='Chopin Mazurka 6 Excerpt')
g.run()
def testPivotChord(self):
from music21 import converter
src = """m1 G: I b3 v d: i b4 V"""
s = converter.parse(src, format='romantext')
p = s.parts[0]
m1 = p.getElementsByClass('Measure')[0]
allRNs = m1.getElementsByClass('RomanNumeral')
notPChord = allRNs[0]
pChord = allRNs[1]
self.assertEqual(pChord.key.tonic.step, 'G')
self.assertEqual(pChord.figure, 'v')
pivot = pChord.pivotChord
self.assertEqual(pivot.key.tonic.step, 'D')
self.assertEqual(pivot.figure, 'i')
self.assertIsNone(notPChord.pivotChord)
#s.show('text')
def badMeter():
myScale = "3/4 a2 a2"
s1 = converter.parse(myScale, format="tinynotation")
s1.show('lily.png')
s1.show('midi')
def testNoChord(self):
from music21 import converter
target_str = '''
T: No Chords
M: 4/4
L: 1/1
K: C
[| "C" C | "NC" C | "C" C | "N.C." C | "C" C
| "No Chord" C | "C" C | "None" C | "C" C | "Other"
C |]
'''
score = converter.parse(target_str, format='abc')
self.assertEqual(len(list(score.flat.getElementsByClass(
'ChordSymbol'))), 9)
self.assertEqual(len(list(score.flat.getElementsByClass(
'NoChord'))), 4)
score = harmony.realizeChordSymbolDurations(score)
self.assertEqual(8, score.getElementsByClass('ChordSymbol')[
-1].quarterLength)
self.assertEqual(4, score.getElementsByClass('ChordSymbol')[
0].quarterLength)
def demoGraphMozartChopin():
from music21.musicxml import testFiles as xmlTest
from music21.humdrum import testFiles as kernTest
dpi = 300
mozartStream = converter.parse(xmlTest.mozartTrioK581Excerpt) # @UndefinedVariable
g = graph.plot.Plot3DBarsPitchSpaceQuarterLength(mozartStream.stripTies(),
dpi=dpi,
title='Mozart Trio K. 581, Excerpt',
colors=['#CD4F39'],
alpha=.8)
g.run()
chopinStream = converter.parse(kernTest.mazurka6)
g = graph.plot.Plot3DBarsPitchSpaceQuarterLength(chopinStream.stripTies(),
dpi=dpi,
title='Chopin Mazurka 6, Excerpt',
colors=['#6495ED'], alpha=.8)
g.run()
def testInstrumentTranspositionC(self):
# generate all transpositions on output
from music21 import converter
from music21.musicxml import testPrimitive
s = converter.parse(testPrimitive.transposing01)
raw = fromMusic21Object(s)
self.assertEqual(raw.count(''), 6)
def parse_single_midi_file_with_time(infile, outfile):
try:
score = music21.converter.parse(infile)
key = score.analyze("key")
for msg in MidiFile(infile):
if msg.time != 0:
outfile.write(
str(msg.hex()) + "," +
str(key) + "," + str(msg.time) + '\n'
)
else:
outfile.write(
str(msg.hex()) + "," + str(key) + '\n'
)
outfile.write("\n")
except Exception as e:
print(e)
pass
def extract_corpus(song):
"""Convert midi file into json object)"""
return music21.converter.parse(song)
def xtestGenerateFirstSpeciesThreeVoices(self):
'''
A First Species, Three-Voice Counterpoint Generator by Jackie Rogoff (MIT 2010)
written as continuation of
a UROP (Undergraduate Research Opportunities Program) project at M.I.T. summer 2010.
'''
counterpoint1 = ModalCounterpoint()
cf = cantusFirmi[0]# getRandomCF()
environLocal.printDebug(['Using: ', cf['notes']])
cantusFirmus = stream.Part(converter.parse(cf['notes'], "4/4").notes)
baseNote = Note(cf['mode'])
thisScale = scale.MinorScale(baseNote)
(middleVoice, topVoice) = counterpoint1.generateFirstSpeciesThreeVoices(
cantusFirmus, thisScale, 'random')
score = stream.Score()
score.insert(0, meter.TimeSignature('4/4'))
score.insert(0, topVoice)
score.insert(0, middleVoice)
score.insert(0, cantusFirmus)
# score.show('text')
# score.show('musicxml')
score.show('midi')
score.show('lily.png')