def testAllNoteheadTypes(self): staff = musicscore_pb2.Staff( staffline_distance=10, center_line=[Point(x=0, y=50), Point(x=100, y=50)], glyph=[ Glyph(type=Glyph.CLEF_TREBLE, x=1, y_position=reader.TREBLE_CLEF_EXPECTED_Y), Glyph(type=Glyph.NOTEHEAD_FILLED, x=10, y_position=-6), Glyph(type=Glyph.NOTEHEAD_EMPTY, x=10, y_position=-6), Glyph(type=Glyph.NOTEHEAD_WHOLE, x=10, y_position=-6), ]) notes = conversions.page_to_notesequence( reader.ScoreReader().read_page( musicscore_pb2.Page( system=[musicscore_pb2.StaffSystem(staff=[staff])]))) self.assertEqual( notes, music_pb2.NoteSequence(notes=[ Note(pitch=librosa.note_to_midi('C4'), start_time=0, end_time=1), Note(pitch=librosa.note_to_midi('C4'), start_time=1, end_time=3), Note(pitch=librosa.note_to_midi('C4'), start_time=3, end_time=7), ]))
def testBeams(self): beam_1 = musicscore_pb2.LineSegment(start=Point(x=10, y=20), end=Point(x=40, y=20)) beam_2 = musicscore_pb2.LineSegment(start=Point(x=70, y=40), end=Point(x=90, y=40)) beam_3 = musicscore_pb2.LineSegment(start=Point(x=70, y=60), end=Point(x=90, y=60)) staff = musicscore_pb2.Staff( staffline_distance=10, center_line=[Point(x=0, y=50), Point(x=100, y=50)], glyph=[ Glyph(type=Glyph.CLEF_TREBLE, x=1, y_position=reader.TREBLE_CLEF_EXPECTED_Y), # 2 eighth notes. Glyph(type=Glyph.NOTEHEAD_FILLED, x=10, y_position=-4, beam=[beam_1]), Glyph(type=Glyph.NOTEHEAD_FILLED, x=40, y_position=-1, beam=[beam_1]), # 1 quarter note. Glyph(type=Glyph.NOTEHEAD_FILLED, x=50, y_position=0), # 2 sixteenth notes. Glyph(type=Glyph.NOTEHEAD_FILLED, x=60, y_position=-2, beam=[beam_2, beam_3]), Glyph(type=Glyph.NOTEHEAD_FILLED, x=90, y_position=2, beam=[beam_2, beam_3]), ]) notes = conversions.page_to_notesequence( reader.ScoreReader().read_page( musicscore_pb2.Page( system=[musicscore_pb2.StaffSystem(staff=[staff])]))) self.assertEqual( notes, music_pb2.NoteSequence(notes=[ Note(pitch=librosa.note_to_midi('E4'), start_time=0, end_time=0.5), Note(pitch=librosa.note_to_midi('A4'), start_time=0.5, end_time=1), Note(pitch=librosa.note_to_midi('B4'), start_time=1, end_time=2), Note(pitch=librosa.note_to_midi('G4'), start_time=2, end_time=2.25), Note(pitch=librosa.note_to_midi('D5'), start_time=2.25, end_time=2.5), ]))
def testDummy(self): # Create a single staff, and a single vertical which is the correct height # of a stem. The vertical has x = 20 and goes from struct = structure.Structure( staff_detector=staves_base.ComputedStaves( staves=[[[10, 50], [90, 50]]], staffline_distance=[12], staffline_thickness=2, staves_interpolated_y=[[50] * 100]), beams=beams.ComputedBeams(np.zeros((0, 2, 2))), verticals=verticals.ComputedVerticals( lines=[[[20, 38], [20, 38 + 12 * 4]]]), connected_components=components.ComputedComponents([])) stems = stems_module.Stems(struct) # Create a Page with Glyphs. input_page = musicscore_pb2.Page(system=[ musicscore_pb2.StaffSystem(staff=[ musicscore_pb2.Staff( staffline_distance=12, center_line=[ musicscore_pb2.Point(x=10, y=50), musicscore_pb2.Point(x=90, y=50) ], glyph=[ # Cannot have a stem because it's a flat. musicscore_pb2.Glyph(type=musicscore_pb2.Glyph.FLAT, x=15, y_position=-1), # On the right side of the stem, the correct distance away. musicscore_pb2.Glyph( type=musicscore_pb2.Glyph.NOTEHEAD_FILLED, x=25, y_position=-1), # Too high for the stem. musicscore_pb2.Glyph( type=musicscore_pb2.Glyph.NOTEHEAD_FILLED, x=25, y_position=4), # Too far right from the stem. musicscore_pb2.Glyph( type=musicscore_pb2.Glyph.NOTEHEAD_FILLED, x=35, y_position=-1), ]) ]) ]) page = stems.apply(input_page) self.assertFalse(page.system[0].staff[0].glyph[0].HasField("stem")) self.assertTrue(page.system[0].staff[0].glyph[1].HasField("stem")) self.assertEqual( page.system[0].staff[0].glyph[1].stem, musicscore_pb2.LineSegment(start=Point(x=20, y=38), end=Point(x=20, y=38 + 12 * 4))) self.assertFalse(page.system[0].staff[0].glyph[2].HasField("stem")) self.assertFalse(page.system[0].staff[0].glyph[3].HasField("stem"))
def apply(self, page): """Splits the staves in the page into systems with barlines.""" assert len(page.system) == 1 systems_map = dict( (i, (i, i)) for i in moves.xrange(len(page.system[0].staff))) for start, end in zip(self.barline_staff_start, self.barline_staff_end): for staff in moves.xrange(start, end + 1): start = min(start, systems_map[staff][0]) end = max(end, systems_map[staff][1]) for staff in moves.xrange(start, end + 1): systems_map[staff] = (start, end) system_inds = sorted(set(systems_map.values()), key=lambda x: x[0]) staves = page.system[0].staff systems = [ musicscore_pb2.StaffSystem(staff=staves[start:end + 1]) for (start, end) in system_inds ] self._assign_barlines(systems) return musicscore_pb2.Page(system=systems)
def testBass_simple(self): staff = musicscore_pb2.Staff( staffline_distance=10, center_line=[Point(x=0, y=50), Point(x=100, y=50)], glyph=[ Glyph(type=Glyph.CLEF_BASS, x=1, y_position=reader.BASS_CLEF_EXPECTED_Y), Glyph(type=Glyph.NOTEHEAD_FILLED, x=10, y_position=0), ]) notes = conversions.page_to_notesequence( reader.ScoreReader().read_page( musicscore_pb2.Page( system=[musicscore_pb2.StaffSystem(staff=[staff])]))) self.assertEqual( notes, music_pb2.NoteSequence(notes=[ Note( pitch=librosa.note_to_midi('D3'), start_time=0, end_time=1) ]))
def glyph_predictions_to_page(self, predictions): """Converts the glyph predictions to a Page message. Args: predictions: NumPy array which is equal to `self.get_detected_glyphs().eval()` (but multiple tensors are evaluated in a single run for efficiency.) Shape `(num_glyphs, 3)`. Returns: A `Page` message holding a single `StaffSystem`, with `Staff` messages that only hold `Glyph`s. Structural information is added to the page by `OMREngine`. """ num_staves = ( predictions[:, int(GlyphsTensorColumns.STAFF_INDEX)].max() + 1 if predictions.size else 0) def create_glyph(glyph): return musicscore_pb2.Glyph( x=glyph[GlyphsTensorColumns.X], y_position=glyph[GlyphsTensorColumns.Y_POSITION], type=glyph[GlyphsTensorColumns.TYPE]) def generate_staff(staff_num): glyphs = predictions[ predictions[:, int(GlyphsTensorColumns.STAFF_INDEX)] == staff_num] # For determinism, sort glyphs by x, breaking ties by position (low to # high). glyph_order = np.lexsort( glyphs[:, [GlyphsTensorColumns.Y_POSITION, GlyphsTensorColumns. X]].T) glyphs = glyphs[glyph_order] return musicscore_pb2.Staff(glyph=map(create_glyph, glyphs)) return musicscore_pb2.Page(system=[ musicscore_pb2.StaffSystem( staff=map(generate_staff, range(num_staves))) ])
def testTreble_accidentals(self): staff_1 = musicscore_pb2.Staff( staffline_distance=10, center_line=[Point(x=0, y=50), Point(x=100, y=50)], glyph=[ Glyph(type=Glyph.CLEF_TREBLE, x=1, y_position=reader.TREBLE_CLEF_EXPECTED_Y), Glyph(type=Glyph.NOTEHEAD_FILLED, x=10, y_position=-6), Glyph(type=Glyph.FLAT, x=16, y_position=-4), Glyph(type=Glyph.NOTEHEAD_FILLED, x=20, y_position=-4), Glyph(type=Glyph.NOTEHEAD_FILLED, x=30, y_position=-2), Glyph(type=Glyph.NOTEHEAD_FILLED, x=40, y_position=-4), ]) staff_2 = musicscore_pb2.Staff( staffline_distance=10, center_line=[Point(x=0, y=150), Point(x=100, y=150)], glyph=[ Glyph(type=Glyph.CLEF_TREBLE, x=1, y_position=reader.TREBLE_CLEF_EXPECTED_Y), Glyph(type=Glyph.NOTEHEAD_FILLED, x=10, y_position=-6), Glyph(type=Glyph.NOTEHEAD_FILLED, x=20, y_position=-4), Glyph(type=Glyph.NOTEHEAD_FILLED, x=30, y_position=-2), Glyph(type=Glyph.SHARP, x=35, y_position=-2), Glyph(type=Glyph.NOTEHEAD_FILLED, x=40, y_position=-2), Glyph(type=Glyph.NATURAL, x=45, y_position=-2), Glyph(type=Glyph.NOTEHEAD_FILLED, x=50, y_position=-2), ]) notes = conversions.page_to_notesequence( reader.ScoreReader().read_page( musicscore_pb2.Page(system=[ musicscore_pb2.StaffSystem(staff=[staff_1]), musicscore_pb2.StaffSystem(staff=[staff_2]) ]))) self.assertEqual( notes, music_pb2.NoteSequence(notes=[ # First staff. Note(pitch=librosa.note_to_midi('C4'), start_time=0, end_time=1), Note(pitch=librosa.note_to_midi('Eb4'), start_time=1, end_time=2), Note(pitch=librosa.note_to_midi('G4'), start_time=2, end_time=3), Note(pitch=librosa.note_to_midi('Eb4'), start_time=3, end_time=4), # Second staff. Note(pitch=librosa.note_to_midi('C4'), start_time=4, end_time=5), Note(pitch=librosa.note_to_midi('E4'), start_time=5, end_time=6), Note(pitch=librosa.note_to_midi('G4'), start_time=6, end_time=7), Note(pitch=librosa.note_to_midi('G#4'), start_time=7, end_time=8), Note(pitch=librosa.note_to_midi('G4'), start_time=8, end_time=9), ]))
def testKeySignatures(self): # One staff per system, two systems. staff_1 = musicscore_pb2.Staff(glyph=[ Glyph(type=Glyph.CLEF_TREBLE, x=5, y_position=reader.TREBLE_CLEF_EXPECTED_Y), # D major key signature. Glyph(type=Glyph.SHARP, x=15, y_position=+4), Glyph(type=Glyph.SHARP, x=25, y_position=+1), # Accidental which cannot be interpreted as part of the key # signature. Glyph(type=Glyph.SHARP, x=35, y_position=+2), Glyph(type=Glyph.NOTEHEAD_FILLED, x=45, y_position=+2), # D#5 Glyph(type=Glyph.NOTEHEAD_EMPTY, x=55, y_position=+1), # C#5 Glyph(type=Glyph.NOTEHEAD_FILLED, x=65, y_position=-3), # F#4 # New measure. The key signature should be retained. Glyph(type=Glyph.NOTEHEAD_EMPTY, x=105, y_position=-3), # F#4 Glyph(type=Glyph.NOTEHEAD_FILLED, x=125, y_position=+1), # C#5 # Accidental is not retained. Glyph(type=Glyph.NOTEHEAD_FILLED, x=145, y_position=+2), # D5 ]) staff_2 = musicscore_pb2.Staff(glyph=[ Glyph(type=Glyph.CLEF_TREBLE, x=5, y_position=reader.TREBLE_CLEF_EXPECTED_Y), # No key signature on this line. No accidentals. Glyph(type=Glyph.NOTEHEAD_EMPTY, x=25, y_position=-3), # F4 Glyph(type=Glyph.NOTEHEAD_EMPTY, x=45, y_position=+1), # C5 ]) notes = conversions.page_to_notesequence( reader.ScoreReader().read_page( musicscore_pb2.Page(system=[ musicscore_pb2.StaffSystem( staff=[staff_1], bar=[_bar( 0), _bar(100), _bar(200)]), musicscore_pb2.StaffSystem(staff=[staff_2]), ]))) self.assertEqual( notes, music_pb2.NoteSequence(notes=[ # First measure. Note(pitch=librosa.note_to_midi('D#5'), start_time=0, end_time=1), Note(pitch=librosa.note_to_midi('C#5'), start_time=1, end_time=3), Note(pitch=librosa.note_to_midi('F#4'), start_time=3, end_time=4), # Second measure. Note(pitch=librosa.note_to_midi('F#4'), start_time=4, end_time=6), Note(pitch=librosa.note_to_midi('C#5'), start_time=6, end_time=7), Note(pitch=librosa.note_to_midi('D5'), start_time=7, end_time=8), # Third measure on a new line, with no key signature. Note(pitch=librosa.note_to_midi('F4'), start_time=8, end_time=10), Note(pitch=librosa.note_to_midi('C5'), start_time=10, end_time=12), ]))
def testMeasures(self): # 2 staves in the same staff system with multiple bars. staff_1 = musicscore_pb2.Staff( staffline_distance=10, center_line=[Point(x=0, y=50), Point(x=300, y=50)], glyph=[ Glyph(type=Glyph.CLEF_TREBLE, x=1, y_position=reader.TREBLE_CLEF_EXPECTED_Y), # Key signature. Glyph(type=Glyph.SHARP, x=10, y_position=+4), Glyph(type=Glyph.NOTEHEAD_FILLED, x=20, y_position=-2), # Accidental. Glyph(type=Glyph.FLAT, x=40, y_position=-1), Glyph(type=Glyph.NOTEHEAD_FILLED, x=50, y_position=-1), # Second bar. Glyph(type=Glyph.NOTEHEAD_FILLED, x=120, y_position=0), Glyph(type=Glyph.NOTEHEAD_FILLED, x=180, y_position=+4), # Third bar. # Accidental not propagated to this note. Glyph(type=Glyph.NOTEHEAD_FILLED, x=220, y_position=-1), ]) staff_2 = musicscore_pb2.Staff( staffline_distance=10, center_line=[Point(x=0, y=150), Point(x=300, y=150)], glyph=[ Glyph(type=Glyph.CLEF_BASS, x=1, y_position=reader.BASS_CLEF_EXPECTED_Y), # Key signature. Glyph(type=Glyph.FLAT, x=15, y_position=-2), Glyph(type=Glyph.NOTEHEAD_FILLED, x=20, y_position=-2), Glyph(type=Glyph.NOTEHEAD_FILLED, x=50, y_position=+2), # Second bar. Glyph(type=Glyph.NOTEHEAD_FILLED, x=150, y_position=-2), # Third bar. Glyph(type=Glyph.REST_QUARTER, x=220, y_position=0), Glyph(type=Glyph.NOTEHEAD_FILLED, x=280, y_position=-2), ]) staff_system = musicscore_pb2.StaffSystem( staff=[staff_1, staff_2], bar=[_bar(0), _bar(100), _bar(200), _bar(300)]) notes = conversions.page_to_notesequence( reader.ScoreReader().read_page( musicscore_pb2.Page(system=[staff_system]))) self.assertEqual( notes, music_pb2.NoteSequence(notes=[ # Staff 1, bar 1. Note(pitch=librosa.note_to_midi('G4'), start_time=0, end_time=1), Note(pitch=librosa.note_to_midi('Ab4'), start_time=1, end_time=2), # Staff 1, bar 2. Note(pitch=librosa.note_to_midi('B4'), start_time=2, end_time=3), Note(pitch=librosa.note_to_midi('F#5'), start_time=3, end_time=4), # Staff 1, bar 3. Note(pitch=librosa.note_to_midi('A4'), start_time=4, end_time=5), # Staff 2, bar 1. Note(pitch=librosa.note_to_midi('Bb2'), start_time=0, end_time=1), Note(pitch=librosa.note_to_midi('F3'), start_time=1, end_time=2), # Staff 2, bar 2. Note(pitch=librosa.note_to_midi('Bb2'), start_time=2, end_time=3), # Staff 2, bar 3. Note(pitch=librosa.note_to_midi('Bb2'), start_time=5, end_time=6), ]))
def testStaffSystems(self): # 2 staff systems on separate pages, each with 2 staves, and no bars. system_1_staff_1 = musicscore_pb2.Staff( staffline_distance=10, center_line=[Point(x=0, y=50), Point(x=100, y=50)], glyph=[ Glyph(type=Glyph.CLEF_TREBLE, x=1, y_position=reader.TREBLE_CLEF_EXPECTED_Y), Glyph(type=Glyph.NOTEHEAD_FILLED, x=10, y_position=-6), Glyph(type=Glyph.NOTEHEAD_FILLED, x=50, y_position=-2), ]) system_1_staff_2 = musicscore_pb2.Staff( staffline_distance=10, center_line=[Point(x=0, y=150), Point(x=100, y=150)], glyph=[ Glyph(type=Glyph.CLEF_BASS, x=2, y_position=reader.BASS_CLEF_EXPECTED_Y), Glyph(type=Glyph.NOTEHEAD_FILLED, x=10, y_position=0), Glyph(type=Glyph.NOTEHEAD_FILLED, x=40, y_position=2), # Played after the second note in the first staff, although it is to # the left of it. Glyph(type=Glyph.NOTEHEAD_FILLED, x=45, y_position=4), ]) system_2_staff_1 = musicscore_pb2.Staff( staffline_distance=10, center_line=[Point(x=0, y=250), Point(x=100, y=250)], glyph=[ Glyph(type=Glyph.CLEF_TREBLE, x=1, y_position=reader.TREBLE_CLEF_EXPECTED_Y), Glyph(type=Glyph.REST_QUARTER, x=20, y_position=0), Glyph(type=Glyph.NOTEHEAD_FILLED, x=50, y_position=-2), ]) system_2_staff_2 = musicscore_pb2.Staff( staffline_distance=10, center_line=[Point(x=0, y=250), Point(x=100, y=250)], glyph=[ Glyph(type=Glyph.CLEF_BASS, x=2, y_position=reader.BASS_CLEF_EXPECTED_Y), Glyph(type=Glyph.NOTEHEAD_FILLED, x=10, y_position=0), Glyph(type=Glyph.NOTEHEAD_FILLED, x=40, y_position=2), ]) notes = conversions.score_to_notesequence(reader.ScoreReader()( musicscore_pb2.Score(page=[ musicscore_pb2.Page(system=[ musicscore_pb2.StaffSystem( staff=[system_1_staff_1, system_1_staff_2]), ]), musicscore_pb2.Page(system=[ musicscore_pb2.StaffSystem( staff=[system_2_staff_1, system_2_staff_2]), ]), ]), )) self.assertEqual( notes, music_pb2.NoteSequence(notes=[ # System 1, staff 1. Note(pitch=librosa.note_to_midi('C4'), start_time=0, end_time=1), Note(pitch=librosa.note_to_midi('G4'), start_time=1, end_time=2), # System 1, staff 2. Note(pitch=librosa.note_to_midi('D3'), start_time=0, end_time=1), Note(pitch=librosa.note_to_midi('F3'), start_time=1, end_time=2), Note(pitch=librosa.note_to_midi('A3'), start_time=2, end_time=3), # System 2, staff 1. # Quarter rest. Note(pitch=librosa.note_to_midi('G4'), start_time=4, end_time=5), # System 2, staff 2. Note(pitch=librosa.note_to_midi('D3'), start_time=3, end_time=4), Note(pitch=librosa.note_to_midi('F3'), start_time=4, end_time=5), ]))
def testChords(self): stem_1 = musicscore_pb2.LineSegment(start=Point(x=20, y=10), end=Point(x=20, y=70)) stem_2 = musicscore_pb2.LineSegment(start=Point(x=50, y=10), end=Point(x=50, y=70)) staff = musicscore_pb2.Staff( staffline_distance=10, center_line=[Point(x=0, y=50), Point(x=100, y=50)], glyph=[ Glyph(type=Glyph.CLEF_TREBLE, x=1, y_position=reader.TREBLE_CLEF_EXPECTED_Y), # Chord of 2 notes. Glyph(type=Glyph.NOTEHEAD_FILLED, x=10, y_position=-4, stem=stem_1), Glyph(type=Glyph.NOTEHEAD_FILLED, x=10, y_position=-1, stem=stem_1), # Note not attached to a stem. Glyph(type=Glyph.NOTEHEAD_FILLED, x=30, y_position=3), # Chord of 3 notes. Glyph(type=Glyph.NOTEHEAD_FILLED, x=40, y_position=0, stem=stem_2), Glyph(type=Glyph.NOTEHEAD_FILLED, x=60, y_position=2, stem=stem_2), Glyph(type=Glyph.NOTEHEAD_FILLED, x=60, y_position=4, stem=stem_2), ]) notes = conversions.page_to_notesequence( reader.ScoreReader().read_page( musicscore_pb2.Page( system=[musicscore_pb2.StaffSystem(staff=[staff])]))) self.assertEqual( notes, music_pb2.NoteSequence(notes=[ # First chord. Note(pitch=librosa.note_to_midi('E4'), start_time=0, end_time=1), Note(pitch=librosa.note_to_midi('A4'), start_time=0, end_time=1), # Note without a stem. Note(pitch=librosa.note_to_midi('E5'), start_time=1, end_time=2), # Second chord. Note(pitch=librosa.note_to_midi('B4'), start_time=2, end_time=3), Note(pitch=librosa.note_to_midi('D5'), start_time=2, end_time=3), Note(pitch=librosa.note_to_midi('F5'), start_time=2, end_time=3), ]))
def testSmallScore(self): score = musicscore_pb2.Score(page=[ musicscore_pb2.Page(system=[ musicscore_pb2.StaffSystem(staff=[ musicscore_pb2.Staff(glyph=[ musicscore_pb2.Glyph( type=musicscore_pb2.Glyph.NOTEHEAD_FILLED, x=10, y_position=0, note=music_pb2.NoteSequence.Note( start_time=0, end_time=1, pitch=71)), musicscore_pb2.Glyph( type=musicscore_pb2.Glyph.NOTEHEAD_EMPTY, x=110, y_position=-6, note=music_pb2.NoteSequence.Note( start_time=1, end_time=2.5, pitch=61)), ]), musicscore_pb2.Staff(glyph=[ musicscore_pb2.Glyph( type=musicscore_pb2.Glyph.NOTEHEAD_WHOLE, x=10, y_position=2, note=music_pb2.NoteSequence.Note( start_time=0, end_time=4, pitch=50)), musicscore_pb2.Glyph( type=musicscore_pb2.Glyph.NOTEHEAD_FILLED, beam=[ musicscore_pb2.LineSegment(), musicscore_pb2.LineSegment() ], x=110, y_position=-4, note=music_pb2.NoteSequence.Note( start_time=4, end_time=4.25, pitch=60)), ]), ]), ]), ]) self.assertEqual( b"""<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 3.0 Partwise//EN" "http://www.musicxml.org/dtds/partwise.dtd"> <score-partwise version="3.0"> <part-list> <score-part id="P1"> <part-name>Part 1</part-name> </score-part> <score-part id="P2"> <part-name>Part 2</part-name> </score-part> </part-list> <part id="P1"> <measure number="1"> <attributes> <divisions>1024</divisions> <time symbol="common"> <beats>4</beats> <beat-type>4</beat-type> </time> </attributes> <note> <voice>1</voice> <type>quarter</type> <duration>1024</duration> <pitch> <step>B</step> <alter>0</alter> <octave>4</octave> </pitch> </note> <note> <voice>1</voice> <type>half</type> <duration>1536</duration> <pitch> <step>C</step> <alter>1</alter> <octave>4</octave> </pitch> </note> </measure> </part> <part id="P2"> <measure number="1"> <attributes> <divisions>1024</divisions> <time symbol="common"> <beats>4</beats> <beat-type>4</beat-type> </time> </attributes> <note> <voice>1</voice> <type>whole</type> <duration>4096</duration> <pitch> <step>D</step> <alter>0</alter> <octave>3</octave> </pitch> </note> <note> <voice>1</voice> <type>16th</type> <duration>256</duration> <pitch> <step>C</step> <alter>0</alter> <octave>4</octave> </pitch> </note> </measure> </part> </score-partwise> """, musicxml.score_to_musicxml(score))
PREDICTIONS = np.asarray( [[[1, 1, 1, 1, 2, 1], [3, 1, 5, 1, 1, 1], [1, 4, 1, 1, 1, 1]], [[1, 1, 3, 1, 1, 1], [1, 1, 5, 1, 1, 1], [1, 1, 1, 1, 3, 5]]]) # pyformat: disable # Page corresponding to the glyphs in PREDICTIONS. GLYPHS_PAGE = musicscore_pb2.Page(system=[ musicscore_pb2.StaffSystem(staff=[ musicscore_pb2.Staff(glyph=[ musicscore_pb2.Glyph(x=0, y_position=0, type=3), musicscore_pb2.Glyph(x=1, y_position=-1, type=4), musicscore_pb2.Glyph(x=2, y_position=0, type=5), musicscore_pb2.Glyph(x=4, y_position=1, type=2), ]), musicscore_pb2.Staff(glyph=[ musicscore_pb2.Glyph(x=2, y_position=1, type=3), musicscore_pb2.Glyph(x=2, y_position=0, type=5), musicscore_pb2.Glyph(x=4, y_position=-1, type=3), musicscore_pb2.Glyph(x=5, y_position=-1, type=5), ]), ]), ]) class DummyGlyphClassifier(convolutional.Convolutional1DGlyphClassifier): """A 1D convolutional glyph classifier with constant predictions. The predictions have shape (num_staves, num_stafflines, width). """
def testDummy(self): # Create a single staff, and a single vertical which is the correct height # of a stem. The vertical has x = 20 and goes from struct = structure.Structure( staff_detector=staves_base.ComputedStaves( staves=[[[10, 50], [90, 50]], [[11, 150], [91, 150]], [[10, 250], [90, 250]], [[10, 350], [90, 350]]], staffline_distance=[12] * 4, staffline_thickness=2, staves_interpolated_y=[[50] * 100, [150] * 100, [250] * 100, [350] * 100]), beams=beams.ComputedBeams(np.zeros((0, 2, 2))), connected_components=components.ComputedComponents(np.zeros( (0, 5))), verticals=verticals.ComputedVerticals(lines=[ # Joins the first 2 staves. [[10, 50 - 12 * 2], [10, 150 + 12 * 2]], # Another barline, too close to the first one. [[12, 50 - 12 * 2], [12, 150 + 12 * 2]], # This barline is far enough, because the second barline was # skipped. [[13, 50 - 12 * 2], [13, 150 + 12 * 2]], # Single staff barlines are skipped. [[30, 50 - 12 * 2], [30, 50 + 12 * 2]], [[31, 150 - 12 * 2], [31, 150 + 12 * 2]], # Too close to a stem. [[70, 50 - 12 * 2], [70, 50 + 12 * 2]], # Too short. [[90, 50 - 12 * 2], [90, 50 + 12 * 2]], # Another barline which is kept. [[90, 50 - 12 * 2], [90, 150 + 12 * 2]], # Staff 1 has no barlines. # Staff 2 has 2 barlines. [[11, 350 - 12 * 2], [11, 350 + 12 * 2]], [[90, 350 - 12 * 2], [90, 350 + 12 * 2]], ])) barlines = barlines_module.Barlines(struct, close_barline_threshold=3) # Create a Page with Glyphs. input_page = musicscore_pb2.Page(system=[ musicscore_pb2.StaffSystem(staff=[ musicscore_pb2.Staff( staffline_distance=12, center_line=[ musicscore_pb2.Point(x=10, y=50), musicscore_pb2.Point(x=90, y=50) ], glyph=[ # Stem is close to the last vertical on the first staff, so # a barline will not be detected there. musicscore_pb2.Glyph( type=musicscore_pb2.Glyph.NOTEHEAD_FILLED, x=60, y_position=2, stem=musicscore_pb2.LineSegment( start=musicscore_pb2.Point(x=72, y=40), end=musicscore_pb2.Point(x=72, y=80))), ]), musicscore_pb2.Staff(staffline_distance=12, center_line=[ musicscore_pb2.Point(x=10, y=150), musicscore_pb2.Point(x=90, y=150) ]), musicscore_pb2.Staff(staffline_distance=12, center_line=[ musicscore_pb2.Point(x=10, y=250), musicscore_pb2.Point(x=90, y=250) ]), musicscore_pb2.Staff(staffline_distance=12, center_line=[ musicscore_pb2.Point(x=10, y=350), musicscore_pb2.Point(x=90, y=350) ]), ]) ]) page = barlines.apply(input_page) self.assertEqual(3, len(page.system)) self.assertEqual(2, len(page.system[0].staff)) self.assertItemsEqual([10, 13, 90], (bar.x for bar in page.system[0].bar)) self.assertEqual(1, len(page.system[1].staff)) self.assertEqual(0, len(page.system[1].bar)) self.assertEqual(1, len(page.system[2].staff)) self.assertEqual(2, len(page.system[2].bar)) self.assertItemsEqual([11, 90], (bar.x for bar in page.system[2].bar))