-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathaudio.py
70 lines (52 loc) · 1.55 KB
/
audio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# audio.py
# The Raskin Center for Humane Interfaces (RCHI) 2004
# This work is licensed under the Creative Commons
# Attribution-NonCommercial-ShareAlike License. To view
# a copy of this license, visit
# http://creativecommons.org/licenses/by-nc-sa/2.0/
# or send a letter to :
# Creative Commons
# 559 Nathan Abbott Way
# Stanford, California 94305,
# USA.
# --- --- ---
import pygame
# Frequency to initialize mixer with
MIXER_FREQUENCY = 44100
def _checkFileExists(filename):
"Ensures the given file exists and can be read."
f = open(filename, "r")
f.close()
def init():
try:
pygame.mixer.init(MIXER_FREQUENCY)
except:
print "Failed to initialize sound system"
import debug
print debug.get_traceback()
def isAvailable():
return pygame.mixer.get_init() != None
def getSound(soundName):
_checkFileExists(soundName)
if not isAvailable():
return NullSound()
if soundName.endswith(".ogg"):
return MusicSound(soundName)
return pygame.mixer.Sound(soundName)
class MusicSound:
def __init__(self, soundName):
self.soundName = soundName
def set_volume(self, level):
pygame.mixer.music.set_volume(level)
def stop(self):
pygame.mixer.music.stop()
def play(self):
pygame.mixer.music.load(self.soundName)
pygame.mixer.music.play()
class NullSound:
def set_volume(self, level):
pass
def stop(self):
pass
def play(self):
pass