Mercurial > touhou
annotate pytouhou/ui/music.pyx @ 507:507d446dc6cf
Divide the damages inflicted by the number of players.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 17 Nov 2013 17:36:47 +0100 |
parents | 4778c482f24a |
children | 7c3c90468996 |
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 |
421
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
17 from glob import glob |
501
4778c482f24a
Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
474
diff
changeset
|
18 from pytouhou.lib import sdl |
455
6864a38b2413
Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
421
diff
changeset
|
19 from pytouhou.lib cimport sdl |
369
f305cdd6f6c5
Fix crash with the demo version of Touhou 6
Thibaut Girka <thib@sitedethib.com>
parents:
343
diff
changeset
|
20 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
|
21 |
f305cdd6f6c5
Fix crash with the demo version of Touhou 6
Thibaut Girka <thib@sitedethib.com>
parents:
343
diff
changeset
|
22 logger = get_logger(__name__) |
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 |
341
61caded6b4f5
Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
325
diff
changeset
|
25 class MusicPlayer(object): |
61caded6b4f5
Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
325
diff
changeset
|
26 def __init__(self, resource_loader, bgms): |
61caded6b4f5
Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
325
diff
changeset
|
27 self.bgms = [] |
61caded6b4f5
Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
325
diff
changeset
|
28 for bgm in bgms: |
61caded6b4f5
Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
325
diff
changeset
|
29 if not bgm: |
61caded6b4f5
Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
325
diff
changeset
|
30 self.bgms.append(None) |
61caded6b4f5
Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
325
diff
changeset
|
31 continue |
61caded6b4f5
Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
325
diff
changeset
|
32 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
|
33 try: |
f305cdd6f6c5
Fix crash with the demo version of Touhou 6
Thibaut Girka <thib@sitedethib.com>
parents:
343
diff
changeset
|
34 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
|
35 except KeyError: |
f305cdd6f6c5
Fix crash with the demo version of Touhou 6
Thibaut Girka <thib@sitedethib.com>
parents:
343
diff
changeset
|
36 self.bgms.append(None) |
421
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
37 logger.warn(u'Music description “%s” not found.', posname) |
369
f305cdd6f6c5
Fix crash with the demo version of Touhou 6
Thibaut Girka <thib@sitedethib.com>
parents:
343
diff
changeset
|
38 continue |
474
ca22df9e70bc
Fix a crash when handling non-ascii paths
Thibaut Girka <thib@sitedethib.com>
parents:
455
diff
changeset
|
39 globname = join(resource_loader.game_dir, bgm[1].encode('ascii')).replace('.mid', '.*') |
421
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
40 filenames = glob(globname) |
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
41 for filename in reversed(filenames): |
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
42 try: |
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
43 source = sdl.load_music(filename) |
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
44 except sdl.SDLError as error: |
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
45 logger.debug(u'Music file “%s” unreadable: %s', filename, error) |
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
46 continue |
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
47 else: |
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
48 source.set_loop_points(track.start / 44100., track.end / 44100.) #TODO: retrieve the sample rate from the actual track. |
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
49 self.bgms.append(source) |
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
50 logger.debug(u'Music file “%s” opened.', filename) |
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
51 break |
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
52 else: |
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
53 self.bgms.append(None) |
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
54 logger.warn(u'No working music file for “%s”, disabling bgm.', globname) |
342
83c9354ff3ef
Fix crash when changing levels.
Thibaut Girka <thib@sitedethib.com>
parents:
341
diff
changeset
|
55 |
341
61caded6b4f5
Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
325
diff
changeset
|
56 def play(self, index): |
455
6864a38b2413
Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
421
diff
changeset
|
57 cdef sdl.Music bgm |
341
61caded6b4f5
Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
325
diff
changeset
|
58 bgm = self.bgms[index] |
501
4778c482f24a
Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
474
diff
changeset
|
59 if bgm is not None: |
421
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
60 bgm.play(-1) |
341
61caded6b4f5
Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
325
diff
changeset
|
61 |
343
94fdb6c782c1
Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
342
diff
changeset
|
62 |
94fdb6c782c1
Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
342
diff
changeset
|
63 class SFXPlayer(object): |
379
e0e284fcb288
Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
377
diff
changeset
|
64 def __init__(self, loader, volume=.42): |
343
94fdb6c782c1
Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
342
diff
changeset
|
65 self.loader = loader |
421
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
66 self.channels = {} |
343
94fdb6c782c1
Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
342
diff
changeset
|
67 self.sounds = {} |
379
e0e284fcb288
Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
377
diff
changeset
|
68 self.volume = volume |
421
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
69 self.next_channel = 0 |
343
94fdb6c782c1
Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
342
diff
changeset
|
70 |
421
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
71 def get_channel(self, name): |
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
72 if name not in self.channels: |
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
73 self.channels[name] = self.next_channel |
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
74 self.next_channel += 1 |
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
75 return self.channels[name] |
379
e0e284fcb288
Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
377
diff
changeset
|
76 |
e0e284fcb288
Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
377
diff
changeset
|
77 def get_sound(self, name): |
501
4778c482f24a
Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
474
diff
changeset
|
78 cdef sdl.Chunk chunk |
379
e0e284fcb288
Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
377
diff
changeset
|
79 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
|
80 wave_file = self.loader.get_file(name) |
501
4778c482f24a
Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
474
diff
changeset
|
81 try: |
4778c482f24a
Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
474
diff
changeset
|
82 chunk = sdl.load_chunk(wave_file) |
4778c482f24a
Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
474
diff
changeset
|
83 except sdl.SDLError as error: |
4778c482f24a
Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
474
diff
changeset
|
84 logger.warn(u'Sound “%s” not found: %s', name, error) |
4778c482f24a
Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
474
diff
changeset
|
85 chunk = None |
4778c482f24a
Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
474
diff
changeset
|
86 else: |
4778c482f24a
Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
474
diff
changeset
|
87 chunk.set_volume(self.volume) |
455
6864a38b2413
Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
421
diff
changeset
|
88 self.sounds[name] = chunk |
343
94fdb6c782c1
Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
342
diff
changeset
|
89 return self.sounds[name] |
94fdb6c782c1
Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
342
diff
changeset
|
90 |
379
e0e284fcb288
Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
377
diff
changeset
|
91 def play(self, name, volume=None): |
455
6864a38b2413
Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
421
diff
changeset
|
92 cdef sdl.Chunk sound |
379
e0e284fcb288
Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
377
diff
changeset
|
93 sound = self.get_sound(name) |
501
4778c482f24a
Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
474
diff
changeset
|
94 if sound is None: |
4778c482f24a
Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
474
diff
changeset
|
95 return |
421
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
96 channel = self.get_channel(name) |
379
e0e284fcb288
Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
377
diff
changeset
|
97 if volume: |
421
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
98 sdl.mix_volume(channel, volume) |
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
99 sound.play(channel, 0) |
379
e0e284fcb288
Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
377
diff
changeset
|
100 |
e0e284fcb288
Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
377
diff
changeset
|
101 |
e0e284fcb288
Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
377
diff
changeset
|
102 class NullPlayer(object): |
e0e284fcb288
Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
377
diff
changeset
|
103 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
|
104 pass |
e0e284fcb288
Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
377
diff
changeset
|
105 |
421
b1248bab2d0f
Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
385
diff
changeset
|
106 def play(self, name, volume=None): |
379
e0e284fcb288
Make a sound when an enemy is hit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
377
diff
changeset
|
107 pass |