Minim and random numbers

edited November 2013 in Questions about Code

Right, I'm new at this so I'm probably missing something blindingly obvious.

import ddf.minim.*;
AudioPlayer[] player = new AudioPlayer[47];
Minim minim;

int count;


void setup() {
  size(displayWidth, displayHeight);
  minim = new Minim(this);

  for (int b = 0; b < player.length; b++)
  {
     player[b] = minim.loadFile(nf(b+1, 2) + ".mp3");
  }
  player[count].play();
  }

void draw() {
  if(!player[count].isPlaying())
  {
    count = int(random(1, 47));
    if (count > 47) { count = 47; }
    player[count].play();   

  }
  print(count);
  }

void stop()
{
  player[count].close();
  minim.stop();
  super.stop();
}    

Basically, what I wanted it to do was to, out of a pool of 47 mp3 files, select one at random, play it, and then select and play another one, and so on. It works fine for a while - a couple of hours, with each track lasting between 3-5 minutes. Then, for some reason, the randomly generated int count, which is supposed to have a value of between 1 and 47, just explodes, and starts giving values like 203958028021984 or something, which kills the playback. I'm stumped. I thought

if (count > 47) { count = 47; }

would keep it in line, but it doesn't seem to help.

Answers

  • not sure i believe you about the random numbers tbh 8)

    but you might need to rewind samples after you've played them (or before you play them, either way will work.)

  • I don't know if it will make any difference as far as the problem you're having, but I think line 20 should probably be if(!player.isPlaying()) { because you don't really care which file is playing there right? Did you check how much memory it was using after a couple hours?

  • Thanks for the suggestions. When I try changing line 20 as you described, though, it tells me that isPlaying() cannot be invoked on the array type Audioplayer[].

    I'll let it run and see how much memory it uses.

  • yes, player is an array, so that won't work. it was correct how it was.

  • Right, I just let it run until the point where the random numbers go weird. It starts out at about 275mb used (it doesn't just shuffle audio, but also shuffles and rotates some svg files. Didn't include those lines as it didn't seem relevant) and ended up at about 286mb. A previous instance, running overnight, hit about 300mb if I recall correctly.

Sign In or Register to comment.