Fill a 4 char matrix with noise, what is the Fastest way using JAVA ?

djlbe's icon

Hi !

Since few days, I do my first steps with mxj and jitter, I'm searching about the fastest way for fill matrix with content. Do know a faster way that the one I'm using below? I think that using the method setcell2d is not a good idea...

import com.cycling74.max.*;
import com.cycling74.jitter.*;
import java.util.Random;

public class max_t extends MaxObject
{

JitterMatrix outTest;

Random rand;
int[] argb ;
int a ;

public max_t(String context) {
declareIO(1, 1); // declare 1 inlets, 1 outlet
outTest = new JitterMatrix(4,"Char", 1280, 720);
argb = new int[4];
}

public void bang() {
    for(int i = 0 ; i < 1280; i++) {
        for(int j = 0 ; j < 720; j++) {
            argb[0] = 255;
            argb[1] = (int) (Math.random() * 255);
            argb[2] = (int) (Math.random() * 255);
            argb[3] = (int) (Math.random() * 255);
            outTest.setcell2d(i,j, argb);
        }
    }
    outlet(0, "jit_matrix", outTest.getName());
}
}

Jesse's icon

You can use most Jitter objects within Java. In this case you would simply use jit.noise.

import com.cycling74.jitter.JitterMatrix;
import com.cycling74.jitter.JitterObject;
import com.cycling74.max.MaxObject;

public class NoiseTester extends MaxObject
{
    private JitterMatrix mx;
    private JitterObject noise;

    public NoiseTester()
    {
        mx = new JitterMatrix( 4, "char", 1280, 720 );
        noise = new JitterObject( "jit.noise" );
    }

    public void bang()
    {
        noise.matrixcalc( mx, mx );
        outlet( 0, "jit_matrix", mx.getName() );
    }
}

djlbe's icon

sorry to be unprecise (again). in fact I was more interested to find a "quick pixel per pixel" method. I found a method with array but I read that int java is about 4 bytes, right? so if a range value of 0-255 is sufficient in the case of a char matrice, it could be 4 times quicker using an array with a type data using 1 bytes only no ? but I don't know if this is possible to do

So using array I found this :

import com.cycling74.max.*;
import com.cycling74.jitter.*;
import java.util.Random;

public class max_t extends MaxObject
{

JitterMatrix outTest;

Random rand;

int w = 1280;
int h = 720;
int[] argb = new int[w*h*4];

public max_t(String context) {
declareIO(1, 2); // declare 1 inlets, 1 outlet
outTest = new JitterMatrix(4,"Char", w, h);

}

public void bang() {

    for(int i = 0 ; i < w*h; i++) {
        argb[i*4] = 255;
        argb[i*4+1] = (int) (Math.random() * 255);
        argb[i*4+2] = (int) (Math.random() * 255);
        argb[i*4+3] = (int) (Math.random() * 255);
    }

    outlet(1, argb.length);
    outTest.copyArrayToMatrix(argb);
    outlet(0, "jit_matrix", outTest.getName());
}
}