当前位置: 首页>>代码示例>>Python>>正文


Python midi.read_midifile方法代码示例

本文整理汇总了Python中midi.read_midifile方法的典型用法代码示例。如果您正苦于以下问题:Python midi.read_midifile方法的具体用法?Python midi.read_midifile怎么用?Python midi.read_midifile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在midi的用法示例。


在下文中一共展示了midi.read_midifile方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: midiToNoteStateMatrix

# 需要导入模块: import midi [as 别名]
# 或者: from midi import read_midifile [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 
开发者ID:burliEnterprises,项目名称:tensorflow-music-generator,代码行数:63,代码来源:midi_manipulation.py

示例2: midi_to_note_state_matrix

# 需要导入模块: import midi [as 别名]
# 或者: from midi import read_midifile [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 
开发者ID:notAFK,项目名称:oxfordhack-2016,代码行数:63,代码来源:midi_helpers.py

示例3: parse_midi_to_sequence

# 需要导入模块: import midi [as 别名]
# 或者: from midi import read_midifile [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 
开发者ID:wzds2015,项目名称:music_composition,代码行数:55,代码来源:midi_util.py

示例4: main

# 需要导入模块: import midi [as 别名]
# 或者: from midi import read_midifile [as 别名]
def main():
    file_path = 'out.mid'
    pattern = midi.read_midifile(file_path)
    print pattern 
开发者ID:Anmol-Singh-Jaggi,项目名称:Jukebot,代码行数:6,代码来源:midi_debug.py

示例5: midi_to_sequence

# 需要导入模块: import midi [as 别名]
# 或者: from midi import read_midifile [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) 
开发者ID:Anmol-Singh-Jaggi,项目名称:Jukebot,代码行数:47,代码来源:midi_sequence.py


注:本文中的midi.read_midifile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。