[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] How to check sound format of mixer?



On, Tue Feb 19, 2008, Alistair Buxton wrote:

> I can't seem to find out how you check the sound format that mixer is
> using. For example, when I request 8 bit signed format, I get 8 bit
> unsigned, and all my samples sound terrible (they are raw samples so I
> have to load them into a sndarray.) But to find this out, I had to
> write a test in C using SDL_OpenAudio because even SDL_mixer cannot
> seem to tell you this. Checking the struct output by SDL_OpenAudio
> seems to be the only way to know, so if you don't call it yourself,
> you can't know, and Mix_OpenAudio hides the output from
> SDL_OpenAudio...

You can easily do it the following way. First, you know, there's only
something like AUDIO_S8 (8-bit signed), AUDIO_S16 (16-bit signed) and
their unsigned equivalents AUDIO_U8 and AUDIO_U16.

Now call pygame.mixer.get_init () and check the second value of the tuple.
The tuple has the format (frequency, format, stereo). The frequency is
interesting for us, so let's look at it closer.

For 8 and 16-bit signed, there'll be a -8 or -16 respectively. For
unsigned, it will be 8 or 16. Hooray, we finally know it. For the numpy
sndarray wrapper, I did the following to get the type easily:

    typecode = { 8 : numpy.uint8,   # AUDIO_U8
                 16 : numpy.uint16, # AUDIO_U16 / AUDIO_U16SYS
                 -8 : numpy.int8,   # AUDIO_S8
                 -16 : numpy.int16  # AUDIO_S16 / AUDIO_S16SYS
                 }[pygame.mixer.get_init ()[1]]

> Also it would be nice to have a pygame.mixer.Sound.from_buffer()
> function similar to the Surface.from_buffer() in that it does not
> require Numeric. I don't have anything against sndarray and surfarray

True. I'll put it onto my TODO list.

Regards
Marcus

Attachment: pgpGm9EKBVA9s.pgp
Description: PGP signature