Designing:

One part of the project was that the JAVA applet will be able to create audio data and play it out if a user clicks on a button. The only audio data supported by JAVA is the Sun's au format. Initially we thought that JAVA will have the capabilities to play sound under a JAVA page. However, after some research on the news groups and the world-wide web, we found that the audio capabilities in JAVA are very limited. Because of this, we thought it may be wise to processed the data from the graph and encoded those data into audio format. This was my responsibility in this project as well as helping my other partner debugs his JAVA program. Other responsibilities in this project are the creation of a lesson plan and introduction on amplitude modulation and fourier series.

In order to encoded a set of numbers into audio format, I must know the format of the Sun's au format. From the sound format's FAQ, man pages and MATLAB's function on the SUN workstation, I extracted the following information regarding the format of an audio file. There are two kinds of file formats in an audio file. The first type of format is self-describing. This means that the header fields of that audio file describe the particular encoding variant used in the finished audio file as well as other parameters like the sampling device or a description of the sound. The second type of format is headerless format. This kind of format will not allow any variation in the sound's parameters except in some cases the sampling frequency. It also defines a single encoding scheme used in the audio format.

By looking at the MATLAB's function and its help pages, I was able to convert a set of data or numbers into U-Law file with C codes. This U-Law file is sampled at 8000 Hz. The source code for the conversation is in appendix 1. After I have properly converted the sampled data into U-Law format, I then turn it into an au file by prefixing it with an appropriate header format. The following header format is for the au audio file from the audio format FAQ.


Typedef struct {
        int magic;              /* magic number */
        int dataLocation;       /* offset or pointer to the data */
        int dataSize;           /* number of bytes of data */
        int dataFormat;         /* the data format code */
        int samplingRate;       /* the number of channels */
        char info[4];           /* optional text */
} SNDSoundStruct;




I constructed the header with the following C source code.


static void wr_header(optr, frequency) 
{
        FILE *optr; 
        float frequency; 

        1: writehdr(optr, 0x2e736e64);          /* Sun magic */
        2: writehdr(optr, 24);                  /* header size in bytes */
        3: writehdr(optr, ((unsigned)(~0)));    /* unspecified data size */
        4: writehdr(optr, 1);                   /* Sun uLaw format */
        5: writehdr(optr, frequency);           /* sample rate by definition :-) */
        6: writehdr(optr, 1);                   /* single channel */
}





The first line of the code is the Sun audio format magic number. For most Sun's audio format files, the magic number is 0x2e746e64. This magic number is used to identify the structure as a SNDSooundStruct. It also defines what kind of sound file are the entities. The next line of the code is the offset or pointer to data. In the above case, I entered the header size in bytes as the offset of the sound file. This will tell the player program how many bytes to skip to find the data of the audio. For line 3 of the source code, I did not specify the data size. The reason for this is because I like to keep it simple. This parameter does not hurt the quality of the sound. It is just a good idea for the player or some editing programs. In line 4 of the source code, it specifies what kind of data are the audio format. I entered the value of 1 in the sound header to indicate that the audio file is a Sun U-Law format. The following is a table that indicates some of the values for corresponding format of the audio.

Value

Sound Format

0

unspecified format

1

8-bit mu-law samples

2

8-bit linear samples

3

16-bit linear samples

4

24-bit linear samples

5

32-bit linear samples

6

floating-point samples

7

double-precision float samples

8

fragmented sampled data

There are still many more sound formats. You may want to consult the audio FAQ in the news groups. Line 5 of the source code is the sample rate. The default frequency for a Sun's au file is 8000 Hz. As for the last line, it is just the number of channel. The Sun's au format asks for 1 channel. So a 1 is entered in that line.

The following is the definition for the U-Law encoding scheme. The source code is basically following the definition in its implementation of taking the sample and gets a U-Law value.

U-Law definition: ,

The values of u=100 and 255, A=87.6. Mp is the peak message value. M is the current quantized message value.

The source codes use the U-Law encoding scheme as defined above. This encoding method is sort of like an 8-bit floating point number system. I basically broke the samples from the sine wave into its floating point exponents and fraction with the frexp() function in C codes. Then I adjusted the number with a bias value to have the result between negative one and one. After I have adjusted the result, I wrote it into a file in its floating point form. Before I wrote the data file, I prefixed the file with the header of an au file as described above. The following procedures summarize how I encoded the sample value into U-Law data format.

The quality of the sound is not that good because the sampling frequency was not proper set to the message signal. The reason for this is because if I sampled the data at a reasonable frequency, the audio file will be a very large file. Sometimes it may go beyond one Mb. Therefore, I choose to sacrifice the quality of the sound over speed.

In order to process the data from my partner's inputs, I will need to create a CGI script that will accept data and process it into audio format. This CGI script is the source code in appendix 1. I choose to implement that script in C since C is easier than learning another new language such as perl even if is a very popular language for CGI scripting. I first tested my CGI script by creating a sample page that will allowed me to pass data into the encoding part of the script. From the data, the script should be able to output an appropriate audio file to click upon and we can hear the result. The sample page is in http://heppc.phys.hawaii.edu.

The idea is to use a CGI script so that the audio format can be created with the passing of the sampled data from the JAVA page. At that point, we were still testing whether if the JAVA language will allow us to send back data to the server. We found out that it is possible but have not find a way to actually implemented that yet. The reason for sending back the sampled data is so the CGI script program will be able to encode the data into audio format for the user to listen to the message signal.

Over the course of this project, our adviser, Dr. Sasaki, mentioned that there are some good sound JAVA page on the WEB. This gave us a tremendous boost to our project since we now know that we can implement the sound part in JAVA language. This takes away the limitation of speed and quality of the sound of the message signal. The quality of the sound becomes better and speed is faster since execution is done on the local machine. However, there is one short-coming to all of this. We can not save the audio file because the JAVA applet does not allow us to write or read any data on the user's machine.

The final JAVA applet is in http://www2.hawaii.edu/~vengfai/. This page is totally different from our initial interface which only composed of few input components for amplitude, frequency for only sine wave form. This new JAVA applet will be able to choose different wave forms such as triangular, sinusoidal and square waves. We also implemented the power components into the wave. The applet also contains some arithmetic on the wave forms. The arithmetic part of the applet will allow a high school student to do simple mathematics on the waves. Beside the basic mathematical operations on the waves, the applet also incorporates some filters to do basic communication concepts on the message signal. This applet will do AM and filter it to recover the message signal from a modulated wave. It also has a band-pass filter to demonstrate the basic concept of Fourier series.

next previous

Go back to table of contents