[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-dev] reading an array from lang to C




On Dec 15, 2003, at 8:37 PM, Ronald J. Kuivila wrote:

Hi James,

 Here is some code that attempts to implement sysex output.
It appears that I am not successfully reading the Int8Array that is
the sysex packet prSendsysex.  What am I doing wrong?

void sendsysex(MIDIEndpointRef dest, int size, Byte* data);
void sendsysex(MIDIEndpointRef dest, int size, Byte* data)
{
    MIDISysexSendRequest mpktlist;
    MIDISysexSendRequest *pk = &mpktlist;
    pk -> destination = dest;
    pk -> data = data;
    pk -> bytesToSend = size;
    MIDISendSysex(pk);
}


int prSendSysex(VMGlobals *g, int numArgsPushed);
int prSendSysex(VMGlobals *g, int numArgsPushed)
{
    int err, uid, size;
    PyrInt8Array* packet = g->sp->uob;
    size = packet->size;
    Byte data[size];
    memcpy(data,packet->b, size);

You are allocating a dynamically sized array on the stack (this is new style code, only works in C99 and gcc) then copying the data into it (not sure why since you already have a buffer: the Int8Array itself) and passing that to MIDISendSysex. Since data is on the stack it will go away immediately even though MIDISendSysEx is asynchronous (returns immediately before data has been sent) and requires that data to persist for the duration sending the data.

Likewise MIDISysexSendRequest will need to persist for the duration of sending, but you allocate it on the stack and also do not zero it out or initialize all of its fields.



    PyrSlot *u = g->sp - 1;
    err = slotIntVal(u, &uid);
    if (err) return err;

    MIDIEndpointRef dest;
    MIDIObjectType mtype;
    MIDIObjectFindByUniqueID(uid, (MIDIObjectRef*)&dest, &mtype);
    if (mtype != kMIDIObjectType_Destination) return errFailed;
    if (!dest) return errFailed;

    sendsysex(dest, size, data);
    return errNone;
}


// in the lang:  +MIDIOut { sysex { arg uid, Int8Array; _SendSysex } }
_______________________________________________
sc-dev mailing list
sc-dev@xxxxxxxxxxxxxxx
http://www.create.ucsb.edu/mailman/listinfo/sc-dev


--
--- james mccartney   james@xxxxxxxxxxxxxx   <http://www.audiosynth.com>
SuperCollider - a real time audio synthesis programming language