text: # SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
  #
  # SPDX-License-Identifier: MIT
  
  import time
  import array
  import math
  import board
  import audiobusio
  
  
  # Remove DC bias before computing RMS.
  def mean(values):
      return sum(values) / len(values)
  
  
  def normalized_rms(values):
      minbuf = int(mean(values))
      samples_sum = sum(
          float(sample - minbuf) * (sample - minbuf)
          for sample in values
      )
  
      return math.sqrt(samples_sum / len(values))
  
  
  # Main program
  mic = audiobusio.PDMIn(board.TX, board.D12, sample_rate=16000, bit_depth=16)
  samples = array.array('H', [0] * 160)
  
  
  while True:
      mic.record(samples, len(samples))
      magnitude = normalized_rms(samples)
      print((magnitude,))
      time.sleep(0.1)
  