SHARE
TWEET

Untitled

a guest Dec 8th, 2019 110 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pyaudio
  2. import numpy as np
  3. from scipy import integrate
  4. from time import sleep
  5. import serial
  6. from scipy.signal import savgol_filter
  7.  
  8.  
  9. try:
  10.     ser = serial.Serial('COM3', 115200)
  11. except:
  12.     print("error")
  13. sleep(2)
  14.  
  15. RATE = 44100
  16. CHUNK = 4096
  17. maxbass = 1
  18. maxMid = 1
  19. maxHigh = 1
  20. totalArea = 1
  21. maxarea = 1
  22. counter = 1
  23.  
  24. p = pyaudio.PyAudio()
  25.  
  26. for i in range(p.get_device_count()):
  27.     print(p.get_device_info_by_index(i))
  28.  
  29. deviceInput = int(input("Type the Index of the device you want to use: "))
  30. stream = p.open(format = pyaudio.paInt16, channels = 1, rate = RATE, input = True, frames_per_buffer=CHUNK, input_device_index = deviceInput)
  31.  
  32.  
  33. while True:
  34.     #plt.clf()
  35.     #plt.axis([0,2000,0,3000000])
  36.     if counter == 50:
  37.         maxbass *= 0.8
  38.         maxMid *= 0.98
  39.         maxHigh *= 0.7
  40.         counter = 1
  41.     data = np.fromstring(stream.read(CHUNK), dtype = np.int16)
  42.     data = data * np.hanning(len(data)) # smooth the FFT by windowing data
  43.     data = savgol_filter(data, window_length = 51, polyorder = 4)
  44.  
  45.     fft = abs(np.fft.fft(data).real)
  46.     fft = fft[:int(len(fft)/2)]
  47.     freq = np.fft.fftfreq(CHUNK, 1.0/RATE)
  48.     freq = freq[:int(len(freq)/2)]
  49.  
  50.     fft = fft[0:3000]
  51.     freq = freq[0:3000]
  52.  
  53.     areaLow = integrate.simps(fft[25:400], freq[25:400], even='avg')
  54.     areaMid =  integrate.simps(fft[400:700], freq[400:700], even='avg')
  55.     areaHigh = integrate.simps(fft[700:3000], freq[700:3000], even='avg')
  56.  
  57.     bass = fft[25:400]
  58.     peakBass = fft[np.where(bass==np.max(bass))[0][0]]
  59.  
  60.     areaBass = ((areaLow * 0.3) + (peakBass * 0.7))
  61.    
  62.    
  63.    
  64.     if areaHigh > 255094:
  65.         if areaHigh > maxHigh:
  66.             maxHigh = areaHigh
  67.  
  68.     else:
  69.         areaHigh = 0.0001
  70.    
  71.     if areaMid > 61500:
  72.         if areaMid > maxMid:
  73.             maxMid = areaMid
  74.     else:
  75.         areaMid = 0.0001
  76.  
  77.     if areaBass > 280000:
  78.         #areaMid *= 0.7
  79.  
  80.         #areaHigh *= 0.8
  81.         if areaBass > maxbass:
  82.             areaMid *= 0.9
  83.             maxbass = areaBass
  84.     else:
  85.         areaBass = 0.00001
  86.    
  87.     var = 2
  88.     bassPerc = var * 1.2 * 700 * (areaBass / maxbass)
  89.     #bassPerc = 1.2 * 1024 * (areaBass / maxbass)
  90.    
  91.     midPerc = var* 0.8 * 1024 * (areaMid / maxMid)
  92.    
  93.     highPerc = var * 0.8 * 1024 * (areaHigh / maxHigh)
  94.    
  95.     bassPerc = str(int(bassPerc))
  96.     midPerc = str(int(midPerc))
  97.     highPerc = str(int(highPerc))
  98.     highPerc = highPerc.zfill(4)
  99.     bassPerc = bassPerc.zfill(4)
  100.     midPerc = midPerc.zfill(4)
  101.  
  102.     output = bassPerc + midPerc + highPerc + "$"
  103.     ser.write(output.encode())
  104.     print(output)
  105.     sleep(0.05)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top