[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, Lenard Lindstrom wrote:

> Marcus von Appen wrote:
>> On, Tue Feb 19, 2008, Alistair Buxton wrote:
>> 
>>   
>>>> I'm an oblivious idiot :-). A buffer method's already available in the
>>>> current SVN tree.  You can use pygame.Sound (your_buffer) safely, the
>>>> buffer just has to match the mixer criteria.
>>>> 
>>>> Due to the SDL_Mixer implementation however, the buffer will be copied,
>>>> so a manipulation of the buffer object after creating the Sound will not
>>>> have any impact on it.
>>>> 
>>>>       
>>> I'm looking at that code right now. It looks like it tries to copy it,
>>> and if it can't, it raises an exception. But then there is a block of
>>> code right after which would load it in place, but can not be reached
>>> currently, because the previous code returns on error. Correct? So I'm
>>> guessing this is already WIP by somebody :)
>>>     
>> 
>> The code should be fully functional. To which unreachable lines do you
>> refer?
>> 
>>   
> Found it. The code never reaches the part that handles file objects, since 
> mixer.c:881,882 return an error if the object has no buffer interface:
> 
>        if (PyObject_AsReadBuffer (file, &buf, &buflen) == -1)
>            return -1;
> 
> The following changes to examples\sound.py confirm it.
> 
> file = open(os.path.join('data', 'secosmic_lo.wav'), 'rb')
> try:
>    sound = mixer.Sound(file)
> finally:
>    file.close()
> 
> Running the modified sound.py raises an exception:
> 
> Traceback (most recent call last):
>  File "sound.py", line 21, in <module>
>    sound = mixer.Sound(file)
> TypeError: expected a readable buffer object

Sure, because file() and open() return file objects, not buffers. The
Python File object do not implement a buffer interface, but you can
easily help yourself using

fp = open (your_file)
sound = pygame.mixer.Sound ((buffer(fp.read ())))

Regards
Marcus

Attachment: pgpYX5ydzM7KW.pgp
Description: PGP signature