Skip to content

Commit

Permalink
Persist brightness configuration in scene metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
segfault16 committed May 29, 2020
1 parent 018259e commit adcb683
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
33 changes: 29 additions & 4 deletions audioled/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

logger = logging.getLogger(__name__)

SCENE_META_BRIGHTNESS = "brightness"

def ensure_parent(func):
@wraps(func)
Expand Down Expand Up @@ -566,6 +567,8 @@ def activateScene(self, sceneId):
continue

self._createOrUpdateProcess(dIdx, device, slotId, filterGraph)
# Update devices for scene brightness
self.setBrightnessForActiveScene(self.getBrightnessActiveScene())
dIdx += 1
finally:
self._processingEnabled = True
Expand Down Expand Up @@ -597,10 +600,30 @@ def getController(self):
ctrl.update(update)
return ctrl

def setBrightness(self, value):
def setBrightnessForActiveScene(self, value):
# Store brightness value for current Scene
if self.activeSceneId is None:
return
actSceneId = str(self.activeSceneId)
if actSceneId not in self.sceneMetadata:
logger.error("No metadata for active scene with id {}".format(actSceneId))
return
self.sceneMetadata[actSceneId][SCENE_META_BRIGHTNESS] = value
# Brightness per device
self._sendBrightnessCommand(value)

def getBrightnessActiveScene(self):
# returns brightness for current scene
if self.activeSceneId is None:
return 1.
actSceneId = str(self.activeSceneId)
if actSceneId not in self.sceneMetadata:
logger.error("No metadata for active scene with id {}".format(actSceneId))
return 1.
if SCENE_META_BRIGHTNESS in self.sceneMetadata[actSceneId]:
return self.sceneMetadata[actSceneId][SCENE_META_BRIGHTNESS]
return 1.

def stopProcessing(self):
logger.info('Stop processing')
self._processingEnabled = False
Expand Down Expand Up @@ -753,7 +776,8 @@ def _convertSlotMatrixToScene(self, slotMatrix):
# "refSlot": 12,
# "filtergraph": null // TODO project without slots could be added this way
# }
# }
# },
# "brightness": 1.
# }

outputsForScene = {}
Expand All @@ -773,7 +797,7 @@ def _convertSlotMatrixToScene(self, slotMatrix):

sceneMeta = {}
for sceneId in outputsForScene.keys():
sceneMeta[sceneId] = {"name": "Unnamed scene", "output": outputsForScene[sceneId]}
sceneMeta[sceneId] = {"name": "Unnamed scene", "output": outputsForScene[sceneId], "brightness": 1.}

logger.info("Converted slot matrix to scene meta: {}".format(sceneMeta))
self.sceneMetadata = sceneMeta
Expand All @@ -793,7 +817,7 @@ def _getSlotForDevice(self, dIdx, sceneId, create=False):
if sceneId not in self.sceneMetadata:
if create:
logger.info("Backwards compatibility: Init scene {}".format(sceneId))
self.sceneMetadata[sceneId] = {"name": "Unnamed scene", "output": {}}
self.sceneMetadata[sceneId] = {"name": "Unnamed scene", "output": {}, "brightness": 1.}
else:
return None
outputs = self.sceneMetadata[sceneId]["output"]
Expand Down Expand Up @@ -908,6 +932,7 @@ def _createOrUpdateProcess(self, dIdx, device, slotId, filterGraph):
p = mp.Process(target=output, args=(q, outputDevice, virtualDevice))
p.start()
# Make sure process starts
q.put(BrightnessMessage(self.getBrightnessActiveScene()))
q.put("check_is_processing")
time.sleep(sleepfact * 0.1)
if not q._unfinished_tasks._semlock._is_zero():
Expand Down
20 changes: 12 additions & 8 deletions audioled_controller/midi_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,15 @@ def _sendControllerStatus(self, proj):
sendMsg.value = int(v * 127)
if self._sendMidiCallback is not None:
self._sendMidiCallback(sendMsg)
# Send current brightness value
brightness = proj.getBrightnessActiveScene()
if brightness is not None:
sendMsg = mido.Message('control_change')
sendMsg.channel = 1
sendMsg.control = 7
sendMsg.value = int(brightness * 127)
if self._sendMidiCallback is not None:
self._sendMidiCallback(sendMsg)

def _getUpdatePaths(self, paths: str):
if ',' in paths:
Expand All @@ -548,13 +557,8 @@ def _handleProgramChange(self, program, proj):
if self._sendMidiCallback is not None:
self._sendMidiCallback(self._createEnabledControllersMsg(proj))

# TODO: Send brightness
# brightness = proj.getBrightness() # TODO: Implement
# sendMsg = mido.Message('control_change')
# sendMsg.channel = 1
# sendMsg.control = 7
# sendMsg.value = brightness * 127
# midiBluetooth.send(sendMsg)



def _handleControlChange(self, ctrl, value, proj):
if ctrl in controllerMap:
Expand All @@ -563,7 +567,7 @@ def _handleControlChange(self, ctrl, value, proj):
logger.debug("Propagating control change message")
if controlMsg == modulation.CTRL_BRIGHTNESS:
# Handle brightness globally
proj.setBrightness(value / 127)
proj.setBrightnessForActiveScene(value / 127)
else:
proj.updateModulationSourceValue(0xFFF, controlMsg, controlVal)
else:
Expand Down
3 changes: 2 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,11 +816,12 @@ def remote_brightness_post():
value = int(request.args.get('value'))
floatVal = float(value / 100)
app.logger.info("Setting brightness: {}".format(floatVal))
proj.setBrightness(floatVal)
proj.setBrightnessForActiveScene(floatVal)
return "OK"

@app.route('/remote/favorites/<id>', methods=['POST'])
def remote_favorites_id_post(id):
# TODO: Switch to selecting scenes
filename = "favorites/{}.json".format(id)
global proj
if os.path.isfile(filename):
Expand Down

0 comments on commit adcb683

Please sign in to comment.