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