import stdlib.StdAudio; public class PlayChords { public static void playChord(d
ID: 3550069 • Letter: I
Question
import stdlib.StdAudio;
public class PlayChords {
public static void playChord(double duration, double... frequencies){
final double sliceCount = (int) (StdAudio.SAMPLE_RATE * duration);
final double[] slices = new double[(int) (sliceCount + 1)];
for(int i =0; i < sliceCount; i++){
slices[i] = Math.sin((2 * Math.PI * i * frequencies[i])/StdAudio.SAMPLE_RATE);
}
StdAudio.play(slices);
}
public static void main(String[] args){
//Standard North American dial tone
playChord(1.0, 350.0, 440.0);
//Standard North American ring back tone
for(int i = 0; i < 5; i++){
playChord(2.0, 440, 480);
playChord(4.0, 0);
}
//Standard North American busy tone
for(int i = 0; i < 5; i++){
playChord(0.5, 480, 620);
playChord(0.5, 0);
}
//Play a c major triad chord
double[] cMajorChord = {261.63, 293.66, 329.63};
playChord(1.0, cMajorChord);
//play a half-diminished chord
double[] halfDiminishedChord = {130.81, 155.56, 185.0, 233.08};
playChord(1.0, halfDiminishedChord);
StdAudio.close();
System.exit(0);
}
}
I am working on a method that must convert a duration and an arbitrary number of frequencies into values in the slice array. Those frequencies are going to be in an array called frequencies. I ran my code and it is giving me this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at assignment3solutions.PlayChords.playChord(PlayChords.java:12)
at assignment3solutions.PlayChords.main(PlayChords.java:23)
can anyone give me a hint as to the source of the problem?
Explanation / Answer
Well your slicecount is 4100 or greater than that and frequencies array length is just 2 or 3 or 4. You were ought to get ArrayIndexOutOfBoundException. Here is the corrected code.
public class PlayChords {
public static void playChord(double duration, double... frequencies){
final double sliceCount = (int) (StdAudio.SAMPLE_RATE * duration);
final double[] slices = new double[(int) (sliceCount + 1)];
for(int i =0; i < sliceCount; i++){
for(int j=0; j<frequencies.length; j++){
slices[i] = Math.sin((2 * Math.PI * i * frequencies[j])/StdAudio.SAMPLE_RATE);
}
}
StdAudio.play(slices);
}
public static void main(String[] args){
//Standard North American dial tone
System.out.println("playing North American dial tone now.......");
playChord(1.0, 350.0, 440.0);
//Standard North American ring back tone
System.out.println("playing Standard North American ring back tone now.......");
for(int i = 0; i < 5; i++){
playChord(2.0, 440, 480);
playChord(4.0, 0);
}
//Standard North American busy tone
System.out.println("playing Standard North American busy tone now.......");
for(int i = 0; i < 5; i++){
playChord(0.5, 480, 620);
playChord(0.5, 0);
}
//Play a c major triad chord
double[] cMajorChord = {261.63, 293.66, 329.63};
System.out.println("playing c major triad chord tone now.......");
playChord(1.0, cMajorChord);
//play a half-diminished chord
System.out.println("playing a half-diminished chord now.......");
double[] halfDiminishedChord = {130.81, 155.56, 185.0, 233.08};
playChord(1.0, halfDiminishedChord);
StdAudio.close();
System.exit(0);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.