Ejemplo n.º 1
0
    def __init__(self, music_file):
        log.debug('MusicPlayMode.__init__() START')
        Mode.__init__(self)
        # Make sure moc is running
        subprocess.call(["mocp", "-S"])
        self.music_file = music_file
        self.large_puffers = config['LargePuffers']['ids']
        self.small_puffers = config['PongGame']['puffers']
        self.all_puffers = copy.copy(self.large_puffers)
        self.all_puffers.extend(self.small_puffers)
        log.info('MusicPlayMode.__init__() all_puffers: %s' % str([('%x' % x) for x in self.all_puffers]))
        log.info('MusicPlayMode.__init__() large_puffers: %s' % str([('%x' % x) for x in self.large_puffers]))
        self.puff_duration = config['MusicMode']['puff_duration']
        self.puff_type = 'FP_EVENT_ALTPUFF' if config['PongGame']['use_alt_puff'] else 'FP_EVENT_PUFF'
        self.means = []
        self.meanlen = 10
        self.puff_frequency = None
        self.frequencylen = 30
        self.threshold = 2
        self.threshold_step = 0.004
        self.min_threshold = 0.5
        self.max_threshold = 10
        self.target_density = config['MusicMode']['target_density']
        self.channels = []
        self.min_wait = 0.5
        self.max_wait = 2.5
        self.chunk = (len(self.all_puffers)/2)*80 
        self.sample_rate = 44100
        self.start = time.time()
        self.inp = None
        self.out = None
        self.manual_mask = 0

        try:
            for _ in range(0, len(self.all_puffers)/2):
                self.means.append(RunningMean(self.meanlen))
                self.means[-1].set(0)
                self.channels.append(False)

            self.puff_frequency = RunningMean(self.frequencylen)

            self.inp = alsa.PCM(alsa.PCM_CAPTURE, alsa.PCM_NORMAL, 'hw:Loopback,1,0')
            self.out = alsa.PCM(alsa.PCM_PLAYBACK, alsa.PCM_NORMAL, 'plughw:0,0')

            self.inp.setchannels(2)
            self.inp.setrate(self.sample_rate)
            self.inp.setformat(alsa.PCM_FORMAT_S16_LE)
            self.inp.setperiodsize(self.chunk)

            self.out.setchannels(2)
            self.out.setrate(self.sample_rate)
            self.out.setformat(alsa.PCM_FORMAT_S16_LE)
            self.out.setperiodsize(self.chunk)
        except Exception as e:
            log.error('MusicPlayMode.__init__() %s: %s' % (type(e), e))
            if self.inp:
                self.inp.close()
            if self.out:
                self.out.close()
        log.debug('MusicPlayMode.__init__() END')
Ejemplo n.º 2
0
    def run(self):
        log.debug('MusicPlayMode.run() START')
        time.sleep(0.5)
        try:
            self.start_playback()
            while not self.terminate:
                l, data = self.inp.read()
                self.inp.pause(1)
                if l:
                    try:
                        matrix = self.calculate_levels(data)
                        puffer_state = (len(self.channels)*2) * ['    ']
                        puffcount = 0
                        puffmask = 0x00000000
                        for i in range(0, len(self.channels)):
                            diff = matrix[i] - self.means[i].mean()
                            if diff > 0:
                                self.means[i].set(matrix[i])
                                if diff > self.threshold:
                                    puffer_idx = i*2
                                    if self.channels[i]:
                                        puffer_idx += 1
                                    self.channels[i] = not(self.channels[i])
                                    puffer_state[puffer_idx] = 'PUFF'
                                    puffmask = puffmask | self.all_puffers[puffer_idx]
                                    puffcount += 1
                            else:
                                self.means[i].push(matrix[i])

                        self.puff_frequency.push(puffcount)
                        puff_density = self.puff_frequency.mean()
                        if self.target_density > puff_density and self.threshold > self.min_threshold:
                            self.threshold -= self.threshold_step
                        elif self.threshold < self.max_threshold:
                            self.threshold += self.threshold_step
                        log.debug('Music t=%5.2f d=%5.3f t=%5.3f' % (
                                time.time() - self.start,
                                puff_density, 
                                self.threshold))
                        if self.manual_mask > 0:
                            puffmask = puffmask | self.manual_mask
                            self.manual_mask = 0
                        if puffmask != 0:
                            e = FpEvent(puffmask, self.puff_type, pack('<H', self.puff_duration))
                            log.info('Event: %s' % str(e))
                            Visualizer().info(e)
                            FpSerial().write(e.serialize())
                        self.out.write(data)
                    except Exception as e:
                        log.exception("END: %s: %s" % (type(e), e))
                        break
                self.inp.pause(0)
            self.stop_playback()
        finally:
            try:
                self.inp.close()
                self.out.close()
            except Exception as e:
                log.error('while closing ALSA devices %s: %s' % (type(e), e))
        log.debug('MusicPlayMode.run() END')
