コード例 #1
0
 def playMp3(self, filename):
     global t
     global mp3state
     spotify.pause()
     if previous_tag == current_tag and 'PAUSED' in ("%s" % mp3state):
         # Resume
         logger.info("Resuming mp3 track.")
         self.p.play()
         remaining = mp3_duration - mp3elapsed
         if remaining >= 0.1:
             self.startLightshow(remaining * 1000)
             return
     # New play
     self.stopMp3()
     self.startMp3(filename)
     mp3state = 'PLAYING'
コード例 #2
0
ファイル: lego.py プロジェクト: miky2184/musicfig
    def playPlaylist(self, playlist_filename, mp3_dir, shuffle=False):
        global mp3state
        list_mp3_to_play = []
        spotify.pause()

        mp3list = mp3_dir +'/'+ playlist_filename + '/*.mp3'
        ##logger.debug(mp3list)

        list_mp3_to_play = glob.glob(mp3list)

        if shuffle:
            random.shuffle(list_mp3_to_play)
        ##logger.debug(list_mp3_to_play)

        self.startMp3(list_mp3_to_play, mp3_dir, True)
        mp3state = 'PLAYING'
コード例 #3
0
    def startLego(self):
        global current_tag
        global previous_tag
        global mp3state
        global p
        global switch_lights
        current_tag = None
        previous_tag = None
        mp3state = None
        nfc = nfctags.Tags()
        nfc.load_tags()
        tags = nfc.tags
        self.base = Dimensions()
        logger.info("Lego Dimensions base activated.")
        self.initMp3()
        try:
            switch_lights = tags['lights']
        except Exception:
            switch_lights = True
        logger.info('Lightshow is %s' %
                    switch_lights)  #("disabled", "enabled")[switch_lights])
        if switch_lights:
            self.base.switch_pad(0, self.GREEN)
        else:
            self.base.switch_pad(0, self.OFF)
        while True:
            tag = self.base.update_nfc()
            if tag:
                status = tag.split(':')[0]
                pad = int(tag.split(':')[1])
                identifier = tag.split(':')[2]
                if status == 'removed':
                    if identifier == current_tag:
                        try:
                            self.lightshowThread.do_run = False
                            self.lightshowThread.join()
                        except Exception:
                            pass
                        self.pauseMp3()
                        if spotify.activated():
                            spotify.pause()
                if status == 'added':
                    if switch_lights:
                        self.base.switch_pad(pad=pad, colour=self.BLUE)

                    # Reload the tags config file
                    nfc.load_tags()
                    tags = nfc.tags

                    # Stop any current songs and light shows
                    try:
                        self.lightshowThread.do_run = False
                        self.lightshowThread.join()
                    except Exception:
                        pass

                    if (identifier in tags['identifier']):
                        if current_tag == None:
                            previous_tag = identifier
                        else:
                            previous_tag = current_tag
                        current_tag = identifier
                        # A tag has been matched
                        if ('mp3' in tags['identifier'][identifier]):
                            filename = tags['identifier'][identifier]['mp3']
                            self.playMp3(filename)
                        if ('slack' in tags['identifier'][identifier]):
                            webhook.Requests.post(tags['slack_hook'], {
                                'text':
                                tags['identifier'][identifier]['slack']
                            })
                        if ('command' in tags['identifier'][identifier]):
                            command = tags['identifier'][identifier]['command']
                            logger.info('Running command %s' % command)
                            os.system(command)
                        if ('spotify' in tags['identifier'][identifier]
                            ) and spotify.activated():
                            if current_tag == previous_tag:
                                self.startLightshow(spotify.resume())
                                continue
                            try:
                                position_ms = int(tags['identifier']
                                                  [identifier]['position_ms'])
                            except Exception:
                                position_ms = 0
                            self.stopMp3()
                            duration_ms = spotify.spotcast(
                                tags['identifier'][identifier]['spotify'],
                                position_ms)
                            if duration_ms > 0:
                                self.startLightshow(duration_ms)
                            else:
                                self.base.flash_pad(pad=pad,
                                                    on_length=10,
                                                    off_length=10,
                                                    pulse_count=6,
                                                    colour=self.RED)
                        if ('spotify' in tags['identifier'][identifier]
                            ) and not spotify.activated():
                            current_tag = previous_tag
                    else:
                        # Unknown tag. Display UID.
                        logger.info('Discovered new tag: %s' % identifier)
                        self.base.switch_pad(pad, self.RED)
