BeatDetect.isRange() throws ArrayIndexOutOfBoundsException

edited July 2015 in Library Questions
...
Caused by: java.lang.ArrayIndexOutOfBoundsException: 27
    at ddf.minim.analysis.BeatDetect.isOnset(Unknown Source)
    at ddf.minim.analysis.BeatDetect.isRange(Unknown Source)
    at BeatDetectRange$BeatListener.draw(BeatDetectRange.java:76)
    at BeatDetectRange.draw(BeatDetectRange.java:53)
...

But the buffer size is 2048, so it shouldn't throw this exception for an index less than 2048. Why is it doing this? My code is below.

import ddf.minim.*;
import ddf.minim.analysis.*;

Minim minim;
AudioPlayer song;
//BeatDetect beat;
BeatListener beat;

void setup()
{
  size(87*20, 20, P2D);
  minim = new Minim(this);
  song = minim.loadFile("marcus_kellis_theme.mp3", 2048);
  beat = new BeatListener(song);
  song.play();

  ellipseMode(RADIUS);
}

void draw()
{
  background(0);
  beat.draw();
}

class BeatListener {

  BeatDetect beatDetect;
  AudioSource audio;
  ArrayList<NoteListener> listeners;

  BeatListener(AudioSource src) {
    audio = src;
    beatDetect = new BeatDetect(src.bufferSize(), src.sampleRate());
    listeners = new ArrayList<NoteListener>();
    // A0-C8 (piano range)
    for (int i = 21; i < 108; i++) {
      listeners.add(new NoteListener(i));
    }
  }

  // Called each time draw() is called
  void draw() {
    beatDetect.detect(audio.mix);
    for (NoteListener l: listeners) {
      int min = minBand(l.midi), max = maxBand(l.midi);
      if (beatDetect.isRange(min, max, (max - min) / 2)) {
        l.notePlayed();
      }
      l.draw();
    }
  }

  final float SEMITONE = pow(2, 1/12.0);

  float freq(int midi) {
    return 440.0 * pow(SEMITONE, midi - 69);
  }

  float minFreq(int midi) {
    return maxFreq(midi - 1);
  }

  float maxFreq(int midi) {
    return 440.0 * pow(SEMITONE, midi - 69) * sqrt(SEMITONE);
  }

  int minBand(int midi) {
    return (int) (minFreq(midi) * audio.bufferSize() / audio.sampleRate());
  }

  int maxBand(int midi) {
    return (int) (maxFreq(midi) * audio.bufferSize() / audio.sampleRate());
  }
}

class NoteListener {

  int midi;

  float x, y, rad;
  color c;

  NoteListener(int midi) {
    this.midi = midi;

    y = 20;
    x = 20 * (midi - 21) - 10;
    rad = 5;
    colorMode(HSB, 12, 1, 1, 1);
    c = color(midi % 12, 1, 1, 0.5);
  }

  void notePlayed() {
    rad = 10;
  }

  void draw() {
    ellipseMode(RADIUS);
    ellipse(x, y, rad, rad);
    rad -= 1;
    if (rad < 5) rad = 5;
  }
}
Sign In or Register to comment.