Mercurial > code > home > repos > light9
comparison flax/Submaster.py @ 0:45b12307c695
Initial revision
author | drewp |
---|---|
date | Wed, 03 Jul 2002 09:37:57 +0000 |
parents | |
children | 5670f66845ce |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:45b12307c695 |
---|---|
1 from __future__ import division | |
2 from TLUtility import dict_scale, dict_max | |
3 | |
4 import sys | |
5 sys.path.append('../light8') | |
6 | |
7 import Patch | |
8 | |
9 class Submaster: | |
10 "Contain a dictionary of levels, but you didn't need to know that" | |
11 def __init__(self, name, leveldict=None, temporary=0): | |
12 self.name = name | |
13 self.temporary = temporary | |
14 if leveldict: | |
15 self.levels = leveldict | |
16 else: | |
17 self.levels = {} | |
18 self.reload() | |
19 def reload(self): | |
20 if self.temporary: | |
21 return | |
22 try: | |
23 self.levels.clear() | |
24 subfile = file("subs/%s" % self.name) | |
25 for line in subfile.readlines(): | |
26 if not line.strip(): # if line is only whitespace | |
27 continue # "did i say newspace?" | |
28 | |
29 try: | |
30 name, val = line.split(':') | |
31 name = name.strip() | |
32 self.levels[name] = float(val) | |
33 except ValueError: | |
34 print "(%s) Error with this line: %s" % (self.name, | |
35 line[:-1]) | |
36 except IOError: | |
37 print "Can't read file for sub: %s" % self.name | |
38 def save(self): | |
39 if self.temporary: | |
40 return | |
41 | |
42 subfile = file("subs/%s" % self.name, 'w') | |
43 names = self.levels.keys() | |
44 names.sort() | |
45 for name in names: | |
46 val = self.levels[name] | |
47 subfile.write("%s : %s\n" % (name, val)) | |
48 def set_level(self, channelname, level, save=1): | |
49 self.levels[Patch.resolve_name(channelname)] = level | |
50 if save: | |
51 self.save() | |
52 def set_all_levels(self, leveldict): | |
53 self.levels.clear() | |
54 for k, v in leveldict.items(): | |
55 self.set_level(k, v, save=0) | |
56 self.save() | |
57 def get_levels(self): | |
58 return self.levels | |
59 def __mul__(self, scalar): | |
60 return Submaster("%s*%s" % (self.name, scalar), | |
61 dict_scale(self.levels, scalar), temporary=1) | |
62 __rmul__ = __mul__ | |
63 def max(self, *othersubs): | |
64 return sub_maxes(self, *othersubs) | |
65 def __repr__(self): | |
66 levels = ' '.join(["%s:%.2f" % item for item in self.levels.items()]) | |
67 return "<'%s': [%s]>" % (self.name, levels) | |
68 def get_dmx_list(self): | |
69 leveldict = self.get_levels() # gets levels of sub contents | |
70 | |
71 levels = [0] * 68 | |
72 for k, v in leveldict.items(): | |
73 dmxchan = Patch.get_dmx_channel(k) - 1 | |
74 levels[dmxchan] = max(v, levels[dmxchan]) | |
75 | |
76 return levels | |
77 def normalize_patch_names(self): | |
78 """Use only the primary patch names.""" | |
79 # possibly busted -- don't use unless you know what you're doing | |
80 self.set_all_levels(self.levels.copy()) | |
81 def get_normalized_copy(self): | |
82 """Get a copy of this sumbaster that only uses the primary patch | |
83 names. The levels will be the same.""" | |
84 newsub = Submaster("%s (normalized)" % self.name, temporary=1) | |
85 newsub.set_all_levels(self.levels) | |
86 return newsub | |
87 def crossfade(self, othersub, amount): | |
88 """Returns a new sub that is a crossfade between this sub and | |
89 another submaster. | |
90 | |
91 NOTE: You should only crossfade between normalized submasters.""" | |
92 otherlevels = othersub.get_levels() | |
93 keys_set = {} | |
94 for k in self.levels.keys() + otherlevels.keys(): | |
95 keys_set[k] = 1 | |
96 all_keys = keys_set.keys() | |
97 | |
98 xfaded_sub = Submaster("xfade", temporary=1) | |
99 for k in all_keys: | |
100 xfaded_sub.set_level(k, | |
101 linear_fade(self.levels.get(k, 0), | |
102 otherlevels.get(k, 0), | |
103 amount)) | |
104 | |
105 return xfaded_sub | |
106 | |
107 def linear_fade(start, end, amount): | |
108 """Fades between two floats by an amount. amount is a float between | |
109 0 and 1. If amount is 0, it will return the start value. If it is 1, | |
110 the end value will be returned.""" | |
111 level = start + (amount * (end - start)) | |
112 return level | |
113 | |
114 def sub_maxes(*subs): | |
115 return Submaster("max(%r)" % (subs,), | |
116 dict_max(*[sub.levels for sub in subs]), temporary=1) | |
117 | |
118 class Submasters: | |
119 "Collection o' Submaster objects" | |
120 def __init__(self): | |
121 self.submasters = {} | |
122 | |
123 import os | |
124 files = os.listdir('subs') | |
125 | |
126 for filename in files: | |
127 # we don't want these files | |
128 if filename.startswith('.') or filename.endswith('~') or \ | |
129 filename.startswith('CVS'): | |
130 continue | |
131 self.submasters[filename] = Submaster(filename) | |
132 def get_all_subs(self): | |
133 "All Submaster objects" | |
134 l = self.submasters.items() | |
135 l.sort() | |
136 l = [x[1] for x in l] | |
137 songs = [] | |
138 notsongs = [] | |
139 for s in l: | |
140 if s.name.startswith('song'): | |
141 songs.append(s) | |
142 else: | |
143 notsongs.append(s) | |
144 combined = notsongs + songs | |
145 return combined | |
146 def get_sub_by_name(self, name): | |
147 "Makes a new sub if there isn't one." | |
148 return self.submasters.get(name, Submaster(name)) | |
149 __getitem__ = get_sub_by_name | |
150 | |
151 if __name__ == "__main__": | |
152 Patch.reload_data() | |
153 s = Submasters() | |
154 print s.get_all_subs() | |
155 if 0: # turn this on to normalize all subs | |
156 for sub in s.get_all_subs(): | |
157 print "before", sub | |
158 sub.normalize_patch_names() | |
159 sub.save() | |
160 print "after", sub |