ofxALSoundPlayer question

My apologies if this is in the wrong spot. But I’ve got a question about how to get a bunch of sound effect working in the ofxALSoundPlayer. Imagine sound for a bunch of effects. gunshot, hit, splat, etc. I just want to load them all in and call play on them when they are needed.

The ofxALSoundPlayer has a myId variable that is current for the last loaded sound. So would it work to loadSound a bunch of files, store the myId for each file and then add a method to ofxALSoundPlayer that did something like this

  
ofxALSoundPlayer player;  
  
player.loadSound(something);  
  
int num = player.myId;  
  
player.loadSound(something else);  
  
int num2 = player.myId;  
  
player.play(num);  
  
void ofxALSoundPlayer::play(int index)  
{  
	SoundEngine_StartEffect(index);  
	stopped = false;  
}  
  

or am I going about this the wrong way?

MattD

i actually did something quite similar to what you’re proposing. What I did is make a modified loadSound function that returns the id for the loaded sound.

the code looks something like this:
in ofxALSoundPlayer i added this:

  
UInt32 ofxALSoundPlayer::loadSoundWithId(string fileName)  
{  
	if(!SoundEngineInitialized)  
	{  
		ofxALSoundPlayer::initializeSoundEngine();  
	}  
	  
	myId = ofxALSoundGetNextID();   
	  
	SoundEngine_LoadEffect(ofToDataPath(fileName).c_str(), &myId);  
	return myId;  
	  
}  

and then a modified play function

  
void ofxALSoundPlayer::playWithId(UInt32 soundId)  
{  
	SoundEngine_StartEffect(soundId);  
	stopped = false;  
}  

and then used this to load and play the sounds

  
  
ofxALSoundPlayer soundPlayer;  
UInt32 soundBubbles = soundPlayer.loadSoundWithId(ofToDataPath("audios/sound_bubbles.caf",false));  
Uint32 soundEngine = soundPlayer.loadSoundWithId(ofToDataPath("audios/sound_engine.caf",false));  
soundPlayer.playWithId(soundMachine);  
soundPlayer.playWithId(soundBubbles);  
  

not sure if that’s the best way to do things but it seems to work ok.

oops doube post… sorry :confused:

whoa. haha. you guys are overcomplicating it. nice work though!

ofxALSoundPlayer is designed similarly to ofSoundPlayer, where it keeps track of it’s ID on its own, and works right out of the box.

so, if you had 3 sounds you wanted to load, you would declare

  
  
ofxALSoundPlayer sound1;  
ofxALSoundPlayer sound2;  
ofxALSoundPlayer sound3;  
  

in your .h file

and in your .cpp

  
  
sound1.loadSound("firstSound.caf");  
sound2.loadSound("secondSound.caf");  
sound3.loadSound("thirdSound.caf");  
  
  
sound1.play();  
  
sound2.setPan(0.9);  
sound2.play();  
  
sound3.setVolume(0.2);  
sound3.play()  
  

etc…

