Advertisement
Guest User

CircuitPython

a guest
May 24th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.12 KB | None | 0 0
  1. #code was taken from Core Electronics Sound Reactive Lights With CircuitPython: Adafruit Circuit Playground Express
  2. #https://core-electronics.com.au/tutorials/sound-reactive-lights-circuitpython-circuit-playground-express-tutorial.html
  3. #Code is being used under MIT license:
  4. # The MIT License (MIT)0
  5. #
  6. # Copyright (c) 2017 Dan Halbert for Adafruit Industries
  7. # Copyright (c) 2017 Kattni Rembor, Tony DiCola for Adafruit Industries
  8. #
  9. # Permission is hereby granted, free of charge, to any person obtaining a copy
  10. # of this software and associated documentation files (the "Software"), to deal
  11. # in the Software without restriction, including without limitation the rights
  12. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. # copies of the Software, and to permit persons to whom the Software is
  14. # furnished to do so, subject to the following conditions:
  15. #
  16. # The above copyright notice and this permission notice shall be included in
  17. # all copies or substantial portions of the Software.
  18. #
  19. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. # THE SOFTWARE.
  26. #
  27. #Further modifications of code from the original level were done by Grant Miller and Chase Dillard, 2019
  28.  
  29. import array
  30. import math
  31. import time
  32. import microcontroller
  33. import audiobusio
  34. import board
  35. from adafruit_circuitplayground.express import cpx
  36.  
  37. # Exponential scaling factor.
  38. # Should probably be in range -10 .. 10 to be reasonable.
  39. CURVE = 2
  40. SCALE_EXPONENT = math.pow(10, CURVE * -0.1)
  41.  
  42. PEAK_COLOR = (100, 0, 255)
  43. NUM_PIXELS = 10
  44.  
  45. # Number of samples to read at once.
  46. NUM_SAMPLES = 160
  47.  
  48. cpx.pixels.auto_write = False
  49.  
  50.  
  51. # Restrict value to be between floor and ceiling.
  52.  
  53.  
  54. def constrain(value, floor, ceiling):
  55.     return max(floor, min(value, ceiling))
  56.  
  57.  
  58. # Scale input_value between output_min and output_max, exponentially.
  59.  
  60.  
  61. def log_scale(input_value, input_min, input_max, output_min, output_max):
  62.     normalized_input_value = (input_value - input_min) / \
  63.                              (input_max - input_min)
  64.     return output_min + \
  65.         math.pow(normalized_input_value, SCALE_EXPONENT) \
  66.         * (output_max - output_min)
  67.  
  68.  
  69. # Remove DC bias before computing RMS.
  70.  
  71.  
  72. def normalized_rms(values):
  73.     minbuf = int(mean(values))
  74.     samples_sum = sum(
  75.         float(sample - minbuf) * (sample - minbuf)
  76.         for sample in values
  77.     )
  78.  
  79.     return math.sqrt(samples_sum / len(values))
  80.  
  81.  
  82. def mean(values):
  83.     return sum(values) / len(values)
  84.  
  85.  
  86. def volume_color(volume):
  87.     return 200, volume * (255 // NUM_PIXELS), 0
  88.  
  89.  
  90. # Main program
  91.  
  92.  
  93. # Set up NeoPixels and turn them all off.
  94. cpx.pixels.brightness=0.1
  95. cpx.pixels.fill(0)
  96. cpx.pixels.show()
  97.  
  98. """
  99. # For CircuitPython 2.x:
  100. mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
  101.                      frequency=16000, bit_depth=16)
  102. # For Circuitpython 3.0 and up, "frequency" is now called "sample_rate".
  103. # Comment the lines above and uncomment the lines below.
  104. """
  105. mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
  106.                        sample_rate=16000, bit_depth=16)
  107.  
  108. # Record an initial sample to calibrate. Assume it's quiet when we start.
  109. samples = array.array('H', [0] * NUM_SAMPLES)
  110. mic.record(samples, len(samples))
  111. # Set lowest level to expect, plus a little.
  112. input_floor = normalized_rms(samples) + 10
  113. # OR: used a fixed floor
  114. # input_floor = 50
  115.  
  116. # You might want to print the input_floor to help adjust other values.
  117. # print(input_floor)
  118.  
  119. # Corresponds to sensitivity: lower means more pixels light up with lower sound
  120. # Adjust this as you see fit.
  121. input_ceiling = input_floor + 500
  122.  
  123. peak = 0
  124. while True:
  125.     # TO CHANGE
  126.     mic.record(samples, len(samples))
  127.     # END OF TO CHANGE
  128.    
  129.     magnitude = normalized_rms(samples)
  130.     # You might want to print this to see the values.
  131.     # print(magnitude)
  132.  
  133.     # Compute scaled logarithmic reading in the range 0 to NUM_PIXELS
  134.     c = log_scale(constrain(magnitude, input_floor, input_ceiling),
  135.                   input_floor, input_ceiling, 0, NUM_PIXELS)
  136.  
  137.     # Light up pixels that are below the scaled and interpolated magnitude.
  138.     cpx.pixels.fill(0)
  139.     for i in range(NUM_PIXELS):
  140.         if i < c:
  141.             cpx.pixels[i] = volume_color(i)
  142.         # Light up the peak pixel and animate it slowly dropping.
  143.         if c >= peak:
  144.             peak = min(c, NUM_PIXELS - 1)
  145.         elif peak > 0:
  146.             peak = peak - 1
  147.         if peak > 0:
  148.             cpx.pixels[int(peak)] = PEAK_COLOR
  149.     cpx.pixels.show()
  150.  
  151.     #audio portion
  152.  
  153.     cpx.red_led = cpx.switch
  154.  
  155.     if cpx.button_a:
  156.         cpx.play_file("batman_theme_x.wav")
  157.     elif cpx.button_b:
  158.         cpx.play_file("feel_good_x.wav")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement