Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * SE1021
- * Winter 2016-2017
- * Lab 1
- * Name: Brady Cassada
- * Created: 12/7/2016
- */
- import java.util.*;
- public class Lab1 {
- public static void main (String [] args) {
- Scanner in = new Scanner(System.in);
- int userChoice;
- final int DEFAULT_FRAMES = 8000;
- final int DEFAULT_SAMPLE_RATE = 8000;
- final int DEFAULT_CHANNELS = 1;
- final int DEFAULT_VALID_BITS = 8;
- do {
- try {
- System.out.println("Please enter the number corresponding with the menu choices " +
- "below:");
- System.out.println("0 - Exit\n1 - Reverse Play\n2 - Specified Tone");
- userChoice = in.nextInt();
- }
- catch (InputMismatchException e) {
- System.out.println("Invalid Input - Please try again.");
- userChoice = -1;
- }
- switch (userChoice){
- case 1: //reverse play
- System.out.println("Please enter the file name (excluding the .wav extension)" +
- ".");
- String fileInName = in.next();
- String fileIn = fileInName + ".wav";
- WavFile wavFileIn = new WavFile(fileIn);
- reverseWav(wavFileIn, fileInName);
- wavFileIn.close();
- break;
- case 2: //specified tone
- System.out.println("Please enter a file name (excluding the .wav extension).");
- String toneName = in.next();
- System.out.println("Please enter the desired frequency of the tone.");
- int frequency = in.nextInt();
- WavFile tone = new WavFile(toneName + ".wav", DEFAULT_CHANNELS, DEFAULT_FRAMES,
- DEFAULT_VALID_BITS, DEFAULT_SAMPLE_RATE);
- ArrayList<Double> toneSamples = generateSamples(frequency, DEFAULT_SAMPLE_RATE);
- tone.setSamples(toneSamples);
- tone.close();
- break;
- case 3:
- break;
- }
- } while (userChoice != 0);
- System.out.println("Goodbye");
- }
- private static void reverseWav(WavFile wavFileIn, String fileInName) {
- ArrayList<Double> samples = wavFileIn.getSamples();
- ArrayList<Double> samplesRev = samples;
- Collections.reverse(samplesRev);
- WavFile wavFileOut = new WavFile(fileInName+"Rev.wav",
- wavFileIn.getNumChannels(), wavFileIn.getNumFrames(),
- wavFileIn.getValidBits(), wavFileIn.getSampleRate());
- wavFileOut.setSamples(samplesRev);
- wavFileOut.close();
- wavFileIn.close();
- }
- private static ArrayList generateSamples(int frequency, int sampleRate) {
- ArrayList<Double> samples = new ArrayList<>();
- for (int i = 0; i < sampleRate; i++) {
- samples.add((Math.sin(2 * Math.PI * i * frequency / sampleRate)));
- }
- return samples;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement