2019-05-17 01:25:14 (edited by magurp244 2019-05-17 23:38:48)

Adding audio can be fairly straightforward, but where and how complex it is can depend on a number of factors and personal tastes. Here's an example of how to add audio to a Pygame framework:

import pygame
from pygame import mixer
import sys

def Example():
#initialize pygame
    pygame.init()
#initialize sound mixer
    mixer.init()
#create display
    window = pygame.display.set_mode([640,480])
#load sound
    sound = mixer.Sound('tone5.wav')

#main update loop
    while True:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
            #if space is pressed, play sound
                if event.key == pygame.K_SPACE:
                    sound.play()
            #if escape is pressed, quit
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit(0)

    #update window
        pygame.display.update()

Example()

Keep in mind that Pygame and Pyglet typically only handle basic audio, if you want advanced audio like HRTF, EFX, or 3D positional audio you'll need to use a library like OpenAL, such as with my examples. I can provide other examples for Pyglet, or another Pygame example using OpenAL, if you prefer.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2019-05-17 18:21:23

magurp244 wrote:

Adding audio can be fairly straightforward, but where and how complex it is can depend on a number of factors and personal tastes. Here's an example of how to add audio to a Pygame framework:

import pygame
from pygame import mixer
import sys

def Example():
#initialize pygame
    pygame.init()
#initialize sound mixer
    mixer.init()
#create display
    window = pygame.display.set_mode([640,480])
#load sound
    sound = mixer.Sound('tone5.wav')

#main update loop
    while True:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
            #if space is pressed, play sound
                if event.key == pygame.K_SPACE:
                    sound.play()
            #if escape is pressed, quit
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit(0)

    #update window
        pygame.display.update()

Example()

Keep in mind that Pygame and Pyglet typically only handle basic audio, if you want advanced audio like HRTF, EFX, or 3D positional audio you'll need to use a library like OpenAL, such as with my examples. I have provide other examples for Pyglet, or another Pygame example using OpenAL, if you prefer.

Thank you for the quick reply! This is helpful.

What kind of basic audio do  the libraries like pygame and pyglet provide?

Thanks again.

Signature

2019-05-17 23:37:49 (edited by magurp244 2019-05-17 23:39:30)

Pygame only supports stationary sounds, so you can't position them for a stereo effect, but you can adjust the volume and fade out the sound, you can checkout pygames audio documentation [here] and [here].

Pyglet in the last few versions has improved their audio capabilities a bit, using a bit of OpenAL it supports 3D positional and stereo audio, along with pitch and volume control. You can read up more on pyglets audio documentation [here] and [here]. In the past i've modified Pyglets bindings to support more of OpenAL's advanced functions, you could still do this, but it may be more straightforward to use the OpenAL wrapper script in my examples instead.

As mentioned, more advanced features like Reverb or Echo effects, Filters, or HRTF aren't supported in Pygame or Pyglet natively, so if you want to get fancy you'll need to dig deeper into something like OpenAL.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer