# Problem with the volume. Minim Library

**URL:** <https://discourse.processing.org/t/problem-with-the-volume-minim-library/32128>\
**Category:** Libraries\
**Created:** [September 6, 2021, 11:36pm UTC](https://discourse.processing.org/t/problem-with-the-volume-minim-library/32128 "2021-09-06T23:36:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kylu](https://avatars.discourse-cdn.com/v4/letter/k/8491ac/32.png) [@kylu](https://discourse.processing.org/u/kylu)\
**Post date:** [September 6, 2021, 11:36pm UTC](https://discourse.processing.org/t/problem-with-the-volume-minim-library/32128/1 "2021-09-06T23:36:58Z")

</div>

Hey there!  
I’m working in a game proyect, using minim and Fisica libraries. I put the sound tracks but i can change the volume, i try using setVolume() and setGain() but there’s no way to modified it

I let the entery code, but the problem is in the class “Portadas”, with the audio “musica”, thanks!

```auto
import fisica.*;
import ddf.minim.*;

PFont font;
String estado = "inicio";
Boolean comenzar=false;
float a = 0.1; // volumen de la musica
int rectX, rectY, rectTam; // boton inicio
int score = 0; // puntaje inicial
PImage img_red;

FWorld world;
FCompound cage;
FCompound aroPadre;
FBox aro;

Minim minim;
AudioSample emboco;
AudioPlayer musica, buzzer, inicio, ganar, ventilador;

Elementos elementos;
Portadas p;
Pelotas pelotas;

void setup() {
  size(1200, 800);
  inicializarVariables();
}

void draw() {
  
  p.cambiarEscena();
  if (sesenta==60) {
    cage.setGrabbable(true);
  }
}

void inicializarVariables(){
  
  sesenta=70;
  score=0;
  //----------------------------- OBJETOS
  elementos = new Elementos();
  pelotas=new Pelotas();
  p = new Portadas();
  
  minim = new Minim(this);
  Fisica.init(this);
  Fisica.setScale(10);
  world = new FWorld();
  world.setEdges();

  //----------------------------- IMAGENES Y SONIDOS
  img_red= loadImage("red.png");
  buzzer = minim.loadFile( "buzzer.mp3" );
  musica = minim.loadFile( "space-jamLetsGetReadyToRumble.mp3");
  inicio = minim.loadFile( "Fondo-Basketball.mp3");
  ganar = minim.loadFile( "NBA-Sound.mp3");
  ventilador = minim.loadFile("ventilador.mp3");

  // load BD.wav from the data folder
  emboco = minim.loadSample( "canasta.mp3", 512 );
  font= createFont("BerlinSansFB-Reg-48.vlw", 150);
  smooth();
  img_red.resize(600, 600);
  
  // BOTON INICIO
  rectTam=100;
  textFont(font);

  /* aro de basket (detector)*/
  aro= new FBox(30, 10);
  aro.setStatic(true);
  aro.setPosition (width/2+8, 285);
    aro.setName("aro");
  world.add(aro);

  /* contenedor aro de basket */
  aroPadre = createContenedorAro();
  aroPadre.setPosition(width/2+8, 285);
  aroPadre.setStatic(true);
  aroPadre.setBullet(true);
  world.add(aroPadre);

  /* CAJA CONTENEDORA INTERACTIVA */
  cage = createCage();
  cage.setPosition(width/2, height/2);
  cage.setRotation(0);
  cage.setBullet(true);
  cage.setGrabbable(false);
  cage.setStroke(255);
  cage.attachImage(img_red); //CUANDO SE LE AGREGA LA IMAGEN NO SE MUEVE
  world.add(cage);

  elementos.dibujarAro();  
  elementos.dibujarVentilador();
  //pelotas.dibujar();

pelotas.dibujarPelotasBasquet();
pelotas.dibujarPelotasFutbol();

  rectMode(CENTER);
}

//contenedor del aro
FCompound createContenedorAro() {

  FBox b1 = new FBox(10, 30);
  b1.setPosition(30, 0);
  b1.setFill(0);
  b1.setNoStroke();

  FBox b2 = new FBox(10, 30);
  b2.setPosition(-30, 0);
  b2.setFill(0);
  b2.setNoStroke();

  FBox b3 = new FBox(80, 10);
  b3.setPosition(0, 10);
  b3.setFill(0);
  b3.setNoStroke();

  FCompound result = new FCompound();
  result.addBody(b1);
  result.addBody(b2);
  result.addBody(b3);
  return result;
}

//Caja de las pelotas
FCompound createCage() {

  FBox b1 = new FBox(10, 600);
  b1.setPosition(280, 0);
  b1.setFill(0);
  b1.setNoStroke();

  FBox b2 = new FBox(10, 600);
  b2.setPosition(-280, 0);
  b2.setFill(0);
  b2.setNoStroke();

  FBox b3 = new FBox(600, 10);
  b3.setPosition(0, 280);
  b3.setFill(0);
  b3.setNoStroke();

  FBox b4 = new FBox(600, 10);
  b4.setPosition(0, -280);
  b4.setFill(0);
  b4.setNoStroke();

  FCompound result = new FCompound();
  result.addBody(b1);
  result.addBody(b2);
  result.addBody(b3);
  result.addBody(b4);
  return result;
}

void contactStarted(FContact contacto)
{
  FBody cuerpo1 = contacto.getBody1();
  FBody cuerpo2 = contacto.getBody2();
  
  String nombre1 = conseguirNombre(cuerpo1);
  String nombre2 = conseguirNombre(cuerpo2);
  
  if (nombre1 == "aro" && nombre2 == "pelota_futbol_"){
   dividirCirculo((FCircle)cuerpo2);
   score-=1;
   println("EMBOCO PELOTA FUTBOL");
  }
   if (nombre1 == "aro" && nombre2 == "pelota_basquet_"){
  dividirCirculo((FCircle)cuerpo2);
     score+=1;
   println("EMBOCO PELOTA BASQUET");
  } else if (score==1) {
    estado="ganaste";
   cartel=true;
  }
    
  //if (nombre2 == "mouse" && nombre1 == "pelota_basquet"){
  ////dividirCirculo((FCircle)cuerpo1);
  // println("basquet");
  //} 
  
}

void mousePressed () {
  p.boton();
}

```

```auto

PImage img_ventilador2; // global para poder modificarla en la class tiempo 

class Elementos{

  PImage img_fondo, img_aro, img_fondo_inicial,img_viento;
  FBox imgaro;
  FBox vizq; 
  FBox vder;

Elementos(){

  img_fondo = loadImage("fondo.png");
  img_fondo.resize(1200, 800);
  img_fondo_inicial= loadImage("fondoinicial.png");
  img_fondo_inicial.resize(1200, 800);
  img_aro = loadImage("aro.png");
  if(activo==false){
  img_ventilador2= loadImage("ventilador2.png");
  img_ventilador2.resize(150, 150);
  } else if (activo==true) {
  img_ventilador2= loadImage("ventilador2_activo.png");
  img_ventilador2.resize(150, 150);
  }

}

void dibujarAro(){
    
  imgaro = new FBox(30, 30);
  imgaro.setStatic(true);
  imgaro.setGrabbable(false);
  imgaro.setPosition (width/2, height/2);
  world.add(imgaro);
  imgaro.attachImage(img_aro);

}

void dibujarVentilador(){
  
  vizq= new FBox(30, 30);
  vizq.setPosition(100, 700);
  vizq.setStatic(true);
  world.add(vizq);
  vizq.setName("ventilador");
  vizq.attachImage(img_ventilador2);

/*ventilador derecha
   vder= new FBox(30, 30);
   vder.setPosition(1100, 500);
   vder.addForce(1000, 500);  
   vder.setStatic(true);
   world.add(vder);
   vder.setName("ventilador");
   vder.attachImage(img_ventilador);*/

}

}

```

```auto
String conseguirNombre(FBody cuerpo)
{
  String nombre = "nada";
  
  if (cuerpo != null)
  {
    nombre = cuerpo.getName();
    
    if (nombre == null)
    {
      nombre = "nada";
    }
  }
  
  return nombre;
}

void dividirCirculo(FCircle circulo)
{
  
  world.remove(circulo);
    
}

```

```auto
class Pelotas{
PImage img_pelota_basquet, img_pelota_futbol;
    
Pelotas(){
  img_pelota_basquet = loadImage("pelota_basket.png");
  img_pelota_basquet.resize(55,55);
  img_pelota_futbol = loadImage("pelota_futbol.png");
  img_pelota_futbol.resize(50,50);
}

void dibujarPelotasBasquet(){
/* se dibujan las pelotas */
    for (int i=0; i<5; i++) {
    FCircle pelota = new FCircle (50);
     pelota.attachImage(img_pelota_basquet);
    pelota.setName("pelota_basquet_" );
    pelota.setPosition (100, 100 + i *100);
    pelota.setRestitution(0.2);
    pelota.setDensity(70);
    pelota.setGrabbable(false);
    pelota.setPosition(width/2-10+random(-5, 5), height/2-10+random(-5, 5));
    pelota.setBullet(true);
     pelota.setFriction(1);
    world.add(pelota);     
  }
}

void dibujarPelotasFutbol(){
for (int i=0; i<5; i++) {
    FCircle pelota = new FCircle (50);
    pelota.attachImage(img_pelota_futbol);
    pelota.setName("pelota_futbol_" );
    pelota.setPosition (100, 100 + i *100);
    pelota.setRestitution(0.5);
    pelota.setDensity(20);
    pelota.setGrabbable(false);
    pelota.setPosition(width/2-10+random(-5, 5), height/2-10+random(-5, 5));
    pelota.setBullet(true);
    pelota.setFriction(1);
    world.add(pelota);       
  }
      
}

//void dibujar(){

///* se dibujan las pelotas */
// for (int i=0; i<10; i++) {
// FCircle pelota = new FCircle (50);
// pelota.setName("pelota_" + i);
// pelota.setPosition (100, 100 + i *100);
// pelota.setGrabbable(false);
// //pelota.attachImage(img_pelota_basquet);
// pelota.setPosition(width/2-10+random(-5, 5), height/2-10+random(-5, 5));
// pelota.setBullet(true);
// world.add(pelota);
// if (random(10)<4) {
// pelota.attachImage(img_pelota_futbol);
// pelota.setRestitution(0.2);
// pelota.setDensity(20);
// } else {
// pelota.attachImage(img_pelota_basquet);
// pelota.setRestitution(0.4);
// pelota.setDensity(70);
// }
// pelota.setFriction(0);
// }
//}

}

```

```auto
boolean cartel;
class Portadas {
  Tiempo t;
  PImage img_fondo_inicial, img_fondo, img_ganaste, img_perdiste, img_instrucciones;
 

  Portadas () {
    t= new Tiempo();
    cartel=false;
    img_fondo_inicial= loadImage("fondoinicial.png");
    img_fondo_inicial.resize(1200, 800);
    img_fondo = loadImage("fondo.png");
    img_fondo.resize(1200, 800);
    img_ganaste=loadImage("ganaste.png");
    img_instrucciones=loadImage("instrucciones.png");
    img_perdiste = loadImage("perdiste.png");
    

     
  }
  
  void cambiarEscena() {

    if (estado=="inicio") {
      background(img_fondo_inicial);
           inicio.play();
    }
    if (estado=="instrucciones") {
      background(img_instrucciones);
    }
    if (estado.equals("juego")) {
      
      background(img_fondo);
      world.step();
      world.draw();
      
      musica.setGain(0.1);
      musica.play();
 
      inicio.pause();
      t.dibujar();
   
      // puntos
      fill(255);
      textSize(30);
      //println("x:" + mouseX);
      //println("Y:" + mouseY);
      text(score, 130, 100);
    }

    if (estado.equals("ganaste")) {
      background(img_ganaste);
      musica.pause();
      ventilador.pause();
      ganar.play();
    }
    if (estado.equals("perdiste")) {
      background(img_perdiste);
      musica.pause();
      ventilador.pause();
      buzzer.play();
      
       
    }
  }
  boolean botones (float px1, float px2, float py1, float py2) { //FUNCION PARA VERIFICAR SI SE CLICKEO EN LA ZONA DEL BOTÓN
    if (mouseX >= px1 && mouseX <=px2 && mouseY>=py1 && mouseY <=py2) {
      return true;
    } 
    return false;
  }

  void boton() {

    if (estado.equals("inicio")) {
      
      if (botones(440, 760, 554, 645)) { // VERIFICAR SI SE CLICKEO EL BOTÓN DE COMENZAR
        inicializarVariables();
        musica.rewind();
        ventilador.rewind();
              estado= "juego";
      }
      if (botones(441, 761, 673, 736)) { // VERIFICAR SI SE CLICKEO EL BOTÓN DE INSTRUCCIONES
        estado= "instrucciones";
      }
    }

    if (estado.equals("instrucciones")) {
      if (botones(440, 760, 554, 645)) { // VERIFICAR SI SE CLICKEO EL BOTÓN DE COMENZAR
             estado= "juego";
      }
    }
    if (estado.equals("perdiste") && cartel==true) {
          if (botones(440, 757, 542, 630)) { // VERIFICAR SI SE CLICKEO EL BOTÓN DE COMENZAR
            cartel=false;
            estado= "inicio";
      }
    }
    if (estado.equals("ganaste")&& cartel==true) {
      if (botones(440, 757, 542, 630)) { // VERIFICAR SI SE CLICKEO EL BOTÓN DE COMENZAR
        cartel=false;
        estado= "inicio";
      }
    }
  }
}

```

```auto
  int sesenta;
  boolean activo =false;
   class Tiempo {
  
     int time;
  int wait = 1000;
  int segundo =1;
  PImage perdiste = loadImage("perdiste.png");

  Tiempo() {
    
     sesenta=70;
     time = millis();
  }

  void dibujar() {
      
   
    if (millis() - time >= wait) {
      sesenta = sesenta - segundo;
      time = millis();//also update the stored time
    }
        
        if(sesenta<=60){
      textSize(25);
    fill(255, 0, 0);
     text(sesenta, 123, 153);
    }
            
    if (sesenta<=30) {
      world.setGravity(150, 300);
      ventilador.play();
      activo=true;
            
    }
    
/* if(sesenta==30) {
      activo=true; 
      if(activo==true) {
      img_ventilador2= loadImage("ventilador2_activo.png"); // CAMBIA LA IMAGEN
      } else {
        activo=false;

      }
    }*/
    if (sesenta<=0) {
      time = millis();
      estado="perdiste";
      cartel=true;
    }
    if(sesenta==62){
       textSize(300);
    fill(255, 0, 0);
     text("3", width/2-100,height/2);
    }
    if(sesenta==61){
      textSize(300);
    fill(255, 0, 0);
     text("2", width/2-100,height/2);
     }
     if(sesenta==60){
      textSize(300);
    fill(255, 0, 0);
     text("1", width/2-100,height/2);
     }
    }
    
  }

```

---

<div class="post-metadata">

**Author:** ![robertesler](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@robertesler](https://discourse.processing.org/u/robertesler)\
**Post date:** [September 8, 2021, 8:47pm UTC](https://discourse.processing.org/t/problem-with-the-volume-minim-library/32128/2 "2021-09-08T20:47:25Z")

</div>

Hi @kylu, I don’t know the Minim library well but the setGain() and setVolume() methods are apparently inherited from the Controller class: [Controller (compartmental.net)](http://code.compartmental.net/minim/javadoc/). It looks like they are using the low-level Java Sound API call to change the volume or gain of a TargetDataLine, so you would have to set that up for each audio file you want to control. They provide a tutorial in the examples “Advanced/Controllers/getSetGain.pde”.  
But I don’t think this is what you want. You probably want to work with the `getChannel()` method and just manipulate the samples directly. This would mean multiplying each sample by your volume (0-1). I took this code from the AudioSampleGetChannel.pde example:

```auto
import ddf.minim.*;

Minim minim;
AudioSample jingle;

void setup()
{
  size(512, 200, P3D);

  minim = new Minim(this);
  
  jingle = minim.loadSample("jingle.mp3", 2048);
  // get the left channel of the audio as a float array
  // getChannel expects either AudioSample.LEFT or AudioSample.RIGHT as an argument
  float[] leftChannel = jingle.getChannel(AudioSample.LEFT);
  float[] rightChannel = jingle.getChannel(AudioSample.RIGHT);
 
  for(int i = 0; i < leftChannel.length; i++)
  {
     leftChannel[i] *= 0.1; //your volume 0-1, change this number to hear the difference
     rightChannel[i] *= 0.1;
  }
  
  System.arraycopy(leftChannel, 0, rightChannel, 0, leftChannel.length);
}

void draw()
{
  background(0);
  stroke(255);
  for(int i = 0; i < jingle.bufferSize() - 1; i++)
  {
    line(i, 50 - jingle.left.get(i)*50, i+1, 50 - jingle.left.get(i+1)*50);
    line(i, 150 - jingle.right.get(i)*50, i+1, 150 - jingle.right.get(i+1)*50);
  }
  
  text("Press any key to trigger the sample.", 10, 20);
}

void keyPressed()
{
  jingle.trigger();
}

```
