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

Re: [pygame] Sound to String



PyMike wrote:

> 
> I tried that out, and it got close to working. But I get an "unrecognized
> file-type"' error
> Code is below. What did I do wrong?
> 
> f = open("shoot.wav")
> s = f.read()
> snd = repr(s)
> print snd


Sorry, I wasn't clear. You don't need to repr() the string if you are
using it directly. You would only do that if you were writing it to a
file in the form of a string, like so:

f = open("sound.py", "w")
f.write("snd = " + repr(s))

Then sound.py becomes the basis of your decoding script. The string you
pass to mixer.Sound should look like lots of these: "\x00", not lots of
these "\\x00".

BTW, if you are concerned about the length, as other threads would
suggest, you would be better off using base64 encoding, possibly with a
bz2 stage.

import binascii  #or base64 (same functions with different names)

f = open("shoot.wav")
snd = f.read()

b64 = binascii.b2a_base64(snd)

# save b64 to file like above, then

snd = binascii.a2b_base64(b64)
#etc

To compress the wav before base64 encoding it, try bz2.compress().


douglas