Changeset - f31df46edfdd
[Not reviewed]
default
0 1 0
drewp@bigasterisk.com - 20 months ago 2023-05-30 02:36:24
drewp@bigasterisk.com
reformat
1 file changed with 12 insertions and 10 deletions:
0 comments (0 inline, 0 general)
light9/effect/effecteval.py
Show inline comments
 
@@ -55,86 +55,88 @@ def lerp(a, b, t):
 

	
 
def noise(t):
 
    return pnoise1(t % 1000.0, 2)
 

	
 

	
 
def clamp(lo, hi, x):
 
    return max(lo, min(hi, x))
 

	
 

	
 
def clamp255(x):
 
    return min(255, max(0, x))
 

	
 

	
 
def _8bit(f):
 
    if not isinstance(f, (int, float)):
 
        raise TypeError(repr(f))
 
    return clamp255(int(f * 255))
 

	
 

	
 
@dataclass
 
class EffectEval2:
 
    graph: SyncedGraph
 
    uri: EffectUri
 

	
 
    effectFunction: Optional[URIRef]=None
 
    effectFunction: Optional[URIRef] = None
 

	
 
    def __post_init__(self):
 
        self.graph.addHandler(self._compile)
 
        self.effectFunction = L9['todo']
 

	
 
    def _compile(self):
 
        if not self.graph.contains((self.uri, RDF.type, L9['Effect'])):
 
            raise ValueError(f'{self.uri} not an :Effect')
 

	
 
        self.function = effect_scale   
 
        self.function = effect_scale
 
        devs = []
 
        for s in self.graph.objects(self.uri, L9['setting']):
 
            d = typedValue(DeviceUri, self.graph, s, L9['device'])     
 
            da = typedValue(DeviceAttr, self.graph, s, L9['deviceAttr'])     
 
            d = typedValue(DeviceUri, self.graph, s, L9['device'])
 
            da = typedValue(DeviceAttr, self.graph, s, L9['deviceAttr'])
 
            v = typedValue(VTUnion, self.graph, s, L9['value'])
 
            devs.append((d, da, v))
 
        self.devs = DeviceSettings(self.graph, devs)
 

	
 
    def compute(self, inputs:EffectSettings) -> DeviceSettings:
 
    def compute(self, inputs: EffectSettings) -> DeviceSettings:
 

	
 
        s=0
 
        for e,ea,v in inputs.asList():
 
        s = 0
 
        for e, ea, v in inputs.asList():
 
            if not isinstance(v, float):
 
                raise TypeError
 
            if ea==L9['strength']:
 
            if ea == L9['strength']:
 
                s = v
 

	
 
        return effect_scale(s,self.devs )
 
        return effect_scale(s, self.devs)
 

	
 
        return self.function(inputs)
 

	
 

	
 
def effect_scale(strength: float, devs: DeviceSettings) -> DeviceSettings:
 
    out = []
 
    for d,da,v in devs.asList():
 
    for d, da, v in devs.asList():
 
        out.append((d, da, scale(v, strength)))
 
    return DeviceSettings(devs.graph, out)
 

	
 

	
 
@dataclass
 
class EffectEval:
 
    """
 
    runs one effect's code to turn effect attr settings into output
 
    device settings. No effect state; suitable for reload().
 
    """
 
    graph: SyncedGraph
 
    effect: EffectClass
 
    simpleOutputs: SimpleOutputs
 

	
 
    def outputFromEffect(self, effectSettings: BareEffectSettings, songTime: float, noteTime: float) -> Tuple[DeviceSettings, Dict]:
 
        """
 
        From effect attr settings, like strength=0.75, to output device
 
        settings like light1/bright=0.72;light2/bright=0.78. This runs
 
        the effect code.
 
        """
 
        # todo: what does the next comment line mean?
 
        # both callers need to apply note overrides
 

	
 
        strength = float(effectSettings.s[EffectAttr(L9['strength'])])
 
        if strength <= 0:
 
            return DeviceSettings(self.graph, []), {'zero': True}
 

	
 
        report = {}
0 comments (0 inline, 0 general)