Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Play a simple song Write a program called Playsimplesong to play a song represen

ID: 3874830 • Letter: P

Question

Play a simple song Write a program called Playsimplesong to play a song represented as notes in text. Write the program so that it: 1. Declares and creates a symbol table using the algs31.BinarysearchST class; 2. Reads in a file notes frequencies.txt where each line is a pair of strings separated by whitespace. The first string is the name of a musical note and the second a double value that is its sound frequency as found on a piano. For example, the note A4 is paired with the frequency 440 and the note C4 with the frequency 261.626. As each line is read, an entry is made in the symbol table where the note name is the key and the frequency is the value. 3. Reads in a song file, where each line contains a note name and a duration in seconds, separated by whitespace. A sample file is sample simple song txt, which plays every C for half a second. Another song you might try is lotr.txt. Looking up the frequency corresponding to the note name, the program calls the method below to play the note. Both the notes and frequencies file and the song file should be placed into the Eclipse data directory and, as in the GPA program, read in using StdIn and the fromFile method. 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; 3. Convert the numeric strings into numeric values using the method parseDouble in the Double class. To play each note, place into your program and call this method: public static void playTone (double frequency, double duration) f final int slicecount = (int) (StdAudio . SAMPLE RATE * duration); final double[] slices = new double [slicecount+1]; for (int i = 0; i

Explanation / Answer

Solution: See the code below:

-------------------------------------------

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.util.HashMap;

import stdlib.StdAudio;

/**
* SongPlayer class
*
*/
public class SongPlayer {

   private HashMap<String, Double> notesTable;
   private double frequencies[];
   private double durations[];

   /**
   * Constructor
   */
   public SongPlayer() {
       notesTable = new HashMap<String, Double>();
   }

   /**
   * creates symbol table of notes and their frequencies
   *
   * @param songNotesFile
   */
   public void readFromSongNotesFile(String songNotesFile) {
       File file = new File(songNotesFile);
       try {
           BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
           String line;
           // loop to read song notes file line by line
           while ((line = reader.readLine()) != null) {
               String[] tokens = line.split(" ");
               String note = tokens[0]; // note value
               double freq = Double.parseDouble(tokens[1]); // frequency value
               notesTable.put(note, freq);
           }
           reader.close();
       } catch (FileNotFoundException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
   }

   /**
   * read a song to play from a file
   *
   * @param songFile
   */
   public void readSongToPlayFromFile(String songFile) {
       File file = new File(songFile);
       try {
           int numLines = (int) Files.lines(file.toPath()).count();
           frequencies = new double[numLines];
           durations = new double[numLines];
           int i = 0;
           BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
           String line;
           // loop to read song file line by line
           while ((line = reader.readLine()) != null) {
               String[] tokens = line.split(" ");
               double freq = Double.parseDouble(tokens[0]); // frequency value
               double duration = Double.parseDouble(tokens[1]); // duration
                                                                   // value
               frequencies[i] = freq;
               durations[i] = duration;
               i++;
           }
           reader.close();
       } catch (FileNotFoundException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
   }

   /**
   * plays a tone
   *
   * @param frequency
   * @param duration
   */
   public void playTone(double frequency, double duration) {
       final int sliceCount = (int) (StdAudio.SAMPLE_RATE * duration);
       final double[] slices = new double[sliceCount + 1];
       for (int i = 0; i <= sliceCount; i++) {
           slices[i] = Math.sin(2 * Math.PI * i * frequency / StdAudio.SAMPLE_RATE);
       }
       StdAudio.play(slices);
   }

   /**
   * plays a song
   */
   public void playSong() {
       for (int i = 0; i < frequencies.length; i++) {
           double freq = frequencies[i];
           double duration = durations[i];
           playTone(freq, duration);
       }
   }

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       SongPlayer player = new SongPlayer();
       String songNotesFileName = "./notes_frequencies.txt"; // change this as
                                                               // per your file
       player.readFromSongNotesFile(songNotesFileName);
       String songFileName = "./sample_sigle_song.txt"; // change this as per
                                                           // your file
       player.readSongToPlayFromFile(songFileName);
       player.playSong();
   }
}

-------------------------------------------