Please some help to understand how to use the minim sound library

I guys, I need help to understand how to play two songs without overlapping. I am using the minim library but I do not know how to detect the final part of the first track to play the second song (or maybe there are others better ways to do it). This is my code, thanks for the tips:)

import ddf.minim.*;
Minim minim;
AudioPlayer player[]=new AudioPlayer[4];
String filenames[] = new String[]{"sound1.mp3", "sound2.mp3"};

void setup(){
 minim = new Minim(this);
 for(int i=0;i<2;i++){
   player[i] = minim.loadFile(filenames[i]);
   player[i].play();
   print(filenames);
 }
}

void draw(){}

void stop(){
for(int i=0;i<2;i++){
   player[i].close();
 }
 minim.stop();
 super.stop();
}

Answers

  • Well, don't launch playing of both sounds in setup() at the same time. Wait for the end of the first one to play the second one.

  • Hi Philho, thanks for the replay, actually I am proceeding step by step to understand the minimum functions, but probably is better if I ask directly what I would like to achieve.

    I am trying to write a sort of musical player. On the top I have a alphabet composed by random images (for the moment I linked only 'a' and 'b') which are connected with the keyboard.

    Using the images as letters, is possible to write on the bottom.

    Each letter written on the bottom should be connected with a sound track and played one after other.

    What I need to know is how to link each image to the sound track and how let the sound plays without overlapping.

    Can you help me?:)

    import ddf.minim.*;
    Minim minim;
    AudioPlayer song;
    
    PImage img;
    boolean isLoaded = false;
    int count = 0;
    int numeriA = int(random(1, 4)); 
    int ANumber=int(numeriA);
    int numeriB = int(random(1, 4)); 
    int BNumber=int(numeriB);
    
    void setup()
    {
      //size(1000, 500);
      size(1200, 800);
      background(255);
      smooth();
      caricaimmagini();
      caricasuono();
    }
    void draw() {
      if (isLoaded) image(img, count*80, 600);
    }
    
    class Module
    {
      float x;
      float y;
      float w;
      float h;
    
      Module() {
      }
    }
    void caricaimmagini() {
      ModularGrid grid = new ModularGrid(9, 3, 10, 50, 400, 50, 50); //colonne / spaziogriglie/ margine sopra/ margine sotto/ margine sinistro/ margine destro
    
    
      Module startModule = grid.modules[5][1];
      img =loadImage("A"+ANumber+".jpg");
      image(img, startModule.x, startModule.y, img.width, img.height);
    
      Module startModule2 = grid.modules[4][1];
      img =loadImage("B"+BNumber+".jpg");
      image(img, startModule2.x, startModule2.y, img.width, img.height);
    
      //print("A"+ANumber+".jpg");
    
      grid.display();
    }
    void caricasuono() {
      minim = new Minim(this);
    
      song = minim.loadFile("Atjazz - Slide It In [DiY Discs].mp3");
    }
    
    
    
    
    
    class ModularGrid
    {
      int cols;
      int rows;
      float gutterSize;
      float top;
      float bottom;
      float left;
      float right;
    
    
      Module[][] modules;
      float moduleWidth;
      float moduleHeight;
    
      ModularGrid(int _cols, int _rows, float _gutterSize, float _top, float _bottom, float _left, float _right)
      {
        cols = _cols;
        rows = _rows;
        gutterSize = _gutterSize;
        top = _top;
        bottom = _bottom;
        left = _left;
        right = _right;
    
        modules = new Module[cols][rows];
    
        // cache the width of entire grid. Remember to cast as float, otherwise the columns will not line up
        // First we find the width of the page without the pagemargin
        float actualPageWidth = ((float)width - (left+right));
        // then we find the gutter sizes. We subtract by 1 as the furthermost right module will not have a right-side gutter
        float totalGutterWidth = (cols-1) * gutterSize;
        // then we simply find the column size by subtracting the gutterwidth from the page size and dividing by number of cols
        moduleWidth = (actualPageWidth - totalGutterWidth) / (float)cols;
    
        // cache the height of each column. This is the exact same calculation as before, and we should probably put
        float actualPageHeight = ((float)height - (top+bottom));
        float totalGutterHeight = (rows-1) * gutterSize;
        moduleHeight = (actualPageHeight - totalGutterHeight) / (float)rows;
    
        for (int i = 0; i < cols; i++)
        {
          for (int j = 0; j < rows; j++)
          {
            modules[i][j] = new Module();
            modules[i][j].x = left + (i*moduleWidth) + (i*gutterSize);
            modules[i][j].y = top + (j*moduleHeight) + (j*gutterSize);
            modules[i][j].w = moduleWidth;
            modules[i][j].h = moduleHeight;
          }
        }
      }
    
      void display()
      {
        noFill();
        noStroke();
    
        // draw the big bounding box
        rect(left, top, width - (left+right), height - (top+bottom));
    
        // draw all modules
        for (int i = 0; i < cols; i++)
        {
          for (int j = 0; j < rows; j++)
          {
            stroke(255, 0, 0, 100);
            rect(modules[i][j].x, modules[i][j].y, modules[i][j].w, modules[i][j].h);
          }
        }
      }
    }
    
    
    void keyPressed() 
    { 
      if (key=='a') { 
    
        img = loadImage("A"+ANumber+".jpg");
        count ++;
        isLoaded = true;
      } 
    
      if (key=='b') { 
        img = loadImage("B"+BNumber+".jpg");
        count ++;
        isLoaded = true;
      } else if (key == CODED) {
        if (keyCode == UP ) {
          //noLoop();
          background(255);
          isLoaded = false;
          caricaimmagini();
          count=0;
        }
      }
    }
    
  • edited April 2015

    Hi GoToLoop, the problem for the moment is that I do not understand how to link the image with the sound.

    For each letter I have three images, and each of these three images should be connected with a specific sound track.(so the same for each alphabet letter).

    How is possible to expres in code... "when 'a' is pressed, chose one random Aimg (here my basic way )

    int numeriA = int(random(1, 4)); 
    int ANumber=int(numeriA);
    ...
    void draw() {
      if (isLoaded) image(img, count*80, 600);
    }
    ...
    void keyPressed() 
    { 
      if (key=='a') { 
    
        img = loadImage("A"+ANumber+".jpg");
        count ++;
        isLoaded = true;
      } 
    

    and load the corresponding soundtrack which it will be played in a second moment (maybe with a 'ENTER')...

    thanks

  • Thank you very much, I updated my sketch:)

    here two options both that do not work....

    1) overlapping of the sound: ... { if (key=='a') {

        img = loadImage("A"+ANumber+".jpg");
        song = minim.loadFile("A"+ANumber+".mp3");
        count ++;
        isLoaded = true;
        song.play();
      } 
    
      if (key=='b') { 
        img = loadImage("B"+BNumber+".jpg");
        song = minim.loadFile("B"+BNumber+".mp3");
        count ++;
        isLoaded= true;
        song.play();
      } 
     else if (key == CODED) {
        if (keyCode == UP ) {
          //noLoop();
          background(255);
          isLoaded = false;
          caricaimmagini();
          count=0;
        }
      }
    }
    ...
    

    2) it plays only the last letter drawn

    ...
    { 
      if (key=='a') { 
    
        img = loadImage("A"+ANumber+".jpg");
        song = minim.loadFile("A"+ANumber+".mp3");
        count ++;
        isLoaded = true;
      } 
    
      if (key=='b') { 
        img = loadImage("B"+BNumber+".jpg");
        song = minim.loadFile("B"+BNumber+".mp3");
        count ++;
        isLoaded= true;
      } 
     else if (key == CODED) {
        if (keyCode == UP ) {
          //noLoop();
          background(255);
          isLoaded = false;
          caricaimmagini();
          count=0;
        }
      }
      else if (key == CODED) {
        if (keyCode == DOWN ) {
          sound.play();
        }
      }
    }
    ....
    

    How is possible to let play the letter one after the other and not all together? Thanks a lott!

Sign In or Register to comment.