def setup(): #setup pixels pixelBrightness = 0.05 pixels = neopixel.NeoPixel( board.NEOPIXEL, 10, brightness=pixelBrightness ) #determine beightness (Value can be between 0 and 1) #setup recording recordLength = 5 SAMPLERATE = 8000 NUM_SAMPLES = 160 ##setup speaker spkrenable = DigitialInOut(bouard.SPEAKER_ENABLE) spkrenable.direction = Direction.OUTPUT spkrenable.value = True #setup recording mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16) samples = array.array('H', [0] * NUM_SAMPLES) #debounce time debounceTime = 0.2 buttonD = DigitalInOut(board.BUTTON_A) #button a is the down button buttonD.direction = Direction.INPUT buttonD.pull = Pull.DOWN buttonU = DigitalInOut(board.BUTTON_B) # button b is the up button buttonU.direction = Direction.INPUT buttonU.pull = Pull.DOWN
def microphoneRead(): b = array.array("H") for i in range(200): b.append(0) with audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, bit_depth=16) as mic: mic.record(b, len(b)) time.sleep(2) print(normalized_rms(b))
def __init__(self, board, button, samples): super().__init__(button) self.samples = samples self.board = board self.mic = audiobusio.PDMIn( board.TX, board.D12, sample_rate=16000, bit_depth=16)
def __init__(self): # Define I2C: self._i2c = board.I2C() # Define touch: # Initially, self._touches stores the pin used for a particular touch. When that touch is # used for the first time, the pin is replaced with the corresponding TouchIn object. # This saves a little RAM over using a separate read-only pin tuple. # For example, after `clue.touch_2`, self._touches is equivalent to: # [board.D0, board.D1, touchio.TouchIn(board.D2)] self._touches = [board.D0, board.D1, board.D2] self._touch_threshold_adjustment = 0 # Define buttons: self._a = digitalio.DigitalInOut(board.BUTTON_A) self._a.switch_to_input(pull=digitalio.Pull.UP) self._b = digitalio.DigitalInOut(board.BUTTON_B) self._b.switch_to_input(pull=digitalio.Pull.UP) self._gamepad = gamepad.GamePad(self._a, self._b) # Define LEDs: self._white_leds = digitalio.DigitalInOut(board.WHITE_LEDS) self._white_leds.switch_to_output() self._pixel = neopixel.NeoPixel(board.NEOPIXEL, 1) self._red_led = digitalio.DigitalInOut(board.L) self._red_led.switch_to_output() # Define audio: self._mic = audiobusio.PDMIn( board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16, ) self._sample = None self._samples = None self._sine_wave = None self._sine_wave_sample = None # Define sensors: # Accelerometer/gyroscope: self._accelerometer = adafruit_lsm6ds.LSM6DS33(self._i2c) # Magnetometer: self._magnetometer = adafruit_lis3mdl.LIS3MDL(self._i2c) # DGesture/proximity/color/light sensor: self._sensor = adafruit_apds9960.apds9960.APDS9960(self._i2c) # Humidity sensor: self._humidity = adafruit_sht31d.SHT31D(self._i2c) # Barometric pressure sensor: self._pressure = adafruit_bmp280.Adafruit_BMP280_I2C(self._i2c) # Create displayio object for passing. self.display = board.DISPLAY
def is_hardware_PDM(clock, data): try: p = audiobusio.PDMIn(clock, data) p.deinit() return True except ValueError: return False except RuntimeError: return True
def __init__(self, sample_rate=16000, bit_depth=16): self.mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=sample_rate, bit_depth=bit_depth) # Record an initial sample to calibrate. Assume it's quiet when we start. self.mic.record(self.samples, len(self.samples)) # Set lowest level to expect, plus a little. self.min_magnitude = normalized_rms(self.samples) + 10 # Corresponds to sensitivity: lower means more pixels light up with lower sound # Adjust this as you see fit. self.max_magnitude = self.min_magnitude + 500
def __init__(self): # Define I2C: self._i2c = cutebot._i2c # Define buttons: self._a = digitalio.DigitalInOut(board.BUTTON_A) self._a.switch_to_input(pull=digitalio.Pull.UP) self._b = digitalio.DigitalInOut(board.BUTTON_B) self._b.switch_to_input(pull=digitalio.Pull.UP) self._gamepad = gamepad.GamePad(self._a, self._b) # Define LEDs: self._white_leds = digitalio.DigitalInOut(board.WHITE_LEDS) self._white_leds.switch_to_output() self._pixel = neopixel.NeoPixel(board.NEOPIXEL, 1) self._red_led = digitalio.DigitalInOut(board.L) self._red_led.switch_to_output() # Define audio: self._mic = audiobusio.PDMIn( board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16, ) self._sample = None self._samples = None self._sine_wave = None self._sine_wave_sample = None # Define sensors: # Accelerometer/gyroscope: self._accelerometer = adafruit_lsm6ds.lsm6ds33.LSM6DS33(self._i2c) # Magnetometer: self._magnetometer = adafruit_lis3mdl.LIS3MDL(self._i2c) # DGesture/proximity/color/light sensor: self._sensor = adafruit_apds9960.apds9960.APDS9960(self._i2c) # Humidity sensor: self._humidity = adafruit_sht31d.SHT31D(self._i2c) # Barometric pressure sensor: self._pressure = adafruit_bmp280.Adafruit_BMP280_I2C(self._i2c) # Create displayio object for passing. self.display = board.DISPLAY
def __init__(self, numberOfSamples=64): self._NUM_SAMPLES = numberOfSamples self._SCALE_EXPONENT = math.pow(10, 2 * -0.1) # curve is 2 self._mic = audiobusio.PDMIn( board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16) # Record an initial sample to calibrate. Assume it's quiet when we start. self._samples = array.array('H', [0] * self._NUM_SAMPLES) self._mic.record(self._samples, len(self._samples)) # Set lowest level to expect, plus a little. self._input_floor = self.normalized_rms(self._samples) + 10 self._input_ceiling = self._input_floor + 500 self._level = 0 self._magnitude = 0 self._enabled = False
def __init__(self): # Only create the cpb module member when we aren't being imported by Sphinx if ("__module__" in dir(digitalio.DigitalInOut) and digitalio.DigitalInOut.__module__ == "sphinx.ext.autodoc"): return super().__init__() self._sample = None # Define mic/sound sensor: self._mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16) self._samples = None
return sum(values) / len(values) def volume_color(i): return i * (255 // NUM_PIXELS), 50, 0 pixels = neopixel.NeoPixel(board.NEOPIXEL, NUM_PIXELS, brightness=0.1, auto_write=False) pixels.fill(0) pixels.show() mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16) samples = array.array('H', [0] * NUM_SAMPLES) mic.record(samples, len(samples)) input_floor = normalized_rms(samples) + 10 # Lower number means more sensitive - more LEDs will light up with less sound. sensitivity = 500 input_ceiling = input_floor + sensitivity peak = 0 tip = 0 decay = 0 while True: mic.record(samples, len(samples)) magnitude = normalized_rms(samples) print((magnitude), )
def volume_color(i): return (200, i * (255 // NUM_PIXELS), 0) # Main program. # Set up NeoPixels and turn them all off. pixels = neopixel.NeoPixel(board.NEOPIXEL, NUM_PIXELS, brightness=0.1, auto_write=False) pixels.fill(0) pixels.show() mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, frequency=16000, bit_depth=16) # Record an initial sample to calibrate. Assume it's quiet when we start. samples = array.array('H', [0] * NUM_SAMPLES) mic.record(samples, len(samples)) # Set lowest level to expect, plus a little. input_floor = normalized_rms(samples) + 10 # OR: used a fixed floor # input_floor = 50 # You might want to print the input_floor to help adjust other values. # print(input_floor) # Corresponds to sensitivity: lower means more pixels light up with lower sound # Adjust this as you see fit. input_ceiling = input_floor + 500
from adafruit_pybadger import PyBadger import array import math import time import audiobusio import displayio import board pybadger = PyBadger() pybadger.auto_dim_display(delay=30) first_display = True run_slow = False mic = audiobusio.PDMIn(board.TX, board.D12, sample_rate=16000, bit_depth=16) samples1 = array.array('H', [0] * 150) display = board.DISPLAY # To synchronize the display update to the waveform update, # turn off automatic display refresh. # Use explicit refresh function calls to update the display. # The display refresh feature requires CircuitPython v5.0.0 or later. board.DISPLAY.auto_refresh = False # Create a bitmap with two colors ncolors = 3 bitmap = displayio.Bitmap(display.width, display.height, ncolors)
def record(): """ Returns a buffer of recorded sound.""" buf = bytearray(8000) with audiobusio.PDMIn(MICROPHONE_CLOCK, MICROPHONE_DATA) as mic: mic.record(buf, len(buf)) return buf
import audiobusio import board import array # Prep a buffer to record into b = bytearray(200) with audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA) as mic: print(mic)
import array import math buf = bytearray(4000) print(3) time.sleep(1) print(2) time.sleep(1) print(1) time.sleep(1) print("recording", time.monotonic()) trigger = digitalio.DigitalInOut(board.A1) trigger.switch_to_output(value=True) with audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, frequency=16000) as mic: mic.record(buf, len(buf)) trigger.value = False print("done recording", time.monotonic()) for i in buf[:10]: print(i) print(min(buf), max(buf)) time.sleep(0.1) speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) speaker_enable.switch_to_output(value=True) trigger.value = True length = 16000 // 440
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)) # signed 16 bit s16 = array.array("H", [0] * 10000) pdm = audiobusio.PDMIn(clock_pin=board.D11, data_pin=board.D12, sample_rate=24000, bit_depth=16) print("starting read") trigger.value = False count = pdm.record(s16, len(s16)) trigger.value = True print("read done") print("recorded {} samples".format(count)) for v in s16[:count]: print(v) magnitude = normalized_rms(s16) print("magnitude", magnitude) print("count", count)
# record PDM mic to SPI memory import audiobusio import board # Prep a buffer to record into. The array interface doesn't allow for # constructing with a set size so we append to it until we have the size # we want. b = array.array("H") for i in range(200): b.append(0) with audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, bit_depth=16) as mic: mic.record(b, len(b))
but is extremely slow """ pixels = neopixel.NeoPixel(board.NEOPIXEL, c.NUM_PIXELS, brightness=1, auto_write=False) """ Initialise thermistor. """ thermistor = adafruit_thermistor.Thermistor(board.TEMPERATURE, c.RMEASURED, c.NOMINALRESISTOR, c.NOMINALTEMPERATURE, c.BETACOEF) """ Initialise light ADC. """ light = AnalogIn(board.LIGHT) """ Initialise sound measurements. """ mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=c.SAMPLE_RATE, bit_depth=c.BIT_DEPTH) # Record an initial sample to calibrate. Assume it's quiet when we start. # TODO: this is dangerous, get rid of it # an array of unsigned shorts ('H') samples = array.array('H', [0] * c.NUM_SAMPLES) mic.record(samples, len(samples)) # Set lowest level to expect, plus a little. # input_floor = normalised_rms(samples) + 10 # TODO: does this line need to be here? light_last = temp_last = sound_last = neopixel_last = time.monotonic() def mean(values):