It looks like you may have missed the alternative solution in my email. On Thu, Jun 7, 2018, 01:04 Alec Bennett <wrybr...@gmail.com> wrote:
> > I think the answer that sticks out to me is that if these are musical > horn sounds, you might consider using pygame.mixer.music, which will stream > the sounds as needed. > > A very good idea, and the route I originally took, but as far as I can > tell pygame.mixer.music doesn't support polyphony, and I occasionally need > to play multiple sounds at once. > > > > > > On Wed, Jun 6, 2018 at 10:59 PM, Daniel Foerster <pydsig...@gmail.com> > wrote: > >> I think the answer that sticks out to me is that if these are musical >> horn sounds, you might consider using pygame.mixer.music, which will stream >> the sounds as needed. However, this won't work if you have other music >> playing or if you want multiple horns to be able to play at the same time. >> >> A second solution is to load the sounds in the background of the game, >> especially if you're going to launch on a menu where the sounds won't be >> needed. You can pre-populate a dictionary with dummy sounds and use a >> thread to go through and load each sound into the dictionary while the user >> interacts with the menu. >> >> SOUND_PATH_MAPPING = {'horn_funny': 'horn_funny.ogg', 'horn_sad': >> 'extra_sounds/horn_sad.wav'} >> >> SOUNDS = {name: None for name in SOUND_PATH_MAPPING} >> >> def load_sounds(): >> >> for name, path in SOUND_PATH_MAPPING: >> >> SOUNDS[name] = pygame.mixer.Sound(path) >> >> >> def load_sounds_background(): >> >> thread.start_new_thread(load_sounds, ()) >> >> >> If there's a legitimate chance that the player might try to use the horn >> sounds before they've finished loading, just have the horn logic check for >> Nones before trying to play or have a dummy sound object with a play method >> that does nothing. >> >> A full example of how you might do this with a class: >> https://gist.github.com/pydsigner/231c0812f9f91050dd83c744d6d5dc4b >> >> On Thu, Jun 7, 2018 at 12:31 AM, Alec Bennett <wrybr...@gmail.com> wrote: >> >>> Sorry, I left out the line where I try to save the file: >>> >>> > pickle.dump( sound_obj, open( "sound.pickled", "wb" ) ) >>> >>> >>> >>> On Wed, Jun 6, 2018 at 10:29 PM, Alec Bennett <wrybr...@gmail.com> >>> wrote: >>> >>>> I'm building a musical horn for my car with PyGame, and it works >>>> perfectly, but since it needs to load each of the 20 sounds on startup it >>>> takes about 30 seconds to load. I'm running it on a Raspberry Pi, which >>>> doesn't help of course. >>>> >>>> I thought I'd simply save the Sound objects as pickle objects, but that >>>> produces an error: >>>> >>>> > sound_obj = pygame.mixer.Sound("whatever.wav") >>>> >>>> > can't pickle Sound objects >>>> >>>> I also tried the dill module (https://github.com/uqfoundation/dill) >>>> but with similar results: >>>> >>>> > sound_obj = pygame.mixer.Sound("whatever.wav") >>>> >>>> > Can't pickle <type 'Sound'>: it's not found as __builtin__.Sound >>>> >>>> I don't imagine can think of some clever way to save the preloaded >>>> Sounds, or otherwise speed up the load times? >>>> >>>> >>>> >>>> >>>> >>> >> >