use 16kHz sampling and average over 2 fft points for broader spectrum
This commit is contained in:
parent
ae3bfd264d
commit
3bc690d470
|
@ -11,8 +11,8 @@ from twinklclient import TwinklSocket, TwinklMessage
|
||||||
|
|
||||||
WIDTH = 6
|
WIDTH = 6
|
||||||
HEIGHT = 8
|
HEIGHT = 8
|
||||||
AUDIO_RATE = 4000 # sampling rate in Hz
|
AUDIO_RATE = 16000 # sampling rate in Hz
|
||||||
WINDOW_SIZE = 8 # average fft over WINDOW_SIZE audio frames
|
WINDOW_SIZE = 14 # average fft over WINDOW_SIZE audio frames
|
||||||
|
|
||||||
BOX_MAP = [
|
BOX_MAP = [
|
||||||
[357, 18, 369, 186, 249, 228, 51],
|
[357, 18, 369, 186, 249, 228, 51],
|
||||||
|
@ -156,18 +156,24 @@ class Fft_output:
|
||||||
self._out = twinkl_output
|
self._out = twinkl_output
|
||||||
|
|
||||||
self._count = 0
|
self._count = 0
|
||||||
self._data = []
|
self._data = [0] * width
|
||||||
for _ in range(width):
|
|
||||||
self._data.append(0)
|
|
||||||
|
|
||||||
|
|
||||||
def add(self, data):
|
def add(self, data):
|
||||||
"""add a set of fft data to the internal store, output the result if enough data is
|
"""add a set of fft data to the internal store, output the result if enough data is
|
||||||
available"""
|
available"""
|
||||||
abss = numpy.absolute(data[1:self._width+1])
|
|
||||||
|
binsize = 2
|
||||||
|
|
||||||
|
abss = numpy.absolute(data[0:binsize * self._width])
|
||||||
|
|
||||||
|
# sum fft values into bins
|
||||||
|
absum = [0] * self._width
|
||||||
|
for i in range(1, self._width * binsize):
|
||||||
|
absum[i / binsize] = absum[i / binsize] + abss[i]
|
||||||
|
|
||||||
for i in range(self._width):
|
for i in range(self._width):
|
||||||
self._data[i] = self._data[i] + abss[i]
|
self._data[i] = self._data[i] + absum[i]
|
||||||
|
|
||||||
self._count = self._count + 1
|
self._count = self._count + 1
|
||||||
if (self._count == self._windowsize):
|
if (self._count == self._windowsize):
|
||||||
|
@ -180,14 +186,13 @@ class Fft_output:
|
||||||
def output_twinkl(self):
|
def output_twinkl(self):
|
||||||
"""output graph to twinkl client"""
|
"""output graph to twinkl client"""
|
||||||
# correct for disproportionately large first and second column
|
# correct for disproportionately large first and second column
|
||||||
self._data[0] = self._data[0] / 2.5
|
self._data[0] = self._data[0] / 2
|
||||||
self._data[1] = self._data[1] / 1.5
|
self._data[1] = self._data[1] / 2.5
|
||||||
|
self._data[2] = self._data[2] / 1.5
|
||||||
abss = numpy.absolute(self._data)
|
|
||||||
|
|
||||||
self._background.clear()
|
self._background.clear()
|
||||||
for col in range(self._width):
|
for col in range(self._width):
|
||||||
normalized = min(int(abss[col] / (self._height * self._windowsize * 300)), self._height)
|
normalized = min(int(self._data[col] / (self._height * self._windowsize * 700)), self._height)
|
||||||
color = COLORS[normalized-1]
|
color = COLORS[normalized-1]
|
||||||
for row in range(self._height - normalized, self._height):
|
for row in range(self._height - normalized, self._height):
|
||||||
self._out.set_box(col, row, color[0], color[1], color[2])
|
self._out.set_box(col, row, color[0], color[1], color[2])
|
||||||
|
@ -200,6 +205,7 @@ def init_audio(rate):
|
||||||
ain = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NORMAL)
|
ain = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NORMAL)
|
||||||
ain.setformat(alsaaudio.PCM_FORMAT_S16_LE)
|
ain.setformat(alsaaudio.PCM_FORMAT_S16_LE)
|
||||||
ain.setrate(rate)
|
ain.setrate(rate)
|
||||||
|
ain.setperiodsize(64)
|
||||||
|
|
||||||
return ain
|
return ain
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue