Need help with a Java problem please: The playChord method (copy/paste): When do
ID: 3840785 • Letter: N
Question
Need help with a Java problem please:
The playChord method (copy/paste):
When done correctly, you will have two files: a reference class in a file Chord.java and a program class PlayMusic.java.
Specifications For this assignment you will create a package called music. The Chord class In the package you will create a reference class called Chord. A chord consists of a duration in seconds (a double value) and a sequence of frequencies (an array of double values). These will be the instance variables. It has the following API: public Chord (double duration, double frequencies) This is the constructor. NB: In this constructor, copy the values in the parameters frequencies to the instance variable frequencies. Use a for loop to do this. public void play This plays the chord by calling the playChord method, a private method whose code is provided below. public String toString This returns a string version of a chord object. It should be formatted as the duration, followed by a colon, followed by the frequencies, all enclosed in square brackets. For example, if the durations is 1.5 and the frequencies are 440.0, 880.0, and 1760.0 this method would return C1. 5: 440.0 880.0 1760.01 The play Chord method: private void playChord(double duration, double frequencies) f final int slicecount (int) (StdAudio. SAMPLE RATE duration) final double CJ slices E new double slice Count+1]; for (int i 0; i slicecount i++) f double chord 0.0 for (double frequency: frequencies) chord Math sin (2 Math.PI i frequency StdAudio SAMPLE RATE) slices [i] chord/frequencies length; StedAudio. play (slices);Explanation / Answer
Chord.java:
import java.util.Arrays;
public class Chord {
private double duration, frequencies[];
public Chord(double duration, double[] frequencies) {
this.duration = duration;
this.frequencies = frequencies;
}
public void play() {
playChord(duration, frequencies);
}
private void playChord(double duration, double[] frequencies) {
final int sliceCount = (int) (StdAudio.SAMPLE_RATE * duration);
final double[] slices = new double[sliceCount+1];
for (int i = 0; i <= sliceCount; 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;
}
StdAudio.play(slices);
}
@Override
public String toString() {
return "[" + duration + ": "
+ Arrays.toString(frequencies) + "]";
}
}
PlayMusic.java:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class PlayMusic {
// to store the chords
static ArrayList<Chord> listOfChords = new ArrayList<>();
// Driver function
public static void main(String args[]) throws IOException {
// file name from where the chords data will be read.
final String FILE_NAME = "chords.txt";
BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME));
String line;
while ((line = reader.readLine()) != null) {
// first line consist the duration
double duration = Double.parseDouble(line);
double frequencies[] = null;
// Second line consists the frequencies
line = reader.readLine();
if (line != null) {
// get the string frequencies
String strFrequencies[] = line.split(" ");
// convert string frequencies to double values
frequencies = new double[strFrequencies.length];
int count = 0;
for(String strFre: strFrequencies) {
frequencies[count++] = Double.parseDouble(strFre);
}
}
// add the new chord to the list
listOfChords.add(new Chord(duration, frequencies));
}
// close file reader
reader.close();
// Now iterate on each chord and print and play that
for(Chord chord: listOfChords) {
System.out.println(chord);
chord.play();
}
}
}
I am reading the chords data from the input file chords.txt. The file should be kept in the same folder as java file.. or the path need to be corrected in the code in case of error.
chords.txt:
6
34 34 55.5 543.45 3434
5
432 2342 3223 3223 3 323
I have used the above data in input file. It plays successfully.. although doesn't produce a good sound :p
Thanks!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.