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 byAnonymous Userat
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 byAnonymous Userat
2007-08-22 08:03 AM
Hi! First you have to import the types module, using:
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?
"""'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
Regards,
Sky