Last active
February 11, 2018 10:08
-
-
Save diessica/f1c683aa64b98aedb5e59a703ec35268 to your computer and use it in GitHub Desktop.
intensity.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyaudio | |
import numpy | |
import array | |
import serial | |
import colorsys | |
import time | |
from color import convertPercentToColorValue | |
PORT = '/dev/cu.wchusbserial1430' | |
HSV_VALUE = 0.7 | |
PEAK_THRESHOLD = 3e6 | |
SATURATION_FIR_DEQUE_SIZE = 20 | |
HUE_IIR_ALPHA = 0.9 | |
CHUNK = 2048 | |
FORMAT = pyaudio.paInt16 | |
CHANNELS = 2 | |
RATE = 44100 | |
RECORD_SECONDS = 1000 | |
OCTAVES = 1 | |
BAND_LOWER = 40 | |
BAND_UPPER = 500 | |
class IIR: | |
def __init__(self, alpha): | |
self.alpha = alpha | |
self.prev = 0 | |
def update(self, value): | |
self.prev = (1-self.alpha)*value + self.alpha*self.prev | |
return self.prev | |
def getMagnitude(real, imaginary): | |
magnitudes = [] | |
x = 0 | |
while x < len(real) and x < len(imaginary): | |
magnitudes.append(numpy.sqrt(real[x]*real[x] + imaginary[x]*imaginary[x])) | |
x += 1 | |
return magnitudes | |
def getFrequencyIndex(freq): | |
return freq / (RATE/CHUNK) | |
p = pyaudio.PyAudio() | |
stream = p.open( | |
format=FORMAT, | |
channels=CHANNELS, | |
rate=RATE, | |
input=True, | |
frames_per_buffer=CHUNK | |
) | |
# arduino = serial.Serial(PORT) | |
time.sleep(2) | |
# print "SERIAL NAME: " + arduino.name | |
print("recording") | |
hue_iir = IIR(HUE_IIR_ALPHA) | |
saturation_fir_deque = [] | |
def mapFrequencyToIntensity(freq): | |
if freq < BAND_LOWER: | |
freq = BAND_LOWER | |
elif freq > BAND_UPPER: | |
freq = BAND_UPPER | |
freq_log = numpy.log(freq) / numpy.log(2 ** OCTAVES) | |
lower_log = numpy.log(BAND_LOWER) / numpy.log(2 ** OCTAVES) | |
upper_log = numpy.log(BAND_UPPER) / numpy.log(2 ** OCTAVES) | |
hue = (freq_log - lower_log) / (1.0 * upper_log - 1.0 * lower_log) | |
return hue | |
while True: | |
data = stream.read(CHUNK, exception_on_overflow = False) | |
nums = array.array('h', data) | |
results = numpy.fft.fft(nums) | |
freq_bins = numpy.fft.fftfreq(len(nums), 1.0 / RATE) | |
results = results[0:(len(results)/2 - 1)] | |
freq_bins = 2 * freq_bins[0:(len(freq_bins)/2 - 1)] | |
# magnitudes | |
mags = getMagnitude(results.real, results.imag) | |
lower_band_index = getFrequencyIndex(BAND_LOWER) | |
upper_band_index = getFrequencyIndex(BAND_UPPER) | |
max_mag = max(mags[lower_band_index:upper_band_index]) # 127 | |
min_mag = min(mags[lower_band_index:upper_band_index]) # 0 | |
print 'magnitudes', max_mag, min_mag | |
max_freq_index = mags.index(max_mag) | |
max_freq = freq_bins[max_freq_index] | |
min_freq_index = mags.index(min_mag) | |
min_freq = freq_bins[min_freq_index] | |
new_frequency = BAND_LOWER | |
if max_mag > PEAK_THRESHOLD: | |
new_frequency = max_freq | |
averaged_frequency = hue_iir.update(new_frequency) | |
hue = mapFrequencyToIntensity(averaged_frequency) | |
# [0, 0, 0, 0] | |
packet = [] | |
# for intensity in rgb: | |
# packet.append(intensity) | |
# print 'frequency', max_freq | |
print 'RGB', packet | |
print bytearray(packet) # sixty values from 0-127 each | |
# arduino.write(bytearray(packet)) | |
print("recording done") | |
stream.stop_stream() | |
stream.close() | |
p.terminate() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyaudio | |
import numpy | |
import array | |
import serial | |
import colorsys | |
import time | |
from color import convertPercentToColorValue | |
PORT = '/dev/cu.wchusbserial1430' | |
HSV_VALUE = 0.7 | |
PEAK_THRESHOLD = 3e6 | |
SATURATION_FIR_DEQUE_SIZE = 20 | |
HUE_IIR_ALPHA = 0.9 | |
CHUNK = 2048 | |
FORMAT = pyaudio.paInt16 | |
CHANNELS = 2 | |
RATE = 44100 | |
RECORD_SECONDS = 1000 | |
OCTAVES = 1 | |
BAND_LOWER = 40 | |
BAND_UPPER = 500 | |
class IIR: | |
def __init__(self, alpha): | |
self.alpha = alpha | |
self.prev = 0 | |
def update(self, value): | |
self.prev = (1-self.alpha)*value + self.alpha*self.prev | |
return self.prev | |
def getMagnitude(real, imaginary): | |
magnitudes = [] | |
x = 0 | |
while x < len(real) and x < len(imaginary): | |
magnitudes.append(numpy.sqrt(real[x]*real[x] + imaginary[x]*imaginary[x])) | |
x += 1 | |
return magnitudes | |
def getFrequencyIndex(freq): | |
return freq / (RATE/CHUNK) | |
p = pyaudio.PyAudio() | |
stream = p.open( | |
format=FORMAT, | |
channels=CHANNELS, | |
rate=RATE, | |
input=True, | |
frames_per_buffer=CHUNK | |
) | |
ser = serial.Serial(PORT) | |
time.sleep(2) | |
print "SERIAL NAME: " + ser.name | |
print("recording") | |
hue_iir = IIR(HUE_IIR_ALPHA) | |
saturation_fir_deque = [] | |
def mapFrequencyToHue(freq): | |
if freq < BAND_LOWER: | |
freq = BAND_LOWER | |
elif freq > BAND_UPPER: | |
freq = BAND_UPPER | |
freq_log = numpy.log(freq)/numpy.log(2 ** OCTAVES) | |
lower_log = numpy.log(BAND_LOWER)/numpy.log(2 ** OCTAVES) | |
upper_log = numpy.log(BAND_UPPER)/numpy.log(2 ** OCTAVES) | |
hue = (freq_log - lower_log) / (1.0 * upper_log - 1.0 * lower_log) | |
return hue | |
while True: | |
data = stream.read(CHUNK, exception_on_overflow = False) | |
nums = array.array('h', data) | |
results = numpy.fft.fft(nums) | |
freq_bins = numpy.fft.fftfreq(len(nums), 1.0 / RATE) | |
results = results[0:(len(results)/2 - 1)] | |
freq_bins = 2 * freq_bins[0:(len(freq_bins)/2 - 1)] | |
mags = getMagnitude(results.real, results.imag) | |
lower_band_index = getFrequencyIndex(BAND_LOWER) | |
upper_band_index = getFrequencyIndex(BAND_UPPER) | |
max_mag = max(mags[lower_band_index:upper_band_index]) | |
max_freq_index = mags.index(max_mag) | |
max_freq = freq_bins[max_freq_index] | |
new_frequency = BAND_LOWER | |
if max_mag > PEAK_THRESHOLD: | |
new_frequency = max_freq | |
averaged_frequency = hue_iir.update(new_frequency) | |
hue = mapFrequencyToHue(averaged_frequency) | |
mag_sum = numpy.sum(mags) | |
saturation_fir_deque.append(mag_sum) | |
if len(saturation_fir_deque) > SATURATION_FIR_DEQUE_SIZE: | |
print("popped!") | |
saturation_fir_deque.pop(0) | |
average_sum = numpy.mean(saturation_fir_deque) | |
max_sum = max(saturation_fir_deque) | |
if max_sum == 0: | |
max_sum = 1 | |
saturation = 0.85 + (0.15) * average_sum/max_sum | |
rgb = colorsys.hsv_to_rgb(hue, saturation, HSV_VALUE) | |
rgb = map(convertPercentToColorValue, rgb) | |
packet = [] | |
for color in rgb: | |
packet.append(color) | |
print 'frequency', max_freq | |
print 'RGB', packet | |
print bytearray(packet) | |
ser.write(bytearray(packet)) | |
print("recording done") | |
stream.stop_stream() | |
stream.close() | |
p.terminate() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Adafruit_NeoPixel.h> | |
#define BLUEPIN 6 | |
#define GREENPIN 5 | |
#define REDPIN 3 | |
#define PIN 7 | |
#define N_LEDS 1 | |
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(N_LEDS, PIN, NEO_GRB + NEO_KHZ800); | |
void setup() { | |
Serial.begin(9600); | |
pixels.begin(); | |
} | |
void loop() { | |
if (Serial.available() == 3) { | |
byte r = Serial.read(); | |
byte g = Serial.read(); | |
byte b = Serial.read(); | |
pixels.setPixelColor(0, pixels.Color(r, g, b)); | |
pixels.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment