Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

equalizer.py -s: Symmetric mode (centered)

  • Loading branch information...
commit b3bdb9df2e50d3bcd06f0b91962e1067a34b6002 1 parent 5daf750
Petr Baudis authored August 20, 2012

Showing 1 changed file with 20 additions and 7 deletions. Show diff stats Hide diff stats

  1. 27  host_python/equalizer.py
27  host_python/equalizer.py
@@ -18,8 +18,9 @@
18 18
 RATE = 44100
19 19
 
20 20
 PIXELS = 20
21  
-
22 21
 LAZY = 0
  22
+SYMMETRIC = 0
  23
+
23 24
 HISTORY_SIZE = 4
24 25
 MIN_FREQ = 50
25 26
 MAX_FREQ = 12000
@@ -28,15 +29,16 @@
28 29
 def print_usage():
29 30
     print '''\
30 31
 USAGE:
31  
-    %s [-l] [-n number] [-h]
  32
+    %s [-l] [-n number] [-s] [-h]
32 33
 OPTIONS:
33 34
     -l              lazy mode
34 35
     -n number       number of controlled boxes
  36
+    -s              symmetric mode
35 37
     -h --help       show this help
36 38
 ''' % sys.argv[0]
37 39
 
38 40
 try:
39  
-    opts, args = getopt.getopt(sys.argv[1:], 'n:lh', ['help'])
  41
+    opts, args = getopt.getopt(sys.argv[1:], 'n:lsh', ['help'])
40 42
 except getopt.GetOptError:
41 43
     print_usage()
42 44
     sys.exit(1)
@@ -51,16 +53,23 @@ def print_usage():
51 53
         PIXELS = int(v)
52 54
     elif k == '-l':
53 55
         LAZY = 1
  56
+    elif k == '-s':
  57
+        SYMMETRIC = 1
54 58
     elif k == '-h' or k == '--help':
55 59
         print_usage()
56 60
         sys.exit(0)
57 61
 
58 62
 if LAZY == 1:
59 63
     HISTORY_SIZE = 12
  64
+if SYMMETRIC == 1:
  65
+    EPIXELS = PIXELS / 2
  66
+else:
  67
+    EPIXELS = PIXELS
  68
+# EPIXELS: Effective pixels (for spectrum display)
60 69
 
61 70
 SAMPLE_SIZE = CHUNK_SIZE*HISTORY_SIZE
62 71
 FREQ_STEP = float(RATE) / (CHUNK_SIZE * HISTORY_SIZE)
63  
-PIXEL_FREQ_RANGE = math.pow(float(MAX_FREQ) / MIN_FREQ, 1.0/PIXELS)
  72
+PIXEL_FREQ_RANGE = math.pow(float(MAX_FREQ) / MIN_FREQ, 1.0/EPIXELS)
64 73
 
65 74
 
66 75
 p = pyaudio.PyAudio()
@@ -118,7 +127,7 @@ def get_color(volume):
118 127
         pixel = 0
119 128
         count = 0
120 129
         volumes = []
121  
-        while pixel < PIXELS:
  130
+        while pixel < EPIXELS:
122 131
             total = 0.0
123 132
             while freq < freq_limit:
124 133
                 total += abs(fft[i])**2
@@ -129,9 +138,13 @@ def get_color(volume):
129 138
             freq_limit *= PIXEL_FREQ_RANGE
130 139
             pixel += 1
131 140
             count = 0
132  
-        for pixel in xrange(PIXELS):
  141
+        for pixel in xrange(EPIXELS):
133 142
             c = get_color(volumes[pixel])
134  
-            l.set_pixel(pixel, c[0],  c[1], c[2])
  143
+            if SYMMETRIC == 1:
  144
+                l.set_pixel(PIXELS / 2 + pixel, c[0],  c[1], c[2])
  145
+                l.set_pixel(PIXELS / 2 - (pixel + 1), c[0],  c[1], c[2])
  146
+            else:
  147
+                l.set_pixel(pixel, c[0],  c[1], c[2])
135 148
         work = l.update()
136 149
         # time.sleep(0.05)
137 150
 finally:

0 notes on commit b3bdb9d

Please sign in to comment.
Something went wrong with that request. Please try again.