Sketch in sound support
authorNeil <neil@dip.sun.ac.za>
Wed, 7 Sep 2016 19:41:04 +0000 (21:41 +0200)
committerNeil <neil@dip.sun.ac.za>
Wed, 7 Sep 2016 19:41:22 +0000 (21:41 +0200)
README.txt
data/sounds/mouth_pop_2a.ogg [new file with mode: 0644]
data/sounds/silence.ogg [new file with mode: 0644]
data/sounds/sources.txt [new file with mode: 0644]
tabakrolletjie/constants.py
tabakrolletjie/loader.py
tabakrolletjie/main.py
tabakrolletjie/sound.py [new file with mode: 0644]

index c7b132f8dda03495553a2f1a042f5541399b4121..96cd26580ba6a8d2af703348599ffeaad256d0b0 100644 (file)
@@ -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 (file)
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 (file)
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 (file)
index 0000000..40b4b63
--- /dev/null
@@ -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'
+
+
index 8edc2e914390357b479b1cba77810648a532a7ef..380c27780a3e3672dcaf4c40a1d6731c633f206f 100644 (file)
@@ -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")
index 252960b42b90e9850206dc215d3a8a8e551c24b4..7f012a0f0375ef525f16a265cbd17e7291ddaec5 100644 (file)
@@ -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"))
index f09d6811c875636d38ea9d3d3a84c16ef0899806..f358403142081f1ee03313b9fdc1f1c0a510c848 100644 (file)
@@ -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 (file)
index 0000000..14a01ff
--- /dev/null
@@ -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()