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

Re: [pygame] How to play a sound from a "file" of infinite size?



Hi,
I'm wondering, what is the file format of your audio?

If you can read your raw sample data from your audio file into a
2-dimensional Numeric array, and you are careful about mixer
initialization settings, you can use pygame.sndarray.make_sound to create
the Sound object for you.  You can make it loop and segue (channel.queue)
into other Sound objects with no gap in the buffer, but you don't have
access directly to the samples in the buffer.  The main disadvantage is
(afaik) Sound objects have to be loaded entirely into memory, so it's not
so great for a long piece of music.  You could run a loop that would read
like a 5-second piece of sample data and load it into a Sound object and
then queue it and then wait, but it might not end up being very efficient,
I don't know.

On the other hand, pygame.mixer.music will stream from several different
file formats without loading the whole thing into memory, and will support
looping, but not in a completely seamless manner.





On Mon, February 6, 2006 9:09 pm, Tomasz Primke said:
> I'd like to write a player, that would be able to play files in some rare
> format. I have a library (written in C), that can be used to play such a
> files, but I'm not sure about the way that I should use PyGame to code my
> player.
>
> On a GNU/Linux system, soundcard (a device) is a file /dev/dsp. When
> someone
> wants to play a wav file, all that must be done is write this wav file to
> this device:
>
>   $ dd if=file.wav of=/dev/dsp
>
> when "if" means the input file, and "of" means the output file (the dd
> command reads the input file and write its content to the output file).
>
> In the program written in C all the playing is done as follows:
>
>   char buffer[size]; // size = 20-40 kB
>   while (1)
>    {
>     render_buffer( buffer, size );
>     play_buffer( buffer );
>    }
>
> On GNU/Linux all the play_buffer function does is write the buffer to the
> /dev/dsp file (so the result is the same as with the dd command shown
> before).
>
>
> I could implement playing by wrapping all the code (written in C) in
> Python,
> but I'm afraid, that it would work on GNU/Linux only (because of this
> writting to the /dev/dsp file). Although I want to write a player for that
> system, I don't like the idea of giving up the portability of this
> application so easily.
>
> In PyGame documentation I have found the function pygame.mixer.Sound, that
> "loads a new sound object from a WAV file. File can be a filename or a
> file-like object. (...)". So I thought, that this file-like object idea
> sounds interesting. The problem is that I have never implemented such an
> object yet and I'm not sure how to do it.
>
> As I suppose, a file-like object is an object, that implements all the
> methods described in Python's documentation (point 2.3.9, "File Objects").
> I think I could implement such a class, but the problem is that the buffer
> from the example shown above can be "rendered" forever - so this
> file-object would be of infinite size. The question is: would the
> pygame.mixer.Sound function load such a file-like object?
>


-- 
The Moon is Waxing Gibbous (69% of Full)