/usr/lib/python2.7/dist-packages/pyglet/media/drivers/directsound/__init__.py is in python-pyglet 1.1.4.dfsg-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 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 | # ----------------------------------------------------------------------------
# pyglet
# Copyright (c) 2006-2008 Alex Holkner
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# * Neither the name of pyglet nor the names of its
# contributors may be used to endorse or promote products
# derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------------
'''Windows DirectSound audio implementation.
'''
__docformat__ = 'restructuredtext'
__version__ = '$Id: $'
import ctypes
import math
import time
from pyglet.media import AudioPlayer, Listener, MediaException
from pyglet.media.drivers.directsound import lib_dsound as lib
from pyglet.window.win32 import _user32
class DirectSoundException(MediaException):
pass
def _db(gain):
'''Convert linear gain in range [0.0, 1.0] to 100ths of dB.'''
if gain <= 0:
return -10000
return max(-10000, min(int(1000 * math.log(min(gain, 1))), 0))
class DirectSoundAudioPlayer(AudioPlayer):
_buffer_size = 44800 * 1
_update_buffer_size = _buffer_size // 4
_buffer_size_secs = None
_cone_inner_angle = 360
_cone_outer_angle = 360
UPDATE_PERIOD = 0.05
def __init__(self, audio_format):
super(DirectSoundAudioPlayer, self).__init__(audio_format)
self._playing = False
self._timestamp = 0.
self._buffer = None
self._buffer_playing = False
self._data_size = 0 # amount of buffer filled by this player
self._play_cursor = 0
self._buffer_time = 0. # ts of buffer at buffer_time_pos
self._buffer_time_pos = 0
self._write_cursor = 0
self._timestamps = []
self._eos_count = 0
self._dirty_size = 0
wfx = lib.WAVEFORMATEX()
wfx.wFormatTag = lib.WAVE_FORMAT_PCM
wfx.nChannels = audio_format.channels
wfx.nSamplesPerSec = audio_format.sample_rate
wfx.wBitsPerSample = audio_format.sample_size
wfx.nBlockAlign = wfx.wBitsPerSample * wfx.nChannels // 8
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign
dsbdesc = lib.DSBUFFERDESC()
dsbdesc.dwSize = ctypes.sizeof(dsbdesc)
dsbdesc.dwFlags = (lib.DSBCAPS_GLOBALFOCUS |
lib.DSBCAPS_GETCURRENTPOSITION2 |
lib.DSBCAPS_CTRLFREQUENCY |
lib.DSBCAPS_CTRLVOLUME)
if audio_format.channels == 1:
dsbdesc.dwFlags |= lib.DSBCAPS_CTRL3D
dsbdesc.dwBufferBytes = self._buffer_size
dsbdesc.lpwfxFormat = ctypes.pointer(wfx)
self._buffer = lib.IDirectSoundBuffer()
dsound.CreateSoundBuffer(dsbdesc, ctypes.byref(self._buffer), None)
if audio_format.channels == 1:
self._buffer3d = lib.IDirectSound3DBuffer()
self._buffer.QueryInterface(lib.IID_IDirectSound3DBuffer,
ctypes.byref(self._buffer3d))
else:
self._buffer3d = None
self._buffer_size_secs = \
self._buffer_size / float(audio_format.bytes_per_second)
self._buffer.SetCurrentPosition(0)
def __del__(self):
try:
self._buffer.Stop()
self._buffer.Release()
if self._buffer3d:
self._buffer3d.Release()
except:
pass
def get_write_size(self):
if self._data_size < self._buffer_size:
return self._buffer_size - self._data_size
play_cursor = self._play_cursor
if self._write_cursor == play_cursor and self._buffer_playing:
# Polling too fast, no play cursor movement
return 0
elif self._write_cursor == play_cursor and not self._playing:
# Paused and up-to-date
return 0
elif self._write_cursor < play_cursor:
# Play cursor ahead of write cursor
write_size = play_cursor - self._write_cursor
else:
# Play cursor behind write cursor, wraps around
write_size = self._buffer_size - self._write_cursor + play_cursor
if write_size < self._update_buffer_size and not self._dirty_size:
return 0
return write_size
def write(self, audio_data, length=None):
# Pass audio_data=None, length>0 to write silence
if length is None:
write_size = self.get_write_size()
length = min(audio_data.length, write_size)
if length == 0:
return 0
if self._data_size < self._buffer_size:
self._data_size = min(self._data_size + length, self._buffer_size)
p1 = ctypes.c_void_p()
l1 = lib.DWORD()
p2 = ctypes.c_void_p()
l2 = lib.DWORD()
self._buffer.Lock(self._write_cursor, length,
ctypes.byref(p1), l1, ctypes.byref(p2), l2, 0)
assert length == l1.value + l2.value
if audio_data:
if self._write_cursor >= self._play_cursor:
wc = self._write_cursor
else:
wc = self._write_cursor + self._buffer_size
self._timestamps.append((wc, audio_data.timestamp))
ctypes.memmove(p1, audio_data.data, l1.value)
audio_data.consume(l1.value, self.audio_format)
if l2.value:
ctypes.memmove(p2, audio_data.data, l2.value)
audio_data.consume(l2.value, self.audio_format)
else:
ctypes.memset(p1, 0, l1.value)
if l2.value:
ctypes.memset(p2, 0, l2.value)
pass
self._buffer.Unlock(p1, l1, p2, l2)
self._write_cursor += length
self._write_cursor %= self._buffer_size
def write_eos(self):
if self._write_cursor > self._play_cursor:
wc = self._write_cursor
else:
wc = self._write_cursor + self._buffer_size
self._timestamps.append((wc, 'eos'))
def write_end(self):
if not self._dirty_size:
self._dirty_size = self._buffer_size
def pump(self):
# Update play cursor, check for wraparound and EOS markers
play_cursor = lib.DWORD()
self._buffer.GetCurrentPosition(play_cursor, None)
if play_cursor.value < self._play_cursor:
# Wrapped around
self._buffer_time_pos -= self._buffer_size
self._timestamps = \
[(a - self._buffer_size, t) for a, t in self._timestamps]
self._play_cursor = play_cursor.value
try:
while self._timestamps[0][0] < self._play_cursor:
pos, timestamp = self._timestamps.pop(0)
if timestamp == 'eos':
self._eos_count += 1
else:
self._buffer_time = timestamp
self._buffer_time_pos = pos
except IndexError:
pass
self._timestamp = self._buffer_time + \
(self._play_cursor - self._buffer_time_pos) \
/ float(self.audio_format.bytes_per_second)
# Write silence
if self._dirty_size:
write_size = self.get_write_size()
length = min(write_size, self._dirty_size)
self.write(None, length)
self._dirty_size -= length
if self._dirty_size < 0:
self._dirty_size = 0
if self._playing and not self._buffer_playing:
self._buffer.Play(0, 0, lib.DSBPLAY_LOOPING)
self._buffer_playing = True
def get_time(self):
return self._timestamp
def play(self):
if self._playing:
return
self._playing = True
self._buffer.Play(0, 0, lib.DSBPLAY_LOOPING)
self._buffer_playing = True
def stop(self):
if not self._playing:
return
self._playing = False
self._buffer.Stop()
self._buffer_playing = False
def clear(self):
self._eos_count = 0
self._timestamps = []
self._write_cursor = 0
self._buffer.SetCurrentPosition(0)
self._buffer_time = 0.
self._buffer_time_pos = 0
self._data_size = 0
def clear_eos(self):
if self._eos_count > 0:
self._eos_count -= 1
return True
return False
def _get_source(self):
if self._sources:
return self._sources[0]
return None
def set_volume(self, volume):
volume = _db(volume)
self._buffer.SetVolume(volume)
def set_position(self, position):
if self._buffer3d:
x, y, z = position
self._buffer3d.SetPosition(x, y, -z, lib.DS3D_IMMEDIATE)
def set_min_distance(self, min_distance):
if self._buffer3d:
self._buffer3d.SetMinDistance(min_distance, lib.DS3D_IMMEDIATE)
def set_max_distance(self, max_distance):
if self._buffer3d:
self._buffer3d.SetMaxDistance(max_distance, lib.DS3D_IMMEDIATE)
def set_pitch(self, pitch):
frequency = int(pitch * self.audio_format.sample_rate)
self._buffer.SetFrequency(frequency)
def set_cone_orientation(self, cone_orientation):
if self._buffer3d:
x, y, z = cone_orientation
self._buffer3d.SetConeOrientation(x, y, -z, lib.DS3D_IMMEDIATE)
def set_cone_inner_angle(self, cone_inner_angle):
if self._buffer3d:
self._cone_inner_angle = int(cone_inner_angle)
self._set_cone_angles()
def set_cone_outer_angle(self, cone_outer_angle):
if self._buffer3d:
self._cone_outer_angle = int(cone_outer_angle)
self._set_cone_angles()
def _set_cone_angles(self):
inner = min(self._cone_inner_angle, self._cone_outer_angle)
outer = max(self._cone_inner_angle, self._cone_outer_angle)
self._buffer3d.SetConeAngles(inner, outer, lib.DS3D_IMMEDIATE)
def set_cone_outer_gain(self, cone_outer_gain):
if self._buffer3d:
volume = _db(cone_outer_gain)
self._buffer3d.SetConeOutsideVolume(volume, lib.DS3D_IMMEDIATE)
class DirectSoundListener(Listener):
def _init(self):
# Called after driver_init()
self._buffer = lib.IDirectSoundBuffer()
dsbd = lib.DSBUFFERDESC()
dsbd.dwSize = ctypes.sizeof(dsbd)
dsbd.dwFlags = (lib.DSBCAPS_CTRL3D |
lib.DSBCAPS_CTRLVOLUME |
lib.DSBCAPS_PRIMARYBUFFER)
dsound.CreateSoundBuffer(dsbd, ctypes.byref(self._buffer), None)
self._listener = lib.IDirectSound3DListener()
self._buffer.QueryInterface(lib.IID_IDirectSound3DListener,
ctypes.byref(self._listener))
def __del__(self):
try:
self._buffer.Release()
self._listener.Release()
except:
pass
def _set_volume(self, volume):
self._volume = volume
self._buffer.SetVolume(_db(volume))
def _set_position(self, position):
self._position = position
x, y, z = position
self._listener.SetPosition(x, y, -z, lib.DS3D_IMMEDIATE)
def _set_forward_orientation(self, orientation):
self._forward_orientation = orientation
self._set_orientation()
def _set_up_orientation(self, orientation):
self._up_orientation = orientation
self._set_orientation()
def _set_orientation(self):
x, y, z = self._forward_orientation
ux, uy, uz = self._up_orientation
self._listener.SetOrientation(x, y, -z, ux, uy, -uz, lib.DS3D_IMMEDIATE)
dsound = None
def driver_init():
global dsound
dsound = lib.IDirectSound()
lib.DirectSoundCreate(None, ctypes.byref(dsound), None)
# A trick used by mplayer.. use desktop as window handle since it would
# be complex to use pyglet window handles (and what to do when application
# is audio only?).
hwnd = _user32.GetDesktopWindow()
dsound.SetCooperativeLevel(hwnd, lib.DSSCL_NORMAL)
driver_listener._init()
# Force a context switch, as some Windows audio drivers don't get time
# to process short sounds if Python hogs all the CPU. See issue #163.
from pyglet import clock
clock.Clock._force_sleep = True
driver_listener = DirectSoundListener()
driver_audio_player_class = DirectSoundAudioPlayer
|