(Java / Processing)如何在屏幕上的不同位置创建一个对象的多个实例?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (142)

我无法创建该waveClock对象的多个实例,即使我已将它放入数组中并标记了每个对象的中心位置。我想在一个窗口中创建4个对象,全部响应不同的声音频率/节拍等等

有人可以阐明如何去做这件事吗?我相信这可能是课堂中的变量centerX和问题centerYwaveClock

import ddf.minim.*; //imports minim library
import ddf.minim.analysis.*;

//minim library objects that we will use to analyse tracks for beat onsets
Minim minim; //to use library code
AudioPlayer song;
BeatDetect beat;
BeatListener bl;

//collection of wave patterns that we will animate to visualise beat
//onsets at different frequencies
//build an array list object of type waveClock
ArrayList<waveClock> waveClocks = new ArrayList<waveClock>();

//parameters to control the and colour size off the pattern
//that visualise onsets in the bass, mid and high frequency regions
float minRadius;
float maxRadius;
color bassColour;
color midColour;
color highColour;
color veryHighColour;

//onset is a big increase in energy (beat has been detected)
//threshold is the minimum amount of bands that can change
//if one band has been detected to change, and the threshold is three bands,
//a beat would not be detected therefore there will be no visible change

//we search these frequency bands for the bass
int lowBassBand; //centre frequency 14.355469 Hz
int highBassBand; //centre frequency 315.8203 Hz
//we search these bands for the mids
int lowMidBand; //centre frequency 401.95312 Hz
int highMidBand; //centre frequency 1033.5938 Hz
//we search these bands for the highs
int lowHighBand; //centre frequency 1263.2812 Hz
int highHighBand; //centre frequency 5053.125 Hz
//we search these bands for the very highs
int lowVeryHighBand; //centre frequency 6431.25 Hz
int highVeryHighBand; //centre frequency 16537.5 Hz

//at least this many bands must have an onset 
//for isRange to return true - we set the thresholds for the low, mid
//and high frequency ranges
int numberOfLowOnsetsThreshold; 
int numberOfMidOnsetsThreshold;
int numberOfHighOnsetsThreshold;
int numberOfVeryHighOnsetsThreshold;

//sensitivity - how long do we wait (in ms) before we analyse
//different frequency bands for onsets
int sensitivity;

//global variables
float angnoise, radiusnoise;
float xnoise, ynoise;
float angle = -PI/6;
float radius;
float strokeCol = 254;
int strokeChange = -1;
int speed; //changes speed of visualisation once beat is detected?

class BeatListener implements AudioListener
{
  private BeatDetect beat;
  private AudioPlayer source;

  BeatListener(BeatDetect beat, AudioPlayer source)
  {
    this.source = source;
    this.source.addListener(this);
    this.beat = beat;
  }

  void samples(float[] samps)
  {
    beat.detect(source.mix);
  }

  void samples(float[] sampsL, float[] sampsR)
  {
    beat.detect(source.mix);
  }
}

void setup() {
  //visualiser_v2 global variables get assigned values
  angnoise = random(10);
  radiusnoise = random(10);
  xnoise = random(10);
  ynoise = random(10);

  //for every waveClock we need 180 pixels width, then add 20 pixels for first gap
  size(740, 650);
  background(255);
  //for drawing the beat circles
  minRadius = 5; //smallest radius of a circle in the visualisation
  maxRadius = 80; //largest radius of a circle in the visualisation

  //set the visualisation circle colours
  bassColour = #550E9D;
  midColour = #D613B3;
  highColour = #22E815;
  veryHighColour = #F7F70A;

  //we search a range of frequency bands to see if there are beat
  //onsets in low, mid, high and very high frequencies
  //at the end of the code there is a list of the centre frequencies
  //of each of the 27 bands used by the beat detector when it's analysing songs

  //for now, we search four different frequency ranges for beat onsets but this can be
  //increased. There's nothing to stop the you using overlapping frequency ranges if you
  //want, but currently the low, mid, high and very high bands do not overlap.

  //we search these frequency bands for bass onsets
  lowBassBand = 0; //centre frequency 14.355469 Hz
  highBassBand = 8; //centre frequency 315.8203 Hz
  //we search these frequency bands for the mid onsets
  lowMidBand = 9; //centre frequency 401.95312 Hz
  highMidBand = 13; //centre frequency 1033.5938 Hz
  //we search these bands for the high frequency onsets
  lowHighBand = 14; //centre frequency 1263.2812 Hz
  highHighBand = 20; //centre frequency 5053.125 Hz
  //we search these bands for the very high frequency onnsets
  lowVeryHighBand = 21; //centre frequency 6431.25 Hz
  highVeryHighBand = 25; //centre frequency 16537.5 Hz

  // at least this many bands must have an onset in a given frequency range for the
  //beat detector to register a beat onset using the 'isRange' function

  // for isRange to return true - we set the thresholds for the low, mid, high 
  // and very high frequencies ranges
  numberOfLowOnsetsThreshold = 6;
  numberOfMidOnsetsThreshold = 3; //high - lower + 1 = threshold
  numberOfHighOnsetsThreshold = 3;
  numberOfVeryHighOnsetsThreshold = 2;

  //code is called
  waveClocks.add(new waveClock(100, height/2, minRadius, bassColour, lowBassBand, highBassBand, numberOfLowOnsetsThreshold));
  waveClocks.add(new waveClock(280, height/2, minRadius, midColour, lowMidBand, highMidBand, numberOfMidOnsetsThreshold));  
  waveClocks.add(new waveClock(460, height/2, minRadius, highColour, lowHighBand, highHighBand, numberOfHighOnsetsThreshold));
  waveClocks.add(new waveClock(640, height/2, minRadius, veryHighColour, lowVeryHighBand, highVeryHighBand, numberOfVeryHighOnsetsThreshold));

  //set the min and max radius of each of the viz circles
 /* for (int i = 0; i < waveClocks.size(); i++) {
    //go through the arraylist of waveClocks and set the min and max radius of each circle
    waveClocks.get(i).setMinMaxRadius(minRadius, maxRadius);
  }*/

  //set up the minim objects used to analyse the song
  minim = new Minim(this);

  //song = minim.loadFile("data/underground tracks/2-Hyenah_-_Soak_It-320kb_s_MP3.mp3", 1024);

  song = minim.loadFile("data/underground tracks/8-Johannes_Brecht_-_Holla-320kb_s_MP3.mp3", 1024);

  song.play();

  // a beat detection object that uses the FREQ_ENERGY mode that 
  // expects buffers the length of song's buffer size
  // and samples captured at songs's sample rate
  beat = new BeatDetect(song.bufferSize(), song.sampleRate());

  // set the sensitivity 
  // After a beat has been detected, the algorithm will wait for X milliseconds 
  // before allowing another beat to be reported. You can use this to dampen the 
  // algorithm if it is giving too many false-positives. The default value is 10, 
  // which is essentially no damping. If you try to set the sensitivity to a negative value, 
  // an error will be reported and it will be set to 10 instead. 
  // note that what sensitivity you choose will depend a lot on what kind of audio 
  // you are analyzing. in this example, we use the same BeatDetect object for 
  // detecting kick, snare, and hat, but that this sensitivity is not especially great
  // for detecting snare reliably (though it's also possible that the range of frequencies
  // used by the isSnare method are not appropriate for the song).
  sensitivity = 10;
  beat.setSensitivity(sensitivity);  

  // make a new beat listener, so that we won't miss any buffers for the analysis
  bl = new BeatListener(beat, song);
}

