import time
def read_vlq(f):
result = ''
buffer = unpack('B', f.read(1))[0]
length = 1
while buffer > 127:
print(buffer)
result += '{0:{fill}{n}b}'.format(buffer-128, fill='0', n=7) buffer = unpack('B', f.read(1))[0]
length += 1
result += '{0:{fill}{n}b}'.format(buffer, fill='0', n=7)
return int(result, 2), length
def parse_event(evt, param):
if 128 <= evt <= 143:
print('Note Off event.')
elif 144 <= evt <= 159:
print('Note On event.', unpack('>BB', param))
elif 176 <= evt <= 191:
print('Control Change.')
elif 192 <= evt <= 207:
print('Program Change.')
with open('bwv806a.mid', 'rb') as f:
ad(200))
# HEADER
ad(4) != b'MThd':
raise Exception('not a midi file!')
ad(4))
header_info = f.read(6)
print(unpack('>hhh', header_info))
''' ================================== '''
while True:
track_head = f.read(4)
if track_head != b'MTrk':
if track_head != b'':
ad(20))
raise Exception('not a midi file!')
else:
break
# length of track
len_of_track = unpack('>L', f.read(4))[0]
# print('len_of_track ', len_of_track)
counter = 0
t = 0
last_event = None
while True:
delta_t, len_ = read_vlq(f)
counter += len_
t += delta_t
python怎么读取文件中的数据# print('T ', t, end='')
event_code = f.read(1)
event_type = unpack('>B', event_code)[0]
counter += 1
# print(' event_type ', event_type, end='')
if event_type == 255:
meta_type = f.read(1)
counter += 1
# print(' - meta_type ', meta_type, end='')
data_len, len_= read_vlq(f)
counter += len_
data = f.read(data_len)
counter += data_len
# print(' - ', data)
elif event_type <= 127:
parse_event(last_event, event_ad(1)) counter += 1
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论