Arduino - processing: serial data to toggle mp3 file in minim

Hi,

I’m sending data through the serial port to toggle a sound file on and off in minim.

The string data is all being read correctly in Processing, but the code isn’t accessing the if loops for some reason??

here’s the code:

import processing.serial.*;
import ddf.minim.*;
//import processing.video.*; 

Minim minim;

AudioPlayer player;
Serial myPort;
String val = null;    

void setup() {
  minim = new Minim(this);
  size(640,480);
  String portName = Serial.list()[2];
  myPort = new  Serial(this, portName, 9600);
  myPort.clear();
  myPort.bufferUntil('\n');
  player = minim.loadFile("groove.mp3");
}
 
void serialEvent (Serial myPort) {
  while(myPort.available() > 0) {
    val=myPort.readStringUntil('\n');

    if(val != null){
      val = val.trim(); // let's remove whitespace characters
      if (val.equals("L")) {
        if(player.isPlaying() == true){
          player.pause();
        }
      }
    
      else {
        if(player.isPlaying() == false){
          player.play();
        }
      }
    } 
  }
}

void draw() {
  background(0);
  println(val);
} 

Anyone that could possibly help with this?

1 Like

For starters, you probably should fix those import statements to be:

import processing.serial.*;
import ddf.minim.*;

It may have nothing to do with your problem, but neither processing.serial or ddf.minim are things you can import, they’re just package names.

So it’s printing out the values correctly that your Arduino board is sending? Have you tried throwing in a few println statements to see whether it’s getting into the if blocks? Do you have a playable file called ‘groove.mp3’?

