Problem with writing a matrix to jxf File and reading it back to Jitter

Connector's icon

I still did not get an answer to my question so i try to edit my original post for better understanding and reproducing my Problem to hopefully some hints to my issue:

I am writing an jitter external for Max 9.0.8 with max-sdk-8.2.0:

max.testexternal.c.txt
txt 1.61 KB
testexternal.c.txt
txt 5.39 KB

Inside the external i am filling a 2x2x3 matrix and storing the matrix to brightnessTest.jxf in the Filesystem. After reading back into a matrix object i want to get the stored Values back in a jitter Patch.

Testexternal.maxpat
Max Patch

While storing the values to the matrix with this code:

for (x->currentframe = 0; x->currentframe < 3; x->currentframe++) {
    for (i = 0; i < x->nrOfCells; i++) {
        yCellPos = i / x->cols;
        xCellPos = i % x->cols;

        brightness = 25. * i;
        post("i: %ld", i);
        post("x->currentframe: %ld, xPos: %ld, yPos: %ld", x->currentframe, xCellPos, yCellPos);
        post("brightness: %f", brightness);

        brightnessp = brightness_bp  
            + xCellPos * sizeof(float) 
            + yCellPos * sizeof(float) * (&brightness_minfo)->dim[0]
            + x->currentframe * sizeof(float) * (&brightness_minfo)->dim[0] * (&brightness_minfo)->dim[1];
        *brightnessp = brightness;

    }
}

The output to console looks fine (frames 0,1,2 are filled with values):

storing values to 2x2x3 matrix inside c-external

But when i try to read back the matrix values in the patch

it seems that only the first frame (frames 0) was read back correctly. At the second frame (frames 1) i only get 0. Values and the third frame (frames 2) i get no Values.

reading values from matrix

Hope anyone could give me a hint to solve my problem.

Connector's icon

I have edited my original post from Aug 11 for better understanding my Problem. Hope that anyone can help me.

Connector's icon

I still have problems with filling a jitter matrix inside an Max c-external with float32 values.

For testing reasons i have tried to use different matrix dimensions:

  1. dimcount = 3, dim[0] = 2, dim[1] = 2, dim[2] = 3, planecount = 1, type = float32

  2. dimcount = 3, dim[0] = 3, dim[1] = 2, dim[2] = 3, planecount = 1, type = float32

  3. dimcount = 3, dim[0] = 4, dim[1] = 2, dim[2] = 3, planecount = 1, type = float32

I am initializing my Matrix with this code:

void initMatrix(t_testmatrixset* x) {
    t_jit_matrix_info brightnessinfo;
    if (x->brightnessmatrix == NULL) {
        jit_matrix_info_default(&brightnessinfo);
    
        brightnessinfo.dimcount = 3;
        brightnessinfo.planecount = 1;
        brightnessinfo.dim[0] = x->cols; //2, 3, 4
        brightnessinfo.dim[1] = x->rows; //2
        brightnessinfo.dim[2] = x->frames; //3
        brightnessinfo.type = _jit_sym_float32; 
        x->brightnessmatrix = jit_object_new(_jit_sym_jit_matrix, &brightnessinfo);
        x->nrOfCells = x->rows * x->cols;
    }
}

After initializing i am setting a ascending distinct float value to the jitter matrix while iterating from cell to cell:

t_jit_err testmatrixset_fillmatrix(t_testmatrixset* x) {
    
    t_jit_matrix_info brightness_minfo;
    float* brightness_bp, * brightnessp, brightness;
    long i, yCellPos, xCellPos, index;
    long lock;
    float* fdata;

    initMatrix(x);
    x->currentframe = 0;
    index = 0;
   
    jit_object_method(x->brightnessmatrix, _jit_sym_getinfo, &brightness_minfo);
    lock = jit_object_method(x->brightnessmatrix, _jit_sym_lock, 1);
    jit_object_method(x->brightnessmatrix, _jit_sym_getdata, &brightness_bp);

    for (x->currentframe = 0; x->currentframe < x->frames; x->currentframe++) {
        for (yCellPos = 0; yCellPos < x->rows; yCellPos++) {
            for (xCellPos = 0; xCellPos < x->cols; xCellPos++) {
             

                brightness = (float) index;

                post("xPos: %ld, yPos: %ld, x->currentframe: %ld, brightness: %f" ,xCellPos, yCellPos, x->currentframe,brightness);
           
                long xOffset = xCellPos; 
                long yOffset = yCellPos * (&brightness_minfo)->dim[0];
                long frameOffset = x->currentframe * (&brightness_minfo)->dim[0] * (&brightness_minfo)->dim[1];
                long fullOffset = (xOffset + yOffset + frameOffset);

                brightnessp = brightness_bp + fullOffset;   
                
                post("xOffset: %ld, yOffset: %ld, frameOffset: %ld, fullOffset: %ld",xOffset, yOffset,frameOffset, fullOffset);
                post("dimstride[0]: %ld", (&brightness_minfo)->dimstride[0]);
                post("dimstride[1]: %ld", (&brightness_minfo)->dimstride[1]);
                post("dimstride[2]: %ld", (&brightness_minfo)->dimstride[2]);
                post("dim[0]: %ld", (&brightness_minfo)->dim[0]);
                post("dim[1]: %ld", (&brightness_minfo)->dim[1]);
                post("dim[2]: %ld", (&brightness_minfo)->dim[2]);
                post("planecount: %ld", (&brightness_minfo)->planecount);
                *brightnessp = brightness;
                index+=1;
            }
        }
    }
    jit_object_method(x->brightnessmatrix, _jit_sym_lock, lock);
    post("matrix filled!");
    return JIT_ERR_NONE;
}

