def start(self, config_lock, notification_queue_device_manager_in, notification_queue_device_manager_out, notification_queue_audio_in, notification_queue_audio_out, notification_queue_webserver_in, notification_queue_webserver_out): self._config_lock = config_lock self._notification_queue_device_manager_in = notification_queue_device_manager_in self._notification_queue_device_manager_out = notification_queue_device_manager_out self._notification_queue_audio_in = notification_queue_audio_in self._notification_queue_audio_out = notification_queue_audio_out self._notification_queue_webserver_in = notification_queue_webserver_in self._notification_queue_webserver_out = notification_queue_webserver_out self._current_notification_item = NotificationItem(NotificationEnum.config_refresh, "all_devices") self._cancel_token = False print("NotificationService component started.") while not self._cancel_token: # 1. Check Webserver # 2. Check Output # 3. Check Effects sleep(0.5) if not self._notification_queue_webserver_out.empty(): print("NotificationService: New Notification detected.") self._current_notification_item = self._notification_queue_webserver_out.get() if self._current_notification_item.notification_enum is NotificationEnum.config_refresh: print("Reload config..") self.config_refresh(self._current_notification_item) print("Config reloaded.")
def PutIntoNotificationQueue(self, notificication, device): print("Prepare new Notification") notification_item = NotificationItem(notificication, device) print("Notification Item prepared: " + str(notification_item.notification_enum) + " " + notification_item.device_id) self.notification_queue_out.put(notification_item) print("Notification Item put into queue.")
def PutIntoNotificationQueue(self, notificication, device): self.logger.debug("Preparing new Notification...") notification_item = NotificationItem(notificication, device) self.logger.debug( f"Notification Item prepared: {notification_item.notification_enum} {notification_item.device_id}" ) self.notification_queue_out.put(notification_item) self.logger.debug("Notification Item put into queue.")
def start(self, config_lock, notification_queue_device_manager_in, notification_queue_device_manager_out, notification_queue_audio_in, notification_queue_audio_out, notification_queue_webserver_in, notification_queue_webserver_out): self.logger = logging.getLogger(__name__) self._config_lock = config_lock self._notification_queue_device_manager_in = QueueWrapper( notification_queue_device_manager_in) self._notification_queue_device_manager_out = QueueWrapper( notification_queue_device_manager_out) self._notification_queue_audio_in = QueueWrapper( notification_queue_audio_in) self._notification_queue_audio_out = QueueWrapper( notification_queue_audio_out) self._notification_queue_webserver_in = QueueWrapper( notification_queue_webserver_in) self._notification_queue_webserver_out = QueueWrapper( notification_queue_webserver_out) self._current_notification_item = NotificationItem( NotificationEnum.config_refresh, "all_devices") self._cancel_token = False self.logger.debug("NotificationService component started.") while not self._cancel_token: # 1. Check Webserver # 2. Check Output # 3. Check Effects try: sleep(0.5) if not self._notification_queue_webserver_out.empty(): self.logger.debug( "NotificationService: New Notification detected.") self._current_notification_item = self._notification_queue_webserver_out.get_blocking( ) self.logger.debug("Item get") if self._current_notification_item.notification_enum is NotificationEnum.config_refresh: self.logger.debug("Reloading config...") self.config_refresh(self._current_notification_item) self.logger.debug("Config reloaded.") except KeyboardInterrupt: break
def config_refresh(self, original_notification_item): device_id = original_notification_item.device_id # Summary # 1. Pause every process that has to refresh the config. # 2. Send the refresh command. # 3. Wait for all to finish the process. # 4. Continue the processes. self.logger.debug("1. Pause") # 1. Pause every process that has to refresh the config. self._notification_queue_device_manager_in.put_blocking( NotificationItem(NotificationEnum.process_pause, device_id)) self._notification_queue_audio_in.put_blocking( NotificationItem(NotificationEnum.process_pause, device_id)) self.logger.debug("2. Refresh") # 2. Send the refresh command. self._notification_queue_device_manager_in.put_blocking( NotificationItem(NotificationEnum.config_refresh, device_id)) self._notification_queue_audio_in.put_blocking( NotificationItem(NotificationEnum.config_refresh, device_id)) # 3. Wait for all to finish the process. processes_not_ready = True self.logger.debug("3. Wait") device_ready = False effect_ready = False while processes_not_ready: # Check the notification queue of device_manager, if it is ready to continue. if (not self._notification_queue_device_manager_out.empty()): current_output_out = self._notification_queue_device_manager_out.get_blocking( ) if current_output_out.notification_enum is NotificationEnum.config_refresh_finished: device_ready = True self.logger.debug("Device refreshed the config.") # Check the notification queue of audio, if it is ready to continue. if (not self._notification_queue_audio_out.empty()): current_effects_out = self._notification_queue_audio_out.get_blocking( ) if current_effects_out.notification_enum is NotificationEnum.config_refresh_finished: effect_ready = True self.logger.debug("Audio refreshed the config.") if device_ready and effect_ready: processes_not_ready = False # 4. Continue the processes. self._notification_queue_device_manager_in.put_blocking( NotificationItem(NotificationEnum.process_continue, device_id)) self._notification_queue_audio_in.put_blocking( NotificationItem(NotificationEnum.process_continue, device_id))
def audio_service_routine(self): try: if not self._notification_queue_in.empty(): current_notification_item = self._notification_queue_in.get() if current_notification_item.notification_enum is NotificationEnum.config_refresh: if self.stream is not None: self.stream.stop_stream() self.stream.close() self.init_audio_service() self._notification_queue_out.put(NotificationItem(NotificationEnum.config_refresh_finished, current_notification_item.device_id)) elif current_notification_item.notification_enum is NotificationEnum.process_continue: self._skip_routine = False elif current_notification_item.notification_enum is NotificationEnum.process_pause: self._skip_routine = True if self._skip_routine: return try: in_data = self.audio_buffer_queue.get(block=True, timeout=1) except Empty as e: self.logger.debug("Audio in timeout. Queue is Empty") return # Convert the raw string audio stream to an array. y = np.fromstring(in_data, dtype=np.int16) # Use the type float32. y = y.astype(np.float32) # Process the audio stream. audio_datas = self._dsp.update(y) # Check if value is higher than min value. if audio_datas["vol"] < self._config["general_settings"]["MIN_VOLUME_THRESHOLD"]: # Fill the array with zeros, to fade out the effect. audio_datas["mel"] = np.zeros(self.n_fft_bins) if self._audio_queue.full(): try: pre_audio_data = self._audio_queue.get(block=True, timeout=0.033) del pre_audio_data except Exception as e: pass self._audio_queue.put(audio_datas, False) self.end_time_2 = time() if time() - self.ten_seconds_counter_2 > 10: self.ten_seconds_counter_2 = time() time_dif = self.end_time_2 - self.start_time_2 fps = 1 / time_dif self.logger.info(f"Routine | FPS: {fps:.2f}") self.start_time_2 = time() except IOError: self.logger.exception("IOError while reading the Microphone Stream.") pass except Exception as e: self.logger.error("Could not run AudioService routine.") self.logger.exception(f"Unexpected error in routine: {e}")
def routine(self): # Check the effect queue. if not self._effect_queue.empty(): current_effect_item = self._effect_queue.get_blocking() self.logger.debug( f"Device Manager received new effect: {current_effect_item.effect_enum} {current_effect_item.device_id}" ) current_device = self._devices[current_effect_item.device_id] current_device.effect_queue.put_blocking(current_effect_item) if not self._notification_queue_in.empty(): current_notification_item = self._notification_queue_in.get_blocking( ) self.logger.debug( f"Device Manager received new notification: {current_notification_item.notification_enum} - {current_notification_item.device_id}" ) if current_notification_item.notification_enum is NotificationEnum.config_refresh: devices_count_before_reload = len( self._config["device_configs"].keys()) self.logger.debug( f"Device count before: {devices_count_before_reload}") self.reload_config() devices_count_after_reload = len( self._config["device_configs"].keys()) self.logger.debug( f"Device count after: {devices_count_after_reload}") if (devices_count_before_reload != devices_count_after_reload): self.reinit_devices() if (current_notification_item.device_id == "all_devices"): for key, value in self._devices.items(): self.restart_device(key) else: self.restart_device(current_notification_item.device_id) self._notification_queue_out.put_blocking( NotificationItem(NotificationEnum.config_refresh_finished, current_notification_item.device_id)) elif current_notification_item.notification_enum is NotificationEnum.process_continue: self._skip_routine = False elif current_notification_item.notification_enum is NotificationEnum.process_pause: self._skip_routine = True # Limit the fps to decrease lags caused by 100 percent CPU. self._fps_limiter.fps_limiter() if self._skip_routine: return audio_data = self.get_audio_data() self.refresh_audio_queues(audio_data) self.end_time = time() if time() - self.ten_seconds_counter > 10: self.ten_seconds_counter = time() self.time_dif = self.end_time - self.start_time self.fps = 1 / self.time_dif self.logger.info(f"FPS: {self.fps:.2f}") self.start_time = time()
def audio_service_routine(self): try: if not self._notification_queue_in.empty(): current_notification_item = self._notification_queue_in.get() if current_notification_item.notification_enum is NotificationEnum.config_refresh: if not self.stream is None: self.stream.stop_stream() self.stream.close() self.init_audio_service() self._notification_queue_out.put( NotificationItem( NotificationEnum.config_refresh_finished, current_notification_item.device_id)) elif current_notification_item.notification_enum is NotificationEnum.process_continue: self._skip_routine = False elif current_notification_item.notification_enum is NotificationEnum.process_pause: self._skip_routine = True if self._skip_routine: return in_data = self.audio_buffer_queue.get() # Convert the raw string audio stream to an array. y = np.fromstring(in_data, dtype=np.int16) # Use the type float32 y = y.astype(np.float32) # Process the audio stream audio_datas = self._dsp.update(y) #Check if value is higher than min value if audio_datas["vol"] < self._config["general_settings"][ "MIN_VOLUME_THRESHOLD"]: # Fill the array with zeros, to fade out the effect. audio_datas["mel"] = np.zeros(1) if self._audio_queue.full(): try: pre_audio_data = self._audio_queue.get(block=True, timeout=0.033) del pre_audio_data except: pass self._audio_queue.put(audio_datas, False) self.end_time_2 = time.time() if time.time() - self.ten_seconds_counter_2 > 10: self.ten_seconds_counter_2 = time.time() time_dif = self.end_time_2 - self.start_time_2 fps = 1 / time_dif print("Audio Service Routine | FPS: " + str(fps)) self.start_time_2 = time.time() except IOError: print("IOError during reading the Microphone Stream.") pass