Example #1
0
    def __init__(self, filename='', tempo=120):
        """
        If filename is empty, set the midi file to the __main__file_name.mid
        """
        if filename == '':
            self.filename = 'python-music-gen.mid'
        else:
            self.filename = filename

        self.midi = MidiFileGenerator()
        self.tempo = tempo
Example #2
0
    def __init__(self, filename='', tempo=120):
        """
        If filename is empty, set the midi file to the __main__file_name.mid
        """
        if filename == '':
            self.filename = os.path.basename(
                path.abspath(
                    sys.modules['__main__'].__file__)).split('.')[0] + '.mid'
        else:
            self.filename = filename

        self.midi = MidiFileGenerator()
        self.tempo = tempo
Example #3
0
def midify(vec,
           out_file,
           key="C",
           scale=MAJOR,
           bpm=120,
           count=0.25,
           channel=1,
           min_note="C-1",
           max_note="G9"):

    # transform keys and min/max notes
    key = root_to_midi(key)
    min_note = note_to_midi(min_note)
    max_note = note_to_midi(max_note)

    # select notes
    notes = build_scale(key, scale, min_note, max_note)

    # scale notes
    note_indexes = scale_vec(vec, low=0, high=(len(notes) - 1))

    # determinte note length
    beat = bpm_time(bpm, count)

    # generate midi file
    m = MidiFileGenerator()
    track = MidiTrack(channel=channel, tempo=bpm)

    t = 0
    for i in note_indexes:
        n = notes[i]
        track.add_note(time=t, duration=beat, note=n, velocity=100)
        t += beat

    m.tracks.append(track)
    m.writeToFile(out_file)