P.S.
You can also the ‘</>’ button in the post editor to have code shown properly, or place the code in between two lines of triple backticks ( ``` ).

1 Like

Thanks for the reply.

Yes, there is a file playing called ‘groove.mp3’ It plays within the else loop. The console prints ‘status: Q’ or ‘status: L’ depending on the input from the Arduino.
I have tried println to the console from within the if block: if (val.equals(“L”)) {… and nothing prints out.

Any ideas?

Hello,

Processing has an Auto format in the Edit tab.

Also (in addition to above), please format your code for posting and I will look at it and try it.
See section " Is your post formatted?":

:slight_smile:

Thank you.

Here is the code:

import processing.serial.*;
import ddf.minim.*;
//import processing.video.*; 

Minim minim;

AudioPlayer player;
Serial myPort;
String val = null;    


void setup() {
  minim = new Minim(this);
  size(640, 480);
  String portName = Serial.list()[2];
  myPort = new  Serial(this, portName, 9600);
  myPort.clear();
  myPort.bufferUntil('\n');
  player = minim.loadFile("groove.mp3");
}


void serialEvent (Serial myPort) {
  while (myPort.available() > 0) {
    val=myPort.readStringUntil('\n');

    if (val != null) {
      val = val.trim(); // let's remove whitespace characters
      if (val.equals("L")) {
        if (player.isPlaying() == true) {
          player.pause();
          println("in the Loud if statement");
        }
      }
      //} else {
        if (player.isPlaying() == false) {
          player.play();
          println("in the Quiet if statement");
          //println(val);
        }
      }
    }
  }
}







void draw() {
  background(0);
  println(val);
} 

1 Like

the posted code would never do that, unless you send
status: X from arduino, and then the if equals “X” will never work.

you not posted the arduino code,
not posted the arduino IDE Montor print
not posted the console print

so lots of guessing here

Hello,

This may help:

:slight_smile:

tested code

example

import processing.serial.*;
Serial myPort;
String data;    
boolean diag = true; //__________________________________ print diagnostic info

import ddf.minim.*;
Minim minim;
AudioPlayer player;
String song = "data/groove.mp3"; //______________________ select song

void setup_serial() { //_________________________________ USB arduino..
  printArray(Serial.list());
  String portName = Serial.list()[0]; //_________________ adjust 0.. x port
  myPort = new Serial(this, portName, 115200);
  myPort.clear();
  myPort.bufferUntil('\n');
  println("try connect to "+portName);
}

void serialEvent(Serial p) { //__________________________ handle serial data
  data = trim(p.readStringUntil('\n'));
  if (data != null) {
    if (diag) println(data); //__________________________ print every GOOD line
    if (data.equals("L")) song_play(true); //____________ start
    if (data.equals("Q")) song_play(false); //___________ stop
  }
}

void song_play(boolean on) { //__________________________ PLAY / PAUSE
  if ( !on && player.isPlaying() )  player.pause(); //___ stop
  if ( on && !player.isPlaying() )  player.loop(); //____ start  [l] 'L'
}

void setup() {
  size(640, 480);
  minim = new Minim(this);
  player = minim.loadFile(song);
  setup_serial();
  println("we expect keyboard [l] or [q] or USB line 'L' or 'Q' ");
}

void draw() {} 

void keyPressed() {
  if ( key == 'l' ) song_play(true); //__________________ start
  if ( key == 'q' ) song_play(false); //_________________ stop
}


// arduino code tested with arduino IDE 1.8.10 hourly and Board Leonardo

/*

int dt = 1000; // msec between sending

void setup() {
  Serial.begin(115200);
}

void loop() {
  if (  analogRead(0) > 512 )  Serial.println("L");
  else                         Serial.println("Q");
  delay(dt);                              // sample rate
}

*/

prints

[0] "COM1"
[1] "COM7"
try connect to COM7
we expect keyboard [l] or [q] or USB line 'L' or 'Q' 
L
L

1 Like

Hi again.
Thank you very much for this.
I have tested your processing code example and getting this error message in the console:

==== JavaSound Minim Error ====
==== java.io.FileNotFoundException: data/groove.mp3

=== Minim Error ===
=== Couldn't load the file data/groove.mp3

try connect to /dev/cu.usbmodem1421
we expect keyboard [l] or [q] or USB line 'L' or 'Q' 
Q
Error, disabling serialEvent() for /dev/cu.usbmodem1421
null

This the Processing code I’m using:

import processing.serial.*;
import ddf.minim.*;
//import processing.video.*; 

Minim minim;

AudioPlayer player;
Serial myPort;
String val = null;    


void setup() {
  minim = new Minim(this);
  size(640, 480);
  String portName = Serial.list()[2];
  myPort = new  Serial(this, portName, 9600);
  myPort.clear();
  myPort.bufferUntil('\n');
  player = minim.loadFile("data/groove.mp3");
}


void serialEvent (Serial myPort) {
  while (myPort.available() > 0) {
    val=myPort.readStringUntil('\n');

    if (val != null) {
      val = val.trim(); // let's remove whitespace characters
      if (val.equals("L")) {
        if (player.isPlaying() == true) {
          player.loop();
          println("in the Loud if statement");
        }
      }
      //} else {
        if (player.isPlaying() == false) {
          player.pause();
          println("in the Quiet if statement");
          //println(val);
        }
      }
    }
  }








void draw() {
  background(0);
  println(val);
} 

With this Arduino code (board Leonardo):

// Define hardware connections
#define PIN_GATE_IN 2
#define IRQ_GATE_IN  0
#define PIN_LED_OUT 13
#define PIN_ANALOG_IN A0

// soundISR()
// This function is installed as an interrupt service routine for the pin
// change interrupt.  When digital input 2 changes state, this routine
// is called.
// It queries the state of that pin, and sets the onboard LED to reflect that 
// pin's state.
void soundISR()
{
  int pin_val;

  pin_val = digitalRead(PIN_GATE_IN);
  digitalWrite(PIN_LED_OUT, pin_val);   
}

void setup()
{
  Serial.begin(9600);

  //  Configure LED pin as output
  pinMode(PIN_LED_OUT, OUTPUT);

  // configure input to interrupt
  pinMode(PIN_GATE_IN, INPUT);
  attachInterrupt(IRQ_GATE_IN, soundISR, CHANGE);

  // Display status
  Serial.println("Initialized");
}

void loop()
{
  int value;

  // Check the envelope input
  value = analogRead(PIN_ANALOG_IN);

  // Convert envelope value into a message
  //Serial.print("Status:");
  if(value <= 10)
  {
    Serial.println("Q");
  }
  else
  {
    Serial.println("L");
  }

  // pause for 0.5 second
  delay(500);
}

is printing ‘Q’ and ‘L’ depending on the Arduino input, however the printLn (“in the Quiet if statement”) only gets through rarely.

L
L
L
L
in the Quiet if statement
L
L
L
L

1 Like

i modified your serial event code until it worked here, so i will not
again work with yours.

first, the minim error ?
i expect that files are under data/…
and processing can read from sketchpath and datapath,
but i recommend to use absolute path,

you could however change to

String song = "groove.mp3"; //______________________ select song


other thing is that i recommended with my code example to
separate the song logic from the serial logic,
in my example then also can start from keyboard,
using the same loop/pause logic


why you not repaired the TOP POST CODE posting?

1 Like