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

Re: [pygame] Sound to String



Hi Douglas

Just to let you know, my sound will load and play with pygame without the string compression. It's a mere 0.18 seconds long. I tried some other sounds that were short and I got ~around~ the same results.

After editing sound2.py,

"RIFF\x16\xe9\x00\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00D\xac\x00\x00\x88X\x01\x00\x02\x00\x10\x00data\xf2\xe8\x00\x00\xd2$\xcf(U\xf0r+\xc3\x16\x0c\xf6\xa7\xdc2\x0bH\xf8\x95\xdcy\xd6\x06!\xbc\x05\x17\xe5\xbf\xe4\xd5\xfa\n\xea'\xedN\xfb\xec!\x055\x88\xecy\xcb\xdf\xc8#\xd2x\x16\xf1\xd0|\xf9\x1f\x02\x86\xe2\x04\xf3\xa4'G\x1c\xfd\xfb7B\x86"

import binascii
from cStringIO import StringIO
from pygame import mixer

snd = binascii.a2b_base64(__doc__)
f = StringIO(snd)

mixer.init()
sound = mixer.Sound(f)
sound.play()

I get this traceback:

>>>
Traceback (most recent call last):
  File "C:\Documents and Settings\Michael\Desktop\sound2.py", line 8, in <module>
    snd = binascii.a2b_base64(__doc__)
Error: Incorrect padding
>>>

Then I switched a2b_base64 to b2a_base64 and got this traceback:

>>>
Traceback (most recent call last):
  File "C:\Documents and Settings\Michael\Desktop\sound2.py", line 12, in <module>
    sound = mixer.Sound(f)
error: Unrecognized sound file type
>>>

I'm going to try a few other sound files real quick.


On Fri, Feb 29, 2008 at 1:56 AM, Douglas Bagnall <douglas@xxxxxxxxxxxxxxx> wrote:
hi Mike

This is your main problem:

>> print len(s)
>>
>> 217

Your wave file is broken. In 16 bit mono, 217 bytes leaves room for 94
samples when you take away the headers. That's about 1/500 of a second
at 44.1 kHz.  Your sound is not actually that short: the file is broken.

Then later you wrote:

> f = open("shoot.wav")
> s = f.read()
> f.close()
> print len(s)
> print len(repr(s))
> print len(eval(repr(s)))
>
> b64 = binascii.b2a_base64(snd)


You mean binascii.b2a_base64(s).
snd is not defined yet.

>
> f = open("sound2.py", "wr").write(str(repr(b64)))

repr() always returns a string -- there's no need for str().
Also, f here will always be None, because it is the result of
open().write() not open().

Anyway, at this point sound2.py will contain a python representation of
a sound, and nothing else, which incidentally makes it the module's
__doc__ string.

So then, if you modify sound2.py, adding the following lines:


'RIFFZB\x00\x00WAVEfmt \x10\x00[...]' #this is the existing line

import binascii
from cStringIO import StringIO
from pygame import mixer

snd = binascii.a2b_base64(__doc__)
f = StringIO(snd)

mixer.init()
sound = mixer.Sound(f)
sound.play()


It will play. At least it would if you didn't have a corrupt wav file.


>>>>>> I tried that out, and it got close to working. But I get an
>>>>> "unrecognized file-type"' error


It always helps if you say where an exception occurs. Pasting the
traceback is good.



douglas



--
- PyMike