Asynchronous sound now

master
kaqu 2 years ago
parent 74fbb2607f
commit aec4debea9
  1. 2
      README.md
  2. 5
      game_objects/pong_game.py
  3. 5
      game_objects/pong_object.py
  4. 29
      game_objects/pong_sound.py
  5. 9
      game_objects/pong_viewserver.py

@ -15,8 +15,6 @@ I selected **libevdev**, the [reference](https://python-libevdev.readthedocs.io/
Also, we will have to play some game typical sounds. I chose the **playsound** [library](https://pypi.org/project/playsound/) as the enabler.
Unfortunately it does not support async operation w/ linux, yet for short samples that does no significant harm!
Almost forgot: You'll have to have [pyqt5](https://pypi.org/project/PyQt5/) installed
(see also the following passage on virtual environments for 'automatic' installation).

@ -10,13 +10,12 @@ Pandemic Pong Game Engine
import time
from random import random
from playsound import playsound
import game_objects.pong_constants as pgc # Pong global constants
import game_objects.pong_globalvars as pgv # Pong global variables
from game_objects.pong_player import PongPlayer
from game_objects.pong_object import PongObject
import game_objects.pong_viewserver as vs # Pong TCP view server
from game_objects.pong_sound import AsyncSound
class PongGame:
"""Actual game mechanics (mostly ;)"""
@ -117,7 +116,7 @@ class PongGame:
return False
if (self.playsound != pgc.NOSOUND) and (pgv.bIsLocal == True): # Local play, sound output right away!
playsound(pgc.sounds[self.playsound])
AsyncSound(pgc.sounds[self.playsound])
self.playsound = pgc.NOSOUND
return True

@ -8,11 +8,10 @@ Pandemic Pong Objects logic (i.e. ball ;)
"""
from random import random # For random start data ...
from playsound import playsound # Play some sounds
import game_objects.pong_constants as pgc # Pong global constants
import game_objects.pong_globalvars as pgv # Pong global variables
from game_objects.pong_sound import AsyncSound
class PongObject:
"""Ball object"""
@ -65,7 +64,7 @@ class PongObject:
self.y = sum_y
if (self.playsound != pgc.NOSOUND) and (pgv.bIsLocal == True): # Local play, sound output right away!
playsound(pgc.sounds[self.playsound])
AsyncSound(pgc.sounds[self.playsound])
self.playsound = pgc.NOSOUND
return bChanged

@ -0,0 +1,29 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
pong_sound.py
Pandemic Pong Sound Generation ;)
"""
import threading
from playsound import playsound # Play some sounds
class AsyncSound(threading.Thread):
"""Async. (non-blocking) sound generation"""
def __init__(self, soundfile):
threading.Thread.__init__(self)
self.soundfile = soundfile
self.daemon = True # May be terminated any time ...
self.start()
def run(self):
"""Called async.(!) from start() s.a.!"""
playsound(self.soundfile)
print("Played: {}".format(self.soundfile))
if __name__ == "__main__":
print("***** pong_sound has no function, call 'pandemic_pong.py'!")

@ -13,12 +13,11 @@ import struct # Packing/unpacking
import threading, socket, socketserver
from http.server import BaseHTTPRequestHandler, HTTPServer # TODO: May be replaced by raw TCP server?!
from playsound import playsound
import game_objects.pong_constants as pgc # Pong global constants
import game_objects.pong_globalvars as pgv # Pong global variables
from game_objects.pong_player import PongPlayer
from game_objects.pong_object import PongObject
from game_objects.pong_sound import AsyncSound
TCP_PACKET_SIZE = struct.calcsize(pgc.TCP_TRANSFER_FORMAT) # Size of receiver buffer
server_addr = None
@ -106,11 +105,11 @@ class ViewServer:
# Player2: player_index, state, x, y, delta_x, delta_y, color, delay
# Ball: x, y, w, h, delta_x, delta_y, color, delay, playsound
datatype, sendgame.state, sendgame.delay, sendgame.p1_game, sendgame.p2_game, sendgame.playsound, sendplayer1.player_index, sendplayer1.state, sendplayer1.x, sendplayer1.y, sendplayer1.delta_x, sendplayer1.delta_y, sendplayer1.color, sendplayer1.delay, sendplayer1.score, sendplayer2.player_index, sendplayer2.state, sendplayer2.x, sendplayer2.y, sendplayer2.delta_x, sendplayer2.delta_y, sendplayer2.color, sendplayer2.delay, sendplayer2.score, sendball.x, sendball.y, sendball.w, sendball.h, sendball.delta_x, sendball.delta_y, sendball.color, sendball.delay, sendball.playsound = struct.unpack(pgc.TCP_TRANSFER_FORMAT, data)
if sendgame.playsound != pgc.NOSOUND: # Client side sound pulse reset
playsound(pgc.sounds[sendgame.playsound])
if sendgame.playsound != pgc.NOSOUND: # Client side sound pulse reset
AsyncSound(pgc.sounds[sendgame.playsound])
sendgame.playsound = pgc.NOSOUND
if sendball.playsound != pgc.NOSOUND:
playsound(pgc.sounds[sendball.playsound])
AsyncSound(pgc.sounds[sendball.playsound])
sendball.playsound = pgc.NOSOUND
return 1 # OK
except BlockingIOError: # Always on empty receives ...

Loading…
Cancel
Save