Exemple #1
0
def process_parse_txt(p):
	kb = [-1] * 88
	sheet = list(map(lambda _: [], range(88)))
	sheet2 = IntervalTree()
	t = 0
	last_t = 0
	l = []
	i = 0
	print('Opening {}...'.format(p))
	t0 = time.time()
	t1 = t0
	with open(p) as noteFile:
		next(noteFile) #skip header line
		for line in noteFile:
			action = line.split()
			assert(len(action)==3)
			begin = float(action[0])
			end = float(action[1])
			n = normalize(int(action[2]))
			sheet[n].append((begin, end))
			sheet2.append(Interval(begin, end, n))
	tn = time.time()
	print('Parsed txt file ({:.3f}).'.format(tn - t0))
	print()
	return (sheet, sheet2)
Exemple #2
0
def process_parse_midi(p):
	kb = [-1] * 88
	sheet = list(map(lambda _: [], range(88)))
	sheet2 = IntervalTree()
	mid = mido.MidiFile(p)
	t = 0
	last_t = 0
	l = []
	i = 0
	print('Opening {}...'.format(p))
	t0 = time.time()
	t1 = t0
	for msg in mid:
		'''
		if msg.type == 'note_on' or msg.type == 'note_off':
			t += msg.time
			if msg.type == 'note_on':
				l.append(note_name(normalize(msg.note)))
			elif msg.type == 'note_off':
				pass
			if msg.time != 0:
				print('{}: {}'.format(t, l))
				l = []
		'''
		t += msg.time
		if msg.type == 'note_on' and msg.velocity != 0:
			n = normalize(msg.note)
			if n is None:
				continue
			#assert(kb[n] == -1)
			if kb[n] == -1:
				kb[n] = t
			else:
				# TODO hmmm
				sheet[n].append((kb[n], t))
				kb[n] = t
		#note_ons with zero velocity also count as valid note_offs
		elif msg.type == 'note_off' or (msg.type == 'note_on' and msg.velocity == 0):
			n = normalize(msg.note)
			if n is None:
				continue
			if kb[n] < 0:
				# TODO log
				pass
			else:
				if kb[n] == t:
					# TODO log
					print('- note {} had empty interval, skipping'.format(n))
				else:
					sheet[n].append((kb[n], t))
					sheet2.append(Interval(kb[n], t, n))
				kb[n] = -1

		i += 1
		t2 = time.time()
		if i % 50 == 0:
			print('- {} records at {}:{:04.1f} ({:.3f}, {:.3f})...'.format(i, math.floor(t / 60), t % 60, t2 - t1, t2 - t0))
			t1 = t2
		if i == 200:
			#break
			pass
	tn = time.time()
	print('Parsed midi file ({:.3f}).'.format(tn - t0))
	print()
	return (sheet, sheet2)