本文整理汇总了Python中midi.NoteEvent方法的典型用法代码示例。如果您正苦于以下问题:Python midi.NoteEvent方法的具体用法?Python midi.NoteEvent怎么用?Python midi.NoteEvent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类midi
的用法示例。
在下文中一共展示了midi.NoteEvent方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: midiToNoteStateMatrix
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteEvent [as 别名]
def midiToNoteStateMatrix(midifile, squash=True, span=span):
pattern = midi.read_midifile(midifile)
timeleft = [track[0].tick for track in pattern]
posns = [0 for track in pattern]
statematrix = []
time = 0
state = [[0,0] for x in range(span)]
statematrix.append(state)
condition = True
while condition:
if time % (pattern.resolution / 4) == (pattern.resolution / 8):
# Crossed a note boundary. Create a new state, defaulting to holding notes
oldstate = state
state = [[oldstate[x][0],0] for x in range(span)]
statematrix.append(state)
for i in range(len(timeleft)): #For each track
if not condition:
break
while timeleft[i] == 0:
track = pattern[i]
pos = posns[i]
evt = track[pos]
if isinstance(evt, midi.NoteEvent):
if (evt.pitch < lowerBound) or (evt.pitch >= upperBound):
pass
# print "Note {} at time {} out of bounds (ignoring)".format(evt.pitch, time)
else:
if isinstance(evt, midi.NoteOffEvent) or evt.velocity == 0:
state[evt.pitch-lowerBound] = [0, 0]
else:
state[evt.pitch-lowerBound] = [1, 1]
elif isinstance(evt, midi.TimeSignatureEvent):
if evt.numerator not in (2, 4):
# We don't want to worry about non-4 time signatures. Bail early!
# print "Found time signature event {}. Bailing!".format(evt)
out = statematrix
condition = False
break
try:
timeleft[i] = track[pos + 1].tick
posns[i] += 1
except IndexError:
timeleft[i] = None
if timeleft[i] is not None:
timeleft[i] -= 1
if all(t is None for t in timeleft):
break
time += 1
S = np.array(statematrix)
statematrix = np.hstack((S[:, :, 0], S[:, :, 1]))
statematrix = np.asarray(statematrix).tolist()
return statematrix
示例2: midi_to_note_state_matrix
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteEvent [as 别名]
def midi_to_note_state_matrix(midifile, squash=True, note_range=note_range):
pattern = midi.read_midifile(midifile)
timeleft = [track[0].tick for track in pattern]
posns = [0 for track in pattern]
statematrix = []
time = 0
state = [[0,0] for x in range(note_range)]
statematrix.append(state)
condition = True
while condition:
if time % (pattern.resolution / 4) == (pattern.resolution / 8):
# Crossed a note boundary. Create a new state, defaulting to holding notes
oldstate = state
state = [[oldstate[x][0],0] for x in range(note_range)]
statematrix.append(state)
for i in range(len(timeleft)): #For each track
if not condition:
break
while timeleft[i] == 0:
track = pattern[i]
pos = posns[i]
evt = track[pos]
if isinstance(evt, midi.NoteEvent):
if (evt.pitch < lowest_note) or (evt.pitch >= highest_note):
pass
# print "Note {} at time {} out of bounds (ignoring)".format(evt.pitch, time)
else:
if isinstance(evt, midi.NoteOffEvent) or evt.velocity == 0:
state[evt.pitch-lowest_note] = [0, 0]
else:
state[evt.pitch-lowest_note] = [1, 1]
elif isinstance(evt, midi.TimeSignatureEvent):
if evt.numerator not in (2, 4):
# We don't want to worry about non-4 time signatures. Bail early!
# print "Found time signature event {}. Bailing!".format(evt)
out = statematrix
condition = False
break
try:
timeleft[i] = track[pos + 1].tick
posns[i] += 1
except IndexError:
timeleft[i] = None
if timeleft[i] is not None:
timeleft[i] -= 1
if all(t is None for t in timeleft):
break
time += 1
S = np.array(statematrix)
statematrix = np.hstack((S[:, :, 0], S[:, :, 1]))
statematrix = np.asarray(statematrix).tolist()
return statematrix
示例3: midi_to_sequence
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteEvent [as 别名]
def midi_to_sequence(filepath):
"""
Loads a midi file and outputs the corresponding 'state_matrix'.
state_matrix[tick][pitch] = volume
Each row corresponds to the state of notes in a tick.
:param filepath: The path of the midi file.
:type filepath: str
:returns: The state-matrix and some meta-info (resolution, tempo_event)
:return_type: (2-D list, (int, SetTempoEvent or None))
"""
state_matrix = []
pattern = midi.read_midifile(filepath)
#pprint(pattern, open('pattern_correct', 'w'))
tempo_event = None
for track in pattern:
# Null state
state = [0] * 128
for event in track:
if isinstance(event, midi.EndOfTrackEvent):
# Append the final state 1 time as EndOfTrackEvent has tick = 1
state_matrix += [copy(state)]
break
elif isinstance(event, midi.NoteEvent):
if event.tick > 0:
# A change in state has happened.
# Append the current state to the state_matrix.
state_matrix += event.tick * [copy(state)]
if isinstance(event, midi.NoteOffEvent):
# Make the volume of the pitch to be 0
state[event.pitch] = 0
else:
state[event.pitch] = event.data[1]
# Find the tempo-event in the track.
# This is not required for RNN training.
# But is required to generate coherent music.
for i in xrange(min(10, len(track))):
if isinstance(track[i], midi.SetTempoEvent):
tempo_event = track[i]
break
return state_matrix, (pattern.resolution, tempo_event)