themidibus icon indicating copy to clipboard operation
themidibus copied to clipboard

midibus throw an error

Open mrbbp opened this issue 1 year ago • 2 comments

sorry for this bad question

i use this code alone and it works fine.

import themidibus.*; //Import the library

MidiBus myBus; // The MidiBus

int nModuleL = 2;
int nModuleH = 2;
Boolean bbang= false;

void setup() {
  size(400, 400);
  background(250);
  // List all available Midi devices on STDOUT. This will show each device's index and name.
  MidiBus.list(); 
  //                   Parent In Out
  //                     |    |  |
  //myBus = new MidiBus(this, 0, 1);
  //Create a new MidiBus using the device index to select the Midi input and output devices respectively.
  myBus = new MidiBus(this, 1, 2);
  myBus.sendNoteOff(new Note(0,64,127));
}

void draw() {
}

void controllerChange(int channel, int number, int value) {
    switch(number) {
      case 36:
        nModuleL = int(map(value, 0, 127, 2, 16));
        println("nModuleL:",nModuleL);
        break;
      case 37:
        nModuleH = int(map(value, 0, 127, 2, 16));
        println("nModuleH:",nModuleH);
        break;
      default:
        println("non configuré");
    }
}

i tried to use this in a more complexe sketch and i received error from midibus who does not read the value on the device

The MidiBus Warning: Disabling controllerChange(int channel, int number, int value) because an unkown exception was thrown and caught
java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at themidibus.MidiBus.notifyParent(Unknown Source)
	at themidibus.MidiBus$MReceiver.send(Unknown Source)
	at java.desktop/com.sun.media.sound.AbstractMidiDevice$TransmitterList.sendMessage(AbstractMidiDevice.java:629)
	at java.desktop/com.sun.media.sound.MidiInDevice.callbackShortMessage(MidiInDevice.java:155)
	at java.desktop/com.sun.media.sound.MidiInDevice.nGetMessages(Native Method)
	at java.desktop/com.sun.media.sound.MidiInDevice.run(MidiInDevice.java:127)
	at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException: Cannot read field "x" because "this.buffer[0]" is null
	at sketch_230909a_trace_grille_6.drawGrille(sketch_230909a_trace_grille_6.java:172)
	at sketch_230909a_trace_grille_6.resetMotif(sketch_230909a_trace_grille_6.java:207)
	at sketch_230909a_trace_grille_6.controllerChange(sketch_230909a_trace_grille_6.java:236)
	... 11 more

i provide my sketch here (sorry for the mess)

/*
  v3 :  modif de l'interaction du dessin de la grille
  v4 :  ajout de la copie de l'image dans la grille
  v5 :  nouvel algo pour mélanger les marceaux d'image de la grille
        ajout de touches pour gerer la grille (A-Z horizontal et Q-W vertical)
        R pour remélanger la grille
        S pour sauvegarder
  v6 :  integration de midiBus
        
   TODO:
         Choisir le fichier de travail
*/
import themidibus.*; //Import the library

MidiBus myBus; // The MidiBus

import java.util.Collections;
// pour les morceaux de la grille
ArrayList<PVector> motif = new ArrayList<PVector>();

// nbr de Module en hauteur et largeur, largeur du module, hauteur du module
//int nModuleL,nModuleH;
int nModuleL = 2;
int nModuleH = 2;
float lModule,hModule;
// hauteur et largeur du rectangle dessiné
//int lCadre,hCadre;
int nClicks = 0;
Boolean first = true;
Boolean redraw = false;
Boolean bbang = true;

PVector [] buffer = new PVector[2];

// image à découper
PImage imageAD, imageF;

//compteur numero image
int nImg=0;

void setup() {
  size(600,600);
  //fullScreen();
  pixelDensity(2);
  rectMode(CORNERS);
  noFill();
  strokeWeight(1);
  
  MidiBus.list(); 
  //                   Parent In Out
  //                     |    |  |
  //myBus = new MidiBus(this, 0, 1);
  //Create a new MidiBus using the device index to select the Midi input and output devices respectively.
  myBus = new MidiBus(this, 1, 2);
  delay(100);
  //delay(4000);
  /* melange des morceaux de grille*/
  //for (int h = 0; h < nModuleH; h++) {
  //  for (int l = 0; l < nModuleL; l++) {
  //    motif.add(new PVector(l,h));
  //  }
  //}
  //Collections.shuffle(motif);
  /**/
  imageAD = loadImage("portrait.jpg");
  if (imageAD.width > imageAD.height) {
    imageAD.resize(width, 0);
  } else {
    imageAD.resize(0, height);
  }
  // creation de l'image finale
  imageF = createImage(imageAD.width,imageAD.height,RGB);
}

void draw() {
  if (bbang) {
    myBus.sendNoteOff(new Note(0,64,127));
    println("bang ");
    bbang = false;
  }
  background(225);
  
  image(imageAD,0,0);
  if (mousePressed) {
    //rect(buffer[0].x,buffer[0].y, mouseX, mouseY);
    stroke(255,0,0);
    drawGrille(null);
  } else if (buffer[0] != null) {
    stroke(255);
    drawGrille(buffer[1]);
  } else {
    // etat initial
    //background(255,255,0);
  }

}
void mousePressed() {
  buffer[0] = new PVector(pmouseX,pmouseY);
  loop();
  //nClicks ++;
}
void mouseReleased() {
  buffer[1] = new PVector(mouseX,mouseY);
  //redessine tout
  background(225);
  image(imageAD,0,0);
  stroke(255);
  drawGrille(buffer[1]);
  noLoop();
}
/* pour éviter de rafraichir l'image trop souvent, 
le rafraichissement de l'écran se fait au mouvement de la souris
*/
void mouseMoved() {
  //redraw();
}

