-
Notifications
You must be signed in to change notification settings - Fork 1
/
sound.py
176 lines (159 loc) · 5.03 KB
/
sound.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
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
#unable to reuse Players when alsa is the selected driver on Paul's samsung laptop
#so alsa is disabled for now
#pyglet.options['audio'] = ('directsound', 'openal', 'alsa', 'silent')
from wrapper import *
if api=='pyglet':
pyglet.options['audio'] = ('directsound', 'openal', 'silent')
from collections import deque
from euclid import *
import thread
from traceback import print_exc
import manage
class SoundSlot(object):
__TID=None
__PLAYING=set()
__FREE=deque([ Player() for i in range(15) ])
__SCHEDULED=deque()
@staticmethod
def checkTid():
if SoundSlot.__TID is None:
SoundSlot.__TID=thread.get_ident()
return True
else:
if thread.get_ident()!=SoundSlot.__TID:
print 'checkTid. current: '+str(thread.get_ident())+' '+str(SoundSlot.__TID)+' '+str(thread.get_ident())
return False
else:
return True
@classmethod
def free_player(cls, player):
player.pause()
player.volume=1.0
player.pitch=1.0
cls.__FREE.append(player)
cls.__PLAYING.remove(player)
@classmethod
def sound_toggle(cls):
try:
assert cls.checkTid()
except:
print_exc()
manage.sound_effects=not manage.sound_effects
if not manage.sound_effects:
[ cls.free_player(p) for p in set(cls.__PLAYING) ]
def __init__(self, name="", loop=False, pos=None, snd=None):
try:
assert self.checkTid()
except:
print_exc()
self.__name=name
self.__loop=loop
self.__player=None
self.__pos=pos
self.snd=snd
def __free_player(self):
SoundSlot.free_player(self.__player)
def pause(self):
try:
assert self.checkTid()
except:
print_exc()
if self.__player:
self.__player.pause()
@property
def playing(self):
try:
assert self.checkTid()
except:
print_exc()
return self.__player is not None and self.__player.playing
@property
def pitch(self):
try:
assert self.checkTid()
except:
print_exc()
if self.__player:
return self.__player.pitch
return 0
@pitch.setter
def pitch(self, value):
try:
assert self.checkTid()
except:
print_exc()
if self.__player:
self.__player.pitch=value
@property
def volume(self):
try:
assert self.checkTid()
except:
print_exc()
if self.__player:
return self.__player.volume
return 0
@volume.setter
def volume(self, value):
try:
assert self.checkTid()
except:
print_exc()
if self.__player:
self.__player.volume=value
def play(self, snd=None, pos=None):
try:
assert self.checkTid()
except:
print_exc()
if snd:
self.snd=snd
if pos:
self.__pos=pos
if not manage.sound_effects:
return
if self.__player and self.__player in self.__PLAYING:
self.__free_player()
try:
assert self.snd
self.__player=self.__FREE.popleft()
if self.__pos:
self.__player.position=(self.__pos.x, self.__pos.y, self.__pos.z)
if self.__loop:
self.__player.eos_action=Player.EOS_LOOP
else:
self.__player.eos_action=Player.EOS_PAUSE
self.__player.min_distance=80
self.__player.next()
self.__player.queue(self.snd)
self.__player.play()
@self.__player.event
def on_eos():
try:
assert self.checkTid()
except:
print_exc()
if self.__player and self.__player in self.__PLAYING and self.__player.eos_action!=Player.EOS_LOOP:
self.__free_player()
self.__PLAYING.add(self.__player)
except:
print_exc()
def setPos(self, pos):
try:
assert self.checkTid()
except:
print_exc()
self.__pos=pos
if self.__player:
self.__player.position=self.__pos
GUN_SND=sndLoad('data/gun.wav', streaming=False)
ENGINE_SND=sndLoad('data/spitfire_engine.wav', streaming=False)
SCREECH_SND=sndLoad('data/104026__rutgermuller__Tire_Squeek_www.rutgermuller.wav', streaming=False)
WIND_SND=sndLoad('data/34338__ERH__wind.wav', streaming=False)
WHIZZ_SND=sndLoad('data/90782__kmoon__bullet-flyby-2.wav', streaming=False)
IMPACT_SND=sndLoad('data/116645__woodingp__bullets-hit-edit.wav', streaming=False)
GRIND_SND=sndLoad('data/10338__batchku__no-escape.wav', streaming=False)
def setListener(eye, pos, zen):
listener.position=(eye.x, eye.y, eye.z)
listener.forward_orientation=(pos.x, pos.y, pos.z)
listener.up_orientation=(zen.x, zen.y, zen.z)