|
| 1 | +#!/usr/bin/env python3 |
| 2 | +#** ***************************************************************************** |
| 3 | +# * |
| 4 | +# * If not stated otherwise in this file or this component's LICENSE file the |
| 5 | +# * following copyright and licenses apply: |
| 6 | +# * |
| 7 | +# * Copyright 2023 RDK Management |
| 8 | +# * |
| 9 | +# * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | +# * you may not use this file except in compliance with the License. |
| 11 | +# * You may obtain a copy of the License at |
| 12 | +# * |
| 13 | +# * |
| 14 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | +# * |
| 16 | +# * Unless required by applicable law or agreed to in writing, software |
| 17 | +# * distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | +# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | +# * See the License for the specific language governing permissions and |
| 20 | +# * limitations under the License. |
| 21 | +# * |
| 22 | +#* ****************************************************************************** |
| 23 | +#* |
| 24 | +#* ** Project : RAFT |
| 25 | +#* ** @addtogroup : core |
| 26 | +#* ** @date : 28/08/2025 |
| 27 | +#* ** |
| 28 | +#* ** @brief : AV Sync controller |
| 29 | +#* ** |
| 30 | +#* ****************************************************************************** |
| 31 | +import json |
| 32 | +from os import path |
| 33 | + |
| 34 | +import sys |
| 35 | +MY_PATH = path.realpath(__file__) |
| 36 | +MY_DIR = path.dirname(MY_PATH) |
| 37 | +sys.path.append(path.join(MY_DIR,'../../')) |
| 38 | +from framework.core.logModule import logModule |
| 39 | +from framework.core.avSyncModules import SyncOne2 |
| 40 | + |
| 41 | +class AVSyncController(): |
| 42 | + |
| 43 | + def __init__(self, log: logModule, config: dict): |
| 44 | + self._log = log |
| 45 | + self.controllerType = config.get('type') |
| 46 | + if self.controllerType.lower() == 'syncone2': |
| 47 | + port = config.get('port') |
| 48 | + input = config.get('audio_input','AUTO') |
| 49 | + extended_mode = config.get('extended_mode',False) |
| 50 | + speaker_distance = config.get('speaker_distance', None) |
| 51 | + if port is None: |
| 52 | + raise AttributeError('Cannot initialise SyncOne2 without port set in the rackConfig.') |
| 53 | + self.controller = SyncOne2(port, |
| 54 | + audio_input=input, |
| 55 | + extended_mode=extended_mode, |
| 56 | + speaker_dist=speaker_distance) |
| 57 | + @property |
| 58 | + def audio_trigger_level(self) -> int: |
| 59 | + """Audio Sensor Sensitivity Level. |
| 60 | +
|
| 61 | + The higher the level the easier the sensor will trigger. |
| 62 | + """ |
| 63 | + return self.controller.get_audio_trigger_level() |
| 64 | + |
| 65 | + @audio_trigger_level.setter |
| 66 | + def audio_trigger_level(self, audio_trigger_level: int): |
| 67 | + self.controller.set_audio_trigger_level(audio_trigger_level) |
| 68 | + |
| 69 | + @property |
| 70 | + def frame_rate(self) -> int: |
| 71 | + """Frame rate used in statistics calculations. |
| 72 | + """ |
| 73 | + return self.controller.get_frame_rate() |
| 74 | + |
| 75 | + @frame_rate.setter |
| 76 | + def frame_rate(self, frame_rate: int): |
| 77 | + self.controller.set_frame_rate(frame_rate) |
| 78 | + |
| 79 | + @property |
| 80 | + def mask_length(self) -> int: |
| 81 | + """Mask time length in milliseconds. |
| 82 | +
|
| 83 | + The time AVSync controller will wait after taking a measurement |
| 84 | + before re-arming to take the next measurement. |
| 85 | + """ |
| 86 | + return self.controller.get_mask_len() |
| 87 | + |
| 88 | + @mask_length.setter |
| 89 | + def mask_length(self, mask_length: int): |
| 90 | + self.controller.set_mask_len(mask_length) |
| 91 | + |
| 92 | + @property |
| 93 | + def offset(self) -> int: |
| 94 | + """Manual offset in milliseconds. |
| 95 | + |
| 96 | + This manual offset can be used where equipment delays are known. |
| 97 | + """ |
| 98 | + return self.controller.get_offset() |
| 99 | + |
| 100 | + @offset.setter |
| 101 | + def offset(self, offset: int): |
| 102 | + self.controller.set_offset(offset) |
| 103 | + |
| 104 | + @property |
| 105 | + def video_trigger_level(self) -> int: |
| 106 | + """Video Sensor Sensitivity Level. |
| 107 | +
|
| 108 | + The higher the level the easier the sensor will trigger. |
| 109 | + """ |
| 110 | + return self.controller.get_audio_trigger_level() |
| 111 | + |
| 112 | + @video_trigger_level.setter |
| 113 | + def video_trigger_level(self, video_trigger_level: int): |
| 114 | + self.controller.set_audio_trigger_level(video_trigger_level) |
| 115 | + |
| 116 | + def calibrate(self): |
| 117 | + """Calibrate the AVSync controller. |
| 118 | + """ |
| 119 | + self._log.info('Calibrating AVSync controller.') |
| 120 | + self.controller.calibrate() |
| 121 | + |
| 122 | + def start_measurements(self): |
| 123 | + """Start recording measurements. |
| 124 | +
|
| 125 | + The current measurement held in the buffer of the AVSync controller will |
| 126 | + be cleared before more measurements start being captured. |
| 127 | + """ |
| 128 | + self._log.info('Starting measurement collection from AVSync controller.') |
| 129 | + # Clear the measurements before we start taking more measurements. |
| 130 | + self.controller.clear_results() |
| 131 | + self.controller.start_measuring() |
| 132 | + |
| 133 | + def stop_measurements(self): |
| 134 | + """Stop recording measurements |
| 135 | + """ |
| 136 | + self._log.info('Stopping measurement collection from AVSync controller.') |
| 137 | + self.controller.stop_measuring() |
| 138 | + |
| 139 | + def clear_results(self): |
| 140 | + """Clear store results. |
| 141 | + """ |
| 142 | + self._log.info('Clearing stored results from AVSync controller.') |
| 143 | + self.controller.clear_results() |
| 144 | + |
| 145 | + def get_results(self) -> list[dict]: |
| 146 | + """Return the results of the most recent measurements. |
| 147 | +
|
| 148 | + Returns: |
| 149 | + results (list[dict]): list containing dictionary of each measurement recorded, |
| 150 | + with the following keys: |
| 151 | + {'milliseconds', |
| 152 | + 'frames', |
| 153 | + 'avg_milliseconds', |
| 154 | + 'avg_frames', |
| 155 | + 'span_milliseconds', |
| 156 | + 'span_frames'} |
| 157 | + """ |
| 158 | + self._log.debug('Retrieving results from AVSync controller.') |
| 159 | + results = self.controller.get_results() |
| 160 | + self._log.debug(json.dumps(results)) |
| 161 | + return results |
| 162 | + |
| 163 | +### MAIN ### |
| 164 | +if __name__ == '__main__': |
| 165 | + import time |
| 166 | + CONFIG = { |
| 167 | + 'type': 'SyncONE2', |
| 168 | + 'port': '/dev/ttyACM0', |
| 169 | + 'extended_mode': False, |
| 170 | + 'audio_input': 'EXTERNAL' |
| 171 | + } |
| 172 | + LOG = logModule('AVSync Logger') |
| 173 | + CONTROLLER = AVSyncController(LOG, CONFIG) |
| 174 | + |
| 175 | + # Test Audio Trigger Level |
| 176 | + CONTROLLER.audio_trigger_level = 2 |
| 177 | + res = CONTROLLER.audio_trigger_level |
| 178 | + print(res) |
| 179 | + |
| 180 | + # Test Frame Rate |
| 181 | + CONTROLLER.frame_rate = 30 |
| 182 | + print(CONTROLLER.frame_rate) |
| 183 | + |
| 184 | + # Test Mask Length |
| 185 | + CONTROLLER.mask_length = 300 |
| 186 | + print(CONTROLLER.mask_length) |
| 187 | + |
| 188 | + # Test offset |
| 189 | + CONTROLLER.offset = -10 |
| 190 | + print(CONTROLLER.offset) |
| 191 | + |
| 192 | + # Test Video Trigger Level |
| 193 | + CONTROLLER.video_trigger_level = 3 |
| 194 | + print(CONTROLLER.video_trigger_level) |
| 195 | + |
| 196 | + # Test capturing measurements |
| 197 | + # CONTROLLER.start_measurements() |
| 198 | + # time.sleep(5) |
| 199 | + # CONTROLLER.stop_measurements() |
| 200 | + print(json.dumps(CONTROLLER.get_results())) |
| 201 | + |
0 commit comments