Beispiel #1
0
    def spectrum(self):
        if self._currentSpectrum is None:
            rawData = ''.join(self._dataSinceLastSpectrum)

            numWindows = int(math.floor(len(rawData) / self.bytesPerFrame / self.framesPerWindow))
            if numWindows == 0:
                if self.debug:
                    ansi.warn("Need {} frames for a window! (only have {})",
                            self.framesPerWindow, len(rawData) / self.bytesPerFrame)
                return

            if len(rawData) % (self.framesPerWindow * self.bytesPerFrame) != 0:
                self._dataSinceLastSpectrum = [rawData[numWindows * self.framesPerWindow * self.bytesPerFrame:]]
                rawData = rawData[:numWindows * self.framesPerWindow * self.bytesPerFrame]
            else:
                self._dataSinceLastSpectrum = []

            dataArr = fromstring(rawData, dtype=short)

            # Reshape the array so we can chop it to the correct number of frames.
            dataArr = dataArr.reshape((-1, self.channels))

            if not self.onSpectrum:
                # No per-spectrum listeners, so ditch all but the most recent spectrum window.
                dataArr = dataArr[-self.framesPerWindow:]
                numWindows = 1

            for windowNum in range(numWindows):
                fftOut = self.calcWindow(dataArr.T, windowNum)

                mainLoop.currentProcess.queueCall(self.onSpectrum, fftOut)

            self._currentSpectrum = fftOut

        return self._currentSpectrum
Beispiel #2
0
 def unhandledEvent(self, event):
     ansi.warn("Unhandled event! {!r}", event)