Question

You are given a file chords.txt where each line contains three double values. The first value is a duration and the second and third values are frequencies. Place this file into the data folder on Eclipse. Once again you are to read values in from a file but you must be careful in doing this as different values mean different things. After you open the file, follow this pseudocode: 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 thats included here: public static 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 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(slices);

Chords.txt

1.0 350.0 440.0

2.0 440 480

4.0 0 0

2.0 440 480

4.0 0 0

2.0 440 480

4.0 0 0

0.5 480 620

0.5 0 0

0.5 480 620

0.5 0 0

0.5 480 620

0.5 0 0

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Play.java

package chord; //Please give name of your package

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

public class Play {

   private static final String FILENAME="D:\\WS17.2\\Sample\\src\\chord\\Chords.txt"; //give the absulate path of //file
  
   public static 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);
   }
  
   public static void main(String[] args)
   {
       BufferedReader br = null;
       FileReader fr = null;

       try {

           fr = new FileReader(FILENAME);
           br = new BufferedReader(fr);

           String sCurrentLine;

           br = new BufferedReader(new FileReader(FILENAME));
          
           while ((sCurrentLine = br.readLine()) != null) {
               System.out.println(sCurrentLine);
               StringTokenizer st = new StringTokenizer(sCurrentLine, " ");
               double []freq = new double[2];
               double duration = Double.parseDouble(st.nextToken());
               int i=0;
               while(st.hasMoreTokens())
               {
                   freq[i] = Double.parseDouble(st.nextToken());
                   i++;
               }
               playChord(duration,freq);
           }
          


       } catch (IOException e) {

           e.printStackTrace();

       } finally {

           try {

               if (br != null)
                   br.close();

               if (fr != null)
                   fr.close();

           } catch (IOException ex) {

               ex.printStackTrace();

           }

       }
   }
}

//I am assuing you have StdAudio class with you, If you do not have this class please let me know.

//output:

D Play,java Chors.bt StdAudio.java ー ロ| |BE Outline X |围Task List 1 package chord; chord 3e import java.io.BufferedReader: 4

Add a comment
Know the answer?
Add Answer to:
Chords.txt 1.0 350.0 440.0 2.0 440 480 4.0 0 0 2.0 440 480 4.0 0 0...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given a set of values representing a complete population, the standard deviation is found by taking...

    Given a set of values representing a complete population, the standard deviation is found by taking the square root of the average of the squared deviations of the values from their average value. For an example of what that means, see the following Wikipedia page; in particular, see the "Basic Example". As discussed in Wikipedia, given the following complete population: 2.0 4.0 4.0 4.0 5.0 5.0 7.0 9.0 then the standard deviation is 2.0 Your task is to write a...

  • Use two files for this lab: your C program file, and a separate text file containing...

    Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...

  • Please program in C++ and document the code as you go so I can understand what...

    Please program in C++ and document the code as you go so I can understand what you did for example ///This code does~ Your help is super appreciated. Ill make sure to like and review to however the best answer needs. Overview You will revisit the program that you wrote for Assignment 2 and add functionality that you developed in Assignment 3. Some additional functionality will be added to better the reporting of the students’ scores. There will be 11...

  • #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort,...

    #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort, search // Define computeAvg here // Define findMax here // Define findMin here // Define selectionSort here ( copy from zyBooks 11.6.1 ) // Define binarySearch here int main(void) { // Declare variables FILE* inFile = NULL; // File pointer int singleNum; // Data value read from file int valuesRead; // Number of data values read in by fscanf int counter=0; // Counter of...

  • P1) Write a complete C program that prints out the word YES, if its string command...

    P1) Write a complete C program that prints out the word YES, if its string command line argument contains the sequence the somewhere in it. It prints out the word NO otherwise. Both the word the and partial sequences like in the words theatre or brother qualify. Note: You can use string functions or not but if you do the only ones allowed are strcpy and strlen. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P2) What is the output of the following program (one answer per...

  • First create the two text file given below. Then complete the main that is given. There...

    First create the two text file given below. Then complete the main that is given. There are comments to help you. An output is also given You can assume that the file has numbers in it Create this text file: data.txt (remember blank line at end) Mickey 90 Minnie 85 Goofy 70 Pluto 75 Daisy 63 Donald 80 Create this text file: data0.txt (remember blank line at end) PeterPan 18 Wendy 32 Michael 28 John 21 Nana 12 Main #include...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • This is due in a few hours I don't know if I did this correctly can you double check my code to see if my return statements are fine and I follow the instructions accurately. Thank you!!! Modify m...

    This is due in a few hours I don't know if I did this correctly can you double check my code to see if my return statements are fine and I follow the instructions accurately. Thank you!!! Modify my code if anything return statement seems misplaced or inaccurately called. Here is my code: int write_sudoku_board(const char file_name[ ], int board[9][9]) { int number; int i,j; int a = 9; FILE* fp = fopen("sudoku.txt", "w"); int count = 0; for(i =...

  • c++ question, i put the txt attachment picture for this question... please include comments on calculations...

    c++ question, i put the txt attachment picture for this question... please include comments on calculations and varibles Description In this program, you will write a program to emulate a vending machine (somewhat). In this version of the program, you will use a struct type defined below struct snackType string name; string code; double price; int remaining; Where name contains the snack's name, code contains the 2 digit code for the item, price holds the item's price, and remaining holds...

  • Write in JAVA Get Familiar with the Problem Carefully read the program description and look at...

    Write in JAVA Get Familiar with the Problem Carefully read the program description and look at the data file to gain an understanding of what is to be done. Make sure you are clear on what is to be calculated and how. That is, study the file and program description and ponder! Think! The Storm Class Type Now concentrate on the Storm class that we define to hold the summary information for one storm. First, look at the definition of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT