Get sound file formats, number of channels

Hi to all,

I intend to allow users to upload some sounds to the game I’m building. The file format that is to be allowed is the .wav format. Presently I am using the python wave module to ensure that the uploaded file is of type .wav like this:

        try:
           read_obj=wave.open(sound_sent,'rb')
           #the rest of the code.
           ...
        except wave.Error:
          #show appropriate message.
          ...

That does work except at times even when a .wav file is uploaded it still throws the exception; I suppose due to some error in the way the file is encoded even though it works in panda3d when played without passing through that try-exception block of code. Is there a way to ensure that the file is encoded as .wav using panda3d?

The next question regards getting the number of channels. I need the channels to be 1 for certain types of sounds. Again using the python wave module:

if(read_obj.getnchannels()==1):
 #rest of the code.
...

Is it possible to get the number of channels for a .wav file using panda3d? If so, how is it done?

Thanks in advance.

From 1.9.0 and above, you can directly instrument Panda3D’s .wav loader:

from panda3d.core import WavAudio

wav = WavAudio("/path/to/file.wav")
cursor = wav.open()
if cursor:
    print "Sample rate:", cursor.audioRate()
    print "Length:", cursor.length()
    print "Num channels:", cursor.audioChannels()
else:
    print "File does not exist or is not a valid .wav file!"

You can do the same thing with .ogg file, using VorbisAudio class.

Thanks! Very handy feature.