I have a question about my playChord method. I am trying to convert the duration
ID: 3549968 • Letter: I
Question
I have a question about my playChord method. I am trying to convert the duration and an arbitrary number of frequencies into values in a slice array. The frequencies will be an array called frequencies. I am unsure on how to approach this detail. Any hints or suggestions that would be helpful?
Here is my code so far:
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/StdAudio.SAMPLE_RATE);
}
StdAudio.play(slices);
//**My code goes here**
StdAudio.play(slices);
}
thank you very much
Explanation / Answer
There is just a very simple mistake,you line
slices[i] = Math.sin(2 * Math.PI * i * frequencies/StdAudio.SAMPLE_RATE);
will be like
slices[i] = Math.sin((2 * Math.PI * i * frequencies[i])/StdAudio.SAMPLE_RATE);
This is because multiple arguments are assumed as a array.you code then becomes
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);
}
// you already have a array of frequencies as you're using the multiple arguments syntax
StdAudio.play(slices);
//**My code goes here**
StdAudio.play(slices);
}
}
rest all is fine as far as I think
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.