def test_unpitched_notes(self): with self.assertRaises(musicxml_parser.UnpitchedNoteException): musicxml_parser.MusicXMLDocument(os.path.join( tf.resource_loader.get_data_files_path(), 'testdata/unpitched.xml')) with self.assertRaises(musicxml_reader.MusicXMLConversionError): musicxml_reader.musicxml_file_to_sequence_proto(os.path.join( tf.resource_loader.get_data_files_path(), 'testdata/unpitched.xml'))
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 test_transposed_keysig(self): xml = br"""<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!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/> </score-part> </part-list> <part id="P1"> <measure number="1"> <attributes> <divisions>4</divisions> <key> <fifths>-3</fifths> <mode>major</mode> </key> <time> <beats>4</beats> <beat-type>4</beat-type> </time> <clef> <sign>G</sign> <line>2</line> </clef> <transpose> <diatonic>-5</diatonic> <chromatic>-9</chromatic> </transpose> </attributes> <note> <pitch> <step>G</step> <octave>4</octave> </pitch> <duration>2</duration> <voice>1</voice> <type>quarter</type> </note> </measure> </part> </score-partwise> """ with tempfile.NamedTemporaryFile() as temp_file: temp_file.write(xml) temp_file.flush() musicxml_parser.MusicXMLDocument(temp_file.name) sequence = musicxml_reader.musicxml_file_to_sequence_proto(temp_file.name) self.assertLen(sequence.key_signatures, 1) self.assertEqual(music_pb2.NoteSequence.KeySignature.G_FLAT, sequence.key_signatures[0].key)
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 musicxml_file_to_sequence_proto(musicxml_file): """Converts a MusicXML file to a tensorflow.magenta.NoteSequence proto. Args: musicxml_file: A string path to a MusicXML file. Returns: A tensorflow.magenta.Sequence proto. Raises: MusicXMLConversionError: Invalid musicxml_file. """ try: musicxml_document = musicxml_parser.MusicXMLDocument(musicxml_file) except musicxml_parser.MusicXMLParseError as e: raise MusicXMLConversionError(e) return musicxml_to_sequence_proto(musicxml_document)
def test_harmony_missing_degree(self): xml = br"""<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!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/> </score-part> </part-list> <part id="P1"> <measure number="1"> <attributes> <divisions>2</divisions> <time> <beats>4</beats> <beat-type>4</beat-type> </time> </attributes> <note> <pitch> <step>G</step> <octave>4</octave> </pitch> <duration>2</duration> <voice>1</voice> <type>quarter</type> </note> <harmony> <degree> <!-- missing degree-value text --> <degree-value></degree-value> </degree> </harmony> </measure> </part> </score-partwise> """ with tempfile.NamedTemporaryFile() as temp_file: temp_file.write(xml) temp_file.flush() with self.assertRaises(musicxml_parser.ChordSymbolParseError): musicxml_parser.MusicXMLDocument(temp_file.name)
def my_musicxml_file_to_sequence_proto(musicxml_file): """Converts a MusicXML file to a tensorflow.magenta.NoteSequence proto. Args: musicxml_file: A string path to a MusicXML file. Returns: A tensorflow.magenta.Sequence proto. Raises: MusicXMLConversionError: Invalid musicxml_file. """ from magenta.protobuf import music_pb2 from magenta.music import note_sequence_io from magenta.music import sequences_lib from magenta.music import melodies_lib from magenta.music import Melody from magenta.music import musicxml_parser from magenta.music import musicxml_reader try: musicxml_document = musicxml_parser.MusicXMLDocument(musicxml_file) except musicxml_parser.MusicXMLParseException as e: raise MusicXMLConversionError(e) return my_musicxml_to_sequence_proto(musicxml_document)
def test_mid_measure_meter_change(self): with self.assertRaises(musicxml_parser.MultipleTimeSignatureException): musicxml_parser.MusicXMLDocument(self.mid_measure_meter_filename)
def test_alternating_meter(self): with self.assertRaises(musicxml_parser.AlternatingTimeSignatureException): musicxml_parser.MusicXMLDocument(self.alternating_meter_filename)
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