Jump to content

Problem with python script


Recommended Posts

I downloaded this midi pkg for python from here and I'm trying to implement it. I'm looping through a long binary string, converting it to hex in pieces and taking two hex digits, placing "0x" in front of them like the pkg wants and trying to pass them to the midi pkg:

if len(colour) is 4:
					midi.update_time(x)
					# debug
					print colour
					midi.note_on(channel=0, note=colour)
					x+=10
					print x
					print
					colour="0x"

 

I get this error:

< omar ~/midi > ./rabmidi.py 10000
0x5a
Traceback (most recent call last):
 File "./rabmidi.py", line 75, in ?
evaluate_colours()
 File "./rabmidi.py", line 62, in evaluate_colours
midi.note_on(channel=0, note=colour)
 File "/home/omar/midi/MidiOutFile.py", line 47, in note_on
slc = fromBytes([NOTE_ON + channel, note, velocity])
 File "/home/omar/midi/DataTypeConverters.py", line 143, in fromBytes
return pack('%sB' % len(value), *value)
struct.error: required argument is not an integer

 

I'm assuming that it is not recognizing colour as a variable and passing its value in ths instance:

midi.note_on(channel=0, note=colour)

 

I've tried note=colour[:] and seceral other things I cannot recall. How can I get my script to pass the value through the variable?

Link to comment
Share on other sites

Sounds like that midi function is expecting a number, and you're giving it a string. It's passing the value alright but it's of the wrong type ("not an integer").

Try converting it to a number using the function int(colour, 16) and see if that works. I've a feeling that you're converting your binary to hex unnecessarily, cos you're going to have to convert it back from hex into decimal anyway.

Try calling the function directly with a fixed number, like 12, or 119, and see if it then works. Then set a variable to a number and pass that, see if it still works. Step by step.

Link to comment
Share on other sites

I downloaded this midi pkg for python from here and I'm trying to implement it. I'm looping through a long binary string, converting it to hex in pieces and taking two hex digits, placing "0x" in front of them like the pkg wants and trying to pass them to the midi pkg:

if len(colour) is 4:
					midi.update_time(x)
					# debug
					print colour
					midi.note_on(channel=0, note=colour)
					x+=10
					print x
					print
					colour="0x"

 

put int() around colour before passing it.

 

as for turning it into a hex value, I can't see where you're doing that. that's done by passing an integer to hex() and will automatically put the 0x in front. So from binary to hex, you'd go,

 

something="101010101"

somethingbase10=int(something, 2)

somethingbase16=hex(somethingbase10)

print somethingbase16

0x155

 

I realise it's hardly direct, but python offers no inbuilt way to go from a binary string to a hex string.

 

James

Link to comment
Share on other sites

I changed it to iphitus' way without adding the 0x manually, but it still tells me it is not an integer. If I try to do int(whatever), it tells me "0x5a is an invalid literal for int" If, I put note=0x5a directly in the code, there is no error.

Link to comment
Share on other sites

I changed it to iphitus' way without adding the 0x manually, but it still tells me it is not an integer. If I try to do int(whatever), it tells me "0x5a is an invalid literal for int" If, I put note=0x5a directly in the code, there is no error.

 

where's the documentation for this function?

 

It wants an integer. Not a hex string, not a binary string. dont pass it after applying hex(), pass the number after applying int(). so you'd pass somethingbase10 in the above example.

Link to comment
Share on other sites

If I try to do int(whatever), it tells me "0x5a is an invalid literal for int" If, I put note=0x5a directly in the code, there is no error.

That makes perfect sense. If you just do int("0x5a") then it will give an error, because it can't parse "0x5a" as a decimal int. If you removed the "0x" and did int("5a") that will also fail for the same reason. If you did as I said, and do int("0x5a", 16) then it would work because you're telling it it's a hex string (base 16).

 

If you put 0x5a directly in the code, it's a numeric literal, not a string, so you don't need to do the explicit string parsing. And that shows that the variable's value is getting passed properly.

Link to comment
Share on other sites

I'm going to have to take this up with developers, apparently. All that int(colour,16) does is turn it back into a base10 integer, which I already had in an intermediate step. I still don't understand how I can pass 0x5a like this:

midi.note_on(channel=0, note=0x5a)

but not like this:

notes=0x5a

midi.note_on(channel=0, note=notes)

 

That makes no sense.

Link to comment
Share on other sites

I agree with you, that wouldn't make sense. But I don't think that's what you're doing. I think you're doing

notes="0x5a"
midi.note_on(channel=0, note=notes)

and that doesn't work because it's a string, not a number. I would be quite surprised if you get an error from your code as you wrote it there - what is the error message?

 

Either use int(colour,16) or better still use your integer from your intermediate step.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...