Correct me if I’m wrong but doesn’t that method make doing positional audio more difficult as you have to update the listener location for each sound instance ? or do stuff like globally set the volume of multiple clips playing at the same time ? I understand that you’re trying to maintain the same functionality as ofSoundPlayer, but I was trying to do some game-like audio where you have a 3d environment with multiple sound emitters positioned in 3d space. The OpenAL model is that you can have one listener with multiple sound sources in a sound environment, and from what I can tell that’s what SoundEngine was supposed to do. I say “supposed to” as I haven’t had much luck getting positional audio to work. I did come across this (http://stormyprods.com/SoundEngine/ ) which mentions that Apple has removed SoundEngine from the SDK examples as there were significant problems with the code. The link has a modified version of SoundEngine but it’s not a drop in replacement.

I’ve used the modified version a bit too. it’s actually really nice, except it can’t do looping sounds, and has a few other issues. I was thinking about posting an ofxALSoundPlayer that wraps it as well, since I already have that basically done.

It doesnt make it harder. I guess volumewise sure, you have to call it on each sound, but for listener position stuff, if you call it on any sound it changes the global listener position. I should have made these methods static to be clear, but any method that has ‘ofxAlSoundPlayerSet’ in front of it basically changes a global property.

ah yes that’s way less complicated. I still haven’t figured out how to do positional audio properly though. For the time being I’ve moved to FMOD which works just fine, I’m using your awesome ofxiPhoneCoreLocation addon to create some GPS soundscapes.

Here’s an example of what I’m trying to do:
Setup two sounds, soundOne at location (0,0,0) with a referenceDistance of 20 and MaxDistance of 40, soundTwo at location (30,0,0) also with a referenceDistance of 20 and MaxDistance of 40. If you set the listener location to (0,0,0) you’d head soundOne at full volume. Set the listener location to (17.5,0,0), you’d head a blend of the two sounds. Set the listener location to (30,0,0), you’d hear soundTwo at full volume. Set the listener location to (-50,0,0) or (60,0,0) you’d hear nothing.

Anyways here’s the code is was using, right now when I run it the volume seems to be the same for both clips no matter what, i must be doing something wrong:
in testApp::setup()

  
  
sound1.loadLoopingSound(ofToDataPath("loop1.caf", false));  
sound1.setLocation(0,0,0);  
sound1.ofxALSoundSetReferenceDistance(20.0f);  
sound1.ofxALSoundSetMaxDistance(40.0f);   
sound1.play();  
  
sound2.loadLoopingSound(ofToDataPath("loop3.caf", false));  
sound2.setLocation(30,0,0);  
sound2.ofxALSoundSetReferenceDistance(20.0f);  
sound2.ofxALSoundSetMaxDistance(40.0f);   
sound2.play();  
  

in testApp::update()

  
  
//pan listener back and forth  
float xpos = sin( ofGetElapsedTimef() *0.1) * 80.0f;  
sound1.ofxALSoundSetListenerLocation(xpos,0,0);  
  

Hi
I have been making GPS soundscapes for several years (see aura project on http://stevesymons.net) on PDA’s.

HAve tested openAL and FMOD and settled with FMOD as it handles a sound world easier in code terms - also has other management advantages internally.

I am currently working taking the aura software opensource and onto other platforms… so attached is my hack of the ofSoundPlayer file.

It adds these methods to soundPlayer - and an example (btw i deleted sounds from the data folder)

void loadSound3D(string fileName, bool stream, float _x, float _y, float _z);
void set3DMinMaxDistance(float mindistance, float maxdistance);

And these globally

// --------------------- global 3D functions
void ofSetListenerLocation( float _x, float _y, float _z);
void ofSetListenerBearing( float degrees);

I did develop a 3DsoundPLayer class that extended soundPlayer but there were some problems with setting up FMOD, I got fed up with tracking it. Lazy I know. I can post if someone wants to pick up the baton.

It would be great if you tested this on an iPhone for me - my dev loop is a little sluggish at the moment!

Steve

It would be great if you tested it, played with it.

of3Dsound.zip

ah having spend an hour reading this forum i see that last post was a load of rubbish in the iPhone context… well until ofSoundPlayer gets ported anyway.

apologies.

Still the 3D stuff maybe useful in rest of OF.

:slight_smile:

i think 3d sound for the rest of OF would be great.
i have written my own wrapper for playback but i do not consider it valid enough to share.
still, what i always wondered about openAL though is - can i do multichannel audio with it?
i mean not only 5.1 and 7.1 etc … but can i decide on my speakersetup? or does the soundcard have a specific profile in its openal driver?
maybe someone knows the answer, because i am currently investigating spatial sound with professional multichannel audio cards … and i would prefer to code in openal then in CoreAudio or doing it in MAX/MSP or PD

btw, if anybody is interested - OpenAL-tutorials

This is for anyone wondering why ofxALSoundSetReferenceDistance() and ofxALSoundSetMaxDistance() don’t seem to work as expected… One would expect sounds not to be heard (gain==0) when source is beyond the MaxDistance, but this is not the case on the default setup. I think it is bc of the sound model openAL comes set with.

I found the ofxALSoundSetReferenceDistance() and ofxALSoundSetMaxDistance() to make perfect sense when setting the linear sound model:

  
alDistanceModel(AL_LINEAR_DISTANCE_CLAMPED);  

http://www.youtube.com/watch?v=7Gz2x8R01jE

1 Like