New OpenAL based ofSoundPlayer

Ok, so thanks to the nice clean code from Laurent @ SFML (http://www.sfml-dev.org/) I was able to create a wrapper for an OpenAL sound player. It uses Libsndfile for most file format loading but can also load .ogg files (although no mp3 yet).

This is an of addon which enables you to plug different sound players in or out depending on your needs. So if you want to ditch FMOD in favour of a totally open source solution and use OpenAL then this is for you :slight_smile:

So far there are FMOD and OpenAL backends, which you can switch by changing a #define statement. Shout out to Memo for the base classes which sped up the development of this package.

It’s an alpha release of sorts, but the OpenAL code is pretty solid. However, there are a few small differences from the current FMOD based ofSoundPlayer:

  • setMultiplay() only works on mono samples. In the FMOD version it also works on stereo samples. Neither version works with streams.

  • Streamed sounds can be mono or stereo, but the stereo versions cannot be panned. Stereo samples however _can_ be panned, as can mono samples.

  • ofxSoundGetSpectrum() is not yet implemented in the OpenAL version

  • setPosition() does not work on OpenAL streams (it works on samples though). but it will in the future

  • setSpeed() in the OpenAL player does not accept negative values, i.e. it can’t play backwards…yet!

I’ve implemented some experimental effects using the OpenAL EFX extension. They don’t work on OSX, work somewhat on Windows and work best on Linux. Sorry…this is not high priority right now.

If you are using an older version of OpenFrameworks (prior to 0.062) you may need to make a small modification to ofxThread. Please add this function to the ofxThread addon:

In ofxThread.cpp:

  
  
void ofxThread::waitForThread()  
{  
  if (threadRunning)  
  {  
      // Wait for the thread to finish  
#ifdef TARGET_WIN32  
  
      WaitForSingleObject(myThread, INFINITE);  
       CloseHandle(myThread);  
#else  
      pthread_join(myThread, NULL);  
#endif  
      myThread = NULL;  
  
      // Reset the thread state  
      threadRunning = false;  
  }  
}  
  

In ofxThread.h:

  
  
void waitForThread();  
  

The addon example can be downloaded here:
http://code.google.com/p/digitalstarcode/downloads/list

On Windows and Linux I am using the OpenAL soft implementation (http://kcat.strangesoft.net/openal.html)
On OSX I am using the default OpenAL framework.
On Linux you need to install libsndfile and the OpenAL development packages from the repos:

sudo apt-get install libsndfile1-dev
sudo apt-get install libopenal-dev

I’ve included a static library of openal-soft for linux even though it is also in the repositories because the package version is not the latest, and the latest also had a bug in the echo fx code (which is fixed in my version thanks to the openal-devlist)

There are codeblocks projects for Linux and Windows, and an XCode project.
I’ve probably forgotten something, but give it a try and let me know how you go…

tested in 10.6 and works as advertised.

this is really nice. i really like the move that’s getting made from library-specific implementations to more general interfaces that can have different libraries hooked up to them all throughout OF. very curious to see how this one in particular comes along b/c even though fmod is really powerful…well, you know :slight_smile:

If you want to switch between FMOD and OpenAL then you need to change the #defines in ofxSoundPlayerEngine.h
Uncomment only one of these:

  
//#define USE_FMOD  
#define USE_OPENAL  

One thing I forgot to mention is that if you want to make it a regular addon on OSX you will need to add the OpenAL framework.

Also, the libsndfile static library is only built for Intel machines. It’s not possible to build a universal binary on just one machine, I would need a PPC mac which I don’t have:
http://www.mega-nerd.com/libsndfile/FAQ.html#Q018
http://www.mega-nerd.com/erikd/Blog/Cod-…-sx-ub.html
If someone wants to build a combined i386+ppc binary I would be happy to include it.

kool work! I wanted to get into openAL anyways, so your code will be a great starting point!

Thanks Moka! Actually I learnt a lot of stuff from SFML, which is where most of the audio code comes from. However I’ve also added quite a bit of additional code, and in the next release I’m going to implement re-use of OpenAL sources with playing samples, so that you can load as many samples as you want without running out of sources to play them, something like this:
http://benbritten.com/2009/05/02/lots-a-…-in-openal/

hey grimus,

I have had a look at the ofSoundPlayer recently. Nice work btw.

There was an issue I ran into with multiplay in the soundplayer. It worked fine on osx but caused an ugly runtime crash in windows (after using multiplay for maybe 15 times or so).

I changed a few things in the play function which seemed to have solved the issue:

itr != multiSound.end() instead of itr < multiSound.end()

break instead of itr = multiSounds.end()

It’s a bit late but maybe this patch is still of use.
Best,

Aaah nice…I’ll test this and include the fix.
The multiplay was a late addition, and wasn’t very well tested I guess :slight_smile:

I still have plans to rewrite parts of this to allow for large numbers of samples, because currently I think it will fail to load more than a certain number of sounds, dependent on the hardware if I remember (it fails to allocate a new ALSource). What I need to do is create a pool of currently active sounds, swapping unused ones out.

grimus- this looks awesome. have you made any progress with the re-write? even if not it looks really good so far. have you tried running it on an iphone? it would be good to have a good replacement for the very jankey openAL library that I have in there at the moment.

i tried getting it to compile before but I had some problems… i can’t remember what they were though, so I’ll try again and let you know haha

Hey thanks :slight_smile:

No, apart from stefanix’s bugfix that has gone into the svn I haven’t made any major changes. I don’t have an iPhone to test unfortunately, but I might be doing a project in the future with the iphone4 if I can get my hands on one…

I’m pretty sure I’ve got the ofxSoundPlayer working on 0.061 OF…but let me know if you have any weird compile issues…

This can be prodded around a bit to work on iOS. The hardest part is dealing with libsndfile so I just grabbed a prebuilt binary from another project, http://github.com/mhroth/ZenGarden/tree/master/src/ to be exact and used the OSX header included in ofxSoundPlayer. Other than that it’s just shuffling a few things around to get it building.

another thing to be aware of is that libsndfile is GPL, which means you can’t put it in the app store.

-d

I just set this up and so far it works great. Putting the open in oF. Thanks!

I 'd like to use this but i’m struggling a lot in windows :? with visual studio 2008. i receive 366 errors my log too long…
i have added libsndfile.lib to my lib and sndfile.h to the include directory.and in the project src… what else? I’ve see the codeBlock project for windows but i don’t know how to use codeblock. Anyone have an example for visual studio?
Thanks

edit: now i’have build it and works fine in debug mode!

if i have time i’ll post the visual studio project!

So I thought I had this configured…

When I run the ofxSoundPlayerExample I get:

  
  
error: FreeImage.h: No such file or directory  
  

highlighting libs/openFrameworks/graphics/ofImage.h

What’s going on here?

Also, Grimus, in the initial instructions you state to add the waitForThread() function to the ofxThread.cpp and .h files. I’m using 0062 Linux 64 and this function already exists in both locations, but with slightly different parameters. Replace it anyway or keep what’s in the package?

Also, could someone write a quick “OpenAL in openFrameworks for Dummies” outlining the basic steps? Because I’m a dummy and I’m sure I’m missing something obvious here. And I’m sure it would be helpful for someone else in the future.

Thanks!

Hi,

ofxSoundPlayer hadn’t been updated in a while, so it was still using the 0.06 OF version I think. I’ve updated the Linux project for 0.062, and no, you don’t need to worry about changing anything in ofxThread as this is not necessary anymore.

Get the latest version here:
http://code.google.com/p/digitalstarcode/downloads/list

It’s version 0.1.3

By the way, the next major version of OF, 0.07, will have an OpenAL player by default in the core, at least in the Linux package, so you probably won’t need this addon once that comes out.

Thanks Grimus,
One more question:
Where do I place these files?
I put the ofxSoundPlayerExample in the addonsExamples folder but when I run it I get this error message in reference to src/ofxSoundPlayer/OpenAL/OpenAL.h line 220:

  
  
'ALenum was not declared in this scope'  
  

And what files/folders and where do I need to put them in order to access the ofxSoundPlayer libraries?

Thanks!

I haven’t turned this code into a regular addon. It’s completly self-contained. You should be able to run it directly from the codeblocks project. Putting it in the addonsExamples folder is fine.

Probably you don’t have the OpenAL development libraries installed. I may have forgotten to mention that.

just do:

sudo apt-get install libopenal-dev

Yep, I needed the dev libraries.
Installed and now I get the message:

  
  
cannot find -lopenFrameworksDebug  
  

I tried rebuilding the openFrameworksCompile project, but not luck.

Sorry to pester you with so many questions, but how do I resolve this?

Many thanks!

It seems you haven’t compiled the debug version of the OF library.

If you have OF setup correctly, you should be able to open the ofxSoundPlayerExample_linux.workspace and then rebuild both the Release and Debug versions of the OF lib.

From the “build” menu of codeblocks, select “Rebuild” or “Clean” and then “Build”.

also if you run the install_dependencies script it should compile both Debug and Release versions of the OF library