Spaces:
Running
Running
| from music21 import stream, note, tie, meter | |
| # working version but using Measure | |
| from format_conversions import Format_Converter | |
| # Create Voice 1 (tied C4 over two half notes) | |
| voice1 = stream.Voice() | |
| n1 = note.Note('C4', quarterLength=2.0) | |
| n1.tie = tie.Tie('start') | |
| n2 = note.Note('C4', quarterLength=2.0) | |
| n2.tie = tie.Tie('stop') | |
| voice1.append([n1, n2]) | |
| voice1.append(note.Note('E4', quarterLength=2.0)) | |
| # Create Voice 2 (a single E4 half note) | |
| voice2 = stream.Voice() | |
| voice2.append(note.Note('E4', quarterLength=2.0)) | |
| voice2.append(note.Note('G4', quarterLength=2.0)) | |
| voice2.append(note.Note('C5', quarterLength=2.0)) | |
| voice2.append(note.Note('D5', quarterLength=2.0)) | |
| voice2.append(note.Note('E5', quarterLength=2.0)) | |
| voice2.append(note.Note('G5', quarterLength=2.0)) | |
| voice3 = stream.Voice() | |
| voice3.append(note.Note('G4', quarterLength=2.0)) | |
| voice3.append(note.Note('C5', quarterLength=2.0)) | |
| voice3.append(note.Note('E5', quarterLength=2.0)) | |
| # Put both voices into a Measure | |
| measure = stream.Measure() | |
| measure.timeSignature = meter.TimeSignature('4/4') # optional, but helpful | |
| measure.insert(0, voice1) | |
| measure.insert(0, voice2) | |
| measure.insert(0, voice3) | |
| # Wrap in a Part and Score | |
| part = stream.Part() | |
| part.append(measure) | |
| score = stream.Score() | |
| score.append(part) | |
| # Write to MusicXML | |
| score.write('musicxml', fp='two_voice_example.musicxml') | |
| Format_Converter().m21_to_svg_file(score, "output.svg") | |