1

I am trying to develop a program that can help me to find the Beats Per Minute of a song by clicking, or tapping a button.

  • I have worked out that I need a dynamic array that saves the time (in milliseconds) of each tap, adding a new element on to the end of the Arraylist every time.

  • After a certain amount of elements are added, the BPM is worked out by finding the sum of all elements and dividing that by the amount of elements in the list.

I am not very familiar with Arraylists and was wondering whether somebody could help me implement these steps.

I will be using processing for this program.

Filburt
  • 17,626
  • 12
  • 64
  • 115
JackG
  • 13
  • 3
  • Welcome to StackOverflow! Please see the [ask] page to make a better question. Try to say what are your actual problems – llrs Feb 19 '14 at 14:42

1 Answers1

1

something like this?

ArrayList <Integer> diffs = new ArrayList<Integer>();
int last, now, diff;

void setup() {
  last = millis();
}

void draw() {
}

void mouseClicked() {
  now = millis();
  diff =  now - last;
  last = now;
  diffs.add(diff);

  int sum = 0;
  for (Integer i : diffs) {
  sum = sum + i;
  }

  int average = diffs.size() > 0 ? sum/diffs.size() : 0;

  println ("average = " + average);
}

Actually if you don't need to access each entrie, you don't even need an arraylist...

int last, now, diff, entries, sum;

void setup() {
  last = millis();
}

void draw() {
}

void mouseClicked() {
  now = millis();
  diff =  now - last;
  last = now;
  sum = sum + diff;
  entries++;

  int average = sum/entries ;

  println ("average = " + average);

}
v.k.
  • 2,826
  • 2
  • 20
  • 29
  • Brilliant! Thanks a lot, that's a good start. I will definitely be able to finish this now – JackG Feb 20 '14 at 18:06