def Bass_Clarinet(): """Defines and returns a bass clarinet midi instrument (an instance of MidiInstrument())""" instr = MidiInstrument() instr.name = 'Bass Clarinet' instr.key = 'Bb' instr.clef = 'Treble' instr.set_range(tuple([Note('Bb', 1), Note('B', 5)])) instr.instrument_nr = instr.names.index('Clarinet') return instr
def Vibraphone(): """Defines and returns a vibraphone midi instrument (an instance of MidiInstrument())""" instr = MidiInstrument() instr.name = 'Vibraphone' instr.key = 'C' instr.clef = 'Treble' instr.set_range(tuple([Note('F', 3), Note('F', 6)])) instr.instrument_nr = instr.names.index('Vibraphone') return instr
def Marimba(): """Defines and returns a marimba midi instrument (an instance of MidiInstrument())""" instr = MidiInstrument() instr.name = 'Marimba' instr.key = 'C' instr.clef = 'Treble & Bass' instr.set_range(tuple([Note('C', 2), Note('C', 7)])) instr.instrument_nr = instr.names.index('Marimba') return instr
def Orchestral_Harp(): """Defines and returns an orchestral harp midi instrument (an instance of MidiInstrument())""" instr = MidiInstrument() instr.name = 'Orchestral Harp' instr.key = 'C' instr.clef = 'Treble & Bass' instr.set_range(tuple([Note('C', 1), Note('G', 7)])) instr.instrument_nr = instr.names.index('Orchestral Harp') return instr
def Tenor_Saxophone(): """Defines and returns a tenor saxophone midi instrument (an instance of MidiInstrument())""" instr = MidiInstrument() instr.name = 'Tenor Saxophone' instr.key = 'Bb' instr.clef = 'Treble' instr.set_range(tuple([Note('Ab', 2), Note('E', 5)])) instr.instrument_nr = instr.names.index('Tenor Sax') return instr
def Cello(): """Defines and returns a cello midi instrument (an instance of MidiInstrument())""" instr = MidiInstrument() instr.name = 'Cello' instr.key = 'C' instr.clef = 'Bass' instr.set_range(tuple([Note('C', 2), Note('E', 6)])) instr.instrument_nr = instr.names.index('Cello') return instr
def Contrabass(): """Defines and returns a contrabass midi instrument (an instance of MidiInstrument())""" instr = MidiInstrument() instr.name = 'Contrabass' instr.key = 'C' instr.clef = 'Bass' instr.set_range(tuple([Note('E', 1), Note('G', 5)])) instr.instrument_nr = instr.names.index('Contrabass') return instr
def Violin(): """Defines and returns a violin midi instrument (an instance of MidiInstrument())""" instr = MidiInstrument() instr.name = 'Violin' instr.key = 'C' instr.clef = 'Treble' instr.set_range(tuple([Note('G', 3), Note('E', 7)])) instr.instrument_nr = instr.names.index('Violin') return instr
def Viola(): """Defines and returns a viola midi instrument (an instance of MidiInstrument())""" instr = MidiInstrument() instr.name = 'Viola' instr.key = 'C' instr.clef = 'Alto' instr.set_range(tuple([Note('C', 3), Note('A', 7)])) instr.instrument_nr = instr.names.index('Viola') return instr
def Alto_Flute(): """Defines and returns an alto flute midi instrument (an instance of MidiInstrument())""" instr = MidiInstrument() instr.name = 'Alto Flute' instr.key = 'G' instr.clef = 'Treble' instr.set_range(tuple([Note('G', 3), Note('G', 6)])) instr.instrument_nr = instr.names.index('Flute') return instr
def Flute(): """Defines and returns a flute midi instrument (an instance of MidiInstrument())""" instr = MidiInstrument() instr.name = 'Flute' instr.key = 'C' instr.clef = 'Treble' instr.set_range(tuple([Note('C', 4), Note('C', 7)])) instr.instrument_nr = instr.names.index('Flute') return instr
def Baritone_Saxophone(): """Defines and returns a baritone saxophone midi instrument (an instance of MidiInstrument())""" instr = MidiInstrument() instr.name = 'Baritone Saxophone' instr.key = 'Eb' instr.clef = 'Treble' instr.set_range(tuple([Note('C', 2), Note('A', 4)])) instr.instrument_nr = instr.names.index('Baritone Sax') return instr
def Soprano_Saxophone(): """Defines and returns a soprano saxophone midi instrument (an instance of MidiInstrument())""" instr = MidiInstrument() instr.name = 'Soprano Saxophone' instr.key = 'Bb' instr.clef = 'Treble' instr.set_range(tuple([Note('Ab', 3), Note('E', 6)])) instr.instrument_nr = instr.names.index('Soprano Sax') return instr
def Piano(): """Defines and returns a piano midi instrument (an instance of MidiInstrument())""" instr = MidiInstrument() instr.name = 'Piano' instr.key = 'C' instr.clef = 'Treble & Bass' instr.set_range(tuple([Note('A', 0), Note('C', 8)])) instr.instrument_nr = instr.names.index('Acoustic Grand Piano') return instr
def Alto_Saxophone(): """Defines and returns an alto saxophone midi instrument (an instance of MidiInstrument())""" instr = MidiInstrument() instr.name = 'Alto Saxophone' instr.key = 'Eb' instr.clef = 'Treble' instr.set_range(tuple([Note('Db', 3), Note('A', 5)])) instr.instrument_nr = instr.names.index('Alto Sax') return instr
return m.write_file(file, verbose) if __name__ == '__main__': from mingus.containers.NoteContainer import NoteContainer from mingus.containers.Bar import Bar from mingus.containers.Track import Track from mingus.containers.Instrument import MidiInstrument b = Bar() b2 = Bar('Ab', (3, 4)) n = NoteContainer(['A', 'C', 'E']) t = Track() b + n b + [] b + n b + n b2 + n b2 + n b2 + [] t + b t + b m = MidiInstrument() m.instrument_nr = 13 t.instrument = m t.name = 'Track Name Test' write_NoteContainer('test.mid', n) write_Bar('test2.mid', b) write_Bar('test3.mid', b, 200) write_Bar('test4.mid', b2, 200, 2) write_Track('test5.mid', t, 120)
def MIDI_to_Composition(self, file): (header, track_data) = self.parse_midi_file(file) c = Composition() if header[2]['fps']: print "Don't know how to parse this yet" return c ticks_per_beat = header[2]['ticks_per_beat'] for track in track_data: # this loop will gather data for all notes, # set up keys and time signatures for all bars # and set the tempo and instrument for the track. metronome = 1 # Tick once every quarter note thirtyseconds = 8 # 8 thirtyseconds in a quarter note step = 256.0 # WARNING: Assumes our smallest desired quantization step is a 256th note. meter = (4, 4) key = 'C' bar = 0 beat = 0 now = (bar, beat) b = None started_notes = {} finished_notes = {} b = Bar(key=key, meter=meter) bars = [b] bpm = None instrument = None track_name = None for deltatime, event in track: if deltatime != 0: duration = (ticks_per_beat * 4.0) / float(deltatime) dur_q = int(round(step/duration)) length_q = int(b.length * step) o_bar = bar c_beat = beat + dur_q bar += int(c_beat / length_q) beat = c_beat % length_q while o_bar < bar: o_bar += 1 o_key = b.key b = Bar(key=key, meter=meter) b.key = o_key bars.append(b) now = (bar, beat) if event['event'] == 8: # note off channel = event['channel'] note_int = event['param1'] velocity = event['param2'] note_name = notes.int_to_note(note_int % 12) octave = note_int / 12 - 1 note = Note(note_name, octave) note.channel = channel note.velocity = velocity x = (channel, note_int) start_time = started_notes[x] del started_notes[x] end_time = now y = (start_time, end_time) if y not in finished_notes: finished_notes[y] = [] finished_notes[y].append(note) elif event['event'] == 9: # note on channel = event['channel'] note_int = event['param1'] velocity = event['param2'] x = (channel, note_int) # add the note to the current NoteContainer started_notes[x] = now elif event['event'] == 10: # note aftertouch pass elif event['event'] == 11: # controller select pass elif event['event'] == 12: # program change # WARNING: only the last change in instrument will get saved. i = MidiInstrument() i.instrument_nr = event['param1'] instrument = i elif event['event'] == 0x0f: # meta event Text if event['meta_event'] == 1: pass elif event['meta_event'] == 3: # Track name track_name = event['data'] elif event['meta_event'] == 6: # Marker pass elif event['meta_event'] == 7: # Cue Point pass elif event['meta_event'] == 47: # End of Track pass elif event['meta_event'] == 81: # Set tempo # WARNING: Only the last change in bpm will get saved mpqn = self.bytes_to_int(event['data']) bpm_o = bpm bpm = 60000000 / mpqn elif event['meta_event'] == 88: # Time Signature d = event['data'] thirtyseconds = self.bytes_to_int(d[3]) metronome = self.bytes_to_int(d[2]) / 24.0 denom = 2 ** self.bytes_to_int(d[1]) numer = self.bytes_to_int(d[0]) meter = (numer, denom) b.set_meter(meter) elif event['meta_event'] == 89: # Key Signature d = event['data'] sharps = self.bytes_to_int(d[0]) minor = self.bytes_to_int(d[0]) if minor: key = 'A' else: key = 'C' for i in xrange(abs(sharps)): if sharps < 0: key = intervals.major_fourth(key) else: key = intervals.major_fifth(key) b.key = Note(key) else: print 'Unsupported META event', event['meta_event'] else: print 'Unsupported MIDI event', event t = Track(instrument) t.name = track_name sorted_notes = {} # sort the notes (so they are added to the bars in order) # this loop will also split up notes that span more than one bar. for x in finished_notes: (start_bar, start_beat), (end_bar, end_beat) = x if end_beat == 0: end_bar -= 1 end_beat = int(bars[end_bar].length * step) while start_bar <= end_bar: nc = NoteContainer(finished_notes[x]) b = bars[start_bar] if start_bar < end_bar: # only executes when note spans more than one bar. length_q = int(b.length * step) dur = int(step/(length_q - start_beat)) else: # always executes - add the final section of this note. dur = int(step/(end_beat-start_beat)) if start_beat != 0: at = float(start_beat)/step else: at = 0.0 if start_bar not in sorted_notes: sorted_notes[start_bar] = {} if at not in sorted_notes[start_bar]: sorted_notes[start_bar][at] = (dur, nc) # set our offsets for the next loop start_beat = 0 start_bar += 1 # add all notes to all bars in order. for start_bar in sorted(sorted_notes.keys()): for at in sorted(sorted_notes[start_bar].keys()): dur, nc = sorted_notes[start_bar][at] bars[start_bar].place_notes_at(nc, dur, at) # add the bars to the track, in order for b in bars: b.fill_with_rests() t + b # add the track to the composition c.tracks.append(t) return (c, bpm)
def MIDI_to_Composition(self, file): (header, track_data) = self.parse_midi_file(file) c = Composition() if header[2]['fps']: print("Don't know how to parse this yet") return c ticks_per_beat = header[2]['ticks_per_beat'] for track in track_data: t = Track() b = Bar() metronome = 1 # Tick once every quarter note thirtyseconds = 8 # 8 thirtyseconds in a quarter note meter = (4, 4) key = 'C' for e in track: (deltatime, event) = e duration = float(deltatime) / (ticks_per_beat * 4.0) if duration != 0.0: duration = 1.0 / duration if deltatime != 0: if not b.place_notes(NoteContainer(), duration): t + b b = Bar(key, meter) b.place_notes(NoteContainer(), duration) if event['event'] == 8: if deltatime == 0: pass elif event['event'] == 9: # note on n = Note(notes.int_to_note(event['param1'] % 12), event['param1'] / 12 - 1) n.channel = event['channel'] n.velocity = event['param2'] if len(b.bar) > 0: b.bar[-1][2] + n else: b + n elif event['event'] == 10: # note aftertouch pass elif event['event'] == 11: # controller select pass elif event['event'] == 12: # program change i = MidiInstrument() i.instrument_nr = event['param1'] t.instrument = i elif event['event'] == 0x0f: # meta event Text if event['meta_event'] == 1: pass elif event['meta_event'] == 3: # Track name t.name = event['data'] elif event['meta_event'] == 6: # Marker pass elif event['meta_event'] == 7: # Cue Point pass elif event['meta_event'] == 47: # End of Track pass elif event['meta_event'] == 81: # Set tempo warning Only the last change in bpm will get # saved currently mpqn = self.bytes_to_int(event['data']) bpm = 60000000 / mpqn elif event['meta_event'] == 88: # Time Signature d = event['data'] thirtyseconds = self.bytes_to_int(d[3]) metronome = self.bytes_to_int(d[2]) / 24.0 denom = 2 ** self.bytes_to_int(d[1]) numer = self.bytes_to_int(d[0]) meter = (numer, denom) b.set_meter(meter) elif event['meta_event'] == 89: # Key Signature d = event['data'] sharps = self.bytes_to_int(d[0]) minor = self.bytes_to_int(d[0]) if minor: key = 'A' else: key = 'C' for i in range(abs(sharps)): if sharps < 0: key = intervals.major_fourth(key) else: key = intervals.major_fifth(key) b.key = Note(key) else: print('Unsupported META event', event['meta_event']) else: print('Unsupported MIDI event', event) t + b c.tracks.append(t) return (c, bpm)
def MIDI_to_Composition(self, file): header, track_data = self.parse_midi_file(file) c = Composition() if header[2]["fps"]: print "Don't know how to parse this yet" return c ticks_per_beat = header[2]["ticks_per_beat"] for track in track_data: t = Track() b = Bar() metronome = 1 # Tick once every quarter note thirtyseconds = 8 # 8 thirtyseconds in a quarter note meter = (4,4) key = 'C' for e in track: deltatime, event = e duration = float(deltatime) / (ticks_per_beat * 4.0) if duration != 0.0: duration = 1.0 / duration if deltatime != 0: if not b.place_notes(NoteContainer(), duration): t + b b = Bar(key, meter) b.place_notes(NoteContainer(), duration) # note off if event["event"] == 8: if deltatime == 0: pass # note on elif event["event"] == 9: n = Note(notes.int_to_note(event["param1"] % 12), event["param1"] / 12 - 1) n.channel = event["channel"] n.velocity = event["param2"] if len(b.bar) > 0: b.bar[-1][2] + n else: b + n # note aftertouch elif event["event"] == 10: pass # controller select elif event["event"] == 11: pass # program change elif event["event"] == 12: i = MidiInstrument() i.instrument_nr = event["param1"] t.instrument = i # meta event elif event["event"] == 15: # Track name if event["meta_event"] == 3: t.name = event["data"] # Marker elif event["meta_event"] == 6: pass # Cue Point elif event["meta_event"] == 7: pass # End of Track elif event["meta_event"] == 47: pass # Set tempo #warning Only the last change in bpm will get saved currently elif event["meta_event"] == 81: mpqn = self.bytes_to_int(event["data"]) bpm = 60000000 / mpqn # Time Signature elif event["meta_event"] == 88: d = event["data"] thirtyseconds = self.bytes_to_int(d[3]) metronome = self.bytes_to_int(d[2]) / 24.0 denom = 2 ** self.bytes_to_int(d[1]) numer = self.bytes_to_int(d[0]) meter = (numer, denom) b.set_meter(meter) # Key Signature elif event["meta_event"] == 89: pass else: print "Unsupported META event", event["meta_event"] else: print "Unsupported MIDI event", event t + b c.tracks.append(t) return c, bpm
chords_bars = [] for chord in chords_list: chord_nc = NoteContainer(chords.from_shorthand(chord)) bar = Bar() bar.place_notes(chord_nc, 1) chords_bars.append(bar) solo_track = Track() chords_track = Track() for _ in range(num_progressions): for bar in generate_solo(chords_list): solo_track.add_bar(bar) for bar in chords_bars: chords_track.add_bar(bar) guitar = MidiInstrument() guitar.instrument_nr = 26 solo_track.instrument = guitar piano = MidiInstrument() piano.instrument_nr = 0 chords_track.instrument = piano song = Composition() song.add_track(solo_track) song.add_track(chords_track) MidiFileOut.write_Composition("test.mid", song) filename = "test.mid" call("timidity -Ow {0}".format(filename), shell=True) # fluidsynth -F test.wav -i -n -T wav soundfont.sf2 test.mid