<jupyter_start><jupyter_text>MIDI File Parsing and CreationThis notebook will show you how to parse or create a MIDI file   A. Parse a MIDI file<jupyter_code># load midi file
path_midi = '../testcases/2.midi'
mido_obj = mid_parser.MidiFile(path_midi)

# ticks per beats
print(' > ticks per beat:', mido_obj.ticks_per_beat)

# signatures
print('\n -- time signatures -- ')
print(' > amount:', len(mido_obj.time_signature_changes))
print(mido_obj.time_signature_changes[0])

print('\n -- key signatures --')
print(' > amount:', len(mido_obj.key_signature_changes))

# tempo changes
print('\n -- tempo changes --')
print(' > amount:', len(mido_obj.tempo_changes))
print(mido_obj.tempo_changes[0])

# markers
print('\n -- markers --')
print(' > amount:', len(mido_obj.markers))
print(mido_obj.markers[0])

# instruments
print('\n -- instruments --')
print(' > number of tracks:', len(mido_obj.instruments))
print(' > number of notes:', len(mido_obj.instruments[0].notes))

# convert to seconds
note = mido_obj.instruments[0].notes[20]
mapping = mido_obj.get_tick_to_time_mapping()
tick = note.start
sec = mapping[tick]
print('{} tick at {} seconds'.format(tick, sec))<jupyter_output>> ticks per beat: 480

 -- time signatures -- 
 > amount: 1
4/4 at 0 ticks

 -- key signatures --
 > amount: 0

 -- tempo changes --
 > amount: 257
85.00004250002125 BPM at 0 ticks

 -- markers --
 > amount: 79
"C#:maj" at 0 ticks

 -- instruments --
 > number of tracks: 1
 > number of notes: 748
3840 tick at 5.535648999999999 seconds<jupyter_text>B. Crop a segment<jupyter_code># load midi file
path_midi = 'testcases/2.midi'
mido_obj = mid_parser.MidiFile(path_midi)
beat_resol = mido_obj.ticks_per_beat

# define interval: from 2nd to 8th bar
st = beat_resol * 4 * 2
ed = beat_resol * 4 * 8

# export
mido_obj.dump('seg.midi', segment=(st, ed))<jupyter_output><empty_output><jupyter_text>C. Ceate an empty MIDI file<jupyter_code># create an empty file
mido_obj = mid_parser.MidiFile()
beat_resol = mido_obj.ticks_per_beat

# create an  instrument
track = ct.Instrument(program=0, is_drum=False, name='example track')
mido_obj.instruments = [track]

# create eighth notes
duration = int(beat_resol * 0.5)
prev_end = 0
pitch = 60
for i in range(10):
    # create one note
    start = prev_end
    end = prev_end + duration
    pitch = pitch
    velocity = np.random.randint(1, 127)
    note = ct.Note(start=start, end=end, pitch=pitch, velocity=velocity)
    mido_obj.instruments[0].notes.append(note)
    
    # prepare next
    prev_end = end
    pitch += 1

# create makers
marker_hi = ct.Marker(time=0, text='HI')
mido_obj.markers.append(marker_hi)

# write to file
mido_obj.dump('result.midi')

# reload for check
mido_obj_re = mid_parser.MidiFile('result.midi')
for note in mido_obj_re.instruments[0].notes:
    print(note)

print('\nmarker:', mido_obj_re.markers)<jupyter_output>Note(start=0, end=240, pitch=60, velocity=118)
Note(start=240, end=480, pitch=61, velocity=24)
Note(start=480, end=720, pitch=62, velocity=26)
Note(start=720, end=960, pitch=63, velocity=123)
Note(start=960, end=1200, pitch=64, velocity=58)
Note(start=1200, end=1440, pitch=65, velocity=47)
Note(start=1440, end=1680, pitch=66, velocity=67)
Note(start=1680, end=1920, pitch=67, velocity=83)
Note(start=1920, end=2160, pitch=68, velocity=49)
Note(start=2160, end=2400, pitch=69, velocity=37)

marker: [Marker(text="HI", time=0)]
{'fraction_non_alphanumeric': 0.1434462444771723, 'fraction_numerical': 0.058321060382916054, 'mean_word_length': 4.97887323943662, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '335525', 'license': 'MIT', 'n_tokens_mistral': 1416, 'n_tokens_neox': 1265, 'n_words': 299, 'path': 'examples/parse_and_create_MIDI_files.ipynb', 'repo_name': 'yilin-zhang/miditoolkit', 'size': '3395'}