本文整理汇总了Python中midi.read_midifile函数的典型用法代码示例。如果您正苦于以下问题:Python read_midifile函数的具体用法?Python read_midifile怎么用?Python read_midifile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了read_midifile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testIsEmptyTrack
def testIsEmptyTrack():
pat1 = midi.read_midifile('tests/trk15empty.mid')
assert(isEmptyTrack(15,pat1) == True)
pat2 = midi.read_midifile('tests/empty.mid')
assert(isEmptyTrack(0,pat2) == True)
pat3 = midi.read_midifile('tests/notEmpty.mid')
assert(isEmptyTrack(1,pat3)== False)
示例2: id_pattern_itter
def id_pattern_itter(db):
for record in db["records"]:
record = db["records"][record]
yield (record["id"], midi.read_midifile(StringIO.StringIO(record["data"])))
for bar in record["bars"]:
yield(bar["id"],midi.read_midifile(StringIO.StringIO(bar["data"])))
for track in record["bars"]:
yield(track["id"],midi.read_midifile(StringIO.StringIO(track["data"])))
for bar_track in record["bars_tracks"]:
yield(bar_track["id"],midi.read_midifile(StringIO.StringIO(bar_track["data"])))
示例3: test_mary
def test_mary(self):
midi.write_midifile("mary.mid", mary_test.MARY_MIDI)
pattern1 = midi.read_midifile("mary.mid")
midi.write_midifile("mary.mid", pattern1)
pattern2 = midi.read_midifile("mary.mid")
self.assertEqual(len(pattern1), len(pattern2))
for track_idx in range(len(pattern1)):
self.assertEqual(len(pattern1[track_idx]), len(pattern2[track_idx]))
for event_idx in range(len(pattern1[track_idx])):
event1 = pattern1[track_idx][event_idx]
event2 = pattern1[track_idx][event_idx]
self.assertEqual(event1.tick, event2.tick)
self.assertEqual(event1.data, event2.data)
示例4: main
def main():
#pattern1 = midi.read_midifile("midi/5RAP_04.MID")
bars = []
pattern = midi.read_midifile("midi/5RAP_04.MID")
#pattern = midi.read_midifile("midi/decoy.mid")
#pattern = midi.read_midifile("midi/drum_patterns.mid")
#print_events(pattern, [])
pattern = sanitize(pattern)
midi.write_midifile("test.mid", pattern)
pattern = midi.read_midifile("test.mid")
print pattern
return
示例5: main
def main():
usage = "%prog <seq-id> <midi-file> [<midi-file> ...]"
parser = OptionParser(usage=usage)
parser.add_option("--names", dest="names", action="store", help="read in a NAMES file from the midi auto-collection, intead of reading midi files from the command line")
options, arguments = parser.parse_args()
if options.names is not None:
print "Reading names from %s" % options.names
csv = UnicodeCsvReader(open(options.names))
filenames = []
dirname = os.path.dirname(options.names)
csv.next()
for row in csv:
filename = os.path.join(dirname,row[0])
if not os.path.exists(filename):
continue
seq = ChordSequence.objects.get(id=int(row[2]))
filenames.append((seq,filename))
else:
if len(arguments) < 2:
print >>sys.stderr, "Specify a sequence id and one or more midi files"
sys.exit(1)
seq_id = int(arguments[0])
seq = ChordSequence.objects.get(id=seq_id)
filenames = [(seq,fn) for fn in arguments[1:]]
if len(filename) == 0:
print sys.stderr, "No input files"
sys.exit()
files = []
for seq,filename in filenames:
print "Reading %s" % filename
f = open(filename, 'r')
data = f.read()
# Try reading in the midi data to check it's ok
read_midifile(StringIO(data))
files.append((seq, os.path.basename(filename), ContentFile(data)))
for seq, filename,f in files:
print "Storing %s" % filename
# Create a new midi data record in the database
midi = MidiData()
midi.sequence = seq
midi.save()
# Use the original filename
midi.midi_file.save(filename, f)
midi.save()
示例6: openOptions
def openOptions(): #Options window UI
top=Toplevel()
Label(top, text="Chain Length: ").grid(row=0)
Label(top, text="Instrument: ").grid(row=1)
Label(top, text="Song Length: ").grid(row=2)
Label(top, text="Tempo: ").grid(row=3)
Label(top, text="Input Track: ").grid(row=4)
lengths=range(2,10) #possible chain lengths
c.d.chainLengthPicker=apply(OptionMenu,(top,c.d.chainLength)+tuple(lengths))
c.d.chainLengthPicker.grid(row=0,column=1)
instruments=c.d.instruments.keys()
c.d.instrumentPicker=OptionMenu(top,c.d.instrumentName,*instruments,\
command=setInstrument)
c.d.instrumentPicker.grid(row=1,column=1)
c.d.songLengthPicker=Entry(top,textvariable=c.d.songLength,\
validatecommand=validateLength)
c.d.songLengthPicker.grid(row=2,column=1)
c.d.tempoPicker=Entry(top,textvariable=c.d.tempo,\
validatecommand=validateTempo)
c.d.tempoPicker.grid(row=3,column=1)
tracks=[1] #default track
if(c.d.inputFile.get() !=''):
pat=midi.read_midifile(c.d.inputFile.get())
tracks=range(len(pat))
c.d.trackPicker=apply(OptionMenu,(top,c.d.track)+tuple(tracks))
c.d.trackPicker.grid(row=4,column=1)
示例7: load
def load(self, filename):
'''Load MIDI file'''
pattern = midi.read_midifile(filename)
self.resolution = pattern.resolution
pattern.make_ticks_abs()
events = []
for track in pattern:
for event in track:
events.append(event)
events.sort()
cues = []
for e in events:
if type(e) == midi.events.TextMetaEvent:
if e.text.startswith('cue'):
cues.append(e.tick)
self.cues = cues
self.events = events
self.eventnum = 0
self.tempo = 120.0
self.time = 0
self._playing = False
self.softReset()
while self.eventnum < len(self.events) and self.events[self.eventnum].tick < 2:
event = self.events[self.eventnum]
self.eventnum += 1
self.do_event(event)
示例8: time_series_list_builder
def time_series_list_builder(filename_io_mid):
'''
INPUT: STR : The name of MIDI file 'bwv733_io.mid' extracted out of Ableton into typically 3 separate
MIDI files 'bwv733_t1.mid', 'bwv733_t2.mid', 'bwv733_t3.mid'
and merged with merger_t1s('bwv733.mid')
OUTPUT: time_series LIST [(time INT, pitch INT, duration INT), ...]: time-sorted
'''
tracks = midi.read_midifile(filename_io_mid)
note_on = [0 for i in tracks]
note_off = [0 for i in tracks]
time_series_list = []
for i, track in enumerate(tracks):
note_on[i], note_off[i] = extract_melody(track)
if len(note_on[i]) != len(note_off[i]):
print '''len(note_on)={} and len(note_off)={} @track={}'''\
.format(len(note_on[i]), len(note_off[i]), i)
out = time_series_builder(note_on[i], note_off[i])
if len (out) > 0:
time_series_list.append(out)
return time_series_list
示例9: to_int_array
def to_int_array(file_location):
pattern = midi.read_midifile(file_location)
return [
event.data[0] for track in pattern for event in track
if isinstance(event, midi.NoteOnEvent) and event.data[1] > 0
]
示例10: notes_from_file
def notes_from_file(filename: str) -> List[Note]:
midifile_rel = midi.read_midifile(filename)
midifile_abs = copy.deepcopy(midifile_rel)
midifile_abs.make_ticks_abs()
# Convert MIDI events to our music representation: a list of Note objects
notes = []
active_notes = {}
for ev_rel, ev_abs in zip(midifile_rel[-1], midifile_abs[-1]):
if isinstance(ev_rel, midi.NoteOnEvent) and ev_rel.data[1]:
n = Note()
n.resolution = midifile_rel.resolution
n.tick_abs = ev_abs.tick
n.pitch = ev_rel.data[0]
n.velocity = ev_rel.data[1]
if n.pitch not in active_notes:
active_notes[n.pitch] = {n}
else:
active_notes[n.pitch].add(n)
elif isinstance(ev_rel, midi.NoteOffEvent) or (isinstance(ev_rel, midi.NoteOnEvent) and ev_rel.data[1] == 0):
n = active_notes[ev_rel.data[0]].pop()
n.duration = ev_abs.tick - n.tick_abs
notes.append(n)
assert not any(active_notes.values()), "Some notes were not released"
return sorted(notes, key=lambda note: note.tick_abs)
示例11: Main
def Main():
#these values should match what is in SongData
#and are here just for reference
#REST = 128
#HOLD = 129
#NULL = 130
#END = 131
#BEATS_PER_BAR = 32
#extract features
absolute_pos = 0
folder = os.path.join('downloads','midiworld')
pitches = set()
songs = []
observedstates = set()
featurevectors = []
import csv
with open('features.csv','wb') as out:
try:
print('finding valid songs')
csv_out=csv.writer(out)
valid_filenames = []
for filename in os.listdir(folder):
out_of_range = False
song_path = os.path.join(folder, filename)
pattern = midi.read_midifile(song_path)
song = SongData(pattern, filename)
for action in song.eventset:
if (action > 84 and action < 128) or action < 48:
out_of_range = True
if song.startstate.pitch == 131:
out_of_range = True
if not out_of_range:
valid_filenames.append(song.filename)
print(len(valid_filenames))
pdump(valid_filenames, 'valid_filenames.pkl',noPrefix=True)
示例12: testcreateTune
def testcreateTune(self):
# --- tests if MIDI files are successfully converted to a Tune object ---
# import midi file: C major scale with all quarter notes (refer to TestComputePitches)
# Use Python MIDI library https://github.com/vishnubob/python-midi
# MIDI files are an array of integers with a header
TuneMIDI = midi.read_midifile("../tests/MIDITestFiles/c-major-scale-treble.mid")
# ---- Fail Tune Parameter Constraints ---
self.assertFalse(Tune("wrongFileType.txt"), Clef.TREBLE, "", [""])
# timeSignature has to be (int, int) where int > 0
self.assertFalse(Tune(TuneMIDI), (-1, 0), Clef.BASS, "Title", ["Contributor"])
self.assertFalse(Tune(TuneMIDI, (2.5, 3), Clef.BASS, "Title", ["Contributor"]))
tune = Tune(TuneMIDI, (3,4), Clef.TREBLE, "Title", ["Contributor"])
# --- test Tune setters and getters ---
# If bad input, leave field unchanged
tune.setTimeSignature((4,4))
self.assertEqual(tune.getTimeSignature(), (4,4))
tune.setTimeSignature((-1, 0))
self.assertEqual(tune.getTimeSignature(), (4,4))
tune.setTitle("new title")
self.assertEqual(tune.getTitle(), "new title")
tune.setTitle("this is toooooooooooooooooooooooooooooooooooooooooooo long title")
self.assertEqual(tune.getTitle(), "new title")
tune.setContributors(["person1, person2, person3"])
self.assertEqual(tune.getContributors(), ["person1, person2, person3"])
tune.setContributors(["this is tooooooooooooooooooooooooooooooooo long contributor name"])
self.assertEqual(tune.getContributors(), ["person1, person2, person3"])
frequencies = [261.63, 293.66, 329.63]
# check frequencies and onsets calculated correctly from generateTune
for i in xrange(0, 3):
self.assertEqual(tune[i].getFrequency(), frequencies[i])
self.assertEqual(tune[i].getOnset(), i)
self.assertTrue(samerest.noteEqual(samerestNote))
示例13: load_midi_file
def load_midi_file(self, file_location):
midi_file = midi.read_midifile(file_location)
midi_file.reverse()
midi_file.make_ticks_abs()
resolution = midi_file.resolution
song_info_track = midi_file.pop()
has_tempo_event = False
mpqn = 0 # milliseconds per quarter note
for event in song_info_track:
if type(event) is midi.SetTempoEvent:
has_tempo_event = True
mpqn = (event.data[0] << 16) | (event.data[1] << 8) | event.data[2]
# print "mpqn is&: ", mpqn
if mpqn == 0:
mpqn = 1
current_tempo_in_beats_per_minute = 120
if has_tempo_event:
current_tempo_in_beats_per_minute = 60000000.0/mpqn
self.bpm = current_tempo_in_beats_per_minute
self.ms_per_tick = resolution * (current_tempo_in_beats_per_minute / 60.0)
self.ms_per_tick = 1000.0/self.ms_per_tick
self.song_buffer = SongBuffer(time_per_tick=self.ms_per_tick)
# print self.ms_per_tick
for track in midi_file:
self.song_buffer.add_track(track)
示例14: get_tracknames
def get_tracknames():
filenames = []
count = 0
for root, subFolders, files in os.walk('.'):
count +=1
if count == 9:
break
files = [os.path.join(root, filename) for filename in files if filename.lower().endswith(".mid")]
filenames += files
filenames = filenames[:100]
tracknames = []
for filename in filenames:
print "processing", filename
try:
pattern = midi.read_midifile(filename)
current_tracknames = midiutil.get_track_names(pattern)
current_tracknames = [trackname.rstrip().lstrip().lower().encode('ascii', 'ignore') for trackname in current_tracknames]
#current_tracknames = [trackname for trackname in tracknames if trackname != ""]
tracknames += current_tracknames
except:
print " bad file", filename
tracknames = [' '.join(ch for ch in t if ch.isalnum()) for t in tracknames if t != '']
print tracknames
return tracknames
示例15: get_notes
def get_notes():
# downloaded from http://www.piano-midi.de/muss.htm
filename = 'muss_1.mid'
m = midi.read_midifile(filename)
m.make_ticks_abs()
tick = 120.0
notes = np.array([(n.pitch, int(round(n.tick / tick)))
for n in m[1]
if type(n) == midi.events.NoteOnEvent
and n.velocity > 0
and n.pitch > 0])
note_map = collections.defaultdict(list)
max_pitch = 0
min_pitch = 127
for pitch, t in notes:
note_map[t].append(pitch)
if pitch > max_pitch:
max_pitch = pitch
elif pitch < min_pitch:
min_pitch = pitch
max_time = max(note_map.keys())
output_units = max_pitch - min_pitch + 1
output = np.zeros((max_time, output_units))
for t in range(max_time):
if t in note_map:
for i in note_map[t]:
output[t, i - min_pitch] = 1
return output, min_pitch