Java Reference
In-Depth Information
public void spectrumDataUpdate(double timestamp, double duration,
float[] magnitudes, float[] phases) {
// Shown in Listing 9-26.
}
private double[] createNormArray() {
double[] normArray = new double[bars.length];
double currentNorm = 0.05;
for (int i = 0; i < normArray.length; i++) {
normArray[i] = 1 + currentNorm;
currentNorm *= 2;
}
return normArray;
}
private int[] createBucketCounts(double startFreq, int bandCount) {
int[] bucketCounts = new int[bars.length];
double bandwidth = 22050.0 / bandCount;
double centerFreq = bandwidth / 2;
double currentSpectrumFreq = centerFreq;
double currentEQFreq = startFreq / 2;
double currentCutoff = 0;
int currentBucketIndex = -1;
for (int i = 0; i < bandCount; i++) {
if (currentSpectrumFreq > currentCutoff) {
currentEQFreq *= 2;
currentCutoff = currentEQFreq + currentEQFreq / 2;
++currentBucketIndex;
if (currentBucketIndex == bucketCounts.length) {
break;
}
}
++bucketCounts[currentBucketIndex];
currentSpectrumFreq += bandwidth;
}
return bucketCounts;
}
}
The SpectrumListener constructor takes three parameters. The first is the center frequency of the first equalizer
band. The second is the MediaPlayer instance to which this listener will be attached. The third is the array of
SpectrumBar controls that the listener will update. This array is saved in the class's instance data for later use. The
MediaPlayer instance is used to gain access to the data needed by the listener during initialization. This includes
the value of the audioSpectrumNumBands property as well as the value of audioSpectrumThreshold . The listener's
calculations would be slightly off if either of these two values were changed after the SpectrumListener constructor
was called. We know that AudioPlayer4 does not do this, but if you have to deal with that situation, you would need to
either bind or attach listeners to those properties to be notified of the changes.
 
Search WWH ::




Custom Search