Ejemplo n.º 3
0
 def discover(self):
     while self.wm is None:
         try:
             log.info('Put Wiimote for player %s in discovery mode' % self.name)
             ScoreBoard().display('D%s' % self.name[0])
             self.wm = cwiid.Wiimote()
             self.wm.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_ACC
             self.last_swipe = 0
             log.debug('Connected to wiimote for %s' % self.name)
         except RuntimeError as e:
             log.error('%s - run "sudo hciconfig hci0 up"?' % e)
         except Exception as e:
             log.error('While discovering WiiMote %s / %s' % (type(e), e))
             ScoreBoard().display('ER%s' % self.name)
             time.sleep(1.5)
Ejemplo n.º 4
0
        def __init__(self):
            log.debug('InputManager.__init__: START')
            self.thread = threading.Thread(target=self.run)
            self.terminate = False

            # Config wiimotes
            self.wm1 = None
            self.wm2 = None
            try:
                if config['InputManager']['wiimotes']['enabled']:
                    SwipeMote.IDLE = config['InputManager']['wiimotes'][
                        'swipe_idle']
                    SwipeMote.SWIPE_MIN = config['InputManager']['wiimotes'][
                        'swipe_min']
                    self.wm1 = SwipeMote('1UP', self.wiiswipe)
                    self.wm2 = SwipeMote('2UP', self.wiiswipe)
                    self.discover()
            except KeyError as e:
                log.exception('InputManager.__init__(): %s' % e)

            # Configure keyboard input
            self.keyboard = None
            try:
                if config['InputManager']['keyboard']['enabled']:
                    self.keyboard = Keyboard()
                    self.keyboard.thread.start()
            except Exception as e:
                log.exception('InputManager.__init__(): %s' % e)

            # Configure GPIO buttons
            self.gpiobuttons = None
            try:
                if config['InputManager']['gpio']['enabled']:
                    import fire_pong.gpiobuttons
                    from fire_pong.gpiobuttons import GpioButtons
                    fire_pong.gpiobuttons.log = log
                    self.gpiobuttons = GpioButtons(self.gpiocallback)
                    log.debug(
                        'InputManager.__init__: configured GpioButtons successfully'
                    )
            except Exception as e:
                log.error('InputManager.__init__: %s : %s' % (type(e), e))

            self.event_handler = None
Ejemplo n.º 5
0
        def __init__(self):
            log.debug('InputManager.__init__: START')
            self.thread = threading.Thread(target=self.run)
            self.terminate = False

            # Config wiimotes
            self.wm1 = None
            self.wm2 = None
            try:
                if config['InputManager']['wiimotes']['enabled']:
                    SwipeMote.IDLE = config['InputManager']['wiimotes']['swipe_idle']
                    SwipeMote.SWIPE_MIN = config['InputManager']['wiimotes']['swipe_min']
                    self.wm1 = SwipeMote('1UP', self.wiiswipe)
                    self.wm2 = SwipeMote('2UP', self.wiiswipe)
                    self.discover()
            except KeyError as e:
                log.exception('InputManager.__init__(): %s' % e)

            # Configure keyboard input
            self.keyboard = None
            try:
                if config['InputManager']['keyboard']['enabled']:
                    self.keyboard = Keyboard()
                    self.keyboard.thread.start()
            except Exception as e:
                log.exception('InputManager.__init__(): %s' % e)

            # Configure GPIO buttons
            self.gpiobuttons = None
            try:
                if config['InputManager']['gpio']['enabled']:
                    import fire_pong.gpiobuttons
                    from fire_pong.gpiobuttons import GpioButtons
                    fire_pong.gpiobuttons.log = log
                    self.gpiobuttons = GpioButtons(self.gpiocallback)
                    log.debug('InputManager.__init__: configured GpioButtons successfully')
            except Exception as e:
                log.error('InputManager.__init__: %s : %s' % (type(e), e))
                
            self.event_handler = None
Ejemplo n.º 6
0
 def rumble(self, state):
     try:
         self.wm.rumble = state
     except Exception as e:
         log.error('SwipeMote(%s).rumble(%s) %s', (self.name, self.state, e))