コード例 #4
0
ファイル: lego.py プロジェクト: zork32/jukebox-portal
    def startLego(self):
        global current_tag
        global previous_tag
        global mp3state
        global p
        current_tag = None
        previous_tag = None
        mp3state = None
        nfc = nfctags.Tags()
        nfc.load_tags()
        self.base = Dimensions()
        logger.info("Lego Dimensions base activated.")
        self.initMp3()
        self.base.switch_pad(0, self.GREEN)
        while True:
            tag = self.base.update_nfc()
            if tag:
                status = tag.split(':')[0]
                pad = int(tag.split(':')[1])
                identifier = tag.split(':')[2]
                if status == 'removed':
                    if identifier == current_tag:
                        try:
                            self.lightshowThread.do_run = False
                            self.lightshowThread.join()
                        except Exception:
                            pass
                        self.pauseMp3()
                        if spotify.activated():
                            spotify.pause()
                if status == 'added':
                    self.base.switch_pad(pad=pad, colour=self.BLUE)

                    # Reload the tags config file
                    nfc.load_tags()
                    tags = nfc.tags

                    # Stop any current songs and light shows
                    try:
                        self.lightshowThread.do_run = False
                        self.lightshowThread.join()
                    except Exception:
                        pass

                    if (identifier in tags['identifier']):
                        if current_tag == None:
                            previous_tag = identifier
                        else:
                            previous_tag = current_tag
                        current_tag = identifier
                        # A tag has been matched
                        if ('mp3' in tags['identifier'][identifier]):
                            filename = tags['identifier'][identifier]['mp3']
                            self.playMp3(filename)
                        if ('slack' in tags['identifier'][identifier]):
                            webhook.Requests.post(tags['slack_hook'], {
                                'text':
                                tags['identifier'][identifier]['slack']
                            })
                        if ('spotify' in tags['identifier'][identifier]
                            ) and spotify.activated():
                            if current_tag == previous_tag:
                                self.startLightshow(spotify.resume())
                                continue
                            try:
                                position_ms = int(tags['identifier']
                                                  [identifier]['position_ms'])
                            except Exception:
                                position_ms = 0
                            self.stopMp3()
                            duration_ms = spotify.spotcast(
                                tags['identifier'][identifier]['spotify'],
                                position_ms)
                            if duration_ms > 0:
                                self.startLightshow(duration_ms)
                            else:
                                self.base.flash_pad(pad=pad,
                                                    on_length=10,
                                                    off_length=10,
                                                    pulse_count=6,
                                                    colour=self.RED)
                        if ('spotify' in tags['identifier'][identifier]
                            ) and not spotify.activated():
                            current_tag = previous_tag
                        if ('shutdown' in tags['identifier'][identifier]):
                            volume = 92
                            command = [
                                "amixer", "sset", "Headphone",
                                "{}%".format(volume)
                            ]
                            subprocess.Popen(command)
                            call("sudo nohup shutdown -h now", shell=True)
                        if ('volumeup' in tags['identifier'][identifier]):
                            volume = 97
                            command = [
                                "amixer", "sset", "Headphone",
                                "{}%".format(volume)
                            ]
                            subprocess.Popen(command)
                        if ('volumedown' in tags['identifier'][identifier]):
                            volume = 88
                            command = [
                                "amixer", "sset", "Headphone",
                                "{}%".format(volume)
                            ]
                            subprocess.Popen(command)
                        if ('volumedefault' in tags['identifier'][identifier]):
                            volume = 92
                            command = [
                                "amixer", "sset", "Headphone",
                                "{}%".format(volume)
                            ]
                            subprocess.Popen(command)
                        if ('discoon' in tags['identifier'][identifier]):
                            self.OFFCOLOURS = [
                                'self.RED', 'self.GREEN', 'self.BLUE',
                                'self.PINK', 'self.ORANGE', 'self.PURPLE',
                                'self.LBLUE', 'self.OLIVE'
                            ]
                        if ('discooff' in tags['identifier'][identifier]):
                            self.OFFCOLOURS = [
                                'self.OFF', 'self.OFF', 'self.OFF', 'self.OFF',
                                'self.OFF', 'self.OFF', 'self.OFF', 'self.OFF'
                            ]
                    else:
                        # Unknown tag. Display UID.
                        logger.info('Discovered new tag: %s' % identifier)
                        self.base.switch_pad(pad, self.RED)