Welcome to the new Friends-of-FPC!

Here you can find all kinds of information about the FreePascal Compiler. We have many tutorials and howtos as well as a selection of tools to help you with your programming. We also have some example codes for you. And if you want to contribute some information/ sources/ tools yourself you can do so.
Also we have finally relaunched the FoFPC forum. It's your chance for some Q&A about everything FreePascal.

Friends-of-FPC

Tutorials: Learn how to code with FreePascal.

Source Codes: A collection of examples, miscellaneous source codes and open source stuff.

Tools and Help Files: Intro- duction of some tools that might help you with FPC.

Community

Forum: Ask or answer questions about the FreePascal Compiler, programming or just babble about coding.

Contribute! Contribute your own Tutorial, Source Codes or Tools and send them to us!

Website

About: Information about Friends-of-FPC.org.

OpenAL for starters - Chapter 1 - by Delax

The first program is the example from Ulf Ochsenfahrt contained in the header ZIP file.

Program TestAL;

Uses Crt, Dos, AL;

Var
  buffer : ALuint;
  source : ALuint;

  device : ALCDevice;
  context : ALCContext;

  test : Array[0..1000] OF Byte;
  z : LongInt;

Begin
  For z := 0 to 1000 Do
    test[z] := Round(100*sin(z*(2*pi)/50.0)+128);
  device := alcOpenDevice('DirectSound3D');
  context := alcCreateContext(device, Nil);
  alcMakeContextCurrent(context);

  alGenBuffers(1, buffer);
  alBufferData(buffer, AL_FORMAT_MONO8, test, 1000, 11024);
  Writeln(alGetError);

  alGenSources(1, source);

  alSourcef(source, AL_PITCH, 1.0);
  alSourcef(source, AL_GAIN, 1.0);
  alSourcei(source, AL_BUFFER, buffer);
  alSourcei(source, AL_LOOPING, AL_TRUE);
  alSourcei(source, AL_BUFFER, buffer);
  Writeln(alGetError);
  alSourcePlay(source);
  Writeln(alGetError);
  Readkey;

  alSourceStop(source);

  alcDestroyContext(context);
  alcCloseDevice(device);
End.

Let's take it from the top. First, we define 6 variables. "buffer" and "source" are both ALuint (AL unsigned integer = DWord in FPC). "buffer" is a handle to our buffer and source is the handle to our source.

First a few OpenAL basics. OpenAL has two main objects: the listener and the source(s). The listener is you and the sources are the sources of the sounds. This is needed for 3D applications where the listener for example is your player and the sources are other players in the same level. In this example we don't need to define a listener as we want the source to be the only output.

The next variables are "device" (we use it to define the playing device) and "context" (which we use to create a device context to our playing device). People experienced in Windows or OpenGL programming will feel at home here. And finally we define an array called "test" in which we'll store our waveform and a counter variable called "z".

Now we'll start creating our waveform. To do that we'll use a for-loop with a sine formula to create a sinewave and store it in the array. Now for those who don't know how sounds "look" like: learn that first! Try downloading some tool that creates sounds out of formulas (tip: www.goldwave.com or get yourselft some documentation about it (look for matlab or at Harmony-Central).

Next we define the output. First we set the output device device := alcOpenDevice('DirectSound3D'); to the primary DirectSound device. then we'll get ourself a context context := alcCreateContext(device, Nil); and activate it alcMakeContextCurrent(context);.

We'll create the sound buffer next. This is like defining a display list in OpenGL (for those who know). You create a buffer with alGenBuffers(1, buffer);. The var "buffer" will identify this buffer from now on. With that done we define the buffer format itself. alBufferData(buffer, AL_FORMAT_MONO8, test, 1000, 11024); In our case it's an 8 bit mono buffer with 1000 steps pointing to our array and with 11KHz frequency.

Now we define the sound source. It's basically the same as defining the buffer, but here the attributes are set after another. They should be pretty self explaining.

Now we can play our source with alSourcePlay(source);and stop playing with alSourceStop(source);. When we are done, we free the context and the device alcDestroyContext(context);, alcCloseDevice(device);.

Now some example waveforms for you to play around with.

Wave Form

Normal

Example

Sine Wave

sin(2*pi*frequency*t)

Round(Frequency*sin(Counter1*(2*pi)/50.0)+128);

Saw Wave

1 - 2*abs(1 - 2*frequency*t%2)

Round(1-2*(1-Frequency*Counter1 / 100));

White Noise

amplitude - rand(2*amplitude)

Round(1 - random(12000)+8000);

Pulse Wave

int(2*t*frequency)%2*2-1

Round(((counter1 div 100) mod 2)*Frequency+200);

And that's it for now. Next time we'll port this example to windows and start with a little interactivity. 'Till then..

Delax/ Sundancer Inc.
[delax@sundancerinc.de]

Back to previous page

Useful Links









Link to us