Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. /*
  2.  * SE1021
  3.  * Winter 2016-2017
  4.  * Lab 1
  5.  * Name: Brady Cassada
  6.  * Created: 12/7/2016
  7.  */
  8.  
  9. import java.util.*;
  10.  
  11. public class Lab1 {
  12.     public static void main (String [] args) {
  13.         Scanner in = new Scanner(System.in);
  14.         int userChoice;
  15.         final int DEFAULT_FRAMES = 8000;
  16.         final int DEFAULT_SAMPLE_RATE = 8000;
  17.         final int DEFAULT_CHANNELS = 1;
  18.         final int DEFAULT_VALID_BITS = 8;
  19.  
  20.         do {
  21.             try {
  22.                 System.out.println("Please enter the number corresponding with the menu choices " +
  23.                         "below:");
  24.                 System.out.println("0 - Exit\n1 - Reverse Play\n2 - Specified Tone");
  25.                 userChoice = in.nextInt();
  26.             }
  27.             catch (InputMismatchException e) {
  28.                 System.out.println("Invalid Input - Please try again.");
  29.                 userChoice = -1;
  30.             }
  31.             switch (userChoice){
  32.                 case 1: //reverse play
  33.                     System.out.println("Please enter the file name (excluding the .wav extension)" +
  34.                             ".");
  35.                     String fileInName = in.next();
  36.                     String fileIn = fileInName + ".wav";
  37.                     WavFile wavFileIn = new WavFile(fileIn);
  38.                     reverseWav(wavFileIn, fileInName);
  39.                     wavFileIn.close();
  40.                     break;
  41.                 case 2: //specified tone
  42.                     System.out.println("Please enter a file name (excluding the .wav extension).");
  43.                     String toneName = in.next();
  44.                     System.out.println("Please enter the desired frequency of the tone.");
  45.                     int frequency = in.nextInt();
  46.                     WavFile tone = new WavFile(toneName + ".wav", DEFAULT_CHANNELS, DEFAULT_FRAMES,
  47.                             DEFAULT_VALID_BITS, DEFAULT_SAMPLE_RATE);
  48.                     ArrayList<Double> toneSamples = generateSamples(frequency, DEFAULT_SAMPLE_RATE);
  49.                     tone.setSamples(toneSamples);
  50.                     tone.close();
  51.                     break;
  52.                 case 3:
  53.  
  54.  
  55.                     break;
  56.             }
  57.         } while (userChoice != 0);
  58.         System.out.println("Goodbye");
  59.     }
  60.  
  61.     private static void reverseWav(WavFile wavFileIn, String fileInName) {
  62.         ArrayList<Double> samples = wavFileIn.getSamples();
  63.         ArrayList<Double> samplesRev = samples;
  64.         Collections.reverse(samplesRev);
  65.         WavFile wavFileOut = new WavFile(fileInName+"Rev.wav",
  66.                 wavFileIn.getNumChannels(), wavFileIn.getNumFrames(),
  67.                 wavFileIn.getValidBits(), wavFileIn.getSampleRate());
  68.         wavFileOut.setSamples(samplesRev);
  69.         wavFileOut.close();
  70.         wavFileIn.close();
  71.  
  72.     }
  73.     private static ArrayList generateSamples(int frequency, int sampleRate) {
  74.         ArrayList<Double> samples = new ArrayList<>();
  75.         for (int i = 0; i < sampleRate; i++) {
  76.             samples.add((Math.sin(2 * Math.PI * i *  frequency / sampleRate)));
  77.         }
  78.         return samples;
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement