minim: help with volume control

edited November 2015 in Library Questions

Hi, I have 3 songs. each time a button (in arduino) is pressed the playing song fades out and the upcoming fades in. Now, I could do this without problem. Now, what I need to do is to add a gain control to each player so that, after a song faded in, the user can control the volume through a pot. If the button (to change song) is pushed then the playing song will fade out from the gain it has in that moment and the new song will fade in to the same gain of the previous one. I'm totally lost on this, I tried many thing but the main issue is that I can't really understand when the fade in is over and then, where to put the code to 'activate' the potentiometer. I will assume that the potentiometer is by default set to the max rotation (max volume then) and the first 'automatic' fade in will fade till the max volume possible (so that the potentiometer position is related to the volume.

this is my code:

`import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;
import processing.serial.*;

Minim minim;
Serial myPort;
AudioPlayer player1, player2, player3;

int input = 0;

int myPlayer = 0;
boolean change = false;
boolean init_vol = true;
boolean butt_pushed = false;

static final int FADE = 2500;


void setup(){
  minim = new Minim(this);

  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);

  player1 = minim.loadFile("song1.mp3");
  player2 = minim.loadFile("song2.mp3");
  player3 = minim.loadFile("song3.mp3");

  player1.setGain(-80);
  player2.setGain(-80);
  player3.setGain(-80);

}

void draw(){

  float max_gain = 0;
  float volume = 0;

  if(myPort.available() > 0)
    input = myPort.read();


  if(input != 1000) //to distinguish the button from the potentiometer
    volume = input;
  else
    butt_pushed = true;

  //println(volume);


  if(butt_pushed){ //butt_pushed = change song
    if(change)
      changeSong();
    change = false;
  }else
    change = true;

/*my try to understand when the fade in is over 
  println(player1.getGain());
  if(player1.isPlaying()){
    if(player1.getGain() >= 6){
      t = true;
    }
  }else
    t = false;

  if (t){
         player1.setGain(map(volume,0,100,-80,20));
      println("IN "+volume); 
  }*/

}

void changeSong(){ //change song with fade out/in
  ++myPlayer;

  if(myPlayer == 1){
    player3.shiftGain(player3.getGain(),-80,FADE);
    player1.play();
    player1.shiftGain(-80,20,FADE);
    player2.pause();
    player2.rewind();
  }else if(myPlayer == 2){
    player1.shiftGain(player1.getGain(),-80,FADE);
    player2.play();
    player2.shiftGain(-80,20,FADE);
    player3.pause();
    player3.rewind();
  }else{
    player2.shiftGain(player2.getGain(),-80,FADE);
    player3.play();
    player3.shiftGain(-80,20,FADE);
    player1.pause();
    player1.rewind();
  }



  myPlayer%=3;

}

void stop(){
  player1.close();
  player2.close();
  player3.close();

  minim.stop();
  super.stop(); 
}`

thanks :)

Answers

  • edited November 2015

    ok, I decided to make it simpler..now it works and I'll leave it like this.. basically it's all related to the pot volume, it will fade in till the level of the pot and the volume of each song can be manipulated while the song is playing..

    void draw(){
    
      float volume = 0;
    
      if(myPort.available() > 0)
        input = myPort.read();
    
    
      if(input != 1000)
        volume = map(input,0,100,-80,20);
      else
        butt_pushed = true;
    
    
      if(butt_pushed){ //
        if(change)
          changeSong((int)volume);    
        change = false;
      }else
        change = true;
    
      if(player1.isPlaying()){
        if(player1.getGain() >= volume)
          t = true;
      }else
        t = false;
    
      if(player2.isPlaying()){
        if(player2.getGain() >= volume)
          x = true;
      }else
        x = false;
    
      if(player3.isPlaying()){
        if(player3.getGain() >= volume) 
          z = true;
      }else
        z = false;
    
      if (t){
        if(!butt_pushed){
             player1.setGain(volume);
          println("IN_1 "+volume); 
        }else
          t = false;
      }
    
      if (x){
        if(!butt_pushed){
             player2.setGain(volume);
          println("IN_2 "+volume); 
        }else
          x = false;
      }
    
      if (z){
        if(!butt_pushed){
             player3.setGain(volume);
          println("IN_2 "+volume); 
        }else
          z = false;
      }
    }
    
    void changeSong(int volume){
      ++myPlayer;
    
      if(myPlayer == 1){
        player3.shiftGain(player3.getGain(),-80,FADE);
        player1.play();
        player1.shiftGain(-80,volume,FADE);
        player2.pause();
        player2.rewind();
      }else if(myPlayer == 2){
        player1.shiftGain(player1.getGain(),-80,FADE);
        player2.play();
        player2.shiftGain(-80,volume,FADE);
        player3.pause();
        player3.rewind();
      }else{
        player2.shiftGain(player2.getGain(),-80,FADE);
        player3.play();
        player3.shiftGain(-80,volume,FADE);
        player1.pause();
        player1.rewind();
      }
    
    
    
      myPlayer%=3;
    
    }
    
  • Volumes go from 0-10 :) Or else it will glitch.

Sign In or Register to comment.