本文整理匯總了Python中midi.TimeSignatureEvent方法的典型用法代碼示例。如果您正苦於以下問題:Python midi.TimeSignatureEvent方法的具體用法?Python midi.TimeSignatureEvent怎麽用?Python midi.TimeSignatureEvent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類midi
的用法示例。
在下文中一共展示了midi.TimeSignatureEvent方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: midiToNoteStateMatrix
# 需要導入模塊: import midi [as 別名]
# 或者: from midi import TimeSignatureEvent [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 TimeSignatureEvent [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: dump_sequence_to_midi
# 需要導入模塊: import midi [as 別名]
# 或者: from midi import TimeSignatureEvent [as 別名]
def dump_sequence_to_midi(self, sequence, output_filename, time_step,
resolution, metronome=24):
if self.verbose:
print "Dumping sequence to MIDI file: {}".format(output_filename)
print "Resolution: {}".format(resolution)
print "Time Step: {}".format(time_step)
pattern = midi.Pattern(resolution=resolution)
self.track = midi.Track()
# metadata track
meta_track = midi.Track()
time_sig = midi.TimeSignatureEvent()
time_sig.set_numerator(4)
time_sig.set_denominator(4)
time_sig.set_metronome(metronome)
time_sig.set_thirtyseconds(8)
meta_track.append(time_sig)
pattern.append(meta_track)
# reshape to (SEQ_LENGTH X NUM_DIMS)
sequence = np.reshape(sequence, [-1, self.note_range])
time_steps = sequence.shape[0]
if self.verbose:
print "Total number of time steps: {}".format(time_steps)
tick = time_step
self.notes_on = { n: False for n in range(self.note_range) }
# for seq_idx in range(188, 220):
for seq_idx in range(time_steps):
notes = np.nonzero(sequence[seq_idx, :])[0].tolist()
# this tick will only be assigned to first NoteOn/NoteOff in
# this time_step
# NoteOffEvents come first so they'll have the tick value
# go through all notes that are currently on and see if any
# turned off
for n in self.notes_on:
if self.notes_on[n] and n not in notes:
tick = self.note_off(n, tick)
self.notes_on[n] = False
# Turn on any notes that weren't previously on
for note in notes:
if not self.notes_on[note]:
tick = self.note_on(note, tick)
self.notes_on[note] = True
tick += time_step
# flush out notes
for n in self.notes_on:
if self.notes_on[n]:
self.note_off(n, tick)
tick = 0
self.notes_on[n] = False
pattern.append(self.track)
midi.write_midifile(output_filename, pattern)