This tutorial explains the code of an OALplus application that shows how to setup a listener and a sound source and play a sound using OpenAL. For a full working code see the
oalplus/001_hello.cpp file in the example directory.
First we include the OALplus' header (that includes a header) defining the OpenAL API (types, constants, functions, etc.) This header (or some other header that defines the OpenAL API must be included before including any other OALplus' headers.
Next we'll include the main header that pulls in everything from OALplus except for the wrapper aroung the AL Utility Toolkit (ALUT),
followed by the header that includes ALUT (ALUT is a library that is usually distributed separately from OpenAL and because of this, the wrapper is also included separately from the rest of OALplus):
We will also need to put the main thread to sleep for a while and we use the classes and functions implemented in the standard library for this purpose, so we include the
The main function of this example is pretty straightforward:
First we create an instance of the
Device class. We use here the default constructor that opens the default OpenAL device:
Next we need to create an OpenAL context and make it current. This is done by constructing an instance of the
We use the ALUT library to load the 'Hello world' sound that we are going to play, so we will need an instance of ALUtilityToolkit:
Now we can set the listeners attributes; its position, orientation and velocity. In this example the listener is stationary, located at the origin and oriented to be looking in the direction of the negative-Z axis, while 'up' is specified as the positive-Y axis.
In order to be able to do audio playback we need the sound samples and store them in a Buffer object. In this example the ALUT's
CreateBufferHelloWorld function is used to load the sound samples and store them in a Buffer object:
Next we need to create a Source object,
enqueue the buffer containing the sound sample data to the source's internal queue,
set its position (in this example the source is also stationary and located at (0, 0,-1):
and now we can tell the source to start playing the enqueued sounds:
After starting the playback we wait for 2 seconds for it to finish,
and the application can finish its execution.