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...

  • void insertion_sort(int array[], int length, int & count_comp, int & count_move); void merge_sort(int array[], int length, int & count_comp, int & count_move); void heap_sort(int array...

    void insertion_sort(int array[], int length, int & count_comp, int & count_move); void merge_sort(int array[], int length, int & count_comp, int & count_move); void heap_sort(int array[], int length, int & count_comp, int & count_move); void quick_sort_sort(int array[], int length, int & count_comp, int & count_move); // YOUR // Implementation // should // go // here // or be put in separated .cpp files typedef void (*sort_algo_type)(int array[], int length, int& count_comp, int & count_move); void run_sort_algo(int array[], int length, sort_algo_type sorting,...

  • You are to write three functions for this lab: mean, remove, display. Download the main.cpp file...

    You are to write three functions for this lab: mean, remove, display. Download the main.cpp file provided to get started. Read the comments in the main function to determine exactly what the main function should do. //include any standard libraries needed // - Passes in an array along with the size of the array. // - Returns the mean of all values stored in the array. double mean(const double array[], int arraySize); // - Passes in an array, the size...

  • Binary files The binary file data.dat contains characters and numbers- it has 16 characters, foll...

    java Binary files The binary file data.dat contains characters and numbers- it has 16 characters, followed by numbers of type int alternating with numbers of type double -so after the first 16 characters, there will be an int, followed by a double, followed by an int, followed by a double, and so on. Write a program to open the file and until the end of file is reached, read in the values in the appropriate data types (that is, read...

  • In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...

    In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you. 1-First exercise import java.util.Scanner; public class first { public static int average(int[] array){    int total = 0;    for(int x: array)        total += x;    return total / array.length;    } public static double average(double[] array){    double total = 0;    for(double x: array)        total += x;    return total /...

  • 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...

  • Make sure to create the code with the given functions, and do not overlook them, do...

    Make sure to create the code with the given functions, and do not overlook them, do not create your own functions. This is an assignment that I did on my own and that's passed due, therefore, for educational purposes, I am interested to know what are other ways to properly program this. Please follow instructions as I am doing my best to fully understand every crucial detail in order to become a better programmer who is able to come up...

  • 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...

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