Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
new_note.storedInstrument = instrument.Piano()
notes.append(new_note)
new_chord = chord.Chord(notes)
new_chord.offset = offset
output_notes.append(new_chord)
# pattern is a note
else:
new_note = note.Note(pattern)
new_note.offset = offset
new_note.storedInstrument = instrument.Piano()
output_notes.append(new_note)
# increase offset each iteration so that notes do not stack
offset += 0.5
midi_stream = stream.Stream(output_notes)
midi_stream.write('midi', fp='test_output.mid')
def testMultipartStreams(self):
'''Test the creation of multi-part streams by simply having streams within streams.
'''
q = Stream()
r = Stream()
for x in ['c3','a3','g#4','d2'] * 10:
n = note.Note(x)
n.quarterLength = .25
q.addNext(n)
m = note.Note(x)
m.quarterLength = 1.125
r.addNext(m)
s = Stream() # container
s.append(q)
s.append(r)
s.insertAtOffset(meter.TimeSignature("3/4"), 0)
s.insertAtOffset(meter.TimeSignature("5/4"), 3)
s.insertAtOffset(meter.TimeSignature("3/4"), 8)
def testGetAccidentalCountSumIntermediate(self):
s1 = stream.Stream()
s2 = stream.Stream()
s2.append(note.Note('D-2'))
s2.append(note.Note('F#5'))
self.assertEqual(getAccidentalCountSum([s1, s2]), {'flat': 1, 'sharp': 1})
def testGlissandoB(self):
from music21 import stream, note, spanner
s = stream.Stream()
s.repeatAppend(note.Note(), 12)
for i, n in enumerate(s.notes):
n.transpose(i + (i % 2 * 12), inPlace=True)
# note: this does not support glissandi between non-adjacent notes
n1 = s.notes[0]
n2 = s.notes[1]
sp1 = spanner.Glissando(n1, n2)
sp1.lineType = 'solid'
sp1.label = 'gliss.'
s.append(sp1)
# s.show()
raw = self.xmlStr(s)
self.assertEqual(raw.count('
def testSerializationScaffoldA(self):
from music21 import note, stream
from music21 import freezeThaw
n1 = note.Note()
s1 = stream.Stream()
s2 = stream.Stream()
s1.append(n1)
s2.append(n1)
sf = freezeThaw.StreamFreezer(s2, fastButUnsafe=False)
sf.setupSerializationScaffold()
# test safety
self.assertTrue(s2.hasElement(n1))
self.assertTrue(s1.hasElement(n1))
try:
data[i], color[i] = self.processor.process(current)
except DiscreteAnalysisException:
# current might have no notes...all rests?
data[i], color[i] = (None, None, 0), '#ffffff'
elif windowType == 'noOverlap':
start = 0
end = start + windowSize
i = 0
while True:
if end >= len(self._windowedStream):
end = len(self._windowedStream)
current = stream.Stream()
for j in range(start, end):
current.append(self._windowedStream[j])
try:
data[i], color[i] = self.processor.process(current)
except DiscreteAnalysisException:
# current might have no notes...all rests?
data[i], color[i] = (None, None, 0), '#ffffff'
start = end
end = start + windowSize
i += 1
if i >= windowCount:
break
elif windowType == 'adjacentAverage':
def testShowSubstitution(self):
'''
two streams:
MIDI is CCC
OMR is CCB
Therefore there needs to be an substitution to get from OMR to MIDI
'''
from music21 import stream
from music21 import note
target = stream.Stream()
source = stream.Stream()
noteC1 = note.Note('C4')
noteC2 = note.Note('C4')
noteC3 = note.Note('C4')
noteC4 = note.Note('C4')
noteC5 = note.Note('C4')
noteB = note.Note('B3')
target.append([noteC1, noteC2, noteC3])
source.append([noteC4, noteC5, noteB])
sa = StreamAligner(target, source)
sa.align()
sa.showChanges()
self.assertEqual(target.getElementById(sa.changes[2][0].id).style.color, 'purple')
def testDefinedContextsClef(self):
from music21 import base, note, stream, clef
s1 = stream.Stream()
s2 = stream.Stream()
n = note.Note()
s2.append(n)
s1.append(s2)
# append clef to outer stream
s1.insert(0, clef.AltoClef())
pre = s1.getElementAtOrBefore(0, [clef.Clef])
self.assertEqual(isinstance(pre, clef.AltoClef), True)
# we should be able to find a clef from the lower-level stream
post = s2.getContextByClass(clef.Clef)
self.assertEqual(isinstance(post, clef.AltoClef), True)
post = s2.getClefs(clef.Clef)
self.assertEqual(isinstance(post[0], clef.AltoClef), True)
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()
def serialize_stream(stream, repeats=1):
"""
function that takes a stream of parallel parts
and returns a stream with all parts sequenced one after the other
"""
new_stream = music21.stream.Stream()
copies = len(stream)
for i in range(copies):
for part in reversed(stream):
length = part.duration.quarterLength
new_stream.append(copy.deepcopy(part.flat.elements))
return new_stream, length