C++ sound

Hey guys, does Panda3D in C++ support the playing of sounds?

If it does, then can anyone guide me on the basics of playing a sound?
And if it dosen’t, anyone know of a simple way of putting sound in the game? :unamused:


Yup, the Python API for sounds is nearly the same as the C++ one. Though, instead of loader.loadSfx, you will need to create an AudioManager instance, then use the get_sound function.

Reading the source does wonders:

  //   MySoundEffects = create_AudioManager::AudioManager();
  //   MyMusicManager = create_AudioManager::AudioManager();
  //   ...
  //   my_sound = MySoundEffects.get_sound("neatSfx.mp3");
  //   my_music = MyMusicManager.get_sound("introTheme.mid");

Now, you can do with my_sound or my_music whatever you would do with one in python.

cool, i have an instance of AudioManager but i can’t seem to figure out where to place the audio file for the get_sound to find it.

Also, i haven’t been able to load a model from anywhere besides the default model path “C:\Panda3D-1.4.2\models”. A solution to load the audio and model from a folder relative to the .exe would most appreciated ^^

Update: Ok, i managed to load the sound by putting it beside the exe and simply getting the sound by specifying it’s filename (still can’t seem to do this for the model though). However, i get an illegal indirection whenever i try to call the play function within the AudioSound class.

my code:

  PT(AudioManager) AM = AudioManager::create_AudioManager();
  PT(AudioSound) testAS;
  *testAS = *AM->get_sound("flute.mp3");

What about this:

  PT(AudioManager) AM = AudioManager::create_AudioManager();
  PT(AudioSound) testAS;
  testAS = AM->get_sound("flute.mp3");

Last time I tried, it worked great. Also, loading of models and sounds work great if I supply sounds or models that are in the same directory. Are you sure you are running the executable from the commandline from within the same directory as it resides? It could have something to do with the fact that panda looks in the current working directory. If you double-click your executable in explorer or nautilus it won’t execute it in the same working directory as where it is.

most probably because the play function is pure virtual.
doesnt have any definition.

and AudioManager and AudioSound seems to be an abstract class
so i went to find classes that inherit them

and i found openALAudioManager and sound
but including them and load the sound gave me some memory error
even if i didnt use openAL and use the AudioManager instead.

I wonder if its because i didnt have any lib or dll require for openAL
currently i have the required header files which is AL.h and alc.h

hmm thanks for the help pro-rsoft :smiley: i got the sound to play but seems to play only for the first few seconds and stops, i’d look more into it :slight_smile:

It might be getting garbage collected make sure you save it :slight_smile:. I remember Josh had a problem like this with sounds.

EDIT: typo

@.@ what’s betting garbage and how do i save it?

Update: Problem solved, didn’t realize i had to call the AudioManager’s update in the main loop.

You save it not in heap variable. But you solved it All bets are off!

one more problem, how do i “unload” the sound? I’d like the load the sounds relevant to each level at its start and unload them once the level is over before loading the next level’s sounds.

Note that this code is completely wrong:

  PT(AudioSound) testAS;
  *testAS = *AM->get_sound("flute.mp3"); 

Those asterisks are very bad. The second one dereferences the pointer returned by get_sound(), but doesn’t save the pointer itself (so the sound object immediately destructs). The first one dereferences a NULL, uninitialized pointer, and then copies the sound data into that NULL address space!

Surely you meant to do this way instead (similar to the way pro-rsoft wrote it):

  PT(AudioSound) testAS;
  testAS = AM->get_sound("flute.mp3"); 

Incidentally, you unload a sound just by letting its pointer go out of scope, or resetting its pointer to NULL. Since these are Panda’s magic reference-counting pointers, it can automatically unload the sound when the last reference count goes away.


Oh cool, thanks alot :astonished: