Audiere        

About

Audiere is a portable C++ sound library licensed under the LGPL. It can load and play sounds in WAV, MP3, Ogg Vorbis and FLAC as well as a couple of other formats. However, it does not support 3D positional audio which may easily be a show-stopper in a 3D environment. Still, it's easy to use and implement without any additional wrapper, so it's worth a try if the lack of 3D audio does not bother you.

Compiling/Installing Audiere

Linux

There's a good chance your distribution has Audiere available in its package database. Just don't forget to install the dev package, as well.

MS Windows

Visual C++

There is a binary Win32 version available on Audiere's website that you should be able to use. If you want to compile it yourself, the source package contains all the third-party dependencies you need for VC++ and has project files for VC++ 6 and 7.

MinGW

For MinGW, you will first need to build all the dependencies for Audiere, which include libogg, libvorbis from xiph.org, libspeex, libdumb and FLAC. Most of them can be compiled with MSYS and the standard

./configure --prefix=/mingw && make && make install

command, dumb has instructions for MinGW in its readme.

Note: I had some problems with FLAC. For a start, don't use version 1.1.3, I couldn't compile Audiere against it. Use 1.1.2. In addition, I got errors when building FLAC. However, they only appeared building the additional libOggFLAC parts which are not important. So ignore the error and just copy the headers and the created libFLAC and libFLAC++ to your mingw installation.

With the dependencies built, I opened the Visual C++ project file in Code::Blocks, adjusted the paths and library names and hit compile. You need to remove the paths pointing to Audiere's third-party folder. Oh, and do run

strip --strip-all audiere.dll

This made the size of the DLL about ten times smaller for me.

Important: In the Code::Blocks build settings, you MUST add the defines HAVE_DSOUND and/or HAVE_WINMM, otherwise you'll be left with only the "null" sound device and therefore without the possibility to actually play any sound.

Using Audiere

Audiere is pretty straight-forward to use. The only annoying thing about its API is that, despite being C++, it does not use exceptions to propagate errors. You have to check the return values against 0. Audiere uses reference-counted smart pointers, so you don't need to worry about cleaning up memory.

Here's a very simple console program which will try to play all the files it has been given on its command line:

#include <audiere.h>
 #include <iostream>
 using namespace audiere;
 using namespace std;
 
 #ifdef WIN32
 #include <windows.h>
 void wait(int msecs) 
 {
   Sleep(msecs);
 }
 #else  // assume POSIX
 #include <unistd.h>
 void wait(int msecs) 
 {
  usleep(msecs);
 }
 #endif
 
 int main(int argc, char** argv)
 {
   // open audiere's standard device for the used platform
   AudioDevicePtr device = OpenDevice();
   if (!device)
   {
     cerr << "Could not open audiere device.\n";
     return 1;
   }
   
   for (int i = 1; i < argc; ++i)
   {
     // load the file as a new output stream and play it
     OutputStreamPtr sound = OpenSound(device, argv[i]);
     if (!sound)
     {
       cerr << "Failed to open " << argv[i] << "\n";
       continue;
     }
     // play the sound
     sound->play();
     
     // wait until the sound has finished playing
     while (sound->isPlaying())
     {
       // call the audio device's update function periodically
       // the documentation states that this is probably unnecessary on most platforms,
       // but it increases portability.
       device->update();
       wait(100);
     }
   }
   
   // no need to clean up; audiere takes care of that
   return 0;
 }

This should give you a good idea of audiere's basics. Don't get confused by the naming of OutputStream: If you actually want streamed playback (for background music etc.), you need to call OpenSound with true as a third parameter. Check back with Audiere's API reference for anything else you need.

Using Audiere in an OGRE application is just as simple: Create your Audiere device somewhere with a call to OpenDevice and load sounds wherever you need them with OpenSound. For maximal portability it's a good idea to call device->update() somewhere in your frame listener, but it shouldn't matter on Windows or Linux.

And that's pretty much it, except...

Using Audiere with OGRE's resource system

Unless you have your own external resource file management or want to hardcode the paths into your executable (don't do this), you'll probably want to use OGRE's resource system to load your sounds. This is easy enough to achieve, all we have to do is provide an implementation of Audiere's abstract File class which reads from OGRE's resource system. You can find the code below. This is how to use it in your project:

#include "OgreAudiere.h"
 using namespace audiere;
 
 // ...
 
 FilePtr file = OpenOgreResourceFile("filename");
 OutputStreamPtr sound = OpenSound(device, file);

Here is the necessary header:

// OgreAudiere.h
 
 #ifndef __OGRE_AUDIERE_H__
 #define __OGRE_AUDIERE_H__
  
 /****************************************************************
  * Simple Audiere binding for OGRE
  * by Holger Frydrych
  *
  * This binding implements an audiere::File derived class that
  * uses the Ogre Resource system to load Ogre resources. You
  * can then use such a File object with the audiere sound 
  * library to load sounds from the Ogre resource system.
  * The binding is placed in the public domain, do with it
  * whatever you want.
  ****************************************************************/
  
  
 #include <OgrePrerequisites.h>
 
 namespace audiere
 {
   // forward declaring File
   class File;
  
   // load a File from Ogre resource system.
   File* OpenOgreResourceFile(const Ogre::String& filename);
 }
 
 #endif

And here's the implementation:

// OgreAudiere.cpp
 
 #include "OgreAudiere.h"
 #include <Ogre.h>
 #include <audiere.h>
 
 using namespace Ogre;
 
 namespace audiere
 {
   namespace
   {
     // OgreResourceFile derived from audiere::File. Not for direct use
     class OgreResourceFile : public RefImplementation<File>
     {
       private:
         DataStreamPtr mData;
       
       public:
         OgreResourceFile(DataStreamPtr data)
         : mData(data)
         {
         }
         
         int read(void* buffer, int size)
         {
           return static_cast<int>(mData->read(buffer, static_cast<size_t>(size)));
         }
         
         bool seek(int position, SeekMode mode)
         {
           size_t pos = 0;
           switch (mode)
           {
             case CURRENT:
               pos = mData->tell() + position;
               break;
             case END:
              pos = mData->size() + position;
               break;
             default:
               pos = position;
               break;
           }
           mData->seek(pos);
           return true;
         }
        
         int tell()
         { 
           return static_cast<int>(mData->tell());
         }
     };
   }
  
  
   File* OpenOgreResourceFile(const Ogre::String& filename)
   {
     DataStreamPtr data = ResourceGroupManager::getSingleton().openResource(filename);
     return new OgreResourceFile(data);
   }
 }

Possible Conflict


If you get a conflict such as conflicting attribute types

Then replace the following lines of code:

int read(void* buffer, int size)
bool seek(int position, SeekMode mode)
int tell()


With:

ADR_METHOD(int) read(void* buffer, int size)
ADR_METHOD(bool) seek(int position, SeekMode mode)
ADR_METHOD(int) tell()