annotate pytouhou/ui/music.py @ 394:346614f788f1

Replace gl{Vertex,TexCoord,Color}Pointer with the more modern glVertexAttribPointer.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 06 Feb 2013 20:57:16 +0100
parents d8aab27a2ab2
children b1248bab2d0f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
321
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 ##
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 ## Copyright (C) 2012 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 ##
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 ##
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 ## GNU General Public License for more details.
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 ##
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15
341
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
16 from os.path import join
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
17
343
94fdb6c782c1 Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 342
diff changeset
18 from pyglet.media import AudioData, AudioFormat, StaticSource, Player
321
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 from pyglet.media.riff import WaveSource
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21
369
f305cdd6f6c5 Fix crash with the demo version of Touhou 6
Thibaut Girka <thib@sitedethib.com>
parents: 343
diff changeset
22 from pytouhou.utils.helpers import get_logger
f305cdd6f6c5 Fix crash with the demo version of Touhou 6
Thibaut Girka <thib@sitedethib.com>
parents: 343
diff changeset
23
f305cdd6f6c5 Fix crash with the demo version of Touhou 6
Thibaut Girka <thib@sitedethib.com>
parents: 343
diff changeset
24 logger = get_logger(__name__)
f305cdd6f6c5 Fix crash with the demo version of Touhou 6
Thibaut Girka <thib@sitedethib.com>
parents: 343
diff changeset
25
f305cdd6f6c5 Fix crash with the demo version of Touhou 6
Thibaut Girka <thib@sitedethib.com>
parents: 343
diff changeset
26
321
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 class InfiniteWaveSource(WaveSource):
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 def __init__(self, filename, start, end, file=None):
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 WaveSource.__init__(self, filename, file)
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 self._start = self.audio_format.bytes_per_sample * start
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 self._end = self.audio_format.bytes_per_sample * end
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 if self._end > self._max_offset:
377
70e2ed71b09c Add meaningful exceptions in format parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 369
diff changeset
35 raise Exception('Music ends after the end of the file.')
321
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 self._duration = None
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 def _get_audio_data(self, bytes):
324
c412df42aa15 Fix music looping
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
41 bytes -= bytes % self.audio_format.bytes_per_sample
321
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42
324
c412df42aa15 Fix music looping
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
43 data = b''
321
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 length = bytes
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 while True:
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 size = min(length, self._end - self._offset)
324
c412df42aa15 Fix music looping
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
47 data += self._file.read(size)
321
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 if size == length:
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 break
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 self._offset = self._start
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 self._file.seek(self._offset + self._start_offset)
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 length -= size
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 self._offset += length
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 timestamp = float(self._offset) / self.audio_format.bytes_per_second
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58 duration = float(bytes) / self.audio_format.bytes_per_second
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 return AudioData(data, bytes, timestamp, duration)
325
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
61
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
62
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
63 def seek(self, timestamp):
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
64 raise NotImplementedError('irrelevant')
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
65
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
66
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
67 class ZwavSource(InfiniteWaveSource):
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
68 def __init__(self, filename, format, file=None):
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
69 if file is None:
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
70 file = open(filename, 'rb')
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
71
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
72 self._file = file
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
73
385
d8aab27a2ab2 Add missing imports, and remove side-effects in asserts.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 379
diff changeset
74 magic = self._file.read(4)
d8aab27a2ab2 Add missing imports, and remove side-effects in asserts.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 379
diff changeset
75 assert b'ZWAV' == magic
325
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
76
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
77 self.audio_format = AudioFormat(
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
78 channels=format.wChannels,
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
79 sample_size=format.wBitsPerSample,
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
80 sample_rate=format.dwSamplesPerSec)
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
81
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
82 self._start_offset = 0
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
83 self._offset = format.intro
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
84
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
85 self._file.seek(self._offset)
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
86 self._start = format.intro + format.start
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 324
diff changeset
87 self._end = format.intro + format.duration
341
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
88
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
89
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
90 class MusicPlayer(object):
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
91 def __init__(self, resource_loader, bgms):
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
92 self.bgms = []
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
93 for bgm in bgms:
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
94 if not bgm:
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
95 self.bgms.append(None)
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
96 continue
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
97 posname = bgm[1].replace('bgm/', '').replace('.mid', '.pos')
369
f305cdd6f6c5 Fix crash with the demo version of Touhou 6
Thibaut Girka <thib@sitedethib.com>
parents: 343
diff changeset
98 try:
f305cdd6f6c5 Fix crash with the demo version of Touhou 6
Thibaut Girka <thib@sitedethib.com>
parents: 343
diff changeset
99 track = resource_loader.get_track(posname)
f305cdd6f6c5 Fix crash with the demo version of Touhou 6
Thibaut Girka <thib@sitedethib.com>
parents: 343
diff changeset
100 except KeyError:
f305cdd6f6c5 Fix crash with the demo version of Touhou 6
Thibaut Girka <thib@sitedethib.com>
parents: 343
diff changeset
101 self.bgms.append(None)
f305cdd6f6c5 Fix crash with the demo version of Touhou 6
Thibaut Girka <thib@sitedethib.com>
parents: 343
diff changeset
102 logger.warn('Music description not found: %s', posname)
f305cdd6f6c5 Fix crash with the demo version of Touhou 6
Thibaut Girka <thib@sitedethib.com>
parents: 343
diff changeset
103 continue
341
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
104 wavname = join(resource_loader.game_dir, bgm[1].replace('.mid', '.wav'))
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
105 try:
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
106 source = InfiniteWaveSource(wavname, track.start, track.end)
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
107 except IOError:
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
108 source = None
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
109 self.bgms.append(source)
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
110
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
111 self.player = Player()
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
112
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
113
342
83c9354ff3ef Fix crash when changing levels.
Thibaut Girka <thib@sitedethib.com>
parents: 341
diff changeset
114 def pause(self):
83c9354ff3ef Fix crash when changing levels.
Thibaut Girka <thib@sitedethib.com>
parents: 341
diff changeset
115 self.player.pause()
83c9354ff3ef Fix crash when changing levels.
Thibaut Girka <thib@sitedethib.com>
parents: 341
diff changeset
116
83c9354ff3ef Fix crash when changing levels.
Thibaut Girka <thib@sitedethib.com>
parents: 341
diff changeset
117
341
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
118 def play(self, index):
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
119 bgm = self.bgms[index]
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
120 if self.player.playing:
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
121 self.player.next()
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
122 if bgm:
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
123 self.player.queue(bgm)
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
124 self.player.play()
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
125
343
94fdb6c782c1 Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 342
diff changeset
126
94fdb6c782c1 Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 342
diff changeset
127 class SFXPlayer(object):
379
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
128 def __init__(self, loader, volume=.42):
343
94fdb6c782c1 Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 342
diff changeset
129 self.loader = loader
379
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
130 self.players = {}
343
94fdb6c782c1 Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 342
diff changeset
131 self.sounds = {}
379
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
132 self.volume = volume
343
94fdb6c782c1 Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 342
diff changeset
133
94fdb6c782c1 Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 342
diff changeset
134
379
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
135 def get_player(self, name):
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
136 if name not in self.players:
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
137 self.players[name] = Player()
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
138 self.players[name].volume = self.volume
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
139 return self.players[name]
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
140
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
141
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
142 def get_sound(self, name):
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
143 if name not in self.sounds:
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
144 wave_file = self.loader.get_file(name)
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
145 self.sounds[name] = StaticSource(WaveSource(name, wave_file))
343
94fdb6c782c1 Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 342
diff changeset
146 return self.sounds[name]
94fdb6c782c1 Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 342
diff changeset
147
94fdb6c782c1 Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 342
diff changeset
148
379
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
149 def play(self, name, volume=None):
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
150 sound = self.get_sound(name)
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
151 player = self.get_player(name)
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
152 if volume:
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
153 player.volume = volume
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
154 if player.playing:
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
155 player.next()
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
156 if sound:
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
157 player.queue(sound)
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
158 player.play()
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
159
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
160
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
161 class NullPlayer(object):
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
162 def __init__(self, loader=None, bgms=None):
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
163 pass
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
164
343
94fdb6c782c1 Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 342
diff changeset
165
94fdb6c782c1 Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 342
diff changeset
166 def play(self, name):
379
e0e284fcb288 Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
167 pass