Oscillating audio file volume slider with Minim & Processing

Hi,

I’ve just been using processing for a few weeks but I’m struggling to understand how I can code this with Minim.

I’ve made a slider, which controls the gain of an audio track (setVolume hasn’t worked for me). Either way, I now want to create a slider which, as it’s mapped from 0-10 for example, the audio ‘gain’ will fade in to its full volume and out to no volume, and repeat . The audio file would play as normal at 0; the audio file would frequently rise and fade at 5; and the audio file would rapidly rise and fade when the slider control is at 10.

I’ve thought that this should be possible with an enveloper follower library example but the documentation is not clear to me if someone can help. Any oscillation signals should be silent and just control the gain/volume of the audio track. Is this the right approach?

Thank you in advance!

Here’s my basic code for gain control.

import ddf.minim.*;
import ddf.minim.ugens.*;

Slider sGain; 

float dB; 

Minim minim;
AudioOutput output;
FilePlayer song; 
Gain gain; 

void setup () {

  size(900, 600); 
  background (0);
  
  minim = new Minim(this);
  output = minim.getLineOut();

  song = new FilePlayer( minim.loadFileStream("song.mov"));
  song.loop ();
  
  sGain = new Slider (50, 550, 15, 15, 550, 520, 35, 550);
  
  gain = new Gain(0.f);
  
  song.patch(output); 
}

void draw () {   
  background (0);
  sGain.display();
}

void mouseDragged () {
  sGain.mouseDragged(); 
  dB = map(sGain.y, sGain.cBottom, sGain.cTop, -64, 6); 
  output.setGain (dB);
}

void mousePressed () {
  sGain.mousePressed();
}

void mouseReleased () {
  sGain.mouseReleased();
}


////// SLIDER OOP-Class 

class Slider {

  boolean button = false;

  float x; 
  float y; 
  float cTop; 
  float cBottom; 

  int w;  
  int h; 
  int r; 

  int lStart; 
  int lEnd;

  float xOffset; 
  float yOffset; 

  boolean overBox = false;
  boolean locked = false;


  // Constructor 1 
  Slider ( float tempX, float tempY, int tempW,int tempH, int tempCb, int tempCt, int tempLnS, int tempLnE ) {        

    x = tempX; 
    y= tempY; 
    w = tempW; 
    h = tempH; 
    cBottom = tempCb; // Top Value for slider constraint
    cTop = tempCt;  // Bottom value for slider constraint
    lStart = tempLnS; 
    lEnd = tempLnE; 
    
    r = 3; 
    xOffset = 0.0; 
    yOffset = 0.0;
  }

  //
  void display() {

    stroke (255); 
    strokeWeight (3);
    line (lStart+w, lEnd, lStart+w, lEnd-30);
    line (x-10, y, x+10, y);
    

    if (mouseX > x-w && mouseX <x+w && mouseY >y-h && mouseY <y+h) {
      //stroke(255); 
      overBox = true; 
      rectMode (RADIUS); /// Radius Center 
      stroke (255); 
      strokeWeight (3);
      noFill (); 
      rect(x, y, w, h, r);
  
      
      if (!locked) { 
        rectMode (RADIUS); /// Radius Center 

        noFill (); 
        rect(x, y, w, h, r);
      }
    } else {
      rectMode (RADIUS); /// Radius Center 
      stroke (0); 
      strokeWeight (3);
      noFill (); 
      noStroke (); 
      rect(x, y, w, h, r);

      overBox = false;
    }
  }


  void mousePressed() {

    if (overBox) { 
      locked = true;
    } else {
      locked = false;
    }
    //xOffset = mouseX-bx; 
    yOffset = mouseY-y;
  }

  void mouseDragged() {

    if (locked) {
      //bx = mouseX-xOffset; 
      y = mouseY-yOffset;
      y = constrain(y, cTop, cBottom);
    }
  }

  void mouseReleased() {

    locked = false;
  }
}// slider