How Digital Sound Works

We will view sound as a continuous function of time from the positive real numbers (time) to the interval [-1.0, 1.0] (amplitude). Since a computer can't "hold" a function defined on the reals, we have to approximate the function. We do this by measuring (or "sampling ") the sound several thousand times per second.

ADC picture

This process is called "Analog to Digital Conversion", or ADC. The number of times per second the sound is sampled is called the sample rate and is measured in Hertz. For example, CDs are recorded at 44100 samples per second, or 44.1kHz. But you don't need to understand ADC for CSE332.

Sox

The only sound file format you need to know about is the .dat format described below. You don't even have to know very much about that either, as we're giving you the code that reads and writes that format. In order to play sounds you produce, you need a way to convert between the .dat format and a format that common media players (Windows Media Player, winamp, RealPlayer, etc.) understand, such as .wav. We'll describe one way to do it below; however, you're free to use another converter.

sox is a UNIX command-line utility whose name stands for "SOund eXchange". It lets you convert between many different sound formats including .wav, .au, etc. In particular, sox allows you to convert to and from .dat sound files. .dat files are useful because they are human-readable, text-based, sound files. Note that you will need to perform this conversion to answer one of the write-up questions.

There is a windows version of sox available, and the source archive is known to compile and work on OS X 10.4. The program is also installed on the lab machines. You can download versions from the project page at SourceForge. The windows version is also a command-line program and works in the same way as the UNIX version described below. See here for some hints on using it.

The general strategy for using sox is as follows.

  1. Take a .wav sound file of your choosing (e.g. secret.wav). This sound shouldn't be longer than a few seconds, or your program will run out of memory.
  2. Convert it to a .dat file: sox secret.wav secret.dat
  3. Manipulate it with the program you will write: java Reverse list double secret.dat secret-revealed.dat
  4. Convert it back to a .wav file: sox secret-revealed.dat secret-revealed.wav
  5. Listen to it with your favorite sound player.

That's all there is to it!

The .dat File Format

The .dat file format starts with one line describing the sample rate of the sound file. This line is required. The rest of the file is composed of two columns of numbers. The first column consists of the time (measured in seconds) when the sample was recorded, and the second column contains the value of the sample, between -1.0 and 1.0. This is the beginning of a sample .dat file. Notice that the numbers in the first column increase by 1/44100 each step. This is because the sample rate is 44.1kHz.

  ; Sample Rate 44100
  0               0
  2.2675737e-05   0
  4.5351474e-05   0
  6.8027211e-05   0
  9.0702948e-05   0
  0.00011337868   0
  0.00013605442   0
  0.00015873016   0
  0.00018140590   0
  0.00020408163   0
  

Here is the same file, a little deeper on:

  0.22693878    -0.0062561035
  0.22696145    -0.0043945312
  0.22698413    -0.0068664551
  0.22700680    -0.0115661620
  0.22702948    -0.0145568850
  0.22705215    -0.0145416260
  0.22707483    -0.0121917720
  0.22709751    -0.0123901370
  0.22712018    -0.0145416260
  0.22714286    -0.0144958500
  0.22716553    -0.0147705080
  0.22718821    -0.0157012940
  0.22721088    -0.0129547120
  0.22723356    -0.0127105710
  0.22725624    -0.0181579590
  0.22727891    -0.0191497800
  0.22730159    -0.0145721440
  0.22732426    -0.0122375490
  0.22734694    -0.0124359130
  0.22736961    -0.0108184810
  

Note that you shouldn't have to deal much with the .dat file yourself, as the provided Reverse.java does all the work for you. All you have to do is implement the stacks. We are explaining the format because it will be helpful for you if you want to write a short file by hand to run, to verify if your program works.