Mercurial > touhou
comparison pytouhou/formats/fmt.py @ 325:cddfd3cb4797
Add music support for >PCB.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 21 Jun 2012 15:01:01 +0200 |
parents | |
children | d8aab27a2ab2 |
comparison
equal
deleted
inserted
replaced
324:c412df42aa15 | 325:cddfd3cb4797 |
---|---|
1 # -*- encoding: utf-8 -*- | |
2 ## | |
3 ## Copyright (C) 2012 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | |
4 ## | |
5 ## This program is free software; you can redistribute it and/or modify | |
6 ## it under the terms of the GNU General Public License as published | |
7 ## by the Free Software Foundation; version 3 only. | |
8 ## | |
9 ## This program is distributed in the hope that it will be useful, | |
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 ## GNU General Public License for more details. | |
13 ## | |
14 | |
15 | |
16 from struct import unpack | |
17 | |
18 | |
19 class Track(object): | |
20 def __init__(self): | |
21 self.name = '' | |
22 | |
23 # loop info | |
24 self.intro = 0 | |
25 #self.unknown | |
26 self.start = 0 | |
27 self.duration = 0 | |
28 | |
29 # WAVE header | |
30 self.wFormatTag = 1 | |
31 self.wChannels = 2 | |
32 self.dwSamplesPerSec = 44100 | |
33 self.dwAvgBytesPerSec = 176400 | |
34 self.wBlockAlign = 4 | |
35 self.wBitsPerSample = 16 | |
36 | |
37 | |
38 class FMT(list): | |
39 @classmethod | |
40 def read(cls, file): | |
41 self = cls() | |
42 | |
43 file.seek(0) | |
44 while True: | |
45 track = Track() | |
46 track.name = unpack('<16s', file.read(16))[0] | |
47 if not ord(track.name[0]): | |
48 break | |
49 | |
50 # loop info | |
51 track.intro, unknown, track.start, track.duration = unpack('<IIII', file.read(16)) | |
52 | |
53 # WAVE header | |
54 (track.wFormatTag, | |
55 track.wChannels, | |
56 track.dwSamplesPerSec, | |
57 track.dwAvgBytesPerSec, | |
58 track.wBlockAlign, | |
59 track.wBitsPerSample) = unpack('<HHLLHH', file.read(16)) | |
60 | |
61 assert track.wFormatTag == 1 # We don’t support non-PCM formats | |
62 assert track.dwAvgBytesPerSec == track.dwSamplesPerSec * track.wBlockAlign | |
63 assert track.wBlockAlign == track.wChannels * track.wBitsPerSample // 8 | |
64 assert b'\00\00\00\00' == file.read(4) | |
65 | |
66 self.append(track) | |
67 | |
68 return self | |
69 |