We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › receiving an array from maxlink
Page Index Toggle Pages: 1
receiving an array from maxlink (Read 847 times)
receiving an array from maxlink
Jan 21st, 2007, 1:26am
 
Hi all,

I am playing with connection between max msp and processing. So far I can send single numbers from max and assign them to variables in processing, but how do I send an array of numbers from max into processing?

Here's what I tried to do:

import maxlink.*;

MaxLink link = new MaxLink(this, "message_board"); // get all messages from max link object - refer to object name
public float num1, num2, num3, num4, num5;

public int out1, out2, out3;
int posX, posY;

String oldMessage = "";
String message = "";
int messageCount = 0;
// array to contain numbers an array sent from max
float [] numbers = new float[3];


void setup() {
 size(300,300);
 background(20);
 // inlets from max to processing
 link.declareInlet("num1");
 link.declareInlet("num2");
 link.declareInlet("num3");
 link.declareInlet("num4");
 link.declareInlet("num5");
 // array from max
 num3 = numbers[]; // Here I would like the input from mac - inlet 3 is an array to be written into an array in processing
 
}

void draw() {
 drawMessage();

 stroke(250);
 line(20, 300, 20, num1);
 stroke(250);
 line(30, 300, 30, num2);
 stroke(250);
 line(40, 300, 40, num3);
 
 ellipseColor();
 mouseAction();
 arrayFromMax();
}



// this method must be public - if a message number is different than previous, then notice it!
public void setMessage(String newMessage) {
 messageCount++;
 link.output(messageCount);
 oldMessage = message;
 message = newMessage;
}

void drawMessage() {
 background(20);

 if (oldMessage != "") text("previous message: " + oldMessage, 37, 80);
}

// these two circles will be white if box is checked in max and grey otherwise
void ellipseColor() {
 if(num4==1) {
   fill(255);
   noStroke();
   ellipse(200, 30, 20, 20);
 } else {
   fill(100);
   noStroke();
   ellipse(200, 30, 20, 20);
 }

if(num5==1) {
   fill(255);
   noStroke();
   ellipse(200, 57, 20, 20);
 } else {
   fill(100);
   noStroke();
   ellipse(200, 57, 20, 20);
 }
 
}

// mouseClick and x/y coordinates will be sent to maxlink object in max
void mouseAction() {
 posX=mouseX;
 posY=mouseY;
 
 if(mousePressed) {
   out1=1;
 } else {
  out1=0;
 }
 link.output(1,out1);
 
 noStroke();
 fill(255);
 rect(posX, posY, 20, 20);
 //println("posX");
 out2=posX;
 out3=posY;
 link.output(2,out2);
 link.output(3,out3);
}

void arrayFromMax() {
 fill(255);
 line(numbers[0], 50, numbers[1], 50);
}
Re: receiving an array from maxlink
Reply #1 - Jan 22nd, 2007, 4:55am
 
Unfortunately, you can't do this directly. Here's an example of a way to hack around the limitation:

Code:

import maxlink.*;

MaxLink link;

int MAX_NUMBERS = 50;
float[] numbers = new float[MAX_NUMBERS];
float lastNum;
int idx = 0;

void setup() {
link = new MaxLink(this,"array_test");
link.declareInlet("lastNum","setLastNum");
link.declareMaxFunction("endArray");
}

public void setLastNum(float f) {
numbers[idx++] = f;
if (idx == numbers.length) endArray();
}

public void endArray() {
printArray(numbers,idx);
idx = 0;
}

void printArray(float[] array, int arrayLength) {
for (int i=0; i<arrayLength; i++)
print(array[i]+"\t");
print("\n");
}


Then, in Max, just keep sending floats into inlet 1, and then send the message "endArray" into inlet 1 when the list is done.
Page Index Toggle Pages: 1