天天看點

Google's magenta convert_midi

from magenta.music import midi_io
from magenta.music import note_sequence_io
import os
import tensorflow as tf

full_file_path='./testdata/MIDI/example.mid'
root_dir='testdata'
sub_dir='MIDI'
try:
    sequence = midi_io.midi_to_sequence_proto(tf.io.gfile.GFile(full_file_path, 'rb').read())
    #print('11111',sequence)
    '''
    ticks_per_quarter: 220
    time_signatures {
      numerator: 4
      denominator: 4
    }
    key_signatures {
    }
    tempos {                  #速度/拍子
      qpm: 240.0
    }
    notes {
      pitch: 60
      velocity: 100
      end_time: 0.2375
    }
    notes {
      pitch: 62
      velocity: 100
      start_time: 0.25
      end_time: 0.4875
    }
    total_time: 1.9874999999999998
    source_info {
      encoding_type: MIDI
      parser: PRETTY_MIDI
    }
    '''
 
except midi_io.MIDIConversionError as e:
    print('22222')
    tf.logging.warning(
        'Could not parse MIDI file %s. It will be skipped. Error was: %s',
        full_file_path, e)
    #return None
sequence.collection_name = os.path.basename(root_dir)
sequence.filename = os.path.join(sub_dir, os.path.basename(full_file_path))
sequence.id = note_sequence_io.generate_note_sequence_id(
      sequence.filename, sequence.collection_name, 'midi')
tf.logging.info('Converted MIDI file %s.', full_file_path)
#print(sequence)
'''
id: "/id/midi/testdata/c9376b47d0e4c585bfcd243124b238c6dbbfb6af"
filename: "MIDI\\example.mid"
collection_name: "testdata"
ticks_per_quarter: 220
time_signatures {
  numerator: 4
  denominator: 4
}
key_signatures {
}
tempos {
  qpm: 240.0
}
notes {
  pitch: 60
  velocity: 100
  end_time: 0.2375
}
total_time: 1.9874999999999998
source_info {
  encoding_type: MIDI
  parser: PRETTY_MIDI
}
'''

           

繼續閱讀