本文整理汇总了Python中midi.NoteOnEvent方法的典型用法代码示例。如果您正苦于以下问题:Python midi.NoteOnEvent方法的具体用法?Python midi.NoteOnEvent怎么用?Python midi.NoteOnEvent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类midi
的用法示例。
在下文中一共展示了midi.NoteOnEvent方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: note_on
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOnEvent [as 别名]
def note_on(self, val, tick):
if val >= NOTTINGHAM_MELODY_RANGE:
notes = self.dereference_chord(val - NOTTINGHAM_MELODY_RANGE)
else:
# if note is the top of the range, then it stands for gap in melody
if val == NOTTINGHAM_MELODY_RANGE - 1:
notes = []
else:
notes = [NOTTINGHAM_MELODY_MIN + val]
# print 'turning on {}'.format(notes)
for note in notes:
self.track.append(midi.NoteOnEvent(tick=tick, pitch=note, velocity=70))
tick = 0 # notes that come right after each other should have zero tick
return tick
示例2: midi_to_composition
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOnEvent [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)
示例3: noteStateMatrixToMidi
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOnEvent [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 NoteOnEvent [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: writeChord
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOnEvent [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
示例6: writeNote
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOnEvent [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
示例7: note_state_matrix_to_midi
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOnEvent [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)
示例8: ingest_notes
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOnEvent [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
示例9: note_on
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOnEvent [as 别名]
def note_on(self, val, tick):
self.track.append(midi.NoteOnEvent(tick=tick, pitch=val, velocity=70))
return 0
示例10: nsmatrix2midi
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOnEvent [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)
示例11: parse_midi_to_sequence
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOnEvent [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
示例12: sequence_to_midi
# 需要导入模块: import midi [as 别名]
# 或者: from midi import NoteOnEvent [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