简体   繁体   中英

pygame: using pygame.mixer simultaneously from 2 threads fails

I am trying to build a little sound generator with pygame and numpy. The following code plays the data out of my numpy arrays correctly, but when I try to use this code in a module and access it either from different threads or from different processes to play two sounds at a time, one of the sounds is played before the other one instead of both at the same time

def _play_array(array, ms, vol):
    sound = pygame.sndarray.make_sound(_intern._as_int16(array))
    channel = sound.play(-1)
    channel.set_volume(vol)
    if ms > 50:
        pygame.time.delay(ms-50)
        channel.fadeout(50)
    else:
        pygame.time.delay(ms)
    sound.stop()

update:

I have tried installing audiere, but all that happens is that it comes up with a ridiculously long error traceback, but this apparently is the problem the installer encountered: /Developer/SDKs/MacOSX10.6.sdk/usr/include/stdarg.h:4:25: error: stdarg.h: No such file or directory

(edit) Now I found out it does not work on Mac OS X

update 2:

trying to use snack/tkSnack: RuntimeError: Tk not intialized or not registered with Snack

update 3:

trying to install wxPython to play the sound after writing it to a file - import wx just fails: /usr/local/lib/wxPython-unicode-2.8.12.1/lib/python2.7/site-packages/wx-2.8-mac-unicode/wx/_core_.so: no matching architecture in universal wrapper . I am getting slightly annoyed by this problem...

update 4:

see post

To completely bypass your problem you could use py audiere to make your sounds. with it you could have two different tones at once.

Using your code I don't think you can have two tones at once. This is a snippet from my frequency generator:

speakers = audiere.open_device()

tone = speakers.create_square(start_freq *2)
tone.pan = 1

tone.stop()
tone.play() 

Just have two sounds instead of one.

Sorry it doesn't use your code but perhaps this could be useful to you. :)

This has become to annoying for me. I decided to use the os-wide standard music player to play it, as none of the other methods seem to work.

os.system("open " + path)

works fine on mac.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM