Skip to content

Commit ea4c43e

Browse files
authored
Merge pull request #832 from kattni/cpb-cpx-sound-meter-update
Update sound meter code.
2 parents e65921a + 2227084 commit ea4c43e

1 file changed

Lines changed: 9 additions & 19 deletions

File tree

Introducing_CircuitPlaygroundExpress/CircuitPlaygroundExpress_SoundMeter.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,34 @@
2121
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
# THE SOFTWARE.
2323

24+
# Circuit Playground Sound Meter
25+
2426
import array
2527
import math
26-
2728
import audiobusio
2829
import board
2930
import neopixel
3031

32+
# Color of the peak pixel.
33+
PEAK_COLOR = (100, 0, 255)
34+
# Number of total pixels - 10 build into Circuit Playground
35+
NUM_PIXELS = 10
36+
3137
# Exponential scaling factor.
3238
# Should probably be in range -10 .. 10 to be reasonable.
3339
CURVE = 2
3440
SCALE_EXPONENT = math.pow(10, CURVE * -0.1)
3541

36-
PEAK_COLOR = (100, 0, 255)
37-
NUM_PIXELS = 10
38-
3942
# Number of samples to read at once.
4043
NUM_SAMPLES = 160
4144

4245

4346
# Restrict value to be between floor and ceiling.
44-
45-
4647
def constrain(value, floor, ceiling):
4748
return max(floor, min(value, ceiling))
4849

4950

5051
# Scale input_value between output_min and output_max, exponentially.
51-
52-
5352
def log_scale(input_value, input_min, input_max, output_min, output_max):
5453
normalized_input_value = (input_value - input_min) / \
5554
(input_max - input_min)
@@ -59,8 +58,6 @@ def log_scale(input_value, input_min, input_max, output_min, output_max):
5958

6059

6160
# Remove DC bias before computing RMS.
62-
63-
6461
def normalized_rms(values):
6562
minbuf = int(mean(values))
6663
samples_sum = sum(
@@ -81,20 +78,13 @@ def volume_color(volume):
8178

8279
# Main program
8380

84-
8581
# Set up NeoPixels and turn them all off.
86-
pixels = neopixel.NeoPixel(board.NEOPIXEL, NUM_PIXELS,
87-
brightness=0.1, auto_write=False)
82+
pixels = neopixel.NeoPixel(board.NEOPIXEL, NUM_PIXELS, brightness=0.1, auto_write=False)
8883
pixels.fill(0)
8984
pixels.show()
9085

91-
# For CircuitPython 2.x:
9286
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
93-
frequency=16000, bit_depth=16)
94-
# For Circuitpython 3.0 and up, "frequency" is now called "sample_rate".
95-
# Comment the lines above and uncomment the lines below.
96-
#mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
97-
# sample_rate=16000, bit_depth=16)
87+
sample_rate=16000, bit_depth=16)
9888

9989
# Record an initial sample to calibrate. Assume it's quiet when we start.
10090
samples = array.array('H', [0] * NUM_SAMPLES)

0 commit comments

Comments
 (0)