/usr/lib/krank/Sound.py is in krank 0.7+dfsg2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | #
# Sound.py
import k
from Krank import *
#-----------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------
class Sound (object):
#-------------------------------------------------------------------------------------------
def __init__(self):
#log(log='startup')
k.sound = self
self.sounds = {}
self.soundDir = None
self.soundVolume = 1.0
self.musicVolume = 1.0
pygame.mixer.init()
pygame.mixer.set_num_channels(32)
log('num channels', pygame.mixer.get_num_channels() , log='sound')
#-------------------------------------------------------------------------------------------
def reset (self):
self.sounds = {}
self.soundDir = None
pygame.mixer.music.stop()
pygame.mixer.stop()
#-------------------------------------------------------------------------------------------
def loadTheme (self, set='industry'):
log('sound set', set, log='sound')
self.soundDir = os.path.join('sounds', set)
if os.path.exists(self.soundDir):
for file in os.listdir(self.soundDir):
if os.path.splitext(file)[1] in ['.wav']:
key = os.path.splitext(os.path.basename(file))[0]
#log('sound loaded:', key, '->', file, log='sound')
self.sounds[key] = pygame.mixer.Sound(os.path.join(self.soundDir, file))
self.sounds[key].set_volume(self.soundVolume)
else:
self.soundDir = None
log('invalid sound set')
#-------------------------------------------------------------------------------------------
def play (self, sound, volume=1.0, force=0, event=None):
if volume > 0 and self.soundVolume > 0:
log(sound, volume, force, event, log='sound')
volume = clamp(volume, 0.1, 1.0)
if self.sounds.has_key(sound):
if force:
channel = pygame.mixer.find_channel(1)
channel.play(self.sounds[sound])
else:
channel = self.sounds[sound].play()
if channel:
channel.set_volume(volume)
if event:
channel.set_endevent(event)
else:
log('no channel', log='sound')
#-------------------------------------------------------------------------------------------
def removeEndEvent (self, type):
for i in range(pygame.mixer.get_num_channels()):
channel = pygame.mixer.Channel(i)
if channel.get_endevent() == type:
channel.set_endevent()
#-------------------------------------------------------------------------------------------
def music (self, name='atmo.ogg'):
if self.soundDir:
musicFile = os.path.join(self.soundDir, name)
if os.path.exists(musicFile):
pygame.mixer.music.load(os.path.join(self.soundDir, name))
if self.musicVolume > 0:
pygame.mixer.music.play(-1)
self.setMusicVolume(self.musicVolume)
#-------------------------------------------------------------------------------------------
def setMusicVolume (self, volume):
self.musicVolume = clamp(volume, 0.0, 1.0)
pygame.mixer.music.set_volume(self.musicVolume)
if self.musicVolume > 0:
if not pygame.mixer.music.get_busy():
pygame.mixer.music.play(-1)
else:
if not pygame.mixer.music.get_busy():
pygame.mixer.music.stop()
#-------------------------------------------------------------------------------------------
def getMusicVolume (self):
return pygame.mixer.music.get_volume()
#-------------------------------------------------------------------------------------------
def setMusicVolumeIndex (self, index):
index = clamp(index, 0, 5)
self.setMusicVolume (index*index/25.0)
#-------------------------------------------------------------------------------------------
def getMusicVolumeIndex (self):
v = pygame.mixer.music.get_volume()
for i in range(6):
if abs(v-i*i/25.0) < 0.01: return i
return 5
#-------------------------------------------------------------------------------------------
def setSoundVolume (self, volume):
log(volume, log='sound')
play = self.soundVolume <> clamp(volume, 0.0, 1.0)
self.soundVolume = clamp(volume, 0.0, 1.0)
for key in self.sounds.keys():
self.sounds[key].set_volume(self.soundVolume)
if play:
k.sound.play('part')
#-------------------------------------------------------------------------------------------
def getSoundVolume (self):
return self.soundVolume
#-------------------------------------------------------------------------------------------
def setSoundVolumeIndex (self, index):
index = clamp(index, 0, 5)
self.setSoundVolume (index*index/25.0)
#-------------------------------------------------------------------------------------------
def getSoundVolumeIndex (self):
v = self.soundVolume
for i in range(6):
if abs(v-i*i/25.0) < 0.01: return i
return 5
|