Skip to content

mxm, IT's mad science

Sections
Personal tools
You are here: Home » Products » Open Source » Python Midi Package » Maybe this help you ...
Downloads
You can download mxm products here.

Due to it's technical and international nature, this section is in english.

Max M Has a blog too.

og er glad for mad

 

Comment

Above in this comment thread: Python Midi Package » Notes !

Maybe this help you ...

Posted by Anonymous User at 2004-12-28 11:38 AM
I'm developing a program that uses the Midi module.
I've defined some helper functions for managing midi notes and hex numbers.
Try this:

def note2hex(note):
"""'note': is a string: 'f#' or a tuple or list: ('f#', -1),
the number indicates the octave (0 -> middle 'C')
If there is not number ('note' is a string), we assume
middle C"""
_note = ()
if type(note) == types.StringType:
_note = (note, 0)
else:
_note = note
n = _note[0].lower()
if n == 'c':
nh = 0x3C
elif n == 'c#':
nh = 0x3D
elif n == 'db':
nh = 0x3D
elif n == 'd':
nh = 0x3E
elif n == 'd#':
nh = 0x3F
elif n == 'eb':
nh = 0x3F
elif n == 'e':
nh = 0x40
elif n == 'f':
nh = 0x41
elif n == 'f#':
nh = 0x42
elif n == 'gb':
nh = 0x42
elif n == 'g':
nh = 0x43
elif n == 'g#':
nh = 0x44
elif n == 'ab':
nh = 0x44
elif n == 'a':
nh = 0x45
elif n == 'a#':
nh = 0x46
elif n == 'bb':
nh = 0x46
elif n == 'b':
nh = 0x47
return nh + _note[1]*0x0C # 0x0C -> 12 Semitones

Ops ...

Posted by Anonymous User at 2004-12-28 11:41 AM
Sorry, the indetation is missing ...
I'll try a 'pre' tag ...
<pre>
def note2hex(note):
"""'note': is a string: 'f#' or a tuple or list: ('f#', -1),
the number indicates the octave (0 -> middle 'C')
If there is not number ('note' is a string), we assume
middle C"""
_note = ()
if type(note) == types.StringType:
_note = (note, 0)
else:
_note = note
n = _note[0].lower()
if n == 'c':
nh = 0x3C
elif n == 'c#':
nh = 0x3D
# ... more elif ....
return nh + _note[1]*0x0C # 0x0C -> 12 Semitones
</pre>

How do I get the hex conversion code to work?

Posted by Anonymous User at 2006-03-21 06:55 PM
I attempted to test your code, and got this error message:
Traceback (most recent call last):
File "C:/Documents and Settings/Vincent/My Documents/dap/python/note2hex", line 48, in -toplevel-
note2hex('c#')
File "C:/Documents and Settings/Vincent/My Documents/dap/python/note2hex", line 7, in note2hex
if type(note) == types.StringType:
NameError: global name 'types' is not defined

Is "types" defined elsewhere in some other code? Maybe I'm missing something -- I'm new at Python.

I would like to generate MIDI scores using my own aleaotoric algorithms, and Python seems like the best bet to do this. However, I need to start with MIDI note numbers, I don't quite get the hex that Python Midi uses. Why is there a "0x" preceding everything?