void draw() {
  //clear the screen by painting it black
  //background(0);

  for (int i = 0; i < waveClocks.size(); i++) {

    //has there been a beat in the range? get(circle ID).low band, high band etc.
    if (beat.isRange(waveClocks.get(i).getLowBand(), waveClocks.get(i).getHighBand(), waveClocks.get(i).getOnsetThreshold())) {
      waveClocks.get(i).setMaxRadius();
    }
    //waveClocks.get(i).drawCircle();
    waveClocks.get(i).drawWaveClock();
  }
}

waveClock类在单独的选项卡中

//class is an architecture blueprint
//objects are the actual buildings built from the methods (can make as many as you like)
//constructor is the builder/constructor literally
class waveClock {
  float centerX; //co-ordinates of circle's position
  float centerY; //co-ordinates of circle's position
  float radius; //avg radius
 // float minRadius; //smallest size it can be
 // float maxRadius; //biggest size it can be
  color col; //colour
  int onsetThreshold; //
  int lowBand; //looks at lowest band of frequency and makes circle sensitive to it
  int highBand; //looks at highest band of frequency and makes circle sensitive to it
  boolean onset; //has there been an onset (beat has occurred or not?)

  //the constructor 
  waveClock(float x, float y, float r, color c, int lb, int hb, int t) {
    centerX = x;
    centerY = y;
    radius = r;
    col = c;
    lowBand = lb;
    highBand = hb; 
    onsetThreshold  = t;
  }

  //setters allow you to change the values in the code (i.e. change the position)
  //setters change the values in the variables set initially in the constructor
  //you can update the values

  //getters allow you to pull out the data from a variable and retrieve them (get them)

  //need to call this method on a waveClock object once it's constructed
  //to set the min and max radius
  void setMinMaxRadius(float min, float max) {
    minRadius = min;
    maxRadius = max;
  }

  void setOnset(boolean b) {
    onset = b;
  }

  //setters
  void setRadius(float r) {
    radius = r;
  }

  void setLowBand(int l) {
    lowBand = l;
  }

  void setHighBand(int h) {
    highBand = h;
  }

  void setMaxRadius() {
    radius = maxRadius;
  }

  void setOnsetThreshold(int t) {
    onsetThreshold = t;
  }

  //getters
  float getMaxRadius() {
    return maxRadius;
  }

  int getLowBand() {
    return lowBand;
  }

  int getHighBand() {
    return highBand;
  }

  int getOnsetThreshold() {
    return onsetThreshold;
  }

  void drawWaveClock() {
    radiusnoise += 0.005;
    radius = (noise(radiusnoise)*350) + 1;
    angnoise += 0.005;
    angle += (noise(angnoise)*6) - 3;
    if (angle > 360) {
      angle -= 360;
    } else if (angle < 0) {
      angle += 360;
    }

    xnoise += 0.01;
    ynoise =+ 0.01;
    float centerX = width/2 + (noise(xnoise)*100) - 50;
    float centerY = height/2 + (noise(ynoise)*100) - 50;

    float rad = radians(angle);
    float x1 = centerX + (radius*cos(rad));
    float y1 = centerY + (radius*sin(rad));

    float opprad = rad + PI;
    float x2 = centerX + (radius*cos(opprad));
    float y2 = centerY + (radius*sin(opprad));

    strokeCol += strokeChange;
    if (strokeCol > 354) {
      strokeChange = -1;
    } else if (strokeCol < 0) {
      strokeChange = 1;
    }
    stroke(strokeCol, 60);
    strokeWeight(1);
    line(x1, y1, x2, y2);
  }
}
提问于
用户回答回答于

你从来没有使用过类centerXcenterY变量。相反,你重新计算新的centerXcenterYdrawWaveClock()的功能。

float centerX = width/2 + (noise(xnoise)*100) - 50;
float centerY = height/2 + (noise(ynoise)*100) - 50;

扫码关注云+社区

领取腾讯云代金券