While not at the end of file for StdIn declare an array of length 2 read the dur
ID: 3823097 • Letter: W
Question
While not at the end of file for StdIn declare an array of length 2 read the duration for i in the range 0 to 1 read the frequency into the ith frequencies array entry end play the chord end Note that we are now using nested loops. To play a chord, you will need a different method and that's included here: public static void playChord (double duration, double frequencies) f final in slicecount int (StdAudio. SAMPLE RATE duration) final double slices J new double sli ce Count +1 for (int i 0; i slice Count i++) double chord 0.0 for (double frequency frequencies) chord Math sin 2 Math PI i frequency StdAudio. SAMPLE RATE) slices [i] chord frequencies. length; Std Audio. play slicesExplanation / Answer
import java.util.*;
import java.lang.*;
public class Sound{
static void playChord(double duration, double[] frequencies)
{
final int sliceCount = (int)StdAudio.SAMPLE_RATE*duration;
final double slices[] = new double[sliceCount+1];
for(int j=0;j<=sliceCount;j++)
{
double chord = 0.0;
for(double frequency : frequencies)
{
chord+= Math.sin(2 * Math.PI * j * frequency/StdAudio.SAMPLE_RATE);
}
slices[j] = chord/frequencies.length;
}
StdAudio.play(slices);
}
public static void main(String []args){
Scanner s = new Scanner(System.in);
double duration;
double frequency[] = new double[2];
duration = s.nextDouble();
for(int i = 0;i<2;i++)
{
frequency[i] = s.nextDouble();
}
playChord(duration,frequency);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.