| 199 | |
| 200 | |
| 201 | class Sound(object): |
| 202 | |
| 203 | def __init__(self, what): |
| 204 | |
| 205 | # Doesn't support buffers. |
| 206 | |
| 207 | global sound_serial |
| 208 | |
| 209 | self._channel = None |
| 210 | self._volume = 1. |
| 211 | self.serial = str(sound_serial) |
| 212 | sound_serial += 1 |
| 213 | |
| 214 | if isinstance(what, file): # noqa F821 |
| 215 | self.file = what |
| 216 | else: |
| 217 | self.file = file(os.path.abspath(what), "rb") # noqa F821 |
| 218 | |
| 219 | sounds[self.serial] = self |
| 220 | |
| 221 | def play(self, loops=0, maxtime=0, fade_ms=0): |
| 222 | # avoid new play if the sound is already playing |
| 223 | # -> same behavior as standard pygame. |
| 224 | if self._channel is not None: |
| 225 | if self._channel.get_sound() is self: |
| 226 | return |
| 227 | self._channel = channel = find_channel(True) |
| 228 | channel.set_volume(self._volume) |
| 229 | channel.play(self, loops=loops) |
| 230 | return channel |
| 231 | |
| 232 | def stop(self): |
| 233 | for i in range(0, num_channels): |
| 234 | if Channel(i).get_sound() is self: |
| 235 | Channel(i).stop() |
| 236 | |
| 237 | def fadeout(self, time): |
| 238 | self.stop() |
| 239 | |
| 240 | def set_volume(self, left, right=None): |
| 241 | self._volume = left |
| 242 | if self._channel: |
| 243 | if self._channel.get_sound() is self: |
| 244 | self._channel.set_volume(self._volume) |
| 245 | |
| 246 | def get_volume(self): |
| 247 | return self._volume |
| 248 | |
| 249 | def get_num_channels(self): |
| 250 | rv = 0 |
| 251 | |
| 252 | for i in range(0, num_channels): |
| 253 | if Channel(i).get_sound() is self: |
| 254 | rv += 1 |
| 255 | |
| 256 | return rv |
| 257 | |
| 258 | def get_length(self): |