/usr/share/pyshared/pyknon/genmidi.py is in python-pyknon 1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | from pyknon.MidiFile import MIDIFile
from pyknon.music import Note, NoteSeq
class MidiError(Exception):
pass
class Midi(object):
def __init__(self, number_tracks=1, tempo=60, instrument=0):
"""
instrument: can be an integer or a list
"""
self.number_tracks = number_tracks
self.midi_data = MIDIFile(number_tracks)
for track in range(number_tracks):
self.midi_data.addTrackName(track, 0, "Track {0}".format(track))
self.midi_data.addTempo(track, 0, tempo)
instr = instrument[track] if isinstance(instrument, list) else instrument
self.midi_data.addProgramChange(track, 0, 0, instr)
def seq_chords(self, seqlist, track=0, time=0):
if track + 1 > self.number_tracks:
raise MidiError("You are trying to use more tracks than we have.")
for seq in seqlist:
if isinstance(seq, NoteSeq):
volume = seq[0].volume
dur = seq[0].midi_dur
for note in seq:
self.midi_data.addNote(track, 0, note.midi_number, time, dur, volume)
time += dur
else:
raise MidiError("The input should be a list of NoteSeq but yours is a {0}: {1}".format(type(seqlist), seqlist))
return time
def seq_notes(self, noteseq, track=0, time=0):
if track + 1 > self.number_tracks:
raise MidiError("You are trying to use more tracks than we have.")
for note in noteseq:
if isinstance(note, Note):
self.midi_data.addNote(track, 0, note.midi_number, time, note.midi_dur, note.volume)
else:
# we ignore the rests
pass
time += note.midi_dur
return time
def write(self, filename):
if isinstance(filename, str):
with open(filename, 'wb') as midifile:
self.midi_data.writeFile(midifile)
else:
self.midi_data.writeFile(filename)
|