Generate music with Processing and Reason Propellerhead

Hello,

I am doing a project with aruidno and processing. And the effect that I want to achieve is like when you move your hands close to the device, it will start to play a music. And when you change the distance between your hands and the device, some drum like sound will change accordingly(with the music play at the same time).

Now I am using the arduino midishield to send realtime midi signal to Reason Propellerhead on my computer and using processing as an on-off switcher to control the music player.

But the problem is that is seems they can not work at the same time. When the Reason generate a sound the player is mute and when the player is playing the music the Reason seems have some problem..

Could you give me some advices on how to fix it? I really need your help because I am so confused and have no idea about what's going wrong with it.

Here are the code .

Code for arduino

#include "Ultrasonic.h"
#include <Wire.h>

#define THRESHOLD 30

#define note_MAX 127
#define note_MIN 0

long RangeInCentimeters_new;
long RangeInCentimeters_old;

Ultrasonic ultrasonic(7);
void setup()
{
  Serial.begin(31250);
}
void loop()
{

  String cmd;
  int note = 0;

  RangeInCentimeters_old = RangeInCentimeters_new;
  
  ultrasonic.MeasureInCentimeters();
  RangeInCentimeters_new = ultrasonic.RangeInCentimeters; 
  delay(250);

  note = map( RangeInCentimeters_old,0,40,40,127 );
 

  if(RangeInCentimeters_new<THRESHOLD) 
  {  
    if(RangeInCentimeters_old<THRESHOLD)
      return;    
    else
      cmd = "Music";    
  }
  else{
    if(RangeInCentimeters_old<THRESHOLD)
      cmd = "Stop";
    else
      return;  
  }
  sendmsg(cmd); 
  
}

void sendmsg(String msg)
{
  if(Serial.available() <= 0)
  {
    Serial.println(msg);
  }
}
void noteOn(int cmd, int pitch, int velocity) 
{
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(velocity);
}

Code for processing:

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

SoundFile soundfile;
Serial myPort; 
String val;
boolean firstContact = false;

String status="stop";

void setup()
{
  size(200, 200);
  myPort = new Serial(this, Serial.list()[2], 31250);
  myPort.bufferUntil('\n');
  
  soundfile = new SoundFile(this, "01.mp3");

}

void draw()
{
 
  
}

void serialEvent( Serial myPort) {

  val = myPort.readStringUntil('\n');
  val = trim(val);
  println(val);  
  
  if(val.equals( "Music" )){    
    //SoundFile soundfile = new SoundFile(this, "01.mp3");
    if(status.equals("stop")){
      soundfile.play();
      soundfile.amp(0.5);
      status="playing";
    }
  }
  else{
    if(status.equals("playing")){
      soundfile.stop();
      status="stop";
    }
  }
  
}

Thank you soooo much!! :slight_smile:
Linxi

But the problem is that is seems they can not work at the same time.

Yes that is true. Your computer is stopping you from having access to the same physical device from two different programs. This is built into all operating systems and makes a lot of sense.

What I would suggest you try is to use a program like hairless to translate serial data from your Arduino into a virtual MIDI source. Then route that same MIDI output stream to both Reason and Processing. You can then use MIDI messages on different MIDI channels to control the two programs.

Thank you sooo much!
And I tried to use hairless to turn the serial data into virtual MIDI source. But I still did not know how to route the MIDI output stream into two softwares. Is it using the output option in the hairless or should I do this in the code? Can you give me some more suggestions?

Thank you!

Linxi

But I still did not know how to route the MIDI output stream into two softwares.

That depends on the MIDI routing software you have inside your computer or the software you are trying to talk to. You have to chose the MIDI stream from that, that is Processing and Reason.

Thank you for your advices and patience!
When I use the hairless, the MIDI out option can not be changed, it always shows that "Not Connected". Does it mean that I should make some connections in the MIDI software that I want to use, as you said?

No.
No connection means it is not connected. Are you still using the MIDI shield? Hairless does not need it / can not use it.

No, I did not use the MIDI shield, but it still shows no connection .. Are there something that I should change before using the hairless? And thank you!

Have you got the baud rate correct? Hairless needs a faster rate than midi.