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

Re: [pygame] Re: Some way to pickle or otherwise save a pygame.mixer.Sound object?



The wav format is not super complicated, you could split every track into 15 second chunks and read them off disk as needed so you always have one extra 15 second chunk than you currently need. You could probably even make fake file objects that mapped to the same file on disk using seeks, and use that to buffer over the files. Then you could throw out the buffers as they were used up.

On Sat, Jun 9, 2018, 2:45 PM Christopher Night <cosmologicon@xxxxxxxxx> wrote:
Just to be clear, you need to play two different multi-minute sounds at once?

Because your example has one 13-minute track and one 3-second track. If those are the sounds you need to play at the same time, why don't you just preload the 3-second one into a pygame.mixer.Sound object and play it whenever you need it, and play the 13-minute one with pygame.mixer.music?

On Fri, Jun 8, 2018 at 7:48 PM Alec Bennett <wrybread@xxxxxxxxx> wrote:
I see that pygame.mixer.Sound supports OGG, I guess I should try that. I've never used OGG before, and I wonder if it supports OGG if MP3 would be an easy addition?

Well so much for using OGG (and probably MP3) to speed up preloading. With my 13 minute mono soundfiles, it takes 7.3 seconds to preload the wav file (44.1hz, 16 bit, mono) and 43.6 seconds to preload the OGG file... I have a feeling it's uncompressing it during preload.

My project needs polyphony, but really only the ability to play two sounds at once. I'm currently using this method because it supports that:

channel = 1 # or 2
sound_obj = pygame.mixer.Sound("whatever.wav") # long preload at this step
channel = pygame.mixer.Channel(channel)
channel.play(sound_obj)

This method of playing MP3 works beautifully without preloading, but can only play one sound at a time:

pygame.mixer.init()
pygame.mixer.music.load("whatever.mp3")
pygame.mixer.music.play()
pygame.event.wait()

I don't imagine anyone knows of a way to play two sound files simultaneously using  pygame.mixer.music? Or some other solution in Python under Linux that would do it reliably? I suppose I could slave two instances of mplayer, but I'd of course prefer to not be forking out processes.

If anyone's curious about the files I'm trying to load, I posted some test files here:





On Thu, Jun 7, 2018 at 2:30 PM, Alec Bennett <wrybread@xxxxxxxxx> wrote:


On Thu, Jun 7, 2018 at 7:59 AM, Christopher Night <cosmologicon@xxxxxxxxx> wrote:
I recommend saving the sound as a raw buffer. First in a separate script:

sound = pygame.mixer.Sound("music1.wav")
open("music1.buf", "wb").write(sound.get_raw())

Then in your game:
sound = pygame.mixer.Sound(buffer=open("music1.buf", "rb").read())

I found about 4x speedup of reading a large file on my desktop using this method. That may or may not be enough for you, especially if you combine with other tips in this thread.


Very interesting. Will try this when I'm working on this tonight.