Can't figure out how to do something using ctypes (and maybe struct?)
inhahe
inhahe at gmail.com
Fri Aug 10 18:40:11 EDT 2018
I need to make a list of instances of a Structure, then I need to make an
instance of another Structure, one of the fields of which needs to be an
arbitrary-length array of pointers to the instances in the list. How do I
do that?
Just in case it helps, I'll include what I tried that didn't work:
------
class NoteEvent(ctypes.Structure):
_fields_ = [('type', ctypes.c_int),
('byteSize', ctypes.c_int),
('deltaFrames', ctypes.c_int),
('flags', ctypes.c_int),
('noteLength', ctypes.c_int),
('noteOffset', ctypes.c_int),
('commandCode', ctypes.c_char),
('noteNumber', ctypes.c_char),
('velocity', ctypes.c_char)]
def mkVstEvents(events):
class Events(ctypes.Structure):
_fields_ = [('numEvents', ctypes.c_int),
('reserved', ctypes.c_int),
('eventspointerarray', ctypes.c_void_p * len(events))]
return Events(len(events), 0, tuple([ctypes.pointer(event) for event in
events]))
def mkNoteEvent(deltaFrames, flags, noteNumber, channel, velocity,
noteLength, noteOffset, detune, noteOffVelocity):
vstEvent = NoteEvent(type=1, bytesize=11, deltaFrames=deltaFrames,
flags=1, noteLength=0, noteOffset=0,
commandCode=chr(144+channel),
noteNumber=chr(noteNumber), velocity=chr(velocity))
return vstEvent
def onSendMidiButton(self, event):
plugins[0].plugin.process_events(ctypes.pointer(mkVstEvents([mkNoteEvent(deltaFrames=0,
flags=1, noteNumber=60, channel=0,
velocity=127, noteLength=0, noteOffset=0, detune=0,
noteOffVelocity=0)])))
------
Here's the error I got:
------
Traceback (most recent call last):
File "soundshop.9.1.2.py", line 216, in onSendMidiButton
noteOffVelocity=0)])))
File "soundshop.9.1.2.py", line 76, in mkVstEvents
return Events(len(events), 0, tuple([ctypes.pointer(event) for event in
events]))
RuntimeError: (c_void_p_Array_1) <type 'exceptions.TypeError'>:
incompatible types, LP_NoteEvent instance instead of c_void_p instance
------
I also tried to use struct.pack:
------
def mkVstEvents(events):
st = struct.pack("=ii"+"L"*len(events), len(events), 0,
*(ctypes.pointer(event) for event in events))
return st
-------
Here's the error I got:
------
Traceback (most recent call last):
File "soundshop.9.3.py", line 213, in onSendMidiButton
noteOffVelocity=0)])))
File "soundshop.9.3.py", line 72, in mkVstEvents
st = struct.pack("=ii"+"p"*len(events), len(events), 0,
*(ctypes.pointer(event) for event in events))
struct.error: argument for 'p' must be a string
------
I also tried replacing "p"*len(events) with "i"*len(events) and also with
"L"*len(events), both of which gave me errors.
Thanks for your help.
More information about the Python-list
mailing list