communicate between two macs in processing

Hello world, I would like to know if it is possible to communicate between two macs in processing. What I mean:

Mac1 sends a signal to mac2 -> mac2 plays an audio file -> mac 2 waits for another signal sent by mac1 (loop)

In the code I have written I would love to send a signal to the second mac (mac2) at "int S5_MUSIC = 5;". So that there will be two sounds played at the same time. I need the second mac for a second pair of boxes, to create a surround sound....

This is my processing code:

import processing.serial.*;
import processing.sound.*;


int S1_WAITING_FOR_PEOPLE  = 1;
int S2_PEOPLE_ENTERED      = 2;
int S3_LIGHT_FADE_OUT      = 3;
int S4_DARK                = 4;
int S5_MUSIC               = 5;
int S6_LIGHT_FADE_IN       = 6;
// make sure this is the last!!!!
int TERMINATE              = 7;

int mode = S1_WAITING_FOR_PEOPLE;




int no_one_when_distance_greater_then = 160; // cm
int time_before_dim = 5000; // ms

SoundFile file;
boolean is_playing = false;

Serial myPort;  

int dist;

boolean someone_present = false;
int someone_present_since_time;


int music_playing_since;
int music_duration;

int in_mode_since_ms;

void setup() {
  size(640, 360);

  printArray(Serial.list()); // "/dev/cu.usbmodem1411" cu.usb!!!

  myPort = new Serial(this, Serial.list()[1], 9600);
  //myPort.readStringUntil('\n');
  myPort.bufferUntil('\n');

  // Load a soundfile from the data folder of the sketch and play it back in a loop
  file = new SoundFile(this, "Ex.wav");
  //file.loop();
  music_duration = (int) file.duration() * 1000;
  music_duration += 2000;

}      

void draw() {
  background(0);
  fill(255);
  text("frameCount: "+frameCount, 50, 25);
  text("dist: "+dist, 50, 50);
  text("mode: "+mode, 50, 75);


  if (frameCount == 180) {
    turn_on();
  } else if (frameCount > 180) {

    if ( mode == S1_WAITING_FOR_PEOPLE) {

      if (someone_present == false) {
        if (dist < no_one_when_distance_greater_then) {
          someone_present = true;
          someone_present_since_time = millis();
          change_mode(mode + 1);
        }
      }
    } else if (mode == S2_PEOPLE_ENTERED) {
      text("millis: "+millis(), 50, 100);
      text("someone_present_since_time: "+someone_present_since_time, 50, 125);
      text("time_before_dim: "+time_before_dim, 50, 150);
      if (millis() - someone_present_since_time > time_before_dim) {
        change_mode(mode + 1);
      }
    } else if (mode == S3_LIGHT_FADE_OUT) {
      turn_off();
      change_mode(mode + 1);
    } else if (mode == S4_DARK) 
    {
      change_mode(mode + 1);
    } else if (mode == S5_MUSIC) 
    {
      if (is_playing == false) {
        file.play();
        is_playing = true;
        music_playing_since = millis();
      }
      if (millis() - music_playing_since > music_duration) {
        is_playing = false;
        file.stop();
        change_mode(mode + 1);
      }
    } else if (mode == S6_LIGHT_FADE_IN) 
    {
      // todo
      if (millis() - in_mode_since_ms > 1000) {
        change_mode(mode + 1);
      }

    } else if (mode == TERMINATE) 
    {
      change_mode(S1_WAITING_FOR_PEOPLE);
      someone_present = false; // reset
      println("turn_on();");
      turn_on();
    }
  }
}


void change_mode(int m) {
  mode = m;
  in_mode_since_ms = millis();
}



void serialEvent(Serial p) { 
  String s = p.readString();
  if (s != null) {
    s = s.replace("\r\n", "");
    if (s.contains("Distance Measured")) {
      String[] tokens = split(s, "=");
      dist = int(tokens[1]);
    }
  }
} 


void turn_on() {
  myPort.write("on");
}

void turn_off() {
  myPort.write("off");
}

void keyPressed() {
  if (key == 'a') {
    turn_on();
  }
  if (key == 's') {
    turn_off();
  }
}

Thanks for your effort. Faffie

Answers

  • edit post, highlight code, press ctrl-o to format

  • Again thanks for the tip!

  • @Faffie -- it sounds like you want network communication.

    In addition to the network library, there are lots of options in the libraries list under 'data' and 'io' depending on what you are trying to do.

    Specifically, if your two macs are on the same local subnet, you could use OSC message passing. That is a common way of orchestrating multiple machines to react to one another -- OSC is popular in music / VJ-ing.

  • edited January 2018

    @jeremydouglass Thanks for your reply! This is actually what I was looking for! I really like the OSC! Have you ever worked with it? I have read through the programmers page, wiki, etc. but I am a beginner with coding so for me it is really hard to understand. Do you know some good tutorials? Or do you know how I can Use it? Big thanks!! F

  • @Faffie -- I'd suggest starting out by installing the library and trying to run some of the examples that come with it.

    • Processing > Tools > Add Tool > oscP5
    • Processing > File > Examples > Contributed Libraries > oscP5

    The easiest way is to begin by testing sending OSC messages between two programs on the same computer -- then move one of the programs to another computer and see if they can hear each other over the network. That helps isolate a common category of why-won't-it-work problems.

  • Great! Thank you!

Sign In or Register to comment.