def note_off(self,
              channel=0,
              note=0x40,
              velocity=0x40,
              use_running_status=False):
     note = self._transp(channel, note)
     MidiOutFile.note_off(self, channel, note, velocity, use_running_status)
Example #2
0
 def test_midifiles(self):
     """
     Testing that the the midi files in 'tests/midifiles' can be parsed without errors.
     """
     midi_dir = testdir() + 'midifiles/*.mid'
     for i, midi_in_filename in enumerate(glob.iglob(midi_dir, recursive=True)):
         midi_out = MidiOutFile(io.BytesIO())
         midi_in = MidiInFile(midi_out, midi_in_filename)
         midi_in.read()
         midi_out.close()
     self.assertTrue(i == 4) # there are 5 files being tested
Example #3
0
"""

from mxm.midifile import MidiOutFile
from mxm.midifile import exampledir

QUARTER = 480
BAR = QUARTER*4
NOTE = 0x3C # middle C
GRAND_PIANO = 0

# in type 0 midi files there is only one track.
# there can be different midi channels in the same track, mo matter
# the type of midi file

out_file = open(exampledir('midi-out/mxm_midifile_type_0_minimal.mid'), 'wb')
midi = MidiOutFile(out_file)
midi.header(format=0, nTracks=1, division=QUARTER)

#####################
# track 0 begin

midi.start_of_track()

midi.tempo_bpm(135)
midi.time_signature(nn=4, dd=2, cc=24, bb=8)
midi.patch_change(0, GRAND_PIANO)

# note on
midi.update_time(0)
midi.note_on(0, NOTE, 0x64)
Example #4
0
def write_midi_sequence(midi_file,sequence):
    QUARTER = 480
    BAR = QUARTER*4
    GRAND_PIANO = 0

    out_file = open(midi_file, 'wb')
    midi = MidiOutFile(out_file)
    midi.header(format=1, nTracks=2, division=QUARTER)

    ##########################
    # track 0

    # in type 1 midi files first track MUST be a timing and tempo track
    # and no musical events are allowed.
    midi.start_of_track()
    midi.sequence_name(b'Timing track')
    midi.time_signature(nn=4, dd=2, cc=24, bb=8) # optional
    midi.tempo_bpm(135)
    midi.end_of_track()

    ##########################
    # track 1
    CH = 0
    midi.start_of_track()
    midi.patch_change(CH, GRAND_PIANO)
    midi.sequence_name(b'piano roll %i' % CH)

    midi.update_time(0)

    # go trough all notes in 'sequence' and add them to the track
    for NOTE in sequence.split( ):
        midi.note_on(CH, int(NOTE), 0x64)
        midi.update_time(int(BAR/4))                   # how much to sustain a note
        midi.note_off(CH, int(NOTE), 0x40)
        midi.update_time(int(QUARTER/2))        # how much time to wait between notes

    midi.update_time(BAR*4)
    midi.update_time(0)
    midi.end_of_track()
        'channel': channel,
        'velocity': 0x40,
        'time': time
    })
    notes.append({
        'type': 'note_off',
        'pitch': pitch,
        'channel': channel,
        'velocity': 0x40,
        'time': time + SIXTENTH
    })
notes.sort(key=lambda x: x['time'])

out_file = open(exampledir('midi-out/mxm_midifile_type_0_8ch_random.mid'),
                'wb')
midi = MidiOutFile(out_file)
midi.header(format=0, nTracks=1, division=QUARTER)

#####################
# track 0 begin

midi.start_of_track()

midi.tempo_bpm(135)
midi.time_signature(nn=4, dd=2, cc=24, bb=8)
for ch in range(8):
    midi.patch_change(ch, GRAND_PIANO)

for note in notes:
    midi.update_time(note['time'], relative=False)
    if note['type'] == 'note_on':
            'velocity': 0x40,
            'time': time
        })
        notes.append({
            'type': 'note_off',
            'pitch': pitch,
            'channel': track_n,
            'velocity': 0x40,
            'time': time + SIXTENTH
        })
    notes.sort(key=lambda x: x['time'])
    tracks.append(notes)

out_file = open(exampledir('midi-out/mxm_midifile_type_1_8ch_8trk_random.mid'),
                'wb')
midi = MidiOutFile(out_file)
midi.header(format=1, nTracks=9,
            division=QUARTER)  # 8 notes tracks + 1 timing track

#####################
# track 0 begin
midi.start_of_track()
midi.sequence_name(b'Timing track')
midi.time_signature(nn=4, dd=2, cc=24, bb=8)  # optional
midi.tempo_bpm(135)
midi.end_of_track()

#####################

# >>> print([c.GM_PATCHNAMES[s] for s in sounds])
# ['Acoustic Grand Piano', 'Glockenspiel', 'Drawbar Organ', 'Acoustic Guitar (nylon)',
def write_file(filename, chords):
    """
    Writes a midi file of the given file name from the list of chords given.
    """
    midi = MidiOutFile(open(filename, 'wb'))
    midi.header(format=0, nTracks=12, division=16)
    midi.start_of_track()
    time = 0
    prevBass = 0
    prevChord = []
    for i in range(len(chords)):
        c = str_to_pitches(chords[i], prevBass)
        nextChord = [] if i + 1 >= len(chords) else str_to_pitches(
            chords[i + 1], c[0] if len(c) > 0 else 0)
        for n in c:
            if n not in prevChord:
                midi.update_time(time, relative=False)
                midi.note_on(0, n, V)
        time += 8
        for n in c:
            if n not in nextChord:
                midi.update_time(time, relative=False)
                midi.note_off(0, n, V)
        prevBass = c[0] if len(c) > 0 else 0
        prevChord = c
    midi.update_time(0)
    midi.end_of_track()
"""
The smallest possible complete type 1 midi file. 1 note for 1 bar duration.
"""

from mxm.midifile import MidiOutFile
from mxm.midifile import exampledir

QUARTER = 480
BAR = QUARTER * 4
NOTE = 0x3C  # middle C
GRAND_PIANO = 0

out_file = open(exampledir('midi-out/mxm_midifile_type_1_minimal.mid'), 'wb')
midi = MidiOutFile(out_file)
midi.header(format=1, nTracks=2, division=QUARTER)

##########################
# track 0

# in type 1 midi files first track MUST be a timing and tempo track
# and no musical events are allowed.
midi.start_of_track()
midi.sequence_name(b'Timing track')
midi.time_signature(nn=4, dd=2, cc=24, bb=8)  # optional
midi.tempo_bpm(135)
midi.end_of_track()

##########################
# track 1

CH = 0
def make_midi(melody, file):
    out_file = open(f"./output/{file}.midi", 'wb')
    midi = MidiOutFile(out_file)
    midi.header(format=0, nTracks=1, division=32)
    midi.start_of_track()

    # print(f"Midi_notes: {melody}")

    try:
        for note, duration in melody:
            if note is 0:  # if it is a pause
                midi.note_on(channel=0, note=0)
                midi.update_time(0)
                midi.note_off(channel=0, note=0)
                midi.update_time(duration)
                midi.note_on(channel=0, note=0)
                midi.update_time(0)
                midi.note_off(channel=0, note=0)
                midi.update_time(0)
            else:
                midi.update_time(0)
                if isinstance(note,
                              list):  # checks if note is single, or chord
                    for n in note:
                        midi.note_on(channel=0, note=n)
                else:
                    midi.note_on(channel=0, note=note)
                midi.update_time(duration)
                if isinstance(note, list):
                    for n in note:
                        midi.note_off(channel=0, note=n)
                else:
                    # if note is not 0:
                    midi.note_off(channel=0, note=note)
        midi.update_time(0)
        midi.end_of_track()
    except:
        return False
    return True