473,513 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python Midi Package: writing events non-chronologically

tim
Someone using Python Midi Package from
http://www.mxm.dk/products/public/ lately?

I want to do the following :
write some note events in a midi file
then after doing that, put some controllers at the beginning of the
midifile
(because I want to be able to make those dependant on what notes were
just written)

def midctrls():
global roffset, melchan, roffset, gmt, timedisplay, out_file, midi,
usednotes, n
midi.reset_time() #seems to do nothing
for cha in range(16):
if cha==1:
midi.abs_time=0 #seems to do nothing
midi._relative_time = 0 #seems to do nothing, but I can
imagine why
midi._absolute_time = 0 #seems to do nothing
midi.update_time(new_time=0, relative=0) #although I give
the variable relative=0 it seems to be relative ?
midi.continuous_controller(cha, 0, 122)
(snip)

With this code I want a controller number 0 with value 122 to be written
at the beginning of my midifile.
It is written at the end, how I move the writing position to the beginning?

thanks for any help!
Nov 24 '05 #1
1 2092
tim wrote:
Someone using Python Midi Package from
http://www.mxm.dk/products/public/ lately?

I want to do the following :
write some note events in a midi file
then after doing that, put some controllers at the beginning of the
midifile
(because I want to be able to make those dependant on what notes were
just written)

def midctrls():
global roffset, melchan, roffset, gmt, timedisplay, out_file, midi,
usednotes, n
midi.reset_time() #seems to do nothing
for cha in range(16):
if cha==1:
midi.abs_time=0 #seems to do nothing
midi._relative_time = 0 #seems to do nothing, but I can
imagine why
midi._absolute_time = 0 #seems to do nothing
midi.update_time(new_time=0, relative=0) #although I give
the variable relative=0 it seems to be relative ?
midi.continuous_controller(cha, 0, 122)
(snip)

With this code I want a controller number 0 with value 122 to be written
at the beginning of my midifile.
It is written at the end, how I move the writing position to the beginning?

You should make a wrapper around the library that handles the midi
events in a data structure.

When your musical structure is then is complete, you use the midi
library to write it to disk.

I have attached a simple example here.

--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science

# event classes. Only used internally by notes2midi
class NoteOn:

def __init__(self, time=0, pitch=64, velocity=64):
self.time = time
self.pitch = pitch
self.velocity = velocity

def __str__(self):
r = []
a = r.append
a('NoteOn')
a('time %s' % self.time)
a('pitch %s' % self.pitch)
a('velocity %s' % self.velocity)
return '\n'.join(r)

def render(self, channel, mididevice):
"Writes the event to a midi stream"
mididevice.note_on(channel=channel, note=self.pitch, velocity=self.velocity)

class NoteOff:

def __init__(self, time=0, pitch=64, velocity=64):
self.time = time
self.pitch = pitch
self.velocity = velocity

def __str__(self):
r = []
a = r.append
a('NoteOff')
a('time %s' % self.time)
a('pitch %s' % self.pitch)
a('velocity %s' % self.velocity)
return '\n'.join(r)

def render(self, channel, mididevice):
"Writes the event to a midi stream"
mididevice.note_off(channel=channel, note=self.pitch, velocity=self.velocity)


def notes2midi(notes, midi):
"Turns a list of notes into a midi stream"
# notes have absolute time values!!!

# first turn notes into events (split into note-on / note-off)
events = []
for note in notes:
on_event = NoteOn(note.time, note.pitch, note.velocity)
events.append(on_event)
off_event = NoteOff(note.time+note.duration, note.pitch, note.velocity)
events.append(off_event)
# sort them in order of time
events.sort(lambda x,y: cmp(x.time, y.time))

# midi device writing
# non optional midi framework
midi.header()
midi.start_of_track()
midi.reset_time()
absolute_time = 0
for event in events:
delta_time = event.time - absolute_time
absolute_time = event.time
# write the data
midi.update_time(delta_time)
event.render(0, midi)
midi.end_of_track()
# non optional midi framework
midi.update_time(0)
midi.end_of_track()

midi.eof()
if __name__ == '__main__':

from Pattern import Note

# a short example using notes, with no limits to number of simultaneous voices
# creates a list of ascending notes
track = [
Note(time=24*i, pitch=64+i, velocity=127, duration=24)
for i in range(16)
]
##################
# write the notes to the file

from midi.MidiOutFile import MidiOutFile

out_file = 'note-test.mid'
midi = MidiOutFile(out_file)
notes2midi(track, midi)

Nov 24 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2097
by: Max M | last post by:
If anybody is interrested in Midi, but are not on the Python Midi list, I will just notify that I have released the first version of a high level midi package for Python. It is fully functional....
4
2679
by: sreekant | last post by:
Hi folks I hope someone here might be able to help. I scavenged some info and even code from the net and am trying to write a module that reads a text file and spits out a midi file. So far I...
0
1172
by: Paul Boddie | last post by:
QOTW: "Given these criteria, my recommendations for a first programming language would be Python or Scheme." - Peter Norvig (some time ago, but referenced on comp.lang.lisp this week)...
6
2739
by: =?ISO-8859-2?Q?Maciej_Blizi=F1ski?= | last post by:
For the last couple of days, I've been looking for a Python midi library. I'm generally interested in sending MIDI events via ALSA. It seems like everything out there is pretty old; packages are...
7
2414
by: Protocol | last post by:
Hello all Is Python suitable for building a multi-track midi sequencer (with a gui), that would run on windows / mac ? I fail to find sufficient information on this, being a newbie and all....
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5089
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.