def checkFMajorScale(self, filename): """Verify MusicXML scale file. Verify that it contains the correct pitches (sounding pitch) and durations. Args: filename: file to test. """ # Expected QuantizedSequence # Sequence tuple = (midi_pitch, velocity, start_seconds, end_seconds) expected_quantized_sequence = sequences_lib.QuantizedSequence() expected_quantized_sequence.steps_per_quarter = self.steps_per_quarter expected_quantized_sequence.qpm = 120.0 expected_quantized_sequence.time_signature = ( sequences_lib.QuantizedSequence.TimeSignature(numerator=4, denominator=4)) testing_lib.add_quantized_track_to_sequence( expected_quantized_sequence, 0, [(65, 64, 0, 4), (67, 64, 4, 8), (69, 64, 8, 12), (70, 64, 12, 16), (72, 64, 16, 20), (74, 64, 20, 24), (76, 64, 24, 28), (77, 64, 28, 32)]) # Convert MusicXML to QuantizedSequence source_musicxml = musicxml_parser.MusicXMLDocument(filename) sequence_proto = musicxml_reader.musicxml_to_sequence_proto( source_musicxml) quantized = sequences_lib.QuantizedSequence() quantized.from_note_sequence(sequence_proto, self.steps_per_quarter) # Check equality self.assertEqual(expected_quantized_sequence, quantized)
def checkFMajorScale(self, filename, part_name): """Verify MusicXML scale file. Verify that it contains the correct pitches (sounding pitch) and durations. Args: filename: file to test. part_name: name of the part the sequence is expected to contain. """ expected_ns = common_testing_lib.parse_test_proto( music_pb2.NoteSequence, """ ticks_per_quarter: 220 source_info: { source_type: SCORE_BASED encoding_type: MUSIC_XML parser: MAGENTA_MUSIC_XML } key_signatures { key: F time: 0 } time_signatures { numerator: 4 denominator: 4 } tempos { qpm: 120.0 } total_time: 4.0 """) part_info = expected_ns.part_infos.add() part_info.name = part_name expected_pitches = [65, 67, 69, 70, 72, 74, 76, 77] time = 0 for pitch in expected_pitches: note = expected_ns.notes.add() note.part = 0 note.voice = 1 note.pitch = pitch note.start_time = time time += .5 note.end_time = time note.velocity = 64 note.numerator = 1 note.denominator = 4 # Convert MusicXML to NoteSequence source_musicxml = musicxml_parser.MusicXMLDocument(filename) sequence_proto = musicxml_reader.musicxml_to_sequence_proto(source_musicxml) # Check equality self.assertProtoEquals(expected_ns, sequence_proto)
def emissionModelTrainer(self): ALL_CHORD_LIST = ['N.C', 'C', 'Cm', 'C#', 'C#m', 'D', 'Dm', 'Eb', 'Ebm', 'E', 'Em', 'F', 'Fm', 'F#', 'F#m', 'G', 'Gm', 'G#', 'G#m', 'A', 'Am', 'A#', 'A#m', 'B', 'Bm'] Same_Chord = {'Db': 'C#', 'Dbm': 'C#m', 'D#': 'Eb', 'D#m': 'Ebm', 'Gb': 'F#', 'Gbm': 'F#m', 'Ab': 'G#', 'Abm': 'G#m', 'Bb': 'A#', 'Bbm': 'A#m'} ALL_NOTE_LIST = ['C', 'C#', 'D', 'Eb', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'] path = "D:\FAI\Wikifonia" for file in glob.glob(path): mxlObject = musicxml_parser.MusicXMLDocument(file) mxlSequence = musicxml_reader.musicxml_to_sequence_proto(mxlObject) quantizedNoteSequence = sequences_lib.quantize_note_sequence(mxlSequence, 1) melodies, stats = melody_pipelines.extract_melodies(quantizedNoteSequence) chord_prog, stats = chord_pipelines.extract_chords_for_melodies(quantizedNoteSequence, melodies) if not chord_prog: continue for i in range(len(list(chord_prog[0]))): curChord = list(chord_prog[0])[i] curMel = list(melodies[0])[i] while (curMel > 71): curMel = curMel - 12 while (curMel < 60): curMel = curMel + 12 curChord = re.sub(r'\d+', '', curChord) curChord = curChord[:3] if curChord not in 'N.C.': if len(curChord) == 3 and curChord[2] not in 'm': curChord = curChord[:2] if curChord[1] not in ['#', 'b']: curChord = curChord[:1] if curChord in Same_Chord: curChord = Same_Chord[curChord] if curChord in 'Cb': curChord = 'B' if curChord in 'Fb': curChord = 'E' if curChord in 'Cbm': curChord = 'D' if curChord in 'Fbm': curChord = 'Em' a = ALL_CHORD_LIST.index(re.sub(r'\d+', '', curChord)) b = curMel self.mo_matrix[a][b - 60] = self.mo_matrix[a][b - 60] + 1 normed_mo_matrix = normalize(self.mo_matrix, axis=1, norm='l1') self.mo_matrix = normed_mo_matrix
def testcompressedxmltosequence(self): """Test the translation from compressed MusicXML to Sequence proto. Compare a compressed MusicXML file to an identical uncompressed sequence. """ uncompressed_musicxml = musicxml_parser.MusicXMLDocument( self.flute_scale_filename) compressed_musicxml = musicxml_parser.MusicXMLDocument( self.compressed_filename) uncompressed_proto = musicxml_reader.musicxml_to_sequence_proto( uncompressed_musicxml) self.checkmusicxmlandsequence(compressed_musicxml, uncompressed_proto) self.checkFMajorScale(self.flute_scale_filename, 'Flute')
def testtransposedxmltosequence(self): """Test the translation from transposed MusicXML to Sequence proto. Compare a transposed MusicXML file (clarinet) to an identical untransposed sequence (flute). """ untransposed_musicxml = musicxml_parser.MusicXMLDocument( self.flute_scale_filename) transposed_musicxml = musicxml_parser.MusicXMLDocument( self.clarinet_scale_filename) untransposed_proto = musicxml_reader.musicxml_to_sequence_proto( untransposed_musicxml) self.checkmusicxmlandsequence(transposed_musicxml, untransposed_proto) self.checkFMajorScale(self.clarinet_scale_filename, 'Clarinet in Bb')
def testmultiplecompressedxmltosequence(self): """Test the translation from compressed MusicXML with multiple rootfiles. The example MXL file contains a MusicXML file of the Flute F Major scale, as well as the PNG rendering of the score contained within the single MXL file. """ uncompressed_musicxml = musicxml_parser.MusicXMLDocument( self.flute_scale_filename) compressed_musicxml = musicxml_parser.MusicXMLDocument( self.multiple_rootfile_compressed_filename) uncompressed_proto = musicxml_reader.musicxml_to_sequence_proto( uncompressed_musicxml) self.checkmusicxmlandsequence(compressed_musicxml, uncompressed_proto) self.checkFMajorScale(self.flute_scale_filename, 'Flute')
def cleanDataset(self): path = "D:\FAI\Wikifonia" count = 0 for file in glob.glob(path): try: mxlObject = musicxml_parser.MusicXMLDocument(file) mxlSequence = musicxml_reader.musicxml_to_sequence_proto(mxlObject) quantizedNoteSequence = sequences_lib.quantize_note_sequence(mxlSequence, 1) chord_prog, stats = chord_pipelines.extract_chords(quantizedNoteSequence) melodies, stats = melody_pipelines.extract_melodies(quantizedNoteSequence) ac, stats = chord_pipelines.extract_chords_for_melodies(quantizedNoteSequence, melodies) except: os.remove(file) print(file) count = count + 1
def checkmusicxmltosequence(self, filename): """Test the translation from MusicXML to Sequence proto.""" source_musicxml = musicxml_parser.MusicXMLDocument(filename) sequence_proto = musicxml_reader.musicxml_to_sequence_proto(source_musicxml) self.checkmusicxmlandsequence(source_musicxml, sequence_proto)
def observationModelTrainer(self): ALL_CHORD_LIST = [ 'N.C', 'C', 'Cm', 'C#', 'C#m', 'D', 'Dm', 'Eb', 'Ebm', 'E', 'Em', 'F', 'Fm', 'F#', 'F#m', 'G', 'Gm', 'G#', 'G#m', 'A', 'Am', 'A#', 'A#m', 'B', 'Bm' ] Same_Chord = { 'Db': 'C#', 'Dbm': 'C#m', 'D#': 'Eb', 'D#m': 'Ebm', 'Gb': 'F#', 'Gbm': 'F#m', 'Ab': 'G#', 'Abm': 'G#m', 'Bb': 'A#', 'Bbm': 'A#m' } path = "Wikifonia" for file in glob.glob(path): mxlObject = musicxml_parser.MusicXMLDocument(file) mxlSequence = musicxml_reader.musicxml_to_sequence_proto(mxlObject) quantizedNoteSequence = sequences_lib.quantize_note_sequence( mxlSequence, 1) chord_prog, stats = chord_pipelines.extract_chords( quantizedNoteSequence) previous = None for chord in list(chord_prog[0]): if previous is None: previous = chord continue curChord = re.sub(r'\d+', '', chord) prevChord = re.sub(r'\d+', '', previous) curChord = curChord[:3] prevChord = prevChord[:3] if curChord != 'N.C': if len(curChord) == 3 and curChord[2] != 'm': curChord = curChord[:2] if curChord[1] not in ['#', 'b']: curChord = curChord[:1] if prevChord != 'N.C': if len(prevChord) == 3 and prevChord[2] != 'm': prevChord = prevChord[:2] if prevChord[1] not in ['#', 'b']: prevChord = prevChord[:1] if curChord in Same_Chord: curChord = Same_Chord[curChord] if prevChord in Same_Chord: prevChord = Same_Chord[prevChord] if curChord == 'Cb': curChord = 'B' if prevChord == 'Cb': prevChord = 'B' if curChord == 'Fb': curChord = 'E' if prevChord == 'Fb': prevChord = 'E' if curChord == 'Cbm': curChord = 'D' if prevChord == 'Cbm': prevChord = 'D' if curChord == 'Fbm': curChord = 'Em' if prevChord == 'Fbm': prevChord = 'Em' if prevChord != curChord: a = ALL_CHORD_LIST.index(prevChord) b = ALL_CHORD_LIST.index(curChord) self.ct_matrix[a][b] = self.ct_matrix[a][b] + 1 previous = curChord normed_ct_matrix = normalize(self.ct_matrix, axis=1, norm='l1') self.ct_matrix = normed_ct_matrix