def test_miditable_creation_reference(self): a = MidiTable(testable, ref=True) self.assertEqual(testable[2]['program'], a[2]['program']) a[2]['program'] = 3 self.assertEqual(testable[2]['program'], a[2]['program']) a = MidiTable(testable.data) # another way to make reference self.assertEqual(testable[2]['program'], a[2]['program']) a[2]['program'] = 3 self.assertEqual(testable[2]['program'], a[2]['program'])
def create_miditable(): """This function creates a MidiTable populated with all types of midi messages that have been implemented in kenmido module in order to test the correct functionality of encoding and decoding midi data.""" track0 = MidiTable( 5) # A specialized data structure to implement musical score data track0.TrackName('Song') # track=0 time=0 by default track0.TempoSet(85) track0.TimeSignature(4, 4, track=0) track0.sysex('Hello sysex World') track0.SequenceNumber(365) track0.Copyright('Anustuv Pal') track0.InstrumentName('Free VST') track0.Marker('A marker', time=240) track0.CueMarker('A cue message', time=240) track0.DeviceName('A device') track0.ChannelPrefix(13) track0.MidiPort(1) track0.SmpteOffset() track0.KeySignature('Gm', time=240) track0.SequencerSpecific((1, 1, 2, 3, 5, 8, 13)) # track0.meta(EndOfTrack) # end of tracks are inserted automatically track1 = MidiTable(5, track=1, channel=0) track1.TrackName('Track1') track1.Text('This is a text meta message in track 1') track1.control(time=0, control=CONTROLS['Channel Volume'], cc_val=100) track1.control(time=1, control=CONTROLS['Pan'], cc_val=95, channel=1) track1.program(program=PROGRAMS['Clavi']) track1.program(program=PROGRAMS['Electric Piano'], time=480) track1.meta(metaclasses.Lyrics, 'A word', time=240) score = MidiTable(4, track=1, channel=0) # Mechanism to write notes into the table for time, noteval in zip(range(0, 4 * 240, 240), [61, 65, 68, 72]): score.note_on(time=time, noteval=noteval, score_d=240, midi_d=240, notevel=70) score = merge_tables(track0, track1, score) # simple concatenation score.fix_eots() # ensure end-of-tracks find their appropriate position score.insert_note_offs() score.remove_empty_rows() score.encode_table() tprint(score) return score
def test_extend_table(self): nrows = 5 a = MidiTable(testable) orig_len = len(a) a.__extend_table__(nrows) new_len = len(a) for i in range(orig_len, new_len): for field in a[i]: self.assertEqual(field, 0)
def test_miditable_create_empty_rows_and_remove(self): num = 5 a = MidiTable(num) self.assertEqual(len(a), num) for i in range(num): for field in a[i]: self.assertEqual(field, 0) self.assertTrue(a) self.assertTrue(testable) a.remove_empty_rows() self.assertEqual(len(a), 0)
def gen_data(): D = 480 notes = np.random.choice(range(30, 80), size=N) starttimes = np.arange(0, N * D, D) midi_ds = np.random.random(N) * D * 4 mt = MidiTable(N) mt['noteval'] = notes mt['time'] = starttimes mt['midi_d'] = mt['score_d'] = 240 mt['mtype'] = Mtypes.NON mt['notevel'] = np.linspace(0, 127, N, dtype=int) return mt
def __init__(self, portn=0, record=True, thruport: rtmidi.MidiOut = None): self.port = rtmidi.MidiIn() self.port.open_port(portn) self.inptable = MidiTable(10000) self.start = None self.callbackregistry = CallbackRegistry() if thruport: self.callbackregistry.register_callback( make_thru_output_callback(thruport)) if record: self.callbackregistry.register_callback( make_table_record_callback(self.inptable)) self.input_callback = self.callbackregistry.make_rtmidi_callback()
def generate_testtable(): testtable = MidiTable(20, channel=1, track=1) # print(testtable.maxtime) testtable.TrackName('Song 1', track=0, channel=0) testtable.TempoSet(105) testtable.TimeSignature(4, 4) testtable.TrackName('Track 1') testtable.control('volume', 100) testtable.control('pan', 67) testtable.bend(0) testtable.Marker('A marker', time=240) testtable.Lyrics('Some words...', time=480) testtable.note_on(time=0, noteval=60, score_d=480) testtable.note_on(time=480, noteval=62, score_d=480) testtable.note_on(time=960, noteval=64, score_d=480) testtable.note_on(time=1280, noteval=65, score_d=480) testtable.cleanup() return testtable
def play(inptable, noloops=1, tempo=DEFAULT_TEMPO): """A simple function to play a ``MidiTable`` through an open port. Args: miditable: The ``MidiTable`` object to play. noloops: number of loops to play the table in. """ try: miditable = MidiTable(inptable) tempo = bpm2tempo(DEFAULT_TEMPO) # default tempo for i in range(noloops): for msg in miditable: if msg['del_t'] > 0: delta = tick2second(msg['del_t'], tempo) sleep(delta) if msg['mtype'] == Mtypes.TEMPO: tempo = metaclasses.TempoSet.decoder(msg['data']) elif msg['mtype'] in CHAN_MTYPES: midiout.send_message(msg['data']) except KeyboardInterrupt: midiout.close_port() print("Player stopped by user.")
def test_deleterows(self): a = MidiTable(testable) self.assertTrue(any(a['track'] == 1)) a.deleterows(a['track'] == 1) self.assertLess(len(a), len(testable)) self.assertFalse(any(a['track'] == 1))
def test_miditable_creation_copy(self): a = MidiTable(testable) self.assertEqual(testable[2]['program'], a[2]['program']) a[2]['program'] = 3 self.assertNotEqual(testable[2]['program'], a[2]['program'])
def test_miditable_creation_zero(self): a = MidiTable(1) self.assertIsInstance(a, MidiTable) self.assertEqual(len(a), 0) self.assertIsInstance(a.data, np.ndarray) self.assertFalse(a)
from kenmido import MidiTable, Sequencer, portopen, play from kenmido.rows import Mtypes from theorymuse import tprint, Printer Printer.midi_mode() a = MidiTable(20, channel=1, track=1) a.TrackName('Song 1', track=0, channel=0) a.TempoSet(105) a.TimeSignature(4, 4) a.TrackName('Track 1') a.control('volume', 100) a.control('pan', 67) a.bend(0) a.Marker('A marker', time=240) a.Lyrics('Some words...', time=480) a.note_on(time=0, noteval=60, score_d=480, midi_f=1) a.note_on(time=480, noteval=62, score_d=480, midi_f=1) a.note_on(time=960, noteval=64, score_d=480, midi_f=1) a.note_on(time=960 + 240, noteval=65, score_d=240, midi_f=1) a.fix_eots() a.insert_note_offs() a.remove_empty_rows() a.sort() a.encode_table() a.abs_to_reltime() print(a.maxtime) tprint(a) with portopen('loopMIDI'): play(a, noloops=3)
from kenmido import MidiTable, MIDI_LIB, play, portopen from kenmido.rows import Mtypes # from kenmido.miditable import row_compare from theorymuse import tprint, Printer from test_midifile_manipulation import create_miditable Printer.midi_mode() mt = create_miditable() # mt = MidiTable.load_midifile(MIDI_LIB['soddity']) # mt.sort() nons = MidiTable(10, track=1, channel=0) nons.TrackName('Track 1') mt.save_midifile('test_midi.mid') pt = MidiTable.load_midifile('test_midi.mid') pt.save_midifile('test_midi1.mid') pt1 = MidiTable.load_midifile('test_midi1.mid') pt1.remove_note_offs() pt1.insert_note_offs() print(len(mt), len(pt), len(pt1)) # pt1.cleanup() tprint(mt) for mtype in Mtypes: mtabl1 = len(mt[mt['mtype'] == mtype]) mtabl2 = len(pt[pt['mtype'] == mtype]) mtabl3 = len(pt1[pt1['mtype'] == mtype]) print(mtype, mtabl1, mtabl2, mtabl3)
import numpy as np from kenmido import (MidiTable, MIDI_LIB, play, portopen, merge_tables) from kenmido.miditable import logger from kenmido.miditable import RowCodec # import matplotlib.pyplot as plt from kenmido.rows import Mtypes from theorymuse import tprint, Printer from printer.pprinter import prn_data Printer.midi_mode() nons1 = MidiTable(10, track=1) nons2 = MidiTable(10, track=1, channel=1) nons3 = MidiTable(10, track=2, channel=2) nons4 = MidiTable(10, track=2, channel=3) nons1.TrackName('Song', track=0) nons1.TrackName('Track 1') nons2.TrackName('Track 2') nons3.TrackName('Track 3') nons4.TrackName('Track 4') N = 100 np.random.seed(1000) def gen_data(): D = 480 notes = np.random.choice(range(30, 80), size=N) starttimes = np.arange(0, N * D, D) midi_ds = np.random.random(N) * D * 4
def table(self, other=0): self._table = MidiTable(other)
def __init__(self, miditable=0): self._table = MidiTable(miditable) self.port = rtm.MidiOut() self.isportopen = self.port.is_port_open()