CS 202 - Computer Science II - Fall 2005
Lab 5 - Binary Files


Loyola College > Department of Computer Science > Dr. James Glenn > CS 202 > Labs > Lab 5

Due

Monday, November 7th at 11:59pm. Labs submitted one day late will be assessed a 20% penalty. Labs will not be accepted more than one day late.

Objectives

Reading

Koffman & Wolz, Chapter 8

Introduction
Sound is created by air (or whatever) pressure changing over time. If you graph pressure versus time for a given sound, you may come up with a graph that looks something like this:

To represent a continuous curve digitally, we can sample it at regular intervals. At each sample point, we measure the pressure to the desired resolution and record the result as an integer. As we increase the sample frequency and resolution, we will digitize the sound more accurately. In picture above, the blue curve represents the original sound. The tick marks along the bottom show the times at which we take samples. The tick marks along the side reflect the resolution of our samples; the value recorded will be that represented by the closest tick to the actual curve. The red dots represent the samples and the white curve represents the approximation of the original curve that our samples give us.

If we want to create an audio file without an original sound source, we can synthesize sounds by coming up with what the pressure vs. time graph would be if the sound were actually played. For example, a pure tone has a sine curve for a graph. The shorter the period of the sine curve, the higher the tone. Higher amplitudes represent louder pure tones. For example, if the range of our samples is -128 to 127, then

0 61 108 127 115 76 18 -45 -96 -124 -122 -90 -35 27 83 119 126
could be the series of samples for our hypothetical sine curve.

The .au file format
An audio file must contain information about the sample range and frequency in order to be played back correctly. Different formats specify different ways of recording this information. For this lab, we will create .au files. .au files begin with 6 pieces of information, each written as a 32-bit integer. Common values are given below.

Magic number 0x2e736e64 (".snd" in ASCII)
Data offset 24 (bigger if there is optional data between the header and samples)
Data size -1 (most players will be able to figure it out on their own)
Encoding format 2
Sample rate, in Hz 44100
Number of channels 1
By setting up the header this way, the rest of the file can contain samples in the range -128 to 127.

Assignment

You must complete the part of the AudioClip class that reads and writes the files. Your code to read .au files will go in the AudioClip constructor and the code to write .au files will go in the write method. Both methods will make use of the RandomAccessFile class. In particular, you will need to use the following methods and constructor.

More information can be found, of course, in the Java documentation.

In the constructor, you should

To debug this code, you should see what you've read from the header. The values should be the same as in the table given above, except for the offset field. You may also want to display the first few samples you read, which should be 7 7 7 7 6 7.

In write, you should

To test your output files, you can play them in Windows Media Player.

Extra credit

Modify write to put a message in the file after the header. To do this, you will have to add code after you write the header but before you write the sample data that

Files

The files are contained in a Java archive. You should edit AudioClip and run AudioFun. The archive contains an audio sample called posse_clip.au to test your code on.

Exercises

To be done individually.
  1. Coming later?

Submissions

Submit the source code (.java file) for AudioClip.