OALplus (0.52.0) a C++ wrapper for OpenAL

Hello OpenAL

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.

#include <oalplus/al.hpp>

Next we'll include the main header that pulls in everything from OALplus except for the wrapper aroung the AL Utility Toolkit (ALUT),

#include <oalplus/all.hpp>

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):

#include <oalplus/alut.hpp>

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 chrono and thread headers:

#include <chrono>
#include <thread>

The main function of this example is pretty straightforward:

int main(int argc, char** argv)
{

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 CurrentContext class:

oalplus::CurrentContext context(device);

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:

oalplus::ALUtilityToolkit alut(false, argc, argv);

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.

listener.Position(0.0f, 0.0f, 0.0f);
listener.Velocity(0.0f, 0.0f, 0.0f);
listener.Orientation(0.0f, 0.0f,-1.0f, 0.0f, 1.0f, 0.0f);

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:

oalplus::Buffer buffer = alut.CreateBufferHelloWorld();

Next we need to create a Source object,

enqueue the buffer containing the sound sample data to the source's internal queue,

source.Buffer(buffer);

set its position (in this example the source is also stationary and located at (0, 0,-1):

source.Position(0.0f, 0.0f,-1.0f);

and now we can tell the source to start playing the enqueued sounds:

source.Play();

After starting the playback we wait for 2 seconds for it to finish,

std::chrono::seconds duration(2);
std::this_thread::sleep_for(duration);

and the application can finish its execution.

return 0;
}


Copyright © 2010-2014 Matúš Chochlík, University of Žilina, Žilina, Slovakia.
<matus.chochlik -at- fri.uniza.sk>
<chochlik -at -gmail.com>
Documentation generated on Mon Sep 22 2014 by Doxygen (version 1.8.6).