For better understanding i have added some post functions with debug informations while setting each value in the matrix. For example these are some debug Informations from a test with an 3x2x3 Matrix:

Debug information from 3x2x3 matrix

When i print the matrix i get these values:

jit.print: 0.000,1.000,2.000

jit.print: 4.000,5.000,6.000

jit.print: <DIM 2>

jit.print: 8.000,9.000,10.000

jit.print: 12.000,13.000,14.000

jit.print: <DIM 2>

jit.print: 16.000,17.000,0.000

jit.print: 0.000,0.000,0.000

So as you can See in the matrix Values are missing some Values: 3.0, 7.0, 11. and 15

When i try the same thing with an 4x2x3 Matrix I get following debug Informations:

Debug information from 4x2x3 matrix

When i print the 4x2x3 matrix all expected Values are filled in correctly:

jit.print: 0.000,1.000,2.000,3.000

jit.print: 4.000,5.000,6.000,7.000

jit.print: <DIM 2>

jit.print: 8.000,9.000,10.000,11.000

jit.print: 12.000,13.000,14.000,15.000

jit.print: <DIM 2>

jit.print: 16.000,17.000,18.000,19.000

jit.print: 20.000,21.000,22.000,23.000

so anybody has an idea what could be the problem in the first 3x2x3 Matrix. I figured out that the dimstride values in all matrices are:

dimstride[0]: 4

dimstride[1]: 16

dimstride[2]: 32

i guess that could be the problem for setting float32 values correctly in my 3x2x3 matrix and i have some troubles with my 2x2x3 float Matrix too. So i hope someone can help me with this.

Rob Ramirez's icon

you need to use the info dimstrides and a base char pointer to properly calculate offsets. it should look something more like:

char* bp;
jit_object_method(x->brightnessmatrix, _jit_sym_getdata, &bp);
...
brightnessp = (float*)(bp  
    + xCellPos * info.dimstride[0] 
    + yCellPos * info.dimstride[1]
    + x->currentframe * info.dimstride[2]);
*brightnessp = brightness;

If this doesn't get you sorted, zip up your source file and I'll try and build them with the SDK.

Connector's icon

alright, Thanks for your reply!

i have tried to change my code with your suggestion but it's still not working. So i have prepared 3 Patches so that you can reproduce my problems. In Each Patch you can fill the matrix within the external by bang #1 and fill another matrix within the Patch through setcell messages through bang #2:

Example Patch for 4x2x3 Matrix

TestMatrixSet2x2x3.maxpat
Max Patch
TestMatrixSet3x2x3.maxpat
Max Patch
TestMatrixSet4x2x3.maxpat
Max Patch

It was not possible to attach the zip file directly in the Forum, so you can download it from DropBox: testexternalZip

Let me know if anything is unclear. Thanks!

Connector's icon

Hello Rob!

Have you already had the opportunity to view my patches and c external?

Best Regards C.

Connector's icon

ok. i have had a mistake in my zip File. I changed float* brightness_bp to char* brightness_bp as you suggested in your post. Here is the updated source: cexternalDropbox

but also when i use char pointer instead float i do not get the expected values in my matrix.

The matrix values wich are set in the external are:

matrix set in external

but i would expect:

expected matrix values
Rob Ramirez's icon

pretty sure this line is wrong due to C casting precedence rules:

brightnessp = (float*) brightness_bp + fullOffset;

and should be

brightnessp = (float*)(brightness_bp + fullOffset);

you want to apply the byte offset to the char pointer, then cast to a float pointer

Connector's icon

Alright! Thank you very much! Now it's working fine.

Best Regards

C.