MINIM - I got so far then got stuck -random audio player

edited April 2016 in Library Questions

Im trying to make .mp3 audio play from 3 separate arrays randomly. Each Array has 3 or 4 tracks. On a key or mouse press i want each array to play one track at the same time. Then on mouse press repeat and play a different 3 tracks.

My code is incomplete but I have

import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;

import ddf.minim.*;

Minim minim;
AudioSample[] songArray = new AudioSample [10];

void setup()
{
  size(100, 100);
   minim = new Minim(this);
 for (int i = 0 ; i < 3 ; i++) {
  songArray[i] = minim.loadSample("sound" + i +".mp3");
 }
  for (int p = 0 ; p < 3 ; p++) {
  songArray[p] = minim.loadSample("sounds" + p +".mp3");
 }
  for (int s = 0 ; s < 4 ; s++) {
  songArray[s] = minim.loadSample("soundss" + s +".mp3");
 }

}

void draw()
{

}

 void mousePressed(){

 .play();

 }

If anyone can add useful pointers I'd be grateful

thanks

Answers

  • edited March 2016

    I'm trying to make ".mp3" audio play from 3 separate arrays randomly.

    I only see 1 array there: AudioSample[] songArray = new AudioSample[10]; ~:>

    I've just made instead an example using a 2D array of AudioSample.
    Since I don't have any of those files, I haven't tested it. No idea whether it even works! 3:-O

    // forum.Processing.org/two/discussion/15771/
    // minim-i-got-so-far-then-got-stuck-random-audio-player
    
    // GoToLoop (2016-Mar-30)
    
    import ddf.minim.Minim;
    import ddf.minim.AudioSample;
    
    static final String NAME = "sound", EXT = ".mp3";
    static final int SAMPLE_SETS = 3, FPS = 5;
    
    final AudioSample[][] sampleSets = new AudioSample[SAMPLE_SETS][];
    int idx = -1;
    
    void setup() {
      noLoop();
      frameRate(FPS);
    
      final Minim m = new Minim(this);
    
      sampleSets[0] = new AudioSample[3];
      for (int i = 0; i < 3; sampleSets[0][i] = m.loadSample(NAME + i++ + EXT));
    
      sampleSets[1] = new AudioSample[3];
      for (int i = 0; i < 3; sampleSets[1][i] = m.loadSample(NAME + "s" + i++ + EXT));
    
      sampleSets[2] = new AudioSample[4];
      for (int i = 0; i < 4; sampleSets[2][i] = m.loadSample(NAME + "ss" + i++ + EXT));
    }
    
    void draw() {
      background((color) random(#000000));
      frame.setTitle("Frame: " + frameCount + "    Idx: " + idx);
    }
    
    void keyPressed() {
      idx = (idx + 1) % SAMPLE_SETS;
      for (final AudioSample sample : sampleSets[idx])  sample.trigger();
      redraw();
    }
    
    void mousePressed() {
      keyPressed();
    }
    
  • edited March 2016

    It works - you are clearly very knowledgeable with this (Im quite envious)

    However just as an extra add on when I combine the audio code you have made with the image code I had already it flips out and gives the following message

    ==== JavaSound Minim Error ==== ==== Error parsing ID3v2: String index out of range: -131

    Its so close to working its killing me.

    The combination of image and audio code Ive got now looks like

    import ddf.minim.Minim;
    import ddf.minim.AudioSample;
    
    static final String NAME = "sound", EXT = ".mp3";
    static final int SAMPLE_SETS = 3, FPS = 5;
    
    final AudioSample[][] sampleSets = new AudioSample[SAMPLE_SETS][];
    int idx = -1;
    
    PImage [] picArray = new PImage [181];
    float r = random (.1, 1);
    
    void setup() {
    
        size (1080,720,P3D);
    for (int p = 0; p < picArray.length; p++) {
        picArray[p] = loadImage("clip" + nf(p,3) + ".png");
    
      noLoop();
      frameRate(FPS);
    
      final Minim m = new Minim(this);
    
      sampleSets[0] = new AudioSample[3];
      for (int i = 0; i < 3; sampleSets[0][i] = m.loadSample(NAME + i++ + EXT));
    
      sampleSets[1] = new AudioSample[3];
      for (int i = 0; i < 3; sampleSets[1][i] = m.loadSample(NAME + "s" + i++ + EXT));
    
      sampleSets[2] = new AudioSample[4];
      for (int i = 0; i < 4; sampleSets[2][i] = m.loadSample(NAME + "ss" + i++ + EXT));
    }
    }
    
    void draw() {
    
      frame.setTitle("Frame:  " + frameCount + "    Idx: " + idx);
    }
    
    void keyPressed() {
       for(int b=0; b<181; b++){
        tint(random(255),random(255),random(255),random(255));
        rotate(random(360));
      }   
     image(picArray[int(random(picArray.length))], random (width), random (height),r*(1080),r*(720));
    
      idx = (idx + 1) % SAMPLE_SETS;
      for (final AudioSample sample : sampleSets[idx])  sample.trigger();
      redraw();
    }
    
    
    void mousePressed() {
      keyPressed();
    
    }
    

    Is it a memory issue ? I understand you dont have access to the .png or .mp3 files. The .png = about 50mb for 180 files and the .mp3 are 2mb for 10 files.

    Thanks again for the help.

  • edited March 2016

    Well, there's no chance I can run that! Nonetheless some tips;

    • If OpenGL isn't needed, favor default JAVA2D or FX2D renderers.
    • Keep all drawing related functions within draw() or called from it.
    • We can increase the available RAM for our sketches at CTRL + COMMA (Preferences).
  • I know, it's grown a bit out of control.!

    I've increased the ram to 2000. How do you swap OpenGL ? The only draw functions I could use would only occur on a mouse or key press so guess that rules that option out .

    It did flinch slowly in to life before freaking out, freezing and dying. I'm gonna try it with fewer images.

    Thanks again for all the support.

  • edited March 2016

    GoToLoop - Its working! kind of

    I reduced the image array and used the JAVA2D renderer. It functions. However it only gives one iteration of the image on key or mouse press. The sound changes on each click but the image remains the same. I need to get a new image and new sound with every click.

    Is it likely to be something to do with the frames running past one frame? Or would it be in the void draw?

    so close to finishing , please help. . .

        import ddf.minim.Minim;
        import ddf.minim.AudioSample;
    
        static final String NAME = "sound", EXT = ".mp3";
        static final int SAMPLE_SETS = 3, FPS = 5;
    
        final AudioSample[][] sampleSets = new AudioSample[SAMPLE_SETS][];
        int idx = -1;
    
        PImage [] picArray = new PImage [51];
        float r = random (.1, 1);
    
        void setup() {
    
            size (1080,720,JAVA2D);
    
        for (int p = 0; p < picArray.length; p++) {
            picArray[p] = loadImage("clip" + nf(p,3) + ".png");
        }
          noLoop();
          frameRate(FPS);
    
          final Minim m = new Minim(this);
    
          sampleSets[0] = new AudioSample[3];
          for (int i = 0; i < 3; sampleSets[0][i] = m.loadSample(NAME + i++ + EXT));
    
          sampleSets[1] = new AudioSample[3];
          for (int i = 0; i < 3; sampleSets[1][i] = m.loadSample(NAME + "s" + i++ + EXT));
    
          sampleSets[2] = new AudioSample[4];
          for (int i = 0; i < 4; sampleSets[2][i] = m.loadSample(NAME + "ss" + i++ + EXT));
        }
    
    
        void draw() {
    
          frame.setTitle("Frame:  " + frameCount + "    Idx: " + idx);
        }
    
        void keyPressed() {
          background(255);
           for(int b=0; b<181; b++){
            tint(random(255),random(255),random(255),random(255));
            rotate(random(360));
         image(picArray[int(random(picArray.length))], random (width), random (height),r*(1080),r*(720));
           }
    
          idx = (idx + 1) % SAMPLE_SETS;
          for (final AudioSample sample : sampleSets[idx])  sample.trigger();
          redraw();
        }
    
    
        void mousePressed() {
          keyPressed();
    
        }
    
  • As I had advised before: place all drawing stuff in draw(). :-@
    I don't understand why would you need a loop in order to pick 1 PImage to image() either.

  • Ive stuck it all in draw and its working, thanks. I'm all new to this so apologies if not picking up the instructions straight away, I really do value your input and don't want to p1ss any one off.

    Genuinely don't get the image thing , I followed another tutorial to display random images and that is what it suggested to do. Is there a more efficient way?

  • edited March 2016

    An excerpt of what I'm trying to say: ~O)

    import ddf.minim.Minim;
    import ddf.minim.AudioSample;
    
    static final String NAME = "sound", EXT = ".mp3";
    static final int SAMPLE_SETS = 3, PICS = 51, FPS = 5;
    
    final AudioSample[][] sampleSets = new AudioSample[SAMPLE_SETS][];
    int idx = -1;
    
    final PImage[] pics = new PImage[PICS];
    final float r = random(.1, 1);
    
    void draw() {
      background(-1);
      tint((color) random(#000000));
      rotate(random(TAU));
    
      final PImage rndImg = pics[(int) random(PICS)];
      image(rndImg, random(width), random(height), r*width, r*height);
    
      frame.setTitle("Frame:  " + frameCount + "    Idx: " + idx);
    }
    
Sign In or Register to comment.