void keyReleased() {
  if (key =='s' || key == 'S') {
    save("final-"+nf(nImg,3)+".png");
    nImg++;
    background(255);
  }
  if (key == 'R' || key == 'r') {
    Collections.shuffle(motif);
    redraw();
  }
  if (key == 'Z' || key == 'z') {
    if (nModuleL<= 8) {nModuleL++;}
    resetMotif();
    
  }
  if (key == 'A' || key == 'a') {
    if (nModuleL> 2) {nModuleL--;}
    resetMotif();
  }
  if (key == 'Q' || key == 'q') {
    if (nModuleH<= 8) {nModuleH++;}
    resetMotif();
    
  }
  if (key == 'W' || key == 'w') {
    if (nModuleH> 2) {nModuleH--;}
    resetMotif();
  }
}

void drawGrille(PVector end) {
  if (end == null) {
   buffer[1] = new PVector(mouseX, mouseY); 
  }
  if ((buffer[0].x != mouseX && buffer[0].y != mouseY)) {
    lModule = ceil((buffer[1].x - buffer[0].x)/nModuleL);
    hModule = ceil((buffer[1].y - buffer[0].y)/nModuleH);
    if (mousePressed) {
      //strokeWeight(2);
      for (float y= buffer[0].y; y<=buffer[1].y+6; y+= hModule) {
        line(int(buffer[0].x),int(y),int(buffer[1].x),int(y));
      }
      for (float x= buffer[0].x; x<=buffer[1].x+6; x+= lModule) {
        line(int(x),int(buffer[0].y),int(x),int(buffer[1].y));
      }
    }else  {
      for (int i = 0; i<motif.size();i++) {
        //println(int(motif.get(i).x),int(motif.get(i).y));
        int dx = round(buffer[0].x +(lModule*int(motif.get(i).x)));
        int dy = round(buffer[0].y + (hModule*int(motif.get(i).y)));
        int fx = round(buffer[0].x +(lModule*(i%nModuleL)));
        int fy = round(buffer[0].y + (hModule*int(i/nModuleL)));
        copy(imageAD,dx,dy,ceil(lModule),ceil(hModule),fx,fy,ceil(lModule),ceil(hModule));
        //copy(imageAD,round(buffer[0].x +(lModule*h.get(x))),round(buffer[0].y + (hModule*v.get(y))),round(lModule),round(hModule),
        // round(buffer[0].x +(lModule*x)),round(buffer[0].y + (hModule*y)),round(lModule),round(hModule));
      }
    }
  }
}
void resetMotif() {
  /* melange des morceaux de grille*/
  motif.clear();
  //motif = new ArrayList<PVector>();
  for (int h = 0; h < nModuleH; h++) {
    for (int l = 0; l < nModuleL; l++) {
      motif.add(new PVector(l,h));
    }
  }
  Collections.shuffle(motif);
  drawGrille(null);
  //println(nModuleL,motif.size(),motif);
}


void controllerChange(int channel, int number, int value) {
    switch(number) {
      case 36:
        nModuleL = int(map(value, 0, 127, 2, 16));
        println("nModuleL:",nModuleL);
        //resetMotif();
        break;
      case 37:
        nModuleH = int(map(value, 0, 127, 2, 16));
        println("nModuleH:",nModuleH);
        //resetMotif();
        break;
      default:
        println("non configuré");
    }
    resetMotif();
}

just need an image named portrait.jpgin the sketch folder

any help? thanks

mrbbp avatar Sep 15 '23 16:09 mrbbp

hello, well the midi box is not in fault. i change the box with an other one. Is it possible to halt the processing sketch until the midi device is initialized and send back init value (from the bang function) of the potentiometers used later in the sketch (nModuleH and nModuleL)?

mrbbp avatar Sep 24 '23 14:09 mrbbp

hello, i refactor the code and find my mystake (a null object) i received an error, it hang the prog and have to kill it.

The MidiBus Warning: Disabling controllerChange(int channel, int number, int value) because an unkown exception was thrown and caught
java.lang.reflect.InvocationTargetException
ArrayIndexOutOfBoundsException: Index 131072 out of bounds for length 131072
com.sun.jdi.VMDisconnectedException
	at jdk.jdi/com.sun.tools.jdi.TargetVM.waitForReply(TargetVM.java:310)
	at jdk.jdi/com.sun.tools.jdi.VirtualMachineImpl.waitForTargetReply(VirtualMachineImpl.java:1173)
	at jdk.jdi/com.sun.tools.jdi.PacketStream.waitForReply(PacketStream.java:87)
	at jdk.jdi/com.sun.tools.jdi.JDWP$ObjectReference$InvokeMethod.waitForReply(JDWP.java:4833)
	at jdk.jdi/com.sun.tools.jdi.ObjectReferenceImpl.invokeMethod(ObjectReferenceImpl.java:410)
	at processing.mode.java.runner.Runner.findException(Runner.java:852)
	at processing.mode.java.runner.Runner.reportException(Runner.java:798)
	at processing.mode.java.runner.Runner.exceptionEvent(Runner.java:725)
	at processing.mode.java.runner.Runner.lambda$generateTrace$1(Runner.java:626)
	at java.base/java.lang.Thread.run(Thread.java:833)
ArrayIndexOutOfBoundsException: Index 131072 out of bounds for length 131072

mrbbp avatar Sep 25 '23 07:09 mrbbp