Beispiel #1
0
def noteStateMatrixToMidi(statematrix, name="example"):
    print("Length received statematrix: {}".format(len(statematrix)))
    statematrix = numpy.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)]
    loopcount = 0
    for time, state in enumerate(statematrix + [prevstate[:]]):
        loopcount += 1
        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


#        sys.stdout.flush()

    eot = midi.EndOfTrackEvent(tick=1)
    track.append(eot)

    print(repr(pattern))

    midi.write_midifile("{}".format(name), pattern)
def note_list_net_generate(net, length, path, start_note=60, absolute=False):
    """
    Generate a snippet of music with custom length using a note_list net.
    The net must be seeded with an initial note (which will not be played),
    the default for which is middle C.
    """
    net.reset()
    last = [RelativeNote().get_binary()]
    notes = []
    for i in range(length):
        note = net.run(last)
        if absolute:
            notes.append(AbsoluteNote(map(lambda x: int(round(x)), note[0])))
        else:
            notes.append(RelativeNote(map(lambda x: int(round(x)), note[0])))
        last = note
    midi.write_midifile(
        path, midi.Pattern([note_list_to_midi_XML(notes, start_note)]))
Beispiel #3
0
def noteStateMatrixToMidi(statematrix, name="example"):
    statematrix = numpy.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)
    sys.stdout.flush()
    sleep(1)
    print("Still generating music... (3)")
    print("PERCENT: 60")
    sys.stdout.flush()
    sleep(1)
    print("Still generating music... (4)")
    print("PERCENT: 85")
    sys.stdout.flush()
    sleep(1)
    print("Done generating music, writing out...")
    print("PERCENT: 100")
    sys.stdout.flush()

    # Instantiate a MIDI Pattern (contains a list of tracks)
    pattern = midi.Pattern()
    # Instantiate a MIDI Track (contains a list of MIDI events)
    track = midi.Track()
    # Append the track to the pattern
    pattern.append(track)

    # Instantiate a MIDI note on event, append it to the track
    on = midi.NoteOnEvent(tick=0, velocity=50, pitch=midi.G_3)
    track.append(on)
    on = midi.NoteOnEvent(tick=0, velocity=50, pitch=midi.G_2)
    track.append(on)
    # Instantiate a MIDI note off event, append it to the track
    off = midi.NoteOffEvent(tick=100, pitch=midi.G_3)
    track.append(off)

    # Instantiate a MIDI note on event, append it to the track
import sys
import relative_note_net
import python3_midi as midi

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print('Requires midi files as args, src then dest')
        exit(1)

    filename = sys.argv[1]
    outfile = sys.argv[2]

    notelist = relative_note_net.get_notelist_for_xml(filename)
    tracklist = []
    #  for notelist in notelists:
    #    tracklist.append(relative_note_net.note_list_to_midi(notelist))
    #55 ticks per slice
    tracklist.append(relative_note_net.note_list_to_midi_XML(notelist))

    midi.write_midifile(outfile, midi.Pattern(tracklist))
Beispiel #6
0

def pattern_save(src, path):
    patlib.write_midifile(path, src)
    return path


def pattern_load(path, res):
    midi_data = patlib.read_midifile(path)
    change_res_pat(midi_data, res)
    return midi_data


pattern_repr = {"load": pattern_load,
                "save": pattern_save,
                "instance": patlib.Pattern(),
                "lib": patlib}


# _____________Pianoroll_Multitrack____________
def multitrack_save(src, path):
    src.write(path)
    return path


def multitrack_load(path, res):
    pretty = prelib.PrettyMIDI(path, resolution=res)
    mul = mullib.from_pretty_midi(pretty, resolution=res)

    for t in mul.tracks:
        if t.name == "":