Skip to content

mxm, IT's mad science

Sections
Personal tools
You are here: Home » Products » Open Source » Python Midi Package » How do I get the hex conversion code to work?
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 ...

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?

right code

Posted by Anonymous User at 2006-06-01 05:05 AM
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) == 'str':
_note = (note, 0)
n = _note[0].lower()
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

import types

Posted by Anonymous User at 2007-08-22 08:03 AM
Hi! First you have to import the types module, using:

import types

Regards,
Sky