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

Re: [pygame] Sound to String



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)

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

snd = binascii.a2b_base64(b64)
f = open("sound2.py", "wr").write(str(repr(snd)))
snd = open("sound2.py", "rb").read()

f = StringIO(snd)

mixer.init()
snd = mixer.Sound(f)

On Thu, Feb 28, 2008 at 7:17 PM, PyMike <pymike93@xxxxxxxxx> wrote:
f = open("shoot.wav")
s = f.read()
f.close()
print len(s)
print len(repr(s))
print len(eval(repr(s)))

217
675
217


On Thu, Feb 28, 2008 at 7:11 PM, Douglas Bagnall <douglas@xxxxxxxxxxxxxxx> wrote:
PyMike wrote:

> After doing that, this is the output:
>
> snd = 'RIFFZB\x00\x00WAVEfmt
> \x10\x00\x00\x00\x01\x00\x01\x00D\xac\x00\x00\x88X\x01\x00\x02\x00\x10\x00data6B\x00\x00y\x18\x90\x17\xaf\x16\xd7\x15\x07\x15>\x14}\x13\xc3\x12\x10\x12d\x11\xbe\x10\x1f\x10\x85\x0f\xf1\x0ec\x0e\xd9\rU\r\xd6\x0c\\\x0c\xe6\x0bu\x0b\x07\x0b\x9e\n\xd0\xfd\\\xd9\xcc\xda/\xdc\x84\xdd\xcd\xde\t\xe0:\xe1`\xe2z\xe3\x8a\xe4\x90\xe5\x8c\xe6\x7f\xe7h\xe8I\xe9"\xea\xf2\xea\xbb\xeb|\xec6\xed\xea\xed\x96\xee<\xef\xdc\xefv\xf0\n\xf1\x99\xf1"\xf2\xa6\xf2%\xf3\xa0\xf3\x16\xf4\x88\xf4\xf5\xf4^\xf5\xc4\xf5%\xf6\x83\xf6\xde\xf65\xf7\x89\xf7\xd9\xf7\'\xf8r\xf8\xba\xf8\xff\xf8B\xf9\x82\xf9\xc0\xf9\xb0\x12<*\xa9(&\'\xb0%I$\xef"\xa2!a
> -\x1f\x03\x1e\xe5\x1c\xd2\x1b\xc8'

That's way too short. Are you sure shoot.wav is complete? What is
len(s), len(repr(s)), and len(eval(repr(s))?  The last should be the
same as the first, and the same as the file size reported by the OS. The
middle one will be about 3 times as long.

Also, just to be sure you got this, where you go:

wav = StringIO(snd)

That snd shouldn't be the same as repr(s), but the same as s. repr(s) is
just a convenient way of getting it into a source file. You *don't* want
to be going snd = repr(s).

douglas

> But I still get the same error.
>
> On Thu, Feb 28, 2008 at 5:20 PM, Douglas Bagnall <douglas@xxxxxxxxxxxxxxx>
> wrote:
>
>> 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
>>
>>
>>
>>
>
>





--
- PyMike



--
- PyMike