From: Neil Date: Wed, 7 Sep 2016 19:41:04 +0000 (+0200) Subject: Sketch in sound support X-Git-Tag: tabakrolletjie-v1.0.0~198 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=8f284092fa33c1a96644a5d82a785a2b259e2356;p=tabakrolletjie.git Sketch in sound support --- diff --git a/README.txt b/README.txt index c7b132f..96cd265 100644 --- a/README.txt +++ b/README.txt @@ -19,7 +19,7 @@ Fonts: data/fonts/DejaVu-Copyright.txt Sounds: - +data/sounds/sources.txt Running the Game diff --git a/data/sounds/mouth_pop_2a.ogg b/data/sounds/mouth_pop_2a.ogg new file mode 100644 index 0000000..9498f99 Binary files /dev/null and b/data/sounds/mouth_pop_2a.ogg differ diff --git a/data/sounds/silence.ogg b/data/sounds/silence.ogg new file mode 100644 index 0000000..9675519 Binary files /dev/null and b/data/sounds/silence.ogg differ diff --git a/data/sounds/sources.txt b/data/sounds/sources.txt new file mode 100644 index 0000000..40b4b63 --- /dev/null +++ b/data/sounds/sources.txt @@ -0,0 +1,25 @@ +Sources of sound files +====================== + +silence.ogg +----------- + +Notes: + Generated 2 secs of silence - dd if=/dev/zero of=silence.pcm bs=176400 count=2 ; oggenc -r silence.pcm + Generated by Neil Muller, Aug 2010 + Not copyrightable. + + +mouth_pop_2a.ogg +---------------- + +URL: + http://archive.org/download/Berklee44v5/Berklee44v5.zip +Source: + http://archive.org/details/Berklee44v5 +License: + http://creativecommons.org/licenses/by/3.0 +Notes: + Ogg generated using 'oggenc -q -1 mouth_pop_2a.wav' + + diff --git a/tabakrolletjie/constants.py b/tabakrolletjie/constants.py index 8edc2e9..380c277 100644 --- a/tabakrolletjie/constants.py +++ b/tabakrolletjie/constants.py @@ -27,3 +27,12 @@ FITTINGS_CATEGORY = 1 << 3 FONTS = { 'sans': 'DejaVuSans.ttf', } + +# Sound stuff +FREQ = 44100 +BITSIZE = -16 +CHANNELS = 2 +BUFFER = 1024 +DEFAULT_VOLUME = 1.0 + +NO_SOUND = os.environ.get("TABAK_NO_SOUND", "").lower() in ("1", "y", "yes") diff --git a/tabakrolletjie/loader.py b/tabakrolletjie/loader.py index 252960b..7f012a0 100644 --- a/tabakrolletjie/loader.py +++ b/tabakrolletjie/loader.py @@ -6,6 +6,7 @@ import os import pygame.image import pygame.font import pygame.display +import pygame.mixer from .constants import DEBUG @@ -52,6 +53,15 @@ class Loader(object): # Do we need to cache this? return font + def load_sound(self, *parts): + """Return a pygame sound""" + fn = self.full_path("sounds", *parts) + sound = self._cache.get(fn, None) + if not sound: + sound = pygame.mixer.Sound(fn) + self._cache[fn] = sound + return sound + _DATA_PREFIX = os.path.abspath( os.path.join(os.path.dirname(__file__), "..", "data")) diff --git a/tabakrolletjie/main.py b/tabakrolletjie/main.py index f09d681..f358403 100644 --- a/tabakrolletjie/main.py +++ b/tabakrolletjie/main.py @@ -8,6 +8,7 @@ from .constants import SCREEN_SIZE, TITLE from .engine import Engine from .gamestate import GameState from .scenes.menu import MenuScene +from .sound import sound def main(): @@ -17,6 +18,7 @@ def main(): pygame.display.set_mode(SCREEN_SIZE, pgl.SWSURFACE) pygame.display.set_caption(TITLE) # TODO: set an icon + sound.init() screen = pygame.display.get_surface() gamestate = GameState() diff --git a/tabakrolletjie/sound.py b/tabakrolletjie/sound.py new file mode 100644 index 0000000..14a01ff --- /dev/null +++ b/tabakrolletjie/sound.py @@ -0,0 +1,40 @@ + +from pygame import mixer + +from .constants import FREQ, BITSIZE, CHANNELS, BUFFER, DEFAULT_VOLUME, NO_SOUND +from .loader import loader + +class SoundManager(object): + + def init(self): + """This is not in __init__, because we want to delay until after + other pygame initialistion""" + self._init = False + if not NO_SOUND: + mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER) + silence = loader.load_sound("silence.ogg") + if silence.get_length() < 1: + raise RuntimeError("Sound load error - silence.ogg too short") + try: + self.play_sound("silence.ogg") + self._init = True + except Exception, err: + print "Failed to enable sound: %r" % err + + def play_sound(self, name, volume=DEFAULT_VOLUME): + if self._init: + sound = loader.load_sound(name) + if sound is not None: + sound.set_volume(volume) + sound.play() + + def play_music(self, name, volume=DEFAULT_VOLUME): + pass + + def stop(self): + if self._init: + mixer.fadeout(1000) + mixer.music.stop() + + +sound = SoundManager()