/usr/share/pyshared/soya/sdl_mixer4soya.py is in python-soya 0.15~rc1-10.
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | # -*- indent-tabs-mode: t -*-
# Soya
# Copyright (C) 2005 Jean-Baptiste LAMY -- jiba@tuxfamily.org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""soya.sdl_mixer4soya
A module for interfacing SDL_mixer and pysdl_mixer with the Soya 3D engine.
"""
import pysdl_mixer, os, os.path, time, sys
import soya
DISTANCE_ATTENUATION = 1.0
_BUFFERS = {}
_LISTENER = None
_CAMERA = None
_FRONT = soya.Vector()
_DIR = soya.Vector()
_INITED = 0
_SOURCES = []
_PENDING = []
_CHANNELS = []
def set_body(body):
"""Set the global body level to BODY (a float value between 0.0 and 1.0)."""
pysdl_mixer.channel_body(-1, int(body * 255.0))
def find_file_in_path(filename):
for p in soya.path:
file = os.path.join(p, "sounds", filename)
if os.path.exists(file): return file
raise ValueError("Cannot find file named %s in soya.path!" % filename)
def init(camera, nb_channels = 16, freq = 44100, size = -16, stereo = 2, buffersize = 1024):
"""init(camera, nb_channels = 16, freq = 44100, size = -16, stereo = 2, buffersize = 1024)
Inits OpenAL for Soya. CAMERA will be used to compute the position of the listener ;
it is not required that it is a Camera, though it is usually one."""
global _LISTENER, _CAMERA, _INITED, _CHANNELS
_CAMERA = camera
_FRONT.__init__(_CAMERA, 0.0, 0.0, -1.0)
_DIR .__init__(_CAMERA, 0.0, 0.0, 0.0)
if not _INITED:
soya.BEFORE_RENDER.append(render)
pysdl_mixer.init(freq, size, stereo, buffersize)
pysdl_mixer.allocate_channels(nb_channels)
_CHANNELS = range(nb_channels)
_INITED = 1
def preload_sound(filename, async = 0):
"""Pre-load sound FILENAME, in order to be able to play it immediately later.
If ASYNC, the sound is pre-loaded in an other thread.
The sound is NOT played!"""
if not _BUFFERS.has_key(filename):
if async:
_BUFFERS[filename] = []
import thread
thread.start_new_thread(_load_async, (filename,))
else:
buffer = _BUFFERS[filename] = pysdl_mixer.Sample(find_file_in_path(filename))
class Source(object):
def __init__(self, channel, sound, position, speed, looping, gain):
self.channel = channel
self.sound = sound
self.position = position
self.speed = speed
self.looping = looping
self.gain = gain
_SOURCES.append(self)
self.update(0)
if sound: sound.play(self.channel, -self.looping)
self._first = 1
def stop(self):
pysdl_mixer.halt_channel(self.channel)
def update(self, may_remove = 1):
if may_remove and not pysdl_mixer.channel_is_playing(self.channel): # Play ended
_CHANNELS.append(self.channel)
_SOURCES.remove(self)
return
if self.position:
x, y, z = _CAMERA.transform(self.position)
_DIR.set_xyz(x, 0.0, z)
angle = _FRONT.angle_to(_DIR)
if x < 0.0: angle = 360 - angle
pysdl_mixer.channel_set_position(self.channel, int(angle), int(DISTANCE_ATTENUATION * self.position.distance_to(_CAMERA)))
class AsyncSource(Source):
def __init__(self, channel, filename, position, speed, looping, gain):
Source.__init__(self, channel, None, position, speed, looping, gain)
self.canceled = 0
def stop(self):
if not self.sound: self.canceled = 1
else: Source.stop(self)
def update(self, may_remove = 1):
if self.sound: Source.update(self, may_remove)
def _loaded(self, sound):
if self.canceled:
_CHANNELS.append(self.channel)
_SOURCES.remove(self)
else:
sound.play(self.channel)
self.sound = sound
def play(filename, position = None, speed = None, looping = 0, async = 0, gain = 1.0):
"""play(filename, position = None, speed = None, looping = 0, async = 0, gain = 1.0)
Plays sound FILENAME, at the given POSITION and SPEED (for Doppler effect).
Sound file's data are cached for more efficiency.
If POSITION and SPEED are omitted, the sound is played without 3D features.
If SPEED is ignored (it is present for compatibility with soya.openal4soya).
If LOOPING is true, the sound is played in loop.
If ASYNC is true, the sound is loading asynchronously (in a new thread), and
thus it does not start immediately. ASYNC has not effect if the sound is
already loaded (=cached). Usefull for OggVorbis file (take a while to
uncompress).
FILENAME can be Wave, Ogg Vorbis, MP3 or any other format supported by SDL_mixer file ;
the file is searched in {soya.path}/sounds/."""
# Exit when sound not initialized
if not _INITED: return
if not _CHANNELS:
print "* Soya PySDL_Mixer * Not enough channels for playing %s!" % filename
return
buffer = _BUFFERS.get(filename)
if not buffer:
if async:
import thread
source = AsyncSource(_CHANNELS.pop(), None, position, speed, looping, gain)
_BUFFERS[filename] = [source._loaded]
thread.start_new_thread(_load_async, (filename,))
return source
else:
buffer = _BUFFERS[filename] = pysdl_mixer.Sample(find_file_in_path(filename))
elif isinstance(buffer, list): # a list of waiter
source = AsyncSource(_CHANNELS.pop(), None, position, speed, looping, gain)
buffer.append(source._loaded)
if not async: # Wait until loaded
while isinstance(_BUFFERS[filename], list): time.sleep(0.01)
return source
return Source(_CHANNELS.pop(), buffer, position, speed, looping, gain)
def _load_async(filename):
if sys.platform!="win32":
os.nice(5)
buffer = pysdl_mixer.Sample(find_file_in_path(filename))
_PENDING.append((filename, buffer))
def render():
"""render()
Update the sources's positions and speed.
This function is called before each Soya rendering."""
for source in _SOURCES[:]: source.update()
global _PENDING
if _PENDING: # An async loading is finished ! We can now play it !
for filename, buffer in _PENDING:
for waiter in _BUFFERS[filename]: waiter(buffer)
_BUFFERS[filename] = buffer
_PENDING = []
|