本文整理汇总了Python中midi.NoteOffEvent方法的典型用法代码示例。如果您正苦于以下问题:Python midi.NoteOffEvent方法的具体用法?Python midi.NoteOffEvent怎么用?Python midi.NoteOffEvent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类midi
的用法示例。
在下文中一共展示了midi.NoteOffEvent方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: midi_to_composition
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOffEvent [as 别名]
def midi_to_composition(pattern):
"""
Converts a MIDI pattern to a composition array.
"""
# Extract first pattern
# pattern.make_ticks_abs()
step = pattern.resolution // TICKS_PER_BEAT
track = pattern[1]
# A list of notes currently on
notes = []
composition = []
for event in track[1:-1]:
# TODO: We only care about one note for now
if isinstance(event, midi.NoteOnEvent) and len(notes) == 0:
pitch, velocity = event.data
notes.append(pitch)
if isinstance(event, midi.NoteOffEvent):
pitch, velocity = event.data
if pitch in notes:
notes.remove(pitch)
# Write to composition
composition.append(pitch)# - MIN_NOTE)
composition += [NO_EVENT] * (event.tick // step)
print(event)
print(composition)
示例2: noteStateMatrixToMidi
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOffEvent [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)
示例3: noteStateMatrixToMidi
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOffEvent [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)
示例4: writeChord
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOffEvent [as 别名]
def writeChord(tChord, track):
if len(tChord.absChord) == 0:
return
absChd = tChord.absChord
dur = tChord.dur
tickDur = toMidiTick(dur)
for a in absChd:
track.append(midi.NoteOnEvent(tick=0, velocity=tChord.vol, pitch=toMidiPitch(a)))
track.append(midi.NoteOffEvent(tick = tickDur, pitch = toMidiPitch(absChd[0])))
for a in absChd[1:]:
track.append(midi.NoteOffEvent(tick=0, pitch=toMidiPitch(a))) # Bug fix 26-July-2016
示例5: writeNote
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOffEvent [as 别名]
def writeNote(tNote, track):
track.append(midi.NoteOnEvent(tick=0, velocity=tNote.vol, pitch=toMidiPitch(tNote.absPitch)))
track.append(midi.NoteOffEvent(tick=toMidiTick(tNote.dur), pitch=toMidiPitch(tNote.absPitch)))
# Conversion from Kulitta's durations to MIDI ticks
示例6: note_state_matrix_to_midi
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOffEvent [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)
示例7: ingest_notes
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOffEvent [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
示例8: note_off
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOffEvent [as 别名]
def note_off(self, val, tick):
self.track.append(midi.NoteOffEvent(tick=tick, pitch=val))
return 0
示例9: note_off
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOffEvent [as 别名]
def note_off(self, val, tick):
if val >= NOTTINGHAM_MELODY_RANGE:
notes = self.dereference_chord(val - NOTTINGHAM_MELODY_RANGE)
else:
notes = [NOTTINGHAM_MELODY_MIN + val]
# print 'turning off {}'.format(notes)
for note in notes:
self.track.append(midi.NoteOffEvent(tick=tick, pitch=note))
tick = 0
return tick
示例10: midiToNoteStateMatrix
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOffEvent [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
示例11: nsmatrix2midi
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOffEvent [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)
示例12: midi_to_note_state_matrix
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOffEvent [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
示例13: parse_midi_to_sequence
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOffEvent [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
示例14: midi_to_sequence
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOffEvent [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)
示例15: sequence_to_midi
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOffEvent [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