class VolumeController: def __init__(self): # `Mixer` defaults to 'Master' # `mixers()` gives all available volume controls self.mixer = Mixer(mixers()[0]) # get the current volume self.current_vol = self.mixer.getvolume()[0] def get_handler(self, key): mapping = { 'toggle_mute': self.toggle_mute, 'vol_up': self.volume_up, 'vol_down': self.volume_down, } return mapping.get(key) def toggle_mute(self): if self.mixer.getmute()[0]: self.mixer.setmute(False) else: self.mixer.setmute(True) def volume_up(self): self.current_vol += 5 if self.current_vol > 100: self.current_vol = 100 self.mixer.setvolume(self.current_vol) def volume_down(self): self.current_vol -= 5 if self.current_vol < 0: self.current.vol = 0 self.mixer.setvolume(self.current_vol)
def _switch_gain_override(self): try: gain = Mixer('Gain Override') gain.setmute(0) except ALSAAudioError: gain = Mixer('Gain Override', self._def_id, self._def_card) gain.setmute(0) return None
def set_hdmi_mute(self, mute): try: mix = Mixer('HDMI') mix.setmute(mute) log.debug('Set HDMI sound control to %s', mute) return True except ALSAAudioError, e: log.error('Impossible to set HDMI sound mute to %s <%s>', mute, e) return False
def run(self, commandlist): mixer = Mixer() if len(commandlist) == 1: if mixer.getmute(): mixer.setmute(0) else: mixer.setmute(1) else: command = {'mode':'set'} for i in xrange(0, len(commandlist)-1): pass
def click(self, button): if button == 1: mixer = Mixer(self.mutedev) mixer.setmute(not bool(mixer.getmute()[0])) elif button == 4: mixer = Mixer(self.voldev) try: mixer.setvolume(mixer.getvolume()[0] + self.step) except ALSAAudioError: return elif button == 5: mixer = Mixer(self.voldev) try: mixer.setvolume(mixer.getvolume()[0] - self.step) except ALSAAudioError: return self.update()
class ALSA(IntervalModule): """ Shows volume of ALSA mixer. You can also use this for inputs, btw. Requires pyalsaaudio .. rubric:: Available formatters * `{volume}` — the current volume in percent * `{muted}` — the value of one of the `muted` or `unmuted` settings * `{card}` — the associated soundcard * `{mixer}` — the associated ALSA mixer """ interval = 1 settings = ( "format", ("format_muted", "optional format string to use when muted"), ("mixer", "ALSA mixer"), ("mixer_id", "ALSA mixer id"), ("card", "ALSA sound card"), ("increment", "integer percentage of max volume to in/decrement volume on mousewheel" ), "muted", "unmuted", "color_muted", "color", "channel", ("map_volume", "volume display/setting as in AlsaMixer. increment option is ignored then." )) muted = "M" unmuted = "" color_muted = "#AAAAAA" color = "#FFFFFF" format = "♪: {volume}" format_muted = None mixer = "Master" mixer_id = 0 card = -1 channel = 0 increment = 5 map_volume = False alsamixer = None has_mute = True on_upscroll = "increase_volume" on_downscroll = "decrease_volume" on_leftclick = "switch_mute" on_rightclick = on_leftclick def init(self): self.create_mixer() try: self.alsamixer.getmute() except ALSAAudioError: self.has_mute = False self.fdict = { "card": self.alsamixer.cardname(), "mixer": self.mixer, } self.dbRng = self.alsamixer.getrange() self.dbMin = self.dbRng[0] self.dbMax = self.dbRng[1] def create_mixer(self): self.alsamixer = Mixer(control=self.mixer, id=self.mixer_id, cardindex=self.card) def run(self): self.create_mixer() muted = False if self.has_mute: muted = self.alsamixer.getmute()[self.channel] == 1 self.fdict["volume"] = self.get_cur_volume() self.fdict["muted"] = self.muted if muted else self.unmuted self.fdict["db"] = self.get_db() if muted and self.format_muted is not None: output_format = self.format_muted else: output_format = self.format self.data = self.fdict self.output = { "full_text": output_format.format(**self.fdict), "color": self.color_muted if muted else self.color, } def switch_mute(self): if self.has_mute: muted = self.alsamixer.getmute()[self.channel] self.alsamixer.setmute(not muted) def get_cur_volume(self): if self.map_volume: dbCur = self.get_db() * 100.0 dbMin = self.dbMin * 100.0 dbMax = self.dbMax * 100.0 dbCur_norm = self.exp10((dbCur - dbMax) / 6000.0) dbMin_norm = self.exp10((dbMin - dbMax) / 6000.0) vol = (dbCur_norm - dbMin_norm) / (1 - dbMin_norm) vol = int(round(vol * 100, 0)) return vol else: return self.alsamixer.getvolume()[self.channel] def get_new_volume(self, direction): if direction == "inc": volume = (self.fdict["volume"] + 1) / 100 elif direction == "dec": volume = (self.fdict["volume"] - 1) / 100 dbMin = self.dbMin * 100 dbMax = self.dbMax * 100 dbMin_norm = self.exp10((dbMin - dbMax) / 6000.0) vol = volume * (1 - dbMin_norm) + dbMin_norm if direction == "inc": dbNew = min(self.dbMax, ceil( ((6000.0 * log10(vol)) + dbMax) / 100)) elif direction == "dec": dbNew = max(self.dbMin, floor( ((6000.0 * log10(vol)) + dbMax) / 100)) volNew = int( round(self.map_db(dbNew, self.dbMin, self.dbMax, 0, 100), 0)) return volNew def increase_volume(self, delta=None): if self.map_volume: vol = self.get_new_volume("inc") self.alsamixer.setvolume(vol) else: vol = self.alsamixer.getvolume()[self.channel] self.alsamixer.setvolume( min(100, vol + (delta if delta else self.increment))) def decrease_volume(self, delta=None): if self.map_volume: vol = self.get_new_volume("dec") self.alsamixer.setvolume(vol) else: vol = self.alsamixer.getvolume()[self.channel] self.alsamixer.setvolume( max(0, vol - (delta if delta else self.increment))) def get_db(self): db = (((self.dbMax - self.dbMin) / 100) * self.alsamixer.getvolume()[self.channel]) + self.dbMin db = int(round(db, 0)) return db def map_db(self, value, dbMin, dbMax, volMin, volMax): dbRange = dbMax - dbMin volRange = volMax - volMin volScaled = float(value - dbMin) / float(dbRange) return volMin + (volScaled * volRange) def exp10(self, x): return exp(x * log(10))
class ALSA(IntervalModule): """ Shows volume of ALSA mixer. You can also use this for inputs, btw. Requires pyalsaaudio .. rubric:: Available formatters * `{volume}` — the current volume in percent * `{muted}` — the value of one of the `muted` or `unmuted` settings * `{card}` — the associated soundcard * `{mixer}` — the associated ALSA mixer """ interval = 1 settings = ("format", ( "format_muted", "optional format string to use when muted" ), ("mixer", "ALSA mixer"), ("mixer_id", "ALSA mixer id"), ( "card", "ALSA sound card" ), ("increment", "integer percentage of max volume to in/decrement volume on mousewheel" ), "muted", "unmuted", "color_muted", "color", "channel") muted = "M" unmuted = "" color_muted = "#AAAAAA" color = "#FFFFFF" format = "♪: {volume}" format_muted = None mixer = "Master" mixer_id = 0 card = 0 channel = 0 increment = 5 alsamixer = None has_mute = True on_upscroll = "increase_volume" on_downscroll = "decrease_volume" on_leftclick = "switch_mute" on_rightclick = on_leftclick def init(self): self.create_mixer() try: self.alsamixer.getmute() except ALSAAudioError: self.has_mute = False self.fdict = { "card": self.alsamixer.cardname(), "mixer": self.mixer, } def create_mixer(self): self.alsamixer = Mixer(control=self.mixer, id=self.mixer_id, cardindex=self.card) def run(self): self.create_mixer() muted = False if self.has_mute: muted = self.alsamixer.getmute()[self.channel] == 1 self.fdict["volume"] = self.alsamixer.getvolume()[self.channel] self.fdict["muted"] = self.muted if muted else self.unmuted if muted and self.format_muted is not None: output_format = self.format_muted else: output_format = self.format self.output = { "full_text": output_format.format(**self.fdict), "color": self.color_muted if muted else self.color, } def switch_mute(self): if self.has_mute: muted = self.alsamixer.getmute()[self.channel] self.alsamixer.setmute(not muted) def increase_volume(self, delta=None): vol = self.alsamixer.getvolume()[self.channel] self.alsamixer.setvolume( min(100, vol + (delta if delta else self.increment))) def decrease_volume(self, delta=None): vol = self.alsamixer.getvolume()[self.channel] self.alsamixer.setvolume( max(0, vol - (delta if delta else self.increment)))
class ALSA(IntervalModule): """ Shows volume of ALSA mixer. You can also use this for inputs, btw. Requires pyalsaaudio .. rubric:: Available formatters * `{volume}` — the current volume in percent * `{muted}` — the value of one of the `muted` or `unmuted` settings * `{card}` — the associated soundcard * `{mixer}` — the associated ALSA mixer """ interval = 1 settings = ( "format", ("format_muted", "optional format string to use when muted"), ("mixer", "ALSA mixer"), ("mixer_id", "ALSA mixer id"), ("card", "ALSA sound card"), ("increment", "integer percentage of max volume to in/decrement volume on mousewheel"), "muted", "unmuted", "color_muted", "color", "channel" ) muted = "M" unmuted = "" color_muted = "#AAAAAA" color = "#FFFFFF" format = "♪: {volume}" format_muted = None mixer = "Master" mixer_id = 0 card = 0 channel = 0 increment = 5 alsamixer = None has_mute = True def init(self): self.create_mixer() try: self.alsamixer.getmute() except ALSAAudioError: self.has_mute = False self.fdict = { "card": self.alsamixer.cardname(), "mixer": self.mixer, } def create_mixer(self): self.alsamixer = Mixer( control=self.mixer, id=self.mixer_id, cardindex=self.card) def run(self): self.create_mixer() muted = False if self.has_mute: muted = self.alsamixer.getmute()[self.channel] == 1 self.fdict["volume"] = self.alsamixer.getvolume()[self.channel] self.fdict["muted"] = self.muted if muted else self.unmuted if muted and self.format_muted is not None: output_format = self.format_muted else: output_format = self.format self.output = { "full_text": output_format.format(**self.fdict), "color": self.color_muted if muted else self.color, } def on_leftclick(self): self.on_rightclick() def on_rightclick(self): if self.has_mute: muted = self.alsamixer.getmute()[self.channel] self.alsamixer.setmute(not muted) def on_upscroll(self): vol = self.alsamixer.getvolume()[self.channel] self.alsamixer.setvolume(min(100, vol + self.increment)) def on_downscroll(self): vol = self.alsamixer.getvolume()[self.channel] self.alsamixer.setvolume(max(0, vol - self.increment))
class TelegramSkill(MycroftSkill): def __init__(self): super(TelegramSkill, self).__init__(name="TelegramSkill") def initialize(self): # Handling settings changes self.settings_change_callback = self.on_settings_changed self.on_settings_changed() self.add_event('telegram-skill:response', self.sendHandler) self.add_event('speak', self.responseHandler) # Connection to Telegram API try: self.telegram_updater = Updater(token=self.bottoken, use_context=True) # get telegram Updates self.telegram_dispatcher = self.telegram_updater.dispatcher receive_handler = MessageHandler(Filters.text, self.TelegramMessages) # TODO: Make audio Files as Input possible: Filters.text | Filters.audio self.telegram_dispatcher.add_handler(receive_handler) self.telegram_updater.start_polling(clean=True) # start clean and look for messages wbot = telegram.Bot(token=self.bottoken) except: pass global loaded # get global variable if loaded == 0: # check if bot has just started loaded = 1 # make sure that users gets this message only once bot is newly loaded if self.mute == "false": msg = "Telegram Skill is loaded" self.sendMycroftSay(msg) loadedmessage = "Telegram-Skill on Mycroft Unit \""+ self.UnitName + "\" is loaded and ready to use!" # give User a nice message try: wbot.send_message(chat_id=self.user_id1, text=loadedmessage) # send welcome message to user 1 except: pass try: wbot.send_message(chat_id=self.user_id2, text=loadedmessage) # send welcome message to user 2 except: pass def on_settings_changed(self): global speak_tele speak_tele = 0 self.telegram_updater = None self.mute = str(self.settings.get('MuteIt','')) if (self.mute == 'True') or (self.mute == 'true'): try: self.mixer = Mixer() msg = "Telegram Messages will temporary mute Mycroft" logger.info(msg) except: global audioinit if audioinit == 0: audioinit = 1 msg = "There is a problem with alsa audio, mute is not working!" self.sendMycroftSay(msg) logger.info("There is a problem with alsaaudio, mute is not working!") self.mute = 'false' else: logger.info("Telegram: Muting is off") self.mute = "false" try: # Get Bot Token from settings.json self.UnitName = DeviceApi().get()['name'] MyCroftDevice1 = self.settings.get('MDevice1','') MyCroftDevice2 = self.settings.get('MDevice2','') except: pass try: self.bottoken = "" if MyCroftDevice1 == self.UnitName: logger.debug("Found MyCroft Unit 1: " + self.UnitName) self.bottoken = self.settings.get('TeleToken1', '') elif MyCroftDevice2 == self.UnitName: logger.debug("Found MyCroft Unit 2: " + self.UnitName) self.bottoken = self.settings.get('TeleToken2', '') else: msg = ("No or incorrect Device Name specified! Your DeviceName is: " + self.UnitName) logger.info(msg) self.sendMycroftSay(msg) except: pass try: self.user_id1 = self.settings.get('TeleID1', '') self.user_id2 = self.settings.get('TeleID2', '') self.chat_whitelist = [self.user_id1,self.user_id2] except: pass def TelegramMessages(self, update, context): msg = update.message.text chat_id_test = update.message.chat_id self.chat_id = str(update.message.chat_id) if self.chat_whitelist.count(chat_id_test) > 0 : global speak_tele speak_tele = 1 logger.info("Telegram-Message from User: "******"', '\\\"').replace('(', ' ').replace(')', ' ').replace('{', ' ').replace('}', ' ') msg = msg.casefold() # some skills need lowercase (eg. the cows list) self.add_event('recognizer_loop:audio_output_start', self.muteHandler) self.sendMycroftUtt(msg) else: logger.info("Chat ID " + self.chat_id + " is not whitelisted, i don't process it") nowhite = ("This is your ChatID: " + self.chat_id) context.bot.send_message(chat_id=self.chat_id, text=nowhite) def sendMycroftUtt(self, msg): self.bus.emit(Message('recognizer_loop:utterance',{"utterances": [msg],"lang": self.lang}))#, "session": session_id})) def sendMycroftSay(self, msg): self.bus.emit(Message('speak', {"utterance": msg,"lang": self.lang})) def responseHandler(self, message): global speak_tele if speak_tele == 1: speak_tele = 0 response = message.data.get("utterance") self.bus.emit(Message("telegram-skill:response", {"intent_name": "telegram-response", "utterance": response })) def sendHandler(self, message): sendData = message.data.get("utterance") logger.info("Sending to Telegram-User: " + sendData ) sendbot = telegram.Bot(token=self.bottoken) sendbot.send_message(chat_id=self.chat_id, text=sendData) def muteHandler(self, message): global speak_tele if (self.mute == 'true') or (self.mute == 'True'): self.mixer.setmute(1) wait_while_speaking() self.mixer.setmute(0) self.remove_event('recognizer_loop:audio_output_start') def shutdown(self): # shutdown routine if self.telegram_updater is not None: self.telegram_updater.stop() # will stop update and dispatcher self.telegram_updater.is_idle = False global speak_tele speak_tele = 0 super(TelegramSkill, self).shutdown() def stop(self): global speak_tele speak_tele = 0
class ALSA(IntervalModule): """ Shows volume of ALSA mixer. You can also use this for inputs, btw. Requires pyalsaaudio .. rubric:: Available formatters * `{volume}` — the current volume in percent * `{muted}` — the value of one of the `muted` or `unmuted` settings * `{card}` — the associated soundcard * `{mixer}` — the associated ALSA mixer """ interval = 1 settings = ( "format", ("format_muted", "optional format string to use when muted"), ("mixer", "ALSA mixer"), ("mixer_id", "ALSA mixer id"), ("card", "ALSA sound card"), ("increment", "integer percentage of max volume to in/decrement volume on mousewheel"), "muted", "unmuted", "color_muted", "color", "channel", ("map_volume", "volume display/setting as in AlsaMixer. increment option is ignored then.") ) muted = "M" unmuted = "" color_muted = "#AAAAAA" color = "#FFFFFF" format = "♪: {volume}" format_muted = None mixer = "Master" mixer_id = 0 card = 0 channel = 0 increment = 5 map_volume = False alsamixer = None has_mute = True on_upscroll = "increase_volume" on_downscroll = "decrease_volume" on_leftclick = "switch_mute" on_rightclick = on_leftclick def init(self): self.create_mixer() try: self.alsamixer.getmute() except ALSAAudioError: self.has_mute = False self.fdict = { "card": self.alsamixer.cardname(), "mixer": self.mixer, } self.dbRng = self.alsamixer.getrange() self.dbMin = self.dbRng[0] self.dbMax = self.dbRng[1] def create_mixer(self): self.alsamixer = Mixer( control=self.mixer, id=self.mixer_id, cardindex=self.card) def run(self): self.create_mixer() muted = False if self.has_mute: muted = self.alsamixer.getmute()[self.channel] == 1 self.fdict["volume"] = self.get_cur_volume() self.fdict["muted"] = self.muted if muted else self.unmuted self.fdict["db"] = self.get_db() if muted and self.format_muted is not None: output_format = self.format_muted else: output_format = self.format self.data = self.fdict self.output = { "full_text": output_format.format(**self.fdict), "color": self.color_muted if muted else self.color, } def switch_mute(self): if self.has_mute: muted = self.alsamixer.getmute()[self.channel] self.alsamixer.setmute(not muted) def get_cur_volume(self): if self.map_volume: dbCur = self.get_db() * 100.0 dbMin = self.dbMin * 100.0 dbMax = self.dbMax * 100.0 dbCur_norm = self.exp10((dbCur - dbMax) / 6000.0) dbMin_norm = self.exp10((dbMin - dbMax) / 6000.0) vol = (dbCur_norm - dbMin_norm) / (1 - dbMin_norm) vol = int(round(vol * 100, 0)) return vol else: return self.alsamixer.getvolume()[self.channel] def get_new_volume(self, direction): if direction == "inc": volume = (self.fdict["volume"] + 1) / 100 elif direction == "dec": volume = (self.fdict["volume"] - 1) / 100 dbMin = self.dbMin * 100 dbMax = self.dbMax * 100 dbMin_norm = self.exp10((dbMin - dbMax) / 6000.0) vol = volume * (1 - dbMin_norm) + dbMin_norm if direction == "inc": dbNew = min(self.dbMax, ceil(((6000.0 * log10(vol)) + dbMax) / 100)) elif direction == "dec": dbNew = max(self.dbMin, floor(((6000.0 * log10(vol)) + dbMax) / 100)) volNew = int(round(self.map_db(dbNew, self.dbMin, self.dbMax, 0, 100), 0)) return volNew def increase_volume(self, delta=None): if self.map_volume: vol = self.get_new_volume("inc") self.alsamixer.setvolume(vol) else: vol = self.alsamixer.getvolume()[self.channel] self.alsamixer.setvolume(min(100, vol + (delta if delta else self.increment))) def decrease_volume(self, delta=None): if self.map_volume: vol = self.get_new_volume("dec") self.alsamixer.setvolume(vol) else: vol = self.alsamixer.getvolume()[self.channel] self.alsamixer.setvolume(max(0, vol - (delta if delta else self.increment))) def get_db(self): db = (((self.dbMax - self.dbMin) / 100) * self.alsamixer.getvolume()[self.channel]) + self.dbMin db = int(round(db, 0)) return db def map_db(self, value, dbMin, dbMax, volMin, volMax): dbRange = dbMax - dbMin volRange = volMax - volMin volScaled = float(value - dbMin) / float(dbRange) return volMin + (volScaled * volRange) def exp10(self, x): return exp(x * log(10))
class TelegramSkill(MycroftSkill): def __init__(self): super(TelegramSkill, self).__init__(name="TelegramSkill") def initialize(self): self.mute = str(self.settings.get('MuteIt', '')) if (self.mute == 'True') or (self.mute == 'true'): try: self.mixer = Mixer() msg = "Telegram Messages will temporary Mute Mycroft" logger.info(msg) except: msg = "There is a problem with alsa audio, mute is not working!" logger.info( "There is a problem with alsaaudio, mute is not working!") self.sendMycroftSay(msg) self.mute = 'false' else: logger.info("Telegram: Muting is off") self.mute = "false" self.add_event('telegram-skill:response', self.sendHandler) self.add_event('speak', self.responseHandler) user_id1 = self.settings.get('TeleID1', '') user_id2 = self.settings.get('TeleID2', '') #user_id3 = self.settings.get('TeleID3', '') # makes web-settings too crouded #user_id4 = self.settings.get('TeleID4', '') # makes web-settings too crouded self.chat_whitelist = [ user_id1, user_id2 ] #,user_id3,user_id4] # makes web-settings too crouded # Get Bot Token from settings.json UnitName = DeviceApi().get()['name'] MyCroftDevice1 = self.settings.get('MDevice1', '') MyCroftDevice2 = self.settings.get('MDevice2', '') self.bottoken = "" if MyCroftDevice1 == UnitName: logger.debug("Found MyCroft Unit 1: " + UnitName) self.bottoken = self.settings.get('TeleToken1', '') elif MyCroftDevice2 == UnitName: logger.debug("Found MyCroft Unit 2: " + UnitName) self.bottoken = self.settings.get('TeleToken2', '') else: msg = ( "No or incorrect Device Name specified! Your DeviceName is: " + UnitName) logger.info(msg) self.sendMycroftSay(msg) # Connection to Telegram API self.telegram_updater = Updater( token=self.bottoken) # get telegram Updates self.telegram_dispatcher = self.telegram_updater.dispatcher receive_handler = MessageHandler( Filters.text, self.TelegramMessages ) # TODO: Make audio Files as Input possible: Filters.text | Filters.audio self.telegram_dispatcher.add_handler(receive_handler) self.telegram_updater.start_polling( clean=True) # start clean and look for messages wbot = telegram.Bot(token=self.bottoken) global loaded # get global variable if loaded == 0: # check if bot is just started loaded = 1 # make sure that users gets this message only once bot is newly loaded if self.mute == "false": msg = "Telegram Skill is loaded" self.sendMycroftSay(msg) loadedmessage = "Telegram-Skill on Mycroft Unit \"" + UnitName + "\" is loaded and ready to use!" # give User a nice message try: wbot.send_message( chat_id=user_id1, text=loadedmessage) # send welcome message to user 1 except: pass try: wbot.send_message( chat_id=user_id2, text=loadedmessage) # send welcome message to user 2 except: pass # wbot.send_message(chat_id=user_id1, text=loadedmessage) # send welcome message to user 3 # wbot.send_message(chat_id=user_id1, text=loadedmessage) # send welcome message to user 4 def TelegramMessages(self, bot, update): msg = update.message.text self.chat_id = str(update.message.chat_id) if self.chat_id in self.chat_whitelist: global speak_tele speak_tele = 1 logger.info("Telegram-Message from User: "******"', '\\\"').replace( '(', ' ').replace(')', ' ').replace('{', ' ').replace('}', ' ') msg = msg.casefold( ) # some skills need lowercase (eg. the cows list) self.add_event('recognizer_loop:audio_output_start', self.muteHandler) self.sendMycroftUtt(msg) else: logger.info("Chat ID " + self.chat_id + " is not whitelisted, i don't process it") nowhite = ("This is your ChatID: " + self.chat_id) bot.send_message(chat_id=self.chat_id, text=nowhite) def sendMycroftUtt(self, msg): uri = 'ws://localhost:8181/core' ws = create_connection(uri) utt = '{"context": null, "type": "recognizer_loop:utterance", "data": {"lang": "' + self.lang + '", "utterances": ["' + msg + '"]}}' ws.send(utt) ws.close() def sendMycroftSay(self, msg): uri = 'ws://localhost:8181/core' ws = create_connection(uri) msg = "say " + msg utt = '{"context": null, "type": "recognizer_loop:utterance", "data": {"lang": "' + self.lang + '", "utterances": ["' + msg + '"]}}' ws.send(utt) ws.close() def responseHandler(self, message): global speak_tele if speak_tele == 1: speak_tele = 0 response = message.data.get("utterance") self.bus.emit( Message("telegram-skill:response", { "intent_name": "telegram-response", "utterance": response })) def sendHandler(self, message): sendData = message.data.get("utterance") logger.info("Sending to Telegram-User: " + sendData) sendbot = telegram.Bot(token=self.bottoken) sendbot.send_message(chat_id=self.chat_id, text=sendData) def muteHandler(self, message): global speak_tele if (self.mute == 'true') or (self.mute == 'True'): self.mixer.setmute(1) wait_while_speaking() self.mixer.setmute(0) self.remove_event('recognizer_loop:audio_output_start') def shutdown(self): # shutdown routine self.telegram_updater.stop() # will stop update and dispatcher self.telegram_updater.is_idle = False global speak_tele speak_tele = 0 super(TelegramSkill, self).shutdown() def stop(self): global speak_tele speak_tele = 0
class ALSA(): def init(self): self.muted = "M" self.unmuted = "" self.color_muted = "#AAAAAA" self.color = "#FFFFFF" self.format = "♪: {volume}" self.format_muted = None self.mixer = "Master" self.mixer_id = 0 self.card = 0 self.channel = 0 self.increment = 5 self.map_volume = False self.alsamixer = None self.has_mute = True self.create_mixer() try: self.alsamixer.getmute() except ALSAAudioError: self.has_mute = False self.fdict = { "card": self.alsamixer.cardname(), "mixer": self.mixer, } self.dbRng = self.alsamixer.getrange() self.dbMin = self.dbRng[0] self.dbMax = self.dbRng[1] def create_mixer(self): self.alsamixer = Mixer( control=self.mixer, id=self.mixer_id, cardindex=self.card) def run(self): self.create_mixer() muted = False if self.has_mute: muted = self.alsamixer.getmute()[self.channel] == 1 self.fdict["volume"] = self.get_cur_volume() self.fdict["muted"] = self.muted if muted else self.unmuted self.fdict["db"] = self.get_db() if muted and self.format_muted is not None: output_format = self.format_muted else: output_format = self.format self.output = { "full_text": output_format.format(**self.fdict), "color": self.color_muted if muted else self.color, } def switch_mute(self): if self.has_mute: muted = self.alsamixer.getmute()[self.channel] self.alsamixer.setmute(not muted) def get_cur_volume(self): if self.map_volume: dbCur = self.get_db() * 100.0 dbMin = self.dbMin * 100.0 dbMax = self.dbMax * 100.0 dbCur_norm = self.exp10((dbCur - dbMax) / 6000.0) dbMin_norm = self.exp10((dbMin - dbMax) / 6000.0) vol = (dbCur_norm - dbMin_norm) / (1 - dbMin_norm) vol = int(round(vol * 100, 0)) return vol else: return self.alsamixer.getvolume()[self.channel] def get_new_volume(self, direction): if direction == "inc": volume = (self.fdict["volume"] + 1) / 100 elif direction == "dec": volume = (self.fdict["volume"] - 1) / 100 dbMin = self.dbMin * 100 dbMax = self.dbMax * 100 dbMin_norm = self.exp10((dbMin - dbMax) / 6000.0) vol = volume * (1 - dbMin_norm) + dbMin_norm if direction == "inc": dbNew = min(self.dbMax, ceil(((6000.0 * log10(vol)) + dbMax) / 100)) elif direction == "dec": dbNew = max(self.dbMin, floor(((6000.0 * log10(vol)) + dbMax) / 100)) volNew = int(round(self.map_db(dbNew, self.dbMin, self.dbMax, 0, 100), 0)) return volNew def increase_volume(self, delta=None): if self.map_volume: vol = self.get_new_volume("inc") self.alsamixer.setvolume(vol) else: vol = self.alsamixer.getvolume()[self.channel] self.alsamixer.setvolume(min(100, vol + (delta if delta else self.increment))) def decrease_volume(self, delta=None): if self.map_volume: vol = self.get_new_volume("dec") self.alsamixer.setvolume(vol) else: vol = self.alsamixer.getvolume()[self.channel] self.alsamixer.setvolume(max(0, vol - (delta if delta else self.increment))) def get_db(self): db = (((self.dbMax - self.dbMin) / 100) * self.alsamixer.getvolume()[self.channel]) + self.dbMin db = int(round(db, 0)) return db def map_db(self, value, dbMin, dbMax, volMin, volMax): dbRange = dbMax - dbMin volRange = volMax - volMin volScaled = float(value - dbMin) / float(dbRange) return volMin + (volScaled * volRange) def exp10(self, x): return exp(x * log(10))