Programming Lab 1
|
Step 1: |
Create a subdirectory once you are logged into mambo. You can do this
directly on the macintosh, or you can type these commands in mambo:
mkdir progYou can, of course, name the directory prog anything, or you could put the directory into your class directory, etc. |
|||||||||||||
Step 2: |
In the mambo terminal, go into the directory you just created. To
do this, type:
cd progWhere prog is the name of the directory which you created in Step 1. |
|||||||||||||
Step 3: |
Now you are ready to start programming. Here is a program which
you should write (or copy and paste) into a file called
noise1.cpp on mambo (click here
for copying the file).
This program will write one second of whitenoise to the file test.wav in your current directory when it is run. To write the program in a file, you can use some macintosh text editor. In linux, you can try pico which is a simple text editor which is very easy to learn. If you want to learn a more powerful text editor, then you should learn to use vi or emacs. Ask me and I can get you started with one of them. | |||||||||||||
Step 4: |
To compile the program run the following command in the mambo
terminal:
mkprog noise1This will call the mkprog script which compiles the program and links it to the soundfile library. |
|||||||||||||
Step 5: |
Now that the program has been compiled, there should be a program
in the current directory called noise1. To run the
program type:
noise1This will create a file in the current directory called test.wav. Open up the soundfile in the Peak sound editor on a macintosh to see the waveform of the sound. |
|||||||||||||
Step 6: |
Now that you have compiled and run the program, let's look at the
individual pieces of the program.
The program is written in C/C++, so if you know how to program in
C/C++, then you can skip down to the exercises.
All C program contain at least one function called main. Here is the shorted program which can be written in C: int main(void) { }There are four components to the program:
Inside of the main function, an output soundfile is prepared. Line 7 is where the soundfile is created with a name of test.wav. The soundfile needs to know how many channels and what the sampling rate is, so there is a variable called header which was created to give it that information. Lines 13-16 do all of the work in the program. This is a for loop which means that the code on lines 14 and 15 are called continuously until the loop is exited. The structure of a for loop is: for (start; condition; increment) { }start is the starting code which usually sets a counter to 0, as it does for the noise1 program. The condition code is used to check to see if the loop should be run (again). If the condition is true, then do the loop one more time; otherwise, stop looping. After each loop, the code in increment is called. This code is usually adding one to the counting variable which was initialized in start. For the loop on lines 14-16, the looping variable is i. It is first set to 0 at the start of the loop. The loop is run until i = sampleCount. sampleCount is set to the value 44100, which is one second of sound at the sampling rate of the soundfile. So, the loop will be run 44,100 times before the loop is exited. What is happening inside of the for loop? There are two lines of code which are being called: sample = drand48() * 2 - 1; soundfile.writeSampleDouble(sample);The first line generates a random number in the range from -1 to +1. drand48 is a builtin function (in linux) which will generate a random floating point number from 0.0 to 1.0. This number is then scaled to get it into the range |
|||||||||||||
Exercises: |
|
wget http://peabody.sapp.org/class/350.867/lab/prog1/soundfile-2.0.tgz
Now unpack the compressed file:
tar xvzf soundfile-2.0.tgz
Change directory into the soundfile directory:
cd soundfile-2.0
Now compile the library:
make libraryOSX people will have to modify the Makefile to get it to compile correctly. These changes should be done in the osx-specific file above.
Once the library is compiled, you can compile the example programs in the examples directory by typing this command:
make examples
To compile your own programs, place them in the examples directory and type the command:
make programWhere program.cpp is the name of the program you placed into the examples directory.
To compile your program outside of the example program, you can modify the mkprog script which is used to compile the programs on mambo.