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

Re: [pygame] Sound to String



PyMike wrote:

> Hey! I've been trying to convert my sounds to strings so I can include them
> all in one python file. Can anyone help me?
> 


>>> f = open("some_short_sound.wav")
>>> s = f.read()

The string will look something like this:

>>> s[:50]
('RIFF\xa27\x00\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00D\xac\x00'
'\x88X\x01\x00\x02\x00\x10\x00data~7\x00\x00\xb8\xfb\xa0\xfa\x92\xfc')

You can copy the full string into your source, by (e.g.) saving repr(s)
into a file. You'll end up with a long line like this:

s = 'RIFF\xa27\x00\x00WAVEfmt \x10\x00[...]

Then you go

>>> from cStringIO import StringIO
>>> wav = StringIO(s)

and treat wav as a file object. For example:

>>> from pygame import mixer
>>> mixer.init()
>>> sound = mixer.Sound(wav)
>>> sound.play()


douglas