Look at a typical .dat file (you can open it in Emacs). It will begin something like:
; Sample Rate 22050.0 0.0 -0.0390625 4.5351473922902495E-5 -0.0390625 9.070294784580499E-5 -0.0390625
This means the amplitude is being measured (in a range from -1.0 to 1.0) 22,050 times per second. (CD quality is somewhat higher than this -- check it out for yourself). Sometimes there will be more lines with information about channels etc. but after a while each subsequent line will have two numbers: elapsed time (in increments of 1/sampleRate) and the amplitude at that point in time.
In order to reverse the file, we will read the whole file, putting the sampled amplitudes into a Stack of Doubles. Later, we can remove the amplitudes from the stack and thus obtain them in reverse order (Remember a stack is Last In First Out).
Here is the code needed to read the .dat file and place all the amplitudes into a stack. We are expecting the name of the input file will be provided on the command line as args[0]. Notice that we will skip lines beginning with a semicolon, and we will discard the time readings from the first column. We can generate those time readings for the output file since you will note that we have saved the sampleRate.
Here is code to handle setting up and writing the output file. Our print loop regenerates the sample times using numSteps and sampleRate and pops each amplitude reading off the stack.
The name of the output file came from the command line as args[1]. Here is code to set up and verify the input and output files:
if (args.length != 2) { System.err.println(" Incorrect number of arguments"); System.err.println(" Usage: "); System.err.println("\tjava Reverse inputfile outputfile"); System.exit(1); }
All the code I have provided for you can be placed in the main method. The only other code you need are:
public class Reverse { public static void main(String[]args) {and the import statements. I used:
import java.util.Stack; import java.util.Scanner; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.io.File;
java Reverse mystery.dat mysteryReversed.dat
You can find a mystery file here mystery.dat. Go ahead, download it and reverse it. Then use sox and/or play so you can hear it. To hand in this question submit your Reverse.java program
and write up a description of what you heard when you played the reversed
mystery.