Ejemplo n.º 7
0
    def __init__(self, music_file):
        log.debug('MusicPlayMode.__init__() START')
        Mode.__init__(self)
        # Make sure moc is running
        subprocess.call(["mocp", "-S"])
        self.music_file = music_file
        self.large_puffers = config['LargePuffers']['ids']
        self.small_puffers = config['PongGame']['puffers']
        self.all_puffers = copy.copy(self.large_puffers)
        self.all_puffers.extend(self.small_puffers)
        log.info('MusicPlayMode.__init__() all_puffers: %s' %
                 str([('%x' % x) for x in self.all_puffers]))
        log.info('MusicPlayMode.__init__() large_puffers: %s' %
                 str([('%x' % x) for x in self.large_puffers]))
        self.puff_duration = config['MusicMode']['puff_duration']
        self.puff_type = 'FP_EVENT_ALTPUFF' if config['PongGame'][
            'use_alt_puff'] else 'FP_EVENT_PUFF'
        self.means = []
        self.meanlen = 10
        self.puff_frequency = None
        self.frequencylen = 30
        self.threshold = 2
        self.threshold_step = 0.004
        self.min_threshold = 0.5
        self.max_threshold = 10
        self.target_density = config['MusicMode']['target_density']
        self.channels = []
        self.min_wait = 0.5
        self.max_wait = 2.5
        self.chunk = (len(self.all_puffers) / 2) * 80
        self.sample_rate = 44100
        self.start = time.time()
        self.inp = None
        self.out = None
        self.manual_mask = 0

        try:
            for _ in range(0, len(self.all_puffers) / 2):
                self.means.append(RunningMean(self.meanlen))
                self.means[-1].set(0)
                self.channels.append(False)

            self.puff_frequency = RunningMean(self.frequencylen)

            self.inp = alsa.PCM(alsa.PCM_CAPTURE, alsa.PCM_NORMAL,
                                'hw:Loopback,1,0')
            self.out = alsa.PCM(alsa.PCM_PLAYBACK, alsa.PCM_NORMAL,
                                'plughw:0,0')

            self.inp.setchannels(2)
            self.inp.setrate(self.sample_rate)
            self.inp.setformat(alsa.PCM_FORMAT_S16_LE)
            self.inp.setperiodsize(self.chunk)

            self.out.setchannels(2)
            self.out.setrate(self.sample_rate)
            self.out.setformat(alsa.PCM_FORMAT_S16_LE)
            self.out.setperiodsize(self.chunk)
        except Exception as e:
            log.error('MusicPlayMode.__init__() %s: %s' % (type(e), e))
            if self.inp:
                self.inp.close()
            if self.out:
                self.out.close()
        log.debug('MusicPlayMode.__init__() END')
Ejemplo n.º 8
0
    def run(self):
        log.debug('MusicPlayMode.run() START')
        time.sleep(0.5)
        try:
            self.start_playback()
            while not self.terminate:
                l, data = self.inp.read()
                self.inp.pause(1)
                if l:
                    try:
                        matrix = self.calculate_levels(data)
                        puffer_state = (len(self.channels) * 2) * ['    ']
                        puffcount = 0
                        puffmask = 0x00000000
                        for i in range(0, len(self.channels)):
                            diff = matrix[i] - self.means[i].mean()
                            if diff > 0:
                                self.means[i].set(matrix[i])
                                if diff > self.threshold:
                                    puffer_idx = i * 2
                                    if self.channels[i]:
                                        puffer_idx += 1
                                    self.channels[i] = not (self.channels[i])
                                    puffer_state[puffer_idx] = 'PUFF'
                                    puffmask = puffmask | self.all_puffers[
                                        puffer_idx]
                                    puffcount += 1
                            else:
                                self.means[i].push(matrix[i])

                        self.puff_frequency.push(puffcount)
                        puff_density = self.puff_frequency.mean()
                        if self.target_density > puff_density and self.threshold > self.min_threshold:
                            self.threshold -= self.threshold_step
                        elif self.threshold < self.max_threshold:
                            self.threshold += self.threshold_step
                        log.debug('Music t=%5.2f d=%5.3f t=%5.3f' %
                                  (time.time() - self.start, puff_density,
                                   self.threshold))
                        if self.manual_mask > 0:
                            puffmask = puffmask | self.manual_mask
                            self.manual_mask = 0
                        if puffmask != 0:
                            e = FpEvent(puffmask, self.puff_type,
                                        pack('<H', self.puff_duration))
                            log.info('Event: %s' % str(e))
                            Visualizer().info(e)
                            FpSerial().write(e.serialize())
                        self.out.write(data)
                    except Exception as e:
                        log.exception("END: %s: %s" % (type(e), e))
                        break
                self.inp.pause(0)
            self.stop_playback()
        finally:
            try:
                self.inp.close()
                self.out.close()
            except Exception as e:
                log.error('while closing ALSA devices %s: %s' % (type(e), e))
        log.debug('MusicPlayMode.run() END')