I need help creating a java program with the following requirements listed below
ID: 3841703 • Letter: I
Question
I need help creating a java program with the following requirements listed below. Please make the code as simple to follow and understand as possible.
The Play Music program You will also write a program PlayMusic that will create an array list of Chords, fill it with chord values read from a file, and play the chords The data file consists of pairs of lines In each pair, the first line contains a single double value, which is a duration. The second line contains a list of double values, separated by spaces. To parse this file correctly, use this pseudocode Create an empty array list for chord s While stdIn is not empty read a line from StdIn convert to a double value and store in duration read a line from StdIn split into frequency strings, an array of strings create frequencies, an array of doubles the same size as the array of strings in a loop convert each string in frequencystrings into a double and store it in frequencies create a chord object with the duration and frequencies add that chord to the array list of chords end Following that, have another loop that iterates through the array list of chords and prints and plays each one.Explanation / Answer
Chords.java
import java.util.Arrays;
public class Chords {
private double dur, freq[];
public Chords(double dur, double[] freq) {
this.dur = dur;
this.freq = freq;
}
public void play() {
playChord(dur, freq);
}
private void playChord(double dur, double[] freq) {
final int dataCount = (int) (StdAudio.SAMPLE_RATE * dur);
final double[] data = new double[dataCount + 1];
for (int i = 0; i <= dataCount; i++) {
double chord = 0.0;
for (double frequency : freq) {
chord += Math.sin(2 * Math.PI * i * frequency / StdAudio.SAMPLE_RATE);
}
data[i] = chord / freq.length;
}
StdAudio.play(data);
}
@Override
public String toString() {
return "[" + dur + ": "
+ Arrays.toString(freq) + "]";
}
}
PlayMusic.java:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class PlayMusic {
//using ArrayList to store the chords
static ArrayList<Chords> chordList = new ArrayList<>();
public static void main(String args[]) throws IOException {
final String FILE_NAME = "input.txt";
BufferedReader br = new BufferedReader(new FileReader(FILE_NAME));
String str;
while ((str = br.readLine()) != null) {
double dur = Double.parseDouble(str);
double freq[] = null;
str = br.readLine();
if (str != null) {
String strFreq[] = str.split(" ");
// convert string frequencies to double values
freq = new double[strFreq.length];
int count = 0;
for (String strFre : strFreq) {
freq[count++] = Double.parseDouble(strFre);
}
}
chordList.add(new Chords(dur, freq));
}
br.close();
// Now iterate on each chord and print and play that
for (Chords chord : chordList) {
System.out.println(chord);
chord.play();
}
}
}
input.txt
6
34 34 55.5 543.45 3434
5
432 2342 3223 3223 3 323
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.