Mercurial > code > home > repos > light9
comparison flax/Submaster.py @ 135:5670f66845ce
- results of work from 6.13 rehearsal
author | dmcc |
---|---|
date | Sat, 14 Jun 2003 15:00:47 +0000 |
parents | 45b12307c695 |
children | 304152488ed7 |
comparison
equal
deleted
inserted
replaced
134:f2f73a2171e6 | 135:5670f66845ce |
---|---|
6 | 6 |
7 import Patch | 7 import Patch |
8 | 8 |
9 class Submaster: | 9 class Submaster: |
10 "Contain a dictionary of levels, but you didn't need to know that" | 10 "Contain a dictionary of levels, but you didn't need to know that" |
11 def __init__(self, name, leveldict=None, temporary=0): | 11 def __init__(self, name, leveldict=None): |
12 self.name = name | 12 self.name = name |
13 self.temporary = temporary | |
14 if leveldict: | 13 if leveldict: |
15 self.levels = leveldict | 14 self.levels = leveldict |
16 else: | 15 else: |
17 self.levels = {} | 16 self.levels = {} |
18 self.reload() | 17 self.reload() |
19 def reload(self): | 18 def reload(self): |
20 if self.temporary: | |
21 return | |
22 try: | 19 try: |
23 self.levels.clear() | 20 self.levels.clear() |
24 subfile = file("subs/%s" % self.name) | 21 subfile = file("subs/%s" % self.name) |
25 for line in subfile.readlines(): | 22 for line in subfile.readlines(): |
26 if not line.strip(): # if line is only whitespace | 23 if not line.strip(): # if line is only whitespace |
34 print "(%s) Error with this line: %s" % (self.name, | 31 print "(%s) Error with this line: %s" % (self.name, |
35 line[:-1]) | 32 line[:-1]) |
36 except IOError: | 33 except IOError: |
37 print "Can't read file for sub: %s" % self.name | 34 print "Can't read file for sub: %s" % self.name |
38 def save(self): | 35 def save(self): |
39 if self.temporary: | |
40 return | |
41 | |
42 subfile = file("subs/%s" % self.name, 'w') | 36 subfile = file("subs/%s" % self.name, 'w') |
43 names = self.levels.keys() | 37 names = self.levels.keys() |
44 names.sort() | 38 names.sort() |
45 for name in names: | 39 for name in names: |
46 val = self.levels[name] | 40 val = self.levels[name] |
56 self.save() | 50 self.save() |
57 def get_levels(self): | 51 def get_levels(self): |
58 return self.levels | 52 return self.levels |
59 def __mul__(self, scalar): | 53 def __mul__(self, scalar): |
60 return Submaster("%s*%s" % (self.name, scalar), | 54 return Submaster("%s*%s" % (self.name, scalar), |
61 dict_scale(self.levels, scalar), temporary=1) | 55 dict_scale(self.levels, scalar)) |
62 __rmul__ = __mul__ | 56 __rmul__ = __mul__ |
63 def max(self, *othersubs): | 57 def max(self, *othersubs): |
64 return sub_maxes(self, *othersubs) | 58 return sub_maxes(self, *othersubs) |
65 def __repr__(self): | 59 def __repr__(self): |
66 levels = ' '.join(["%s:%.2f" % item for item in self.levels.items()]) | 60 return "<%s: %r>" % (self.name, self.levels) |
67 return "<'%s': [%s]>" % (self.name, levels) | |
68 def get_dmx_list(self): | 61 def get_dmx_list(self): |
69 leveldict = self.get_levels() # gets levels of sub contents | 62 leveldict = self.get_levels() # gets levels of sub contents |
70 | 63 |
71 levels = [0] * 68 | 64 levels = [0] * 68 |
72 for k, v in leveldict.items(): | 65 for k, v in leveldict.items(): |
73 dmxchan = Patch.get_dmx_channel(k) - 1 | 66 levels[Patch.get_dmx_channel(k) - 1] = v |
74 levels[dmxchan] = max(v, levels[dmxchan]) | |
75 | 67 |
76 return levels | 68 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 | 69 |
114 def sub_maxes(*subs): | 70 def sub_maxes(*subs): |
115 return Submaster("max(%r)" % (subs,), | 71 return Submaster("max(%r)" % (subs,), |
116 dict_max(*[sub.levels for sub in subs]), temporary=1) | 72 dict_max(*[sub.levels for sub in subs])) |
117 | 73 |
118 class Submasters: | 74 class Submasters: |
119 "Collection o' Submaster objects" | 75 "Collection o' Submaster objects" |
120 def __init__(self): | 76 def __init__(self): |
121 self.submasters = {} | 77 self.submasters = {} |
122 | 78 |
123 import os | 79 import os |
124 files = os.listdir('subs') | 80 files = os.listdir('subs') |
125 | 81 |
126 for filename in files: | 82 for filename in files: |
127 # we don't want these files | 83 if filename.startswith('.') or filename.endswith('~'): |
128 if filename.startswith('.') or filename.endswith('~') or \ | |
129 filename.startswith('CVS'): | |
130 continue | 84 continue |
131 self.submasters[filename] = Submaster(filename) | 85 self.submasters[filename] = Submaster(filename) |
132 def get_all_subs(self): | 86 def get_all_subs(self): |
133 "All Submaster objects" | 87 "All Submaster objects" |
134 l = self.submasters.items() | 88 return self.submasters.values() |
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): | 89 def get_sub_by_name(self, name): |
147 "Makes a new sub if there isn't one." | 90 "Makes a new sub if there isn't one." |
148 return self.submasters.get(name, Submaster(name)) | 91 return self.submasters.get(name, Submaster(name)) |
149 __getitem__ = get_sub_by_name | 92 __getitem__ = get_sub_by_name |
150 | 93 |
151 if __name__ == "__main__": | 94 if __name__ == "__main__": |
152 Patch.reload_data() | 95 Patch.reload_data() |
153 s = Submasters() | 96 s = Submasters() |
97 newsub = s['newsub'] | |
98 newsub.set_all_levels({'5' : 1, '7': 0.2}) | |
154 print s.get_all_subs() | 99 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 |