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

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



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?