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


Python midi.write_midifile方法代码示例

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


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

示例1: tChordsToMidi

# 需要导入模块: import midi [as 别名]
# 或者: from midi import write_midifile [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) 
开发者ID:donya,项目名称:PythonKulitta,代码行数:18,代码来源:MidiFuns.py

示例2: voiceToMidi

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

示例3: noteStateMatrixToMidi

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

示例4: noteStateMatrixToMidi

# 需要导入模块: import midi [as 别名]
# 或者: from midi import write_midifile [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) 
开发者ID:llSourcell,项目名称:Music_Generator_Demo,代码行数:43,代码来源:midi_manipulation.py

示例5: note_state_matrix_to_midi

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

示例6: nsmatrix2midi

# 需要导入模块: import midi [as 别名]
# 或者: from midi import write_midifile [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) 
开发者ID:mbelousov,项目名称:schumann,代码行数:48,代码来源:convert.py

示例7: dump_sequence_to_midi

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

示例8: sequence_to_midi

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


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