playing multiple tracks at the same time and adjusting volume

edited November 2016 in Arduino

This is for an art project. I'm trying to play 4 wav. files on an infinite loop at the volume 0. when I press a button, the volume should rise up to 100. each button is connected with a wav. file, so when I press button A, file A's volume would rise to 100. I hope this makes things clear enough?

This is the ARDUINO code, I only have 2 wav files at the moment.

  int ledPin = 2;
  int ledPin3 = 3;
  int value = 0;

  void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, INPUT);
  pinMode(ledPin3, INPUT);
 
}
 
void loop()
{
  if (digitalRead(ledPin) == HIGH)
  {
    Serial.println("1");
    delay (50);
  }
  else
  {
   Serial.println("2");
   delay (50);
  }

    if (digitalRead(ledPin3) == HIGH)
  {
    Serial.println("3");
    delay (50);
  }
  else
  {
   Serial.println("4");
   delay (50);
  }
 
  
}

This is the PROCESSING file, also with two files.

import ddf.minim.*;
import processing.serial.*;
 
Serial port;
float value = 0;
float old = 0;
 
Minim minim;
AudioPlayer player;


 
void setup()
{
  size(50,50);

  minim = new Minim(this);
  player = minim.loadFile("harp.wav");
  player.play();
  player.shiftGain(-80,-80,100);
  player.rewind();

 
  port = new Serial(this, "/dev/cu.usbmodem1421", 9600);
  port.bufferUntil('\n');
}
 
void draw()
{
  background(0);
  if ((value == 1) || (value == 2))
  {
  if (value != old)
  { 
  if (value == 1)
  {
    //volume up
    player.shiftGain(-80, 0, 100);
    
  }
 else if (value == 2)
 {
   //volume down
   player.shiftGain(0,-80,100);
  
 }
 
  }
 old = value;
 
}
}
void serialEvent (Serial port)
{
  value = float(port.readStringUntil('\n'));
}`


This is PROCESSING file version B, 

`import ddf.minim.*;
import processing.serial.*;
 
Serial port;
float value = 0;
 
Minim minim;
Minim minim2;
AudioPlayer player;
AudioPlayer player2;

 
void setup()
{
  size(50,50);

  minim = new Minim(this);
  player = minim.loadFile("basses.wav");
  player.play();
  player.mute();
  player.rewind();
  
  minim2 = new Minim(this);
  player2 = minim2.loadFile("harp.wav");
  player2.play();
  player2.mute();
  player2.rewind();
 
 
  port = new Serial(this, "/dev/cu.usbmodem1421", 9600);
  port.bufferUntil('\n');
}
 
void draw()
{
  background(0);
  if (value == 2)
  {
    player.unmute();
    delay(3000);
    
  }
 if (value == 1);
 {
   player.mute();
   delay(3000);
  
 }
 
   if (value == 4)
  {
    player2.unmute();
    delay(3000);
    
  }
 if (value == 3);
 {
   player2.mute();
   delay(3000);
  
 }
 
}

 
void serialEvent (Serial port)
{
  value = float(port.readStringUntil('\n'));
}

The problem is that a) files are not playing at the same time b) it's clicking when the button is pressed c) overall, it's not really responding to the button, just playing on and off by itself. Any ideas or an easier way to do this maybe? I really appreciate it.

Answers

  • Divide the program into parts, then find which part has problem. Check if the Serial part of the code works, then we can check about the minim part.

  • Divide the program into parts, then find which part has problem. Check if the Serial part of the code works, then we can check about the minim part.

    okay, so the arduino part of the code works fine, i'm getting right signals in the monitor. the processing file version B works fine when it has only one track, when I add a track, it doesn't play the other track and also stops randomly.

  • I have this code. It plays both sounds at the same time. It works only if you don'y interact with the p1 and p2 objects in draw(). However, if you uncomment players by blocks (case1&2 or case3&4) then only one sounds plays and the other one seems to get muted. If you enabled all four cases (1,2,3,4) then there is no sound at all. Weird....

    Kf

    import ddf.minim.*;
    
    AudioPlayer p1;
    AudioPlayer p2;
    Minim m1;//audio context
    Minim m2;
    
    int value=0;
    
    void setup()
    {
      size(300,100);
      m1 = new Minim(this);
      m2 = new Minim(this);
      p1 = m1.loadFile("s2.mp3", 2048);
      p1.play();
      //p1.mute();
      p1.rewind();
    
      p2 = m2.loadFile("s1.mp3", 2048);
      p2.play();
      //p2.mute();
      p2.rewind();
    
      println("Done setup...");
    }
    
    void draw()
    {
      background(0);
      if (value == 2)
      {
        //p1.unmute();
      }
      if (value == 1);
      {
        //p1.mute();
      }
    
      if (value == 4)
      {
        //p2.unmute();
      }
      if (value == 3);
      {
        //p2.mute();
      }
    
      //value=0;
    }
    
    void keyPressed(){
      if(key>='1' && key<='4'){
        value=key-'0'; //To map it to a range btw 1 and 4
        println("Detected " + value);
      }    
    }
    
    void stop()
    {
      p1.close();
      p2.close();
      m1.stop();
      m2.stop();
      super.stop();
    }
    
  • I tried too. Same wired result as @kfrajer.

  • @kena1226

    CHECK line 45 and 58! An if statement does not end with semi-colon. That will fix your problem.

    Kf

  • Oops. Should have noticed earlier.

  • edited November 2016

    i tried and it doesn't work even without the semi colon. I have the same problem with your code too @kfrajer

  • Can you get both sounds to play individually?

    Next step, before you mute any of them, can you play them together?

    Kf

  • Next step, set the mute/unmute commands. Make sure line 49 from my code is run as you need to reset the value of value as I show in my code. As an alternative, you can move your code from draw into your mouse event function. Remember that draw executes 60 fps!

    Kf

  • @kfrajer in your code, I can get both sounds to play together and separately. I don't understand > As an alternative, you can move your code from draw into your mouse event function. Remember that draw executes 60 fps!

    This part, what does it mean? when i set the mute/unmute, it doesn't play anything or ummute anything.

  • Now that you tell me that both songs play simultaneously, then the next step is to get the mute/unmute functions to work. Next code reflect suggested changes. I got the code working before. Note the code before is not complete.

    Kf

    void draw(){;}
    
    void keyPressed(){
      if(key>='1' && key<='4'){
        value=key-'0'; //To map it to a range btw 1 and 4
        println("Detected " + value);     
    
    
        if (value == 2)
        {
          p1.unmute();
        }
        if (value == 1)
        {
          p1.mute();
        }
    
        if (value == 4)
        {
          p2.unmute();
        }
        if (value == 3)
        {
          p2.mute();
        }    
    
      }
    }
    
Sign In or Register to comment.