[PAID] Help to debug patch

I have an audio responsive visual patch which I have worked on and has been written in processing.

I am trying to debug this file as it keeps crashing on me with the error : ArrayIndexOutofBoundsException: 0.

I am having trouble fixing it and would like for someone with more experience to make the file work consistently and with no crashes, I really would like it ready to show for an Art school exhibition in a weeks time (19 Aug)

For the right person this really will be a quick job. Happy to negotiate a fee and can pay through paypal as soon as it is solved.

File uploaded here -

https://workupload.com/file/UqQgqv7

PM me if your interested, thanks Jay

Comments

  • Please post file/tab name and line number

  • Hi Chrisir Thanks for looking..

    It has something to do with the audio in and when it reaches a high peak, I think the voronoi draw regions are struggling to redraw the shapes. I will attach an image to show you exactly where this happens and you can test with noise yourself.

    There are key presses that I have been working with to try and crash the patch, if you press V and P you will have the image that I am expecting to work with in the installation. if you press A it will crash also, this is a keypress I have been playing with to intentionally crash the patch.

    images heres first exception 0 created with audio

    Audio at a peak creates this exception

    Second exception300 when pressing keypress 'a'

    Key press a creates this exception

  • if you press A it will crash also

    this is because 'a' adds a vehicle but not the points. so vehicles.size() here is 301 but you only have 300 points (numbered 0 to 299)

      for (int i=0; i<vehicles.size(); i++) {
        points[i][0]= vehicles.get(i).location.x;
        points[i][1]= vehicles.get(i).location.y;
      }
    

    you are doing a lot in draw() that i wouldn't do in draw(). like instantiating a new voronoi every frame when it doesn't change.

  • the top error looks like a library error. it happens here when doing pretty standard things.

    i'd try another voronoi library. toxiclibs or hemesh perhaps.

  • edited August 2017

    I downloaded your code and it ran, no problems. Did you make any changes recently?

    ****EDIT: I get the errors with r or a...

    Kf

  • The problem is that you have points defined based on nrParticles, but this variable is never updated when you add or remove vehicles. Notice that the 2D array of points also depends on this variable. A quick solution is this below. I am presenting the code of only the main file.

    It is a hacked solution and it works.

    Kf

    /*
    Code for Reduction/Reflection's audioreactive visuals.
     Made by Rodrigo Carvalho / Visiophone (2016)
     www.visiophone-lab.com
    
     Code build over the "Flow Field" example by
     Daniel Shiffman on "The Nature of Code"
     http://natureofcode.com
    
     Needs Mesh library by Lee Byron http://leebyron.com/mesh/
     Works on Processing 3.0.1
     */
    
     import megamu.mesh.*;
    
    
    // Menu GUI. Bolleans to change visualizations
    boolean debug = false;
    boolean view = true;
    boolean info=true;
    boolean voronoi=false;
    boolean lines=true;
    
    FlowField flowfield; // Flowfield object
    ArrayList<Vehicle> vehicles; // An ArrayList of vehicles
    int nrParticles = 30; // number of elements/particles
    //float[][] points = new float[nrParticles][2]; // Array for the VORONOI cells          //   #############   HACK
    
    
    
      void setup() {
    
    
    
    
      size(1280, 720, P3D);
      PJOGL.profile=1;
    
    
    
      // Resolution of the flowfield. nr of cells
      flowfield = new FlowField(50);
    
      // create the elements in a random position
      vehicles = new ArrayList<Vehicle>();
      for (int i = 0; i < nrParticles; i++) {
        vehicles.add(new Vehicle(new PVector(random(width), random(height)), random(2, 15), random(0.1, 1.5)));
      }
    
      noCursor(); // hide the mouse
    
      //// INICIATE VORONOY STUFF                       //   #############   HACK
      //for (int i=0; i<nrParticles; i++) {
      //  points[i][0]= random(800);
      //  points[i][1]= random(800);
      //}
    
      //Audiostuff      
      input = new AudioIn(this, 0);  //Create an Audio input and grab the 1st channel
      input.start();// start the Audio Input
    
      rms = new Amplitude(this); // create a new Amplitude analyze
      rms.input(input);  // Patch the input to an volume analyzer
      input.amp(1.0);
    
      smooth();
    
    
    
    }
    
    void draw() {
    
      //amplitude stuff
      float analise = map(rms.analyze(), 0, 0.5, 0.0, 50.0);
      audioIn+= (analise-audioIn)*0.01; //smoothing the audioIn vall
    
      background(0);
    
      flowfield.update(); // Flowfield update and display 
      if (debug) flowfield.display(); // If debug mode True, display flowfield 
    
      // Tell all the vehicles to follow the flow field
      for (Vehicle v : vehicles) {
        v.follow(flowfield);
        v.run();
      }
    
      // DRAWING VORONOI
      nrParticles=vehicles.size();                                                   //   #############   HACK
      int nrVoronois=int(map(720, 0, height, 0, nrParticles));    
    
      float[][] points = new float[nrParticles][2]; // Array for the VORONOI cells   //   #############   HACK
    
      //GETTING VEHICLES POSITION TO VORONOI'S POINTS
      for (int i=0; i<vehicles.size(); i++) {   
        points[i][0]= vehicles.get(i).location.x;
        points[i][1]= vehicles.get(i).location.y;
      }
    
      Voronoi myVoronoi = new Voronoi(points);
      MPolygon[] myRegions = myVoronoi.getRegions();
    
      for (int i=0; i<nrVoronois; i++)
      {
        // an array of points
       float[][] regionCoordinates = myRegions[i].getCoords();
    
       fill(int(map(i*255.0, 147, i*255.0/nrParticles, 130 * (i % 2), 255)));
    
         //fill(int(map(i*255.0/nrParticles, 130 * (i % 2), 225 * (i % 2),0,0)));
    
    
    
    
    //fill(255,int(map(sum[i],0,10,255,0)));  // dar valor do FFT ao interior do voronoi
        if (voronoi) myRegions[i].draw(this); // draw this shape
      }
    
      float[][] myEdges = myVoronoi.getEdges();
    
      for (int i=0; i<myEdges.length; i++)
    
      {
        float startX = myEdges[i][0];
        float startY = myEdges[i][1];
        float endX = myEdges[i][2];
        float endY = myEdges[i][3];
        stroke(255);
        if (lines) line( startX, startY, endX, endY );
    
    
    }
    
    
    
    
    // Menu GUI
      //if (info) {
        // Instructions
       // fill(0, 220);
       // stroke(180);
       // rect(10, 10, 220, 150);
       // fill(255);
       // text("FPS: "+frameRate, 20, 30);
       // text("VIEW INFO ('i'): "+info, 20, 45);
       // text("VIEW FLOWFIELD ('SPACE'): "+debug, 20, 60);
       // text("VIEW PARTICLE ('p'): "+view, 20, 75);
       // text("VIEW VORONOI REGIONS ('v'):" +voronoi, 20, 90);
       // text("VIEW VORONOI LINES ('l'):" +lines, 20, 105);
       // text("NR ELEMENTS :"+nrParticles, 20, 135);
     // }
    }
    
    // Keyboard Interaction
    void keyPressed() {
      if (key == ' ') {
        debug = !debug;
      }
      if (key=='i') {  
        info=!info;
      }
      if (key=='p') {  
        view=!view;
      }
      if (key=='v') {  
        voronoi=!voronoi;
      }
      if (key=='l') {  
        lines=!lines;
      }
      if (key=='r') {
        vehicles.remove(0);
        println(vehicles.size());
      }
      if (key=='a') {   
        vehicles.add(new Vehicle(new PVector(random(width), 0-3), random(2, 5), random(0.1, 0.5)));
        println(vehicles.size());
      }
    
    }
    
  • There's another problem when you play music into the sketch (I had to add an mp3 to get it to work, I couldn't run both this and an mp3 player at the same time, said the device was busy)

    I caught the exception and printed out the data. it didn't look obviously wrong but it was enough for the voronoi creation to fail. Sometimes it took 20 seconds, sometimes 2...

  • (the if lines condition in line 130 should go around that entire loop. Why ask 300 times if we should be drawing the lines when the answer is the same for every iteration?)

  • edited August 2017

    try this. these are the points[] captured when the original program throws the error, the ones passed to new Voronoi()

    import megamu.mesh.*;
    
    float[][] p = {
      {191.93379, 435.18066}, 
      {1241.7128, 619.70905}, 
      {177.51233, 509.0964}, 
      {177.67902, 370.82825}, 
      {401.1175, 86.39508}, 
      {178.66551, 385.69055}, 
      {181.47653, 370.912}, 
      {44.008057, 623.1406}, 
      {1180.2504, 668.55426}, 
      {236.70093, 360.5268}, 
      {162.71474, 364.4124}, 
      {229.06793, 630.7983}, 
      {1160.9191, 175.0325}, 
      {187.0976, 89.7339}, 
      {40.299427, 333.26407}, 
      {174.55824, 300.72113}, 
      {42.245964, 613.4528}, 
      {1052.59, 390.12827}, 
      {404.60626, 114.41666}, 
      {158.42752, 364.665}, 
      {273.4321, 462.83667}, 
      {399.74722, 97.53956}, 
      {176.85892, 387.83145}, 
      {173.72653, 370.5423}, 
      {179.66408, 380.35544}, 
      {90.35887, 358.0335}, 
      {130.071, 637.1306}, 
      {181.72603, 395.55676}, 
      {179.57057, 370.73193}, 
      {362.99207, 94.122475}, 
      {188.29723, 384.47955}, 
      {233.56076, 369.202}, 
      {1188.7804, 658.7921}, 
      {231.02158, 368.96268}, 
      {242.31448, 386.28854}, 
      {1175.8337, 700.81696}, 
      {42.165783, 600.7142}, 
      {71.06779, 339.72427}, 
      {160.34166, 366.81494}, 
      {17.90739, 622.1003}, 
      {150.99681, 356.31958}, 
      {165.09131, 366.96988}, 
      {1173.8998, 205.34198}, 
      {62.03436, 247.68027}, 
      {236.48889, 349.3746}, 
      {1182.8511, 141.18622}, 
      {86.5769, 371.7633}, 
      {239.35577, 363.10455}, 
      {869.04596, 281.40805}, 
      {374.9875, 89.552315}, 
      {1183.6893, 109.013695}, 
      {1219.1356, 671.28864}, 
      {1207.4469, 624.0905}, 
      {360.2813, 102.17502}, 
      {1224.5648, 627.7899}, 
      {1198.6199, 688.5524}, 
      {180.31859, 369.4852}, 
      {1230.1761, 78.205574}, 
      {186.92912, 455.5119}, 
      {181.55309, 370.31552}, 
      {184.35089, 378.71872}, 
      {94.29643, 281.01193}, 
      {1159.8225, 216.96965}, 
      {178.47241, 369.86877}, 
      {1214.7545, 687.62067}, 
      {182.00177, 372.34738}, 
      {1124.8839, 276.64294}, 
      {883.2351, 313.79904}, 
      {170.30211, 296.69083}, 
      {181.33409, 371.20825}, 
      {66.57278, 575.6429}, 
      {1211.5638, 639.5879}, 
      {21.431509, 625.8947}, 
      {178.30772, 506.7752}, 
      {169.89638, 364.46942}, 
      {847.1292, 284.36105}, 
      {36.768185, 282.5426}, 
      {872.4759, 299.85318}, 
      {169.75644, 367.04312}, 
      {1178.1482, 668.5927}, 
      {67.300064, 295.4078}, 
      {1229.3823, 578.9625}, 
      {376.84592, 115.634766}, 
      {1173.5181, 536.96857}, 
      {1179.1476, 659.9754}, 
      {56.898357, 333.5207}, 
      {1241.5369, 617.3819}, 
      {1189.1089, 151.5517}, 
      {383.8744, 131.09671}, 
      {1.9768496, 637.79095}, 
      {1194.4537, 77.58295}, 
      {403.16693, 83.88915}, 
      {181.24623, 371.07617}, 
      {1083.0854, 308.04883}, 
      {151.43144, 366.29755}, 
      {162.31361, 365.77316}, 
      {151.88045, 359.34543}, 
      {376.05432, 2.2901187}, 
      {1158.243, 146.46878}, 
      {167.73276, 366.10733}, 
      {91.750046, 580.8863}, 
      {1120.8242, 333.4651}, 
      {155.8181, 369.86404}, 
      {160.39688, 369.23944}, 
      {143.74318, 367.131}, 
      {142.78653, 364.8811}, 
      {1185.2637, 157.24527}, 
      {999.57935, 289.8064}, 
      {1104.9742, 692.0447}, 
      {153.34743, 353.81668}, 
      {140.69057, 356.29916}, 
      {191.17741, 392.60092}, 
      {146.22517, 365.26797}, 
      {240.59091, 366.39014}, 
      {153.31804, 366.18924}, 
      {176.05096, 497.49512}, 
      {158.25592, 369.1087}, 
      {10.313982, 637.763}, 
      {836.4176, 312.39444}, 
      {139.45581, 354.2587}, 
      {103.23995, 346.57343}, 
      {153.43591, 365.94434}, 
      {19.592686, 604.2923}, 
      {185.0998, 405.49802}, 
      {156.13591, 366.63727}, 
      {155.53386, 369.9598}, 
      {372.4315, 84.2176}, 
      {312.0059, 29.979189}, 
      {1191.2703, 121.52623}, 
      {1227.2473, 89.28938}, 
      {395.4077, -3.0}, 
      {68.011925, 338.69986}, 
      {50.908043, 108.13047}, 
      {1068.4083, 303.74777}, 
      {-0.62851834, 515.6365}, 
      {142.80171, 361.90747}, 
      {143.66685, 363.25952}, 
      {1165.4358, 86.14475}, 
      {309.2044, 433.21744}, 
      {1193.7312, 658.3104}, 
      {220.03116, 389.3614}, 
      {179.44557, 381.01743}, 
      {61.749065, 340.07404}, 
      {212.10303, 326.98764}, 
      {50.612534, 647.14374}, 
      {1158.1804, 571.3509}, 
      {172.47446, 366.51224}, 
      {1183.9713, 97.24215}, 
      {91.50062, 360.77872}, 
      {155.32886, 369.1468}, 
      {369.2694, 129.5706}, 
      {213.26608, 335.0971}, 
      {230.80194, 497.88184}, 
      {179.64377, 370.80026}, 
      {176.82822, 337.16083}, 
      {372.72644, 146.61385}, 
      {408.16138, 138.31656}, 
      {168.28731, 366.0545}, 
      {279.55246, 473.64786}, 
      {144.30194, 365.5302}, 
      {79.40015, 250.94075}, 
      {1181.2144, 224.92319}, 
      {1183.0956, 104.99542}, 
      {224.89458, 470.45947}, 
      {92.03057, 359.97162}, 
      {87.67493, 321.5049}, 
      {1243.5094, 49.12763}, 
      {1183.7668, 680.14014}, 
      {1257.4648, 653.07336}, 
      {1209.8474, 345.07306}, 
      {222.77943, 362.69574}, 
      {168.54999, 361.97916}, 
      {1183.2633, 702.6186}, 
      {1224.1425, 665.16}, 
      {407.4601, 152.48709}, 
      {1260.6786, 641.44006}, 
      {1212.7993, 641.3508}, 
      {156.61053, 368.95538}, 
      {216.72733, 349.9141}, 
      {826.0135, 290.24844}, 
      {179.44435, 369.8765}, 
      {157.99889, 368.68826}, 
      {183.55315, 297.5186}, 
      {34.049507, 620.17926}, 
      {150.69218, 364.8805}, 
      {1172.693, 106.457634}, 
      {45.195923, 294.14255}, 
      {1205.5836, 680.4883}, 
      {1172.3625, 174.85754}, 
      {1211.2896, 604.16046}, 
      {18.451033, 113.35943}, 
      {1212.5793, 679.9776}, 
      {22.621485, 241.78766}, 
      {10.917636, 314.86285}, 
      {195.40134, 390.73578}, 
      {1071.7499, 294.3752}, 
      {1162.4176, 705.7587}, 
      {175.32141, 491.0032}, 
      {306.48312, 8.1551}, 
      {51.20804, 580.1048}, 
      {254.14497, 569.03955}, 
      {40.694874, 641.827}, 
      {1162.3198, 560.9571}, 
      {179.67996, 366.33972}, 
      {1182.3524, 148.7341}, 
      {241.87808, 372.94226}, 
      {181.28653, 370.9724}, 
      {1199.667, 653.2284}, 
      {213.73724, 389.26364}, 
      {232.74573, 366.4817}, 
      {1191.9807, 621.7438}, 
      {324.3497, 146.56006}, 
      {1181.7496, 134.89792}, 
      {18.332333, 392.7053}, 
      {19.472168, 400.36282}, 
      {141.72589, 363.63394}, 
      {372.66345, 140.20494}, 
      {386.98343, 138.5689}, 
      {440.51822, 146.61996}, 
      {1178.6543, 63.96396}, 
      {1169.3104, 521.4164}, 
      {347.79327, 80.195244}, 
      {1272.767, 659.54065}, 
      {219.50073, 389.74597}, 
      {435.61682, 120.91556}, 
      {1181.4626, 103.20233}, 
      {144.47562, 363.63052}, 
      {1178.4352, 161.90456}, 
      {184.4494, 376.14996}, 
      {98.81744, 362.1291}, 
      {1194.3738, 701.5582}, 
      {150.51607, 370.06802}, 
      {153.48433, 369.0687}, 
      {223.2814, 367.72742}, 
      {1163.447, 569.34784}, 
      {1205.773, 672.707}, 
      {199.73288, 316.01218}, 
      {164.11293, 367.122}, 
      {216.17834, 325.47495}, 
      {1179.436, 702.1569}, 
      {236.65628, 353.0312}, 
      {163.54134, 365.527}, 
      {179.26785, 367.80307}, 
      {173.71394, 370.8542}, 
      {180.34056, 367.04822}, 
      {1191.501, 37.173973}, 
      {1214.625, 658.18365}, 
      {154.16667, 369.72504}, 
      {142.02779, 367.10254}, 
      {1178.3419, 714.33136}, 
      {7.4084263, 385.80496}, 
      {1168.1023, 164.76468}, 
      {262.50406, 687.38666}, 
      {219.07034, 393.16867}, 
      {165.87253, 361.3687}, 
      {32.480732, 384.93665},
      {237.05453, 363.37415}, 
      {56.236267, 661.87823}, 
      {1257.8517, 587.07306}, 
      {410.79132, 156.8459}, 
      {1173.1389, 25.970352}, 
      {54.357517, 342.21317}, 
      {91.33176, 235.26852}, 
      {306.53238, 79.33073}, 
      {83.46039, 267.55606}, 
      {1162.1661, 164.02109},
      {1183.4717, 183.24313}, 
      {130.82861, 603.20605}, 
      {85.712, 641.4358}, 
      {145.14261, 368.81897}, 
      {119.72875, 361.95624}, 
      {1227.4496, 615.7296}, 
      {192.62921, 421.70334}, 
      {159.80641, 367.42487}, 
      {1219.7844, 101.15862}, 
      {385.17612, 122.605545},
      {1171.12, 712.4879}, 
      {180.63629, 365.8647}, 
      {370.065, 113.18054}, 
      {142.8004, 361.91183}, // this point
      {339.44095, 84.780045},
      {91.5462, 359.38644}, 
      {149.00684, 366.73874}, 
      {6.0646667, 642.13336}, 
      {18.474623, 625.2595}, 
      {200.7802, 348.9741}, 
      {1215.846, 644.9719}, 
      {-3.0, 654.0176}, 
      {155.85971, 369.4452}, 
      {144.99048, 369.78015}, 
      {872.38794, 301.79123}, 
      {166.16862, 365.57242}, 
      {344.29056, 106.04744}, 
      {1200.9944, 87.744705}, 
      {53.71205, 638.9313}, 
      {840.4274, 323.94586}, 
      {208.7332, 149.68369}, 
      {220.11244, 352.9988}, 
      {179.8362, 370.93082}, 
      {1093.7501, 293.51672}, 
    };
    
    Voronoi v = new Voronoi(p);
    

    i get an error running it like that, ArrayIndexOutOfBounds[0] within the library, same as the original program

    java.lang.ArrayIndexOutOfBoundsException: 0
        at megamu.mesh.MPolygon.add(MPolygon.java:20)
        at megamu.mesh.Voronoi.<init>(Voronoi.java:146)
    

    if i comment out the marked line (line 283) then it is ok, no error... what's special about that line? don't know.

    it's quite close to this other point

    {142.8004, 361.91183}, // problem point
    {142.80171, 361.90747},
    

    but i'm not sure if that matters

  • if i leave the problem point in and comment out the other point then that also works. so it might be the proximity.

  • ok, it's not that simple - other data sets with values just as close, or closer, render perfectly fine.

  • import megamu.mesh.*;
    
    float[][] p = {
      {142.78653, 364.8811}, 
      {142.80171, 361.90747},
      {142.8004, 361.91183},
      {141.72589, 363.63394},
    };
    
    Voronoi v = new Voronoi(p);
    

    comment out any of those 4 points and it's fine but all 4 together gives the same error...

  • problem is in MPolygon

    public MPolygon(int points){
        coords = new float[points][2];
        count = 0;
    }
    
    public void add(float x, float y){
        coords[count][0] = x;
        coords[count++][1] = y;
    }
    

    is occasionally calls this with points = 0 creating a [0][2] array and setting count to 0. when it tries to add a point to this coords[0][0] doesn't exist.

    a fix is this

    public void add(float x, float y){
      if (count < coords.length) { // added a check
        coords[count][0] = x;
        coords[count++][1] = y;
      }
    }
    

    but i haven't figured out yet why it creates a MPolygon with 0 size and then adds points to it (which points? from where)

    ah, digging a bit further, i think he's using the wrong size when initialising the regions array near the bottom of the Voronoi class.

      //regions[i] = new MPolygon(pointBuckets[i].length); // wrong?
      regions[i] = new MPolygon(faceOrder.length);
    
  • i've raised an issue on github but the repo hasn't been modified in 2 years so we'll see

    https://github.com/leebyron/mesh/issues/2

    i could fork the library and add a patch. but ime that gets messy...

  • I really need to thank you all for having a look at this patch, it's a real lesson picking up on all your experience trying to understand this problem.

    I have tried to change the code to work with your revised examples, it still appears to be crashing out on me with the same exception. Possibly to do with me incorrectly placing this new code in the runtime?

    Koogs you sound real close to a workable version, Is it stable enough to run? It's important to me this. I've got it as close to how I want it as I can a achieve on my own. With a deadline of next weekend, if you're close to having it running well I would be happy to pay you something for your efforts.

  • The hacky way of handling this is to copy the Java files from the library to your project and fiddle with the imports / package names. I got my broken test above to run like this.

    The better way would be to fork the github repo and fix the fork. I can probably do this tomorrow with some luck. (Or you can fork it and I'll add a fix to your repo)

    No payment necessary.

  • there's a new mesh.jar here:

    https://github.com/acdean/mesh/tree/master/library

    which contains my fix. it's compiled with java 1.8 and against Processing 3.3.4. hope that's ok.

    I HAVEN'T FIXED YOUR CODE, just the library. so the 'a' and 'r' bugs still exist. Kf has supplied fixes for those.

  • i'd try another voronoi library. toxiclibs or hemesh perhaps.

    (i tried toxiclibs and it's stable but much slower for 300 points (there didn't seem to be an easy way of drawing just the edges, only the regions so every line was probably being drawn twice). the bigger problem is that the order of the regions isn't consistent from one frame to the next so using your colouring scheme you get terrible flashing.)

    ((and hemesh is for 3d as far as i can tell. stick with the mesh.jar))

  • Thanks koogs, mesh library all updated with your fix.

    Hi kfrager, I have followed your revision/hack and not really sure why I am still having exceptions out of bounds 0 .. Is there something I am missing? Did you manage to get the patch stable with no exceptions? (I have tried this on both 3.01 and provessing 3.3.5).

  • What line are you getting the error at? I believe I have to commented out a couple of lines in setup since I moved everything to draw()... from what I remember. Notice that my fix was more of a hack and you will need to arrange it to optimize it a bit... although I doubt it will make any measurable improvement.

    Kf

  • Right now its running ok, I think the exceptions out of bounds (at the same line as the original error) have something to do with increasing the number of particles at line 26. I can see how the patch will struggle with a higher number

    int nrParticles = 80; // number of elements/particles

    When I push this to 100 + it struggles. I guess that is to be expected, is there any way to improve on this?

  • Post the code, the main class will suffice, but describing changes is nowhere near as good as posting them.

    The 0 error was the library one... Are you sure it updated? You might need to close processing, copy the jar over then reopen. (Exact errors and line numbers also v helpful)

  • Thanks koogs. I think you were right. I am monitoring the patch now and all seems okay, increasing the input.amp slightly higher and the number of elements to where it stays stable, so far so good..

Sign In or Register to comment.