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
    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 #4
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)
class MidiGenerator:
    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

    def add_track(self, track, time, trackname='',
        beat=[], notes=[], velocities=[], length=0):

        # append to existing track
        if track < len(self.midi.tracks):
            track = self.midi.tracks[track]
        else:
            track = MidiTrack(channel=track, tempo=self.tempo)
            self.midi.tracks.append(track)

        beat_index = time
        while beat_index - time < length:
            beat_value, duration_value = beat.next()
            for note in notes.next():
                track.add_note(
                    beat_index,
                    duration_value,
                    note, velocities.next()
                )

            beat_index += beat_value

    def add_arpeggio(self, track, time, chords_beat=[], notes_beat=[],
        chords=[], velocities=[], note_skip=LoopingArray([1, 2, 3]), length=0):

        track = MidiTrack(channel=track, tempo=self.tempo)

        beat_index = time
        bi = beat_index
        while beat_index - time < length:
            chordindex = 0
            beat_value, _ = chords_beat.next()
            chord = chords.next()
            while (bi < beat_index + beat_value) and (bi < length):
                chordindex = note_skip.next()
                note = chord[chordindex % len(chord)]
                notes_beat_value, notes_duration_value = notes_beat.next()
                track.add_note(
                    bi,
                    notes_duration_value,
                    note,
                    velocities.next())

                bi += notes_beat_value
            beat_index += beat_value

        self.midi.tracks.append(track)

    def write(self):
        self.midi.writeToFile(self.filename)
Example #6
0
class MidiGenerator:
    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

    def add_track(self,
                  track,
                  time,
                  trackname='',
                  beat=[],
                  notes=[],
                  velocities=[],
                  length=0):

        # append to existing track
        if track < len(self.midi.tracks):
            track = self.midi.tracks[track]
        else:
            track = MidiTrack(channel=track, tempo=self.tempo)
            self.midi.tracks.append(track)

        beat_index = time
        while beat_index - time < length:
            beat_value, duration_value = beat.next()
            for note in notes.next():
                track.add_note(beat_index, duration_value, note,
                               velocities.next())

            beat_index += beat_value

    def add_arpeggio(self,
                     track,
                     time,
                     chords_beat=[],
                     notes_beat=[],
                     chords=[],
                     velocities=[],
                     note_skip=LoopingArray([1, 2, 3]),
                     length=0):

        track = MidiTrack(channel=track, tempo=self.tempo)

        beat_index = time
        bi = beat_index
        while beat_index - time < length:
            chordindex = 0
            beat_value, _ = chords_beat.next()
            chord = chords.next()
            while (bi < beat_index + beat_value) and (bi < length):
                chordindex = note_skip.next()
                note = chord[chordindex % len(chord)]
                notes_beat_value, notes_duration_value = notes_beat.next()
                track.add_note(bi, notes_duration_value, note,
                               velocities.next())

                bi += notes_beat_value
            beat_index += beat_value

        self.midi.tracks.append(track)

    def write(self):
        self.midi.writeToFile(self.filename)