Mercurial > touhou
comparison music.py @ 326:efcdf2ce747c
Add a very simple music player, for testing purpose.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 21 Jun 2012 15:01:01 +0200 |
parents | |
children | b0b8825296d0 |
comparison
equal
deleted
inserted
replaced
325:cddfd3cb4797 | 326:efcdf2ce747c |
---|---|
1 #!/usr/bin/env python | |
2 # -*- encoding: utf-8 -*- | |
3 ## | |
4 ## Copyright (C) 2012 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | |
5 ## | |
6 ## This program is free software; you can redistribute it and/or modify | |
7 ## it under the terms of the GNU General Public License as published | |
8 ## by the Free Software Foundation; version 3 only. | |
9 ## | |
10 ## This program is distributed in the hope that it will be useful, | |
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 ## GNU General Public License for more details. | |
14 ## | |
15 | |
16 import argparse | |
17 import os | |
18 | |
19 import pyximport | |
20 pyximport.install() | |
21 | |
22 from pytouhou.resource.loader import Loader | |
23 from pytouhou.ui.music import InfiniteWaveSource, ZwavSource | |
24 from pyglet.app import run | |
25 | |
26 | |
27 def get_wav_source(bgm, resource_loader): | |
28 posname = bgm.replace('bgm/', '').replace('.mid', '.pos') | |
29 track = resource_loader.get_track(posname) | |
30 wavname = os.path.join(resource_loader.game_dir, bgm.replace('.mid', '.wav')) | |
31 try: | |
32 source = InfiniteWaveSource(wavname, track.start, track.end) | |
33 except IOError: | |
34 source = None | |
35 return source | |
36 | |
37 | |
38 def get_zwav_source(track, resource_loader): | |
39 fmt = resource_loader.get_fmt('thbgm.fmt') | |
40 try: | |
41 source = ZwavSource('thbgm.dat', fmt[track]) | |
42 except IOError: | |
43 source = None | |
44 return source | |
45 | |
46 | |
47 def main(path, track, zwav, data): | |
48 resource_loader = Loader(path) | |
49 resource_loader.scan_archives(data) | |
50 | |
51 if not zwav: | |
52 source = get_wav_source('bgm/th06_%02d.mid' % track, resource_loader) | |
53 else: | |
54 source = get_zwav_source(track, resource_loader) | |
55 | |
56 source.play() | |
57 | |
58 run() | |
59 | |
60 | |
61 pathsep = os.path.pathsep | |
62 default_data = (pathsep.join(('MD.DAT', 'th6*MD.DAT', '*MD.DAT', '*md.dat')),) | |
63 | |
64 | |
65 parser = argparse.ArgumentParser(description='Player for Touhou 6 music.') | |
66 | |
67 parser.add_argument('data', metavar='DAT', default=default_data, nargs='*', help='Game’s data files') | |
68 parser.add_argument('-p', '--path', metavar='DIRECTORY', default='.', help='Game directory path.') | |
69 parser.add_argument('-t', '--track', metavar='TRACK', type=int, required=True, help='The track to play, in game order.') | |
70 parser.add_argument('-z', '--zwav', action='store_true', default=False, help='Must be set when playing from PCB or newer.') | |
71 | |
72 args = parser.parse_args() | |
73 | |
74 main(args.path, args.track, args.zwav, tuple(args.data)) |