本文整理汇总了Python中midi.EndOfTrackEvent方法的典型用法代码示例。如果您正苦于以下问题:Python midi.EndOfTrackEvent方法的具体用法?Python midi.EndOfTrackEvent怎么用?Python midi.EndOfTrackEvent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类midi
的用法示例。
在下文中一共展示了midi.EndOfTrackEvent方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: tChordsToMidi
# 需要导入模块: import midi [as 别名]
# 或者: from midi import EndOfTrackEvent [as 别名]
def tChordsToMidi(tChords, filename):
# Instantiate a MIDI Pattern (contains a list of tracks)
pattern = midi.Pattern()
# Set the tick per beat resolution
pattern.resolution = RESOLUTION
# Instantiate a MIDI Track (contains a list of MIDI events)
track = midi.Track()
# Append the track to the pattern
pattern.append(track)
# Iterate through each chord and write it to a track
for tChord in tChords:
writeChord(tChord, track)
eot = midi.EndOfTrackEvent(tick=1)
track.append(eot)
# Save the pattern to disk
midi.write_midifile(filename + '.mid', pattern)
示例2: voiceToMidi
# 需要导入模块: import midi [as 别名]
# 或者: from midi import EndOfTrackEvent [as 别名]
def voiceToMidi(voice, filename):
# Instantiate a MIDI Pattern (contains a list of tracks)
pattern = midi.Pattern()
# Set the tick per beat resolution
pattern.resolution = RESOLUTION
# Instantiate a MIDI Track (contains a list of MIDI events)
track = midi.Track()
#track.append(midi.SetTempoEvent())
# Append the track to the pattern
pattern.append(track)
# Iterate through each chord and write it to a track
for tnote in voice:
writeNote(tnote, track)
eot = midi.EndOfTrackEvent(tick=1)
track.append(eot)
# Save the pattern to disk
midi.write_midifile(filename + '.mid', pattern)
# toVoices converts chords to voices. Mathematically, this is essentially
# just a matrix transposition of the pitches. Durations for each chord are
# copied into each of their resulting TNotes. The resulting music will
# sound exactly the same.
示例3: noteStateMatrixToMidi
# 需要导入模块: import midi [as 别名]
# 或者: from midi import EndOfTrackEvent [as 别名]
def noteStateMatrixToMidi(statematrix, name="example", span=span):
statematrix = np.array(statematrix)
if not len(statematrix.shape) == 3:
statematrix = np.dstack((statematrix[:, :span], statematrix[:, span:]))
statematrix = np.asarray(statematrix)
pattern = midi.Pattern()
track = midi.Track()
pattern.append(track)
span = upperBound-lowerBound
tickscale = 55
lastcmdtime = 0
prevstate = [[0,0] for x in range(span)]
for time, state in enumerate(statematrix + [prevstate[:]]):
offNotes = []
onNotes = []
for i in range(span):
n = state[i]
p = prevstate[i]
if p[0] == 1:
if n[0] == 0:
offNotes.append(i)
elif n[1] == 1:
offNotes.append(i)
onNotes.append(i)
elif n[0] == 1:
onNotes.append(i)
for note in offNotes:
track.append(midi.NoteOffEvent(tick=(time-lastcmdtime)*tickscale, pitch=note+lowerBound))
lastcmdtime = time
for note in onNotes:
track.append(midi.NoteOnEvent(tick=(time-lastcmdtime)*tickscale, velocity=40, pitch=note+lowerBound))
lastcmdtime = time
prevstate = state
eot = midi.EndOfTrackEvent(tick=1)
track.append(eot)
midi.write_midifile("{}.midi".format(name), pattern)
示例4: noteStateMatrixToMidi
# 需要导入模块: import midi [as 别名]
# 或者: from midi import EndOfTrackEvent [as 别名]
def noteStateMatrixToMidi(statematrix, name="example", span=span):
statematrix = np.array(statematrix)
if not len(statematrix.shape) == 3:
statematrix = np.dstack((statematrix[:, :span], statematrix[:, span:]))
statematrix = np.asarray(statematrix)
pattern = midi.Pattern()
track = midi.Track()
pattern.append(track)
span = upperBound-lowerBound
tickscale = 55
lastcmdtime = 0
prevstate = [[0,0] for x in range(span)]
for time, state in enumerate(statematrix + [prevstate[:]]):
offNotes = []
onNotes = []
for i in range(span):
n = state[i]
p = prevstate[i]
if p[0] == 1:
if n[0] == 0:
offNotes.append(i)
elif n[1] == 1:
offNotes.append(i)
onNotes.append(i)
elif n[0] == 1:
onNotes.append(i)
for note in offNotes:
track.append(midi.NoteOffEvent(tick=(time-lastcmdtime)*tickscale, pitch=note+lowerBound))
lastcmdtime = time
for note in onNotes:
track.append(midi.NoteOnEvent(tick=(time-lastcmdtime)*tickscale, velocity=40, pitch=note+lowerBound))
lastcmdtime = time
prevstate = state
eot = midi.EndOfTrackEvent(tick=1)
track.append(eot)
midi.write_midifile("{}.mid".format(name), pattern)
示例5: note_state_matrix_to_midi
# 需要导入模块: import midi [as 别名]
# 或者: from midi import EndOfTrackEvent [as 别名]
def note_state_matrix_to_midi(statematrix, name="example", note_range=note_range):
statematrix = np.array(statematrix)
if not len(statematrix.shape) == 3:
statematrix = np.dstack((statematrix[:, :note_range], statematrix[:, note_range:]))
statematrix = np.asarray(statematrix)
pattern = midi.Pattern()
track = midi.Track()
pattern.append(track)
note_range = highest_note-lowest_note
tickscale = 55
lastcmdtime = 0
prevstate = [[0,0] for x in range(note_range)]
for time, state in enumerate(statematrix + [prevstate[:]]):
offNotes = []
onNotes = []
for i in range(note_range):
n = state[i]
p = prevstate[i]
if p[0] == 1:
if n[0] == 0:
offNotes.append(i)
elif n[1] == 1:
offNotes.append(i)
onNotes.append(i)
elif n[0] == 1:
onNotes.append(i)
for note in offNotes:
track.append(midi.NoteOffEvent(tick=(time-lastcmdtime)*tickscale, pitch=note+lowest_note))
lastcmdtime = time
for note in onNotes:
track.append(midi.NoteOnEvent(tick=(time-lastcmdtime)*tickscale, velocity=40, pitch=note+lowest_note))
lastcmdtime = time
prevstate = state
eot = midi.EndOfTrackEvent(tick=1)
track.append(eot)
midi.write_midifile("{}.mid".format(name), pattern)
示例6: ingest_notes
# 需要导入模块: import midi [as 别名]
# 或者: from midi import EndOfTrackEvent [as 别名]
def ingest_notes(track, verbose=False):
notes = { n: [] for n in range(RANGE) }
current_tick = 0
for msg in track:
# ignore all end of track events
if isinstance(msg, midi.EndOfTrackEvent):
continue
if msg.tick > 0:
current_tick += msg.tick
# velocity of 0 is equivalent to note off, so treat as such
if isinstance(msg, midi.NoteOnEvent) and msg.get_velocity() != 0:
if len(notes[msg.get_pitch()]) > 0 and \
len(notes[msg.get_pitch()][-1]) != 2:
if verbose:
print "Warning: double NoteOn encountered, deleting the first"
print msg
else:
notes[msg.get_pitch()] += [[current_tick]]
elif isinstance(msg, midi.NoteOffEvent) or \
(isinstance(msg, midi.NoteOnEvent) and msg.get_velocity() == 0):
# sanity check: no notes end without being started
if len(notes[msg.get_pitch()][-1]) != 1:
if verbose:
print "Warning: skipping NoteOff Event with no corresponding NoteOn"
print msg
else:
notes[msg.get_pitch()][-1] += [current_tick]
return notes, current_tick
示例7: nsmatrix2midi
# 需要导入模块: import midi [as 别名]
# 或者: from midi import EndOfTrackEvent [as 别名]
def nsmatrix2midi(self, statematrix, output_file, tickscale=20,
velocity=85):
"""
Converts state-matrix to MIDI File and saves it to the disk
"""
statematrix = np.asarray(statematrix)
pattern = midi.Pattern()
track = midi.Track()
pattern.append(track)
span = self.upper_bound - self.lower_bound + 1
lastcmdtime = 0
prevstate = [[0, 0] for x in range(span)]
for time, state in enumerate(statematrix + [prevstate[:]]):
offNotes = []
onNotes = []
for i in range(span):
n = state[i]
p = prevstate[i]
if p[0] == 1:
if n[0] == 0:
offNotes.append(i)
elif n[1] == 1:
offNotes.append(i)
onNotes.append(i)
elif n[0] == 1:
onNotes.append(i)
for note in offNotes:
track.append(
midi.NoteOffEvent(tick=(time - lastcmdtime) * tickscale,
pitch=note + self.lower_bound))
lastcmdtime = time
for note in onNotes:
track.append(
midi.NoteOnEvent(tick=(time - lastcmdtime) * tickscale,
velocity=velocity,
pitch=note + self.lower_bound))
lastcmdtime = time
prevstate = state
eot = midi.EndOfTrackEvent(tick=1)
track.append(eot)
midi.write_midifile(output_file, pattern)
示例8: parse_midi_to_sequence
# 需要导入模块: import midi [as 别名]
# 或者: from midi import EndOfTrackEvent [as 别名]
def parse_midi_to_sequence(input_filename, time_step, verbose=False):
sequence = []
pattern = midi.read_midifile(input_filename)
if len(pattern) < 1:
raise Exception("No pattern found in midi file")
if verbose:
print "Track resolution: {}".format(pattern.resolution)
print "Number of tracks: {}".format(len(pattern))
print "Time step: {}".format(time_step)
# Track ingestion stage
notes = { n: [] for n in range(RANGE) }
track_ticks = 0
for track in pattern:
current_tick = 0
for msg in track:
# ignore all end of track events
if isinstance(msg, midi.EndOfTrackEvent):
continue
if msg.tick > 0:
current_tick += msg.tick
# velocity of 0 is equivalent to note off, so treat as such
if isinstance(msg, midi.NoteOnEvent) and msg.get_velocity() != 0:
if len(notes[msg.get_pitch()]) > 0 and \
len(notes[msg.get_pitch()][-1]) != 2:
if verbose:
print "Warning: double NoteOn encountered, deleting the first"
print msg
else:
notes[msg.get_pitch()] += [[current_tick]]
elif isinstance(msg, midi.NoteOffEvent) or \
(isinstance(msg, midi.NoteOnEvent) and msg.get_velocity() == 0):
# sanity check: no notes end without being started
if len(notes[msg.get_pitch()][-1]) != 1:
if verbose:
print "Warning: skipping NoteOff Event with no corresponding NoteOn"
print msg
else:
notes[msg.get_pitch()][-1] += [current_tick]
track_ticks = max(current_tick, track_ticks)
track_ticks = round_tick(track_ticks, time_step)
if verbose:
print "Track ticks (rounded): {} ({} time steps)".format(track_ticks, track_ticks/time_step)
sequence = round_notes(notes, track_ticks, time_step)
return sequence
示例9: midi_to_sequence
# 需要导入模块: import midi [as 别名]
# 或者: from midi import EndOfTrackEvent [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)
示例10: sequence_to_midi
# 需要导入模块: import midi [as 别名]
# 或者: from midi import EndOfTrackEvent [as 别名]
def sequence_to_midi(state_matrix, filepath, meta_info=None):
"""
Converts a state_matrix to the corresponding 'pattern'
and writes the pattern as a midi file.
:param state_matrix: The state matrix.
:type state_matrix: 2-D list
:param filepath: The path of the output midi file.
:type filepath: str
:param meta_info: Resolution and tempo-event of the pattern.
:type meta_info: (int, SetTempoEvent or None) or None
:returns: The pattern sequence corresponding to the state matrix.
:return_type: list
"""
resolution, tempo_event = meta_info if meta_info else None
pattern = midi.Pattern(resolution=resolution)
track = midi.Track()
pattern.append(track)
if tempo_event:
track.append(tempo_event)
# Append the very first tick (which will only have NoteOn events)
notes_on, _ = state_diff([0] * 128, state_matrix[0])
for note in notes_on:
track.append(midi.NoteOnEvent(tick=0, channel=0, data=note))
# Append the rest of the ticks
current_state_index = 0
while current_state_index < len(state_matrix):
next_state_index = get_next_different_state(
state_matrix, current_state_index)
ticks_elapsed = next_state_index - current_state_index
current_state = state_matrix[current_state_index]
next_state = state_matrix[next_state_index] if next_state_index < len(
state_matrix) else [0] * 128
notes_on, notes_off = state_diff(current_state, next_state)
for note in notes_on:
track.append(midi.NoteOnEvent(
tick=ticks_elapsed, channel=0, data=note))
# The rest of the events are happening simultaneously,
# so set time_elapsed (tick) = 0 for them
ticks_elapsed = 0
for note in notes_off:
track.append(midi.NoteOffEvent(
tick=ticks_elapsed, channel=0, data=note))
ticks_elapsed = 0
current_state_index = next_state_index
track.append(midi.EndOfTrackEvent(tick=1))
midi.write_midifile(filepath, pattern)
return pattern