Skip to content

Commit d106e73

Browse files
committed
Added new Sound class that interacts with a specific sound sample.
Added Null objects to mathlib.
1 parent 37673f2 commit d106e73

File tree

2 files changed

+155
-4
lines changed

2 files changed

+155
-4
lines changed

addons/source-python/packages/source-python/engines/sound.py

Lines changed: 141 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
# =============================================================================
44
# >> FORWARD IMPORTS
55
# =============================================================================
6+
# Python Imports
7+
# Inspect
8+
from inspect import getmodule
9+
from inspect import stack
10+
611
# Source.Python Imports
12+
from core import AutoUnload
713
# Engines
814
from _engines import EngineSound
915
from _engines import Channels
@@ -21,6 +27,14 @@
2127
from _engines import PITCH_HIGH
2228
from _engines import SOUND_FROM_LOCAL_PLAYER
2329
from _engines import SOUND_FROM_WORLD
30+
# Filters
31+
from filters.recipients import RecipientFilter
32+
# Mathlib
33+
from mathlib import NULL_VECTOR
34+
# Paths
35+
from paths import PLUGIN_PATH
36+
# Stringtables
37+
from stringtables.downloads import Downloadables
2438

2539

2640
# =============================================================================
@@ -32,6 +46,7 @@
3246
'EngineSound',
3347
'SOUND_FROM_LOCAL_PLAYER',
3448
'SOUND_FROM_WORLD',
49+
'Sound',
3550
'SoundFlags',
3651
'VOL_NORM',
3752
]
@@ -43,8 +58,8 @@
4358
class Attenuations(object):
4459
'''Attenuation values wrapper enumerator.
4560
46-
TODO: Inherit of "enum.IntEnum" once we have upgraded to Python 3.4+.
47-
'''
61+
TODO: Inherit of "enum.IntEnum" once we have upgraded to Python 3.4+.
62+
'''
4863
NONE = ATTN_NONE
4964
NORMAL = ATTN_NORM
5065
IDLE = ATTN_IDLE
@@ -57,8 +72,130 @@ class Attenuations(object):
5772
class PitchTypes(object):
5873
'''Pitch values wrapper enumerator.
5974
60-
TODO: Inherit of "enum.IntEnum" once we have upgraded to Python 3.4+.
61-
'''
75+
TODO: Inherit of "enum.IntEnum" once we have upgraded to Python 3.4+.
76+
'''
6277
NORMAL = PITCH_NORM
6378
LOW = PITCH_LOW
6479
HIGH = PITCH_HIGH
80+
81+
82+
class Sound(AutoUnload):
83+
'''Class used to interact with a specific sound file'''
84+
85+
# Set the base _downloads attribute to know whether
86+
# or not the sample was added to the downloadables
87+
_downloads = None
88+
89+
def __init__(
90+
self, recipients, index, sample, volume=VOL_NORM,
91+
attenuation=Attenuations.NONE, channel=Channels.AUTO,
92+
flags=0, pitch=PitchTypes.HIGH, origin=NULL_VECTOR,
93+
direction=NULL_VECTOR, origins=(), update_positions=True,
94+
sound_time=0.0, speaker_entity=-1, download=False):
95+
'''Store all the given attributes and set the module for unloading'''
96+
97+
# Get the file that called
98+
caller = getmodule(stack()[1][0])
99+
100+
# Is the calling file in a plugin?
101+
if PLUGIN_PATH in caller.__file__:
102+
103+
# Set the module to the plugin's module so that
104+
# _unload_instance will fire when the plugin is unloaded
105+
self.__module__ = caller.__name__
106+
107+
# Set sample as a private attribute, since it should never change
108+
self._sample = sample
109+
110+
# Set all the base attributes
111+
self.recipients = recipients
112+
self.index = index
113+
self.volume = volume
114+
self.attenuation = attenuation
115+
self.channel = channel
116+
self.flags = flags
117+
self.pitch = pitch
118+
self.origin = origin
119+
self.direction = direction
120+
self.origins = origins
121+
self.update_positions = update_positions
122+
self.sound_time = sound_time
123+
self.speaker_entity = speaker_entity
124+
125+
# Should the sample be downloaded by clients?
126+
if download:
127+
128+
# Get the file that called
129+
caller = getmodule(stack()[1][0])
130+
131+
# Is the calling file in a plugin?
132+
if PLUGIN_PATH in caller.__file__:
133+
134+
# Set the module to the plugin's module so that
135+
# _unload_instance will fire when the plugin is unloaded
136+
self.__module__ = caller.__name__
137+
138+
# Add the sample to Downloadables
139+
self._downloads = Downloadables()
140+
self._downloads.add('sound/{0}'.format(self.sample))
141+
142+
def play(self, *recipients):
143+
'''Plays the sound using either the given
144+
recipients or the instance's recipients'''
145+
146+
# Get the recipients to play the sound to
147+
recipients = RecipientFilter(*(recipients or self.recipients))
148+
149+
# Is the sound precached?
150+
if not self.is_precached():
151+
152+
# Precache the sound
153+
self.precache()
154+
155+
# Play the sound
156+
EngineSound.emit_sound(
157+
recipients, self.index, self.channel, self.sample,
158+
self.volume, self.attenuation, self.flags, self.pitch,
159+
self.origin, self.direction, self.origins,
160+
self.update_positions, self.sound_time, self.speaker_entity)
161+
162+
def stop(self, index=None, channel=None):
163+
'''Stops a sound from being played'''
164+
165+
# Was an index passed in?
166+
if index is None:
167+
168+
# Use the instance's index
169+
index = self.index
170+
171+
# Was a channel passed in?
172+
if channel is None:
173+
174+
# Use the instance's index
175+
channel = self.channel
176+
177+
# Stop the sound
178+
EngineSound.stop_sound(index, channel, self.sample)
179+
180+
def precache(self):
181+
'''Precaches the sample'''
182+
EngineSound.precache_sound(self.sample)
183+
184+
def is_precached(self):
185+
'''Returns whether or not the sample is precached'''
186+
return EngineSound.is_sound_precached(self.sample)
187+
188+
@property
189+
def sample(self):
190+
'''Returns the sample (filename) of the Sound instance'''
191+
return self._sample
192+
193+
@property
194+
def duration(self):
195+
'''Returns the duration of the sample'''
196+
return EngineSound.get_sound_duration(self.sample)
197+
198+
def _unload_instance(self):
199+
'''Removes the sample from the downloads list'''
200+
if not self._downloads is None:
201+
self._downloads._unload_instance()

addons/source-python/packages/source-python/mathlib.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,23 @@
1616
# >> ALL DECLARATION
1717
# =============================================================================
1818
__all__ = [
19+
'NULL_QANGLE',
20+
'NULL_QUATERNION',
21+
'NULL_RADIANEULER',
22+
'NULL_VECTOR',
1923
'Plane',
2024
'QAngle',
2125
'Quaternion',
2226
'RadianEuler',
2327
'Vector',
2428
]
29+
30+
31+
# =============================================================================
32+
# >> GLOBAL VARIABLES
33+
# =============================================================================
34+
# Set the Null objects
35+
NULL_QANGLE = QAngle(.0, .0, .0)
36+
NULL_QUATERNION = Quaternion(.0, .0, .0, .0)
37+
NULL_RADIANEULER = RadianEuler(.0, .0, .0)
38+
NULL_VECTOR = Vector()

0 commit comments

Comments
 (0)