SHARE
TWEET

Untitled

a guest Jun 19th, 2017 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. import subprocess
  3. import skimage.io
  4. import traceback
  5. import numpy
  6. import numpy as np
  7. import os
  8. import sys
  9. from os import system
  10. from platform import system as platform
  11. import skimage.io
  12. import wave
  13. import pyaudio
  14. import matplotlib.pyplot as plt
  15.  
  16.  
  17. plt.matshow([[1,0],[0,1]], fignum=1)
  18. plt.draw()
  19.  
  20. if platform() == 'Darwin':  # How Mac OS X is identified by Python
  21.     system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')
  22.  
  23. i = 0
  24. width=256
  25. height=256
  26.  
  27. # Number of bytes to be captured from audio stream
  28. # CHUNK = 512
  29. # CHUNK = 1024
  30. # CHUNK = 1024
  31. # CHUNK = 2048
  32. CHUNK = 4096
  33. # CHUNK = 9192
  34.  
  35. # number of bytes used per FFT fourier slice
  36. # length=512
  37. length = 1024
  38. # length=2048
  39. # length = 4096
  40.  
  41. #  forward step in sliding window [ CHUNK    [[length]-> ]step      CHUNK   ]
  42. # step=32
  43. # step=64
  44. # step = 128
  45. step=256
  46. # step=512
  47. # step<length : some overlap
  48.  
  49. image=numpy.array(bytearray(os.urandom(width*width)))
  50. image=image.reshape(width,width)
  51.  
  52. def get_audio_input_stream():
  53.   INDEX = 0  # 1
  54.   # FORMAT = pyaudio.paInt8
  55.   FORMAT = pyaudio.paInt16
  56.   # FORMAT = pyaudio.paInt32
  57.   # FORMAT = pyaudio.paFloat32
  58.   CHANNELS = 1
  59.   # RATE = 22500
  60.   RATE = 48000 #* 2 = 96000Hz max on mac
  61.   INPUT_BLOCK_TIME = 0.05
  62.   # INPUT_BLOCK_TIME = 0.1
  63.   INPUT_FRAMES_PER_BLOCK = int(RATE * INPUT_BLOCK_TIME)
  64.  
  65.   stream = pyaudio.PyAudio().open(
  66.     format=FORMAT,
  67.     channels=CHANNELS,
  68.     rate=RATE,
  69.     input=True,
  70.     frames_per_buffer=CHUNK,
  71.     input_device_index=INDEX)
  72.   return stream
  73.  
  74.  
  75.  
  76. def next_frame():
  77.   stream = get_audio_input_stream()
  78.   while True:
  79.     try:
  80.       dataraw = stream.read(CHUNK)
  81.     except IOError as e:
  82.       print(e) # [Errno -9981] Input overflowed  WHY?
  83.       stream = get_audio_input_stream() # reset
  84.       continue
  85.     data0 = numpy.fromstring(dataraw, dtype='int16')
  86.     yield data0
  87.  
  88. def record():
  89.   global i
  90.   global image
  91.   global winName
  92.   FILENAME = 'recording.wav'
  93.   # r = numpy.array()
  94.   hamming_window = np.hamming(length) # minimize fourier frequency drain
  95.   #hamming hanning bartlett 'blackman'
  96.   r = numpy.empty(length)
  97.   stream = get_audio_input_stream()
  98.   offset = 0
  99.   while True:
  100.     try:
  101.       dataraw = stream.read(CHUNK)
  102.     except IOError as e:
  103.       print(e) # [Errno -9981] Input overflowed  WHY?
  104.       stream=get_audio_input_stream()
  105.       continue
  106.     data0 = numpy.fromstring(dataraw, dtype='int16')
  107.     # data0 = numpy.fromstring(dataraw, dtype='int8')
  108.     if(i<20 and numpy.sum(np.abs(data0))<1000*width):
  109.       continue
  110.     r=numpy.append(r,data0)
  111.     while offset < r.size - length :
  112.       data = r[offset:offset+length]
  113.       data=data*hamming_window  # minimize fourier frequency drain
  114.       offset=offset + step
  115.  
  116.       data = numpy.fft.fft(data)#.abs()
  117.       data = numpy.absolute(data)
  118.       data = data[0:height]/256.0#.split(data,512)
  119.       data = numpy.log2(data*0.05+1.0)#//*50.0;
  120.       numpy.putmask(data, data > 255, 255)
  121.  
  122.       image[i] = data
  123.       i = i+1
  124.       if(i==width):
  125.         print("i %d\r"%i)
  126.         i=0
  127.         # image=image.T
  128.         image=numpy.rot90(image)
  129.         plt.matshow(image, fignum=1)
  130.         plt.draw()
  131.         plt.pause(0.01)
  132.         # result=spec2word(image) #todo: reconnect
  133.         # subprocess.call(["say"," %s"%result])
  134.         # cv2.imshow(winName,image)
  135.         # if cv2.waitKey(10) == 27: BREAKS portAudio !!
  136.  
  137. if __name__ == '__main__':
  138.   record()
RAW Paste Data
Top