Question

Play musical chords (JAVA PROGRAMMING) Notes and frequencies Every musical note has a name and a...

Play musical chords (JAVA PROGRAMMING)

Notes and frequencies

Every musical note has a name and a frequency. You are given a file where each line contains a note name, a tab character, and a frequence, which is a floating point value. For example, here are the first few lines from the file notes_frequencies.txt:

A0      27.5
A#0     29.1353
B0      30.8677
C1      32.7032
C#1     34.6479
D1      36.7081
D#1     38.8909
E1      41.2035
F1      43.6536

Playing chords

Write a program named PlayChords that first reads the data in the notes/frequencies file into a symbol table with the note name as the key and the frequency as the value.

The program then reads from a second file called chords.txt where each line contains one or more notes names separated by whitespace. Play the specified chords one by one by gathering the notes on each line into an array and then call the method specified below to play the resulting chord.

Here's the method that should be placed into your program.

        public static void playChordOneSecond(double... frequencies) {
                final int sliceCount = (int) (StdAudio.SAMPLE_RATE * 1.0);
                final double[] slices = new double[sliceCount+1];
                for (double frequency: frequencies) {
                        for (int i = 0; i <= sliceCount; i++) {
                                slices[i] += Math.sin(2 * Math.PI * i * frequency / StdAudio.SAMPLE_RATE);
                        }
                }
                StdAudio.play(slices);
        }

Helpful information

Place the data files into the data folder in Eclipse. Copy and paste the above method into your PlayChords class.

To process a text file where each line contains a fixed set of data fields:

  1. Use the method readLine in the StdIn class, which returns a string;
  2. Split the string into an array of strings using the instance method split in the String class. Each of those strings in the array is a note name.
  3. Declare a double array of the same length as the note array. Fill that double array by looking up for each note its corresponding frequency in the symbol table.

Homework Answers

Answer #1

Please import std audio libraries first.

Then,

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scaner;

public class PlayChordsromFile {

public static void main(String [] args) {

File file = new File("risingchords.txt");

try {

Scanner sc = new Scanner(file);

while (sc.hasNextLine()) {

String line = sc.nextLine();

String temp[]=new double[temp.length];

for(int i=0;i<temp.length,i++){

array[i]=Double.parseDouble(temp[i]);

}

playChord(array);

}

sc.close();

}

catch (FileNotFoundException e) {

e.printStackTrace();

}

}

public static void playChord(double... frequencies) {

double duration = 0.5;

final int sliceCount = (int) (StdAudio.SAMPLE_RATE * duration);

final double[] slices = new double[sliceCount+1];

for (int i = 0; i<= sliceCount, i++) {

for (double frequency: frequencies) {

slices[i] += Math.sin(2 * Math.PI * i * frequency / StdAudio.SAMPLE_RATE);

}

slices[i] /= fequencies.length;

}

StdAudio.play(slices);

}

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Java class called Grades in a class file called Grades.java. 2. Grades reads from...
Write a Java class called Grades in a class file called Grades.java. 2. Grades reads from a text file containing a series of course grades (a value between 0.0 and 100.0) with one grade entry per line. However, the first line in the file is an integer value specifying how many grade entries are contained in the file. 3. The Grades class contains four static methods: a. A method called loadGrades() that opens the file, reads in the data and...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A string data field named symbol1 for the stock’s symbol. • A string data field named name for the stock’s name. • A double data field named previousClosingPrice that stores the stock price for the previous day. • A double data field named currentPrice that stores the stock price for the current time. • A constructor that creates a stock with the specified symbol and...
Can someone please tell me what each line of this Java code does exactly? LINE BY...
Can someone please tell me what each line of this Java code does exactly? LINE BY LINE PLEASE!!! package p2; public class Course {    // class fields private String code; // course code, such as "EE333" private String name; // course name, such as "Engineering Programming w/ Objects" public Note notes[]; // attached note of a course private int noteSize; // size of the note by lines    // construct a Course with a code and a name, i.e....
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
Write a Java class called CityDistances in a class file called CityDistances.java.    2. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    2. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
Java programming. Write a public Java class called WriteToFile that opens a file called words.dat which...
Java programming. Write a public Java class called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor. The string should contain the following words: {“the”, “quick”, “brown”, “fox”}
I need this before the end of the day please :) In Java 10.13 Lab 10...
I need this before the end of the day please :) In Java 10.13 Lab 10 Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
JAVA PROGRAMMING: Write a program called TimeSymbolTables that creates three symbol tables, each of a different...
JAVA PROGRAMMING: Write a program called TimeSymbolTables that creates three symbol tables, each of a different implementation. Each symbol table will contain as a key a word read from a text file and as a value the number of times that word occurs in the text file. Have the program fill the first symbol table with these counts, keeping track of how long that takes using a Stopwatch object. It then does the same thing with the second symbol table....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT