2n2u

Untitled

Nov 19th, 2015
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.74 KB | None | 0 0
  1. class Recorder(object):
  2.  
  3.     def __init__(self, g_layout):
  4.         # get the needed Java classes
  5.         Clock.schedule_once(partial(self.my_init, g_layout))
  6.     #
  7.     def my_init(self, g_layout, dt):
  8.         self.buffer_size = 4096
  9.         if platform() == 'android':
  10.             self.MediaRecorder = autoclass('android.media.MediaRecorder')
  11.             self.AudioSource = autoclass('android.media.MediaRecorder$AudioSource')
  12.             self.AudioFormat = autoclass('android.media.AudioFormat')
  13.             self.AudioRecord = autoclass('android.media.AudioRecord')
  14.         # set the system config
  15.         self.SampleRate = 44100
  16.         self.ChannelConfig = self.AudioFormat.CHANNEL_IN_MONO
  17.         self.AudioEncoding = self.AudioFormat.ENCODING_PCM_16BIT
  18.         self.minBufferSize = self.AudioRecord.getMinBufferSize(self.SampleRate, self.ChannelConfig, self.AudioEncoding)
  19.         self.sData = []
  20.         self.mic = get_input(callback=self.mic_callback, source='mic', buffersize=self.buffer_size)#self.minBufferSize)
  21.         self.rform = g_layout
  22.         self.converted_buf = []
  23.         self.writetofile = []
  24.         self.allsamples = []
  25.         self.downsamples = []
  26.         self.LPCg = []
  27.         self.xmax = 0
  28.         self.ymax = 0
  29.  
  30.             #self.LPCg=getLPCpwr(self.converted_buf, self.SampleRate,[0, 0, 0, 0, 1])
  31.  
  32.  
  33.     def mic_callback(self, buf):
  34.         self.sData.append(str(buf))
  35.         self.converted_buf = np.frombuffer(buf, dtype=np.int16)
  36.         self.allsamples = np.append(self.allsamples, self.converted_buf)
  37.         self.writetofile.append(self.converted_buf)
  38.         if np.amax(self.converted_buf) > self.ymax:
  39.             self.ymax = np.amax(self.converted_buf)
  40.         self.rform.plot.points.append((self.xmax, np.amax(self.converted_buf)))
  41.         self.xmax += 1
  42.  
  43.  
  44.         # Auto-Scale Y-Axis
  45.         self.rform.graph.ymax = int(self.ymax)
  46.         # Scale X and Y Ticks
  47.         self.rform.graph.y_ticks_major = int(int(self.ymax)/5)
  48.         self.rform.graph.y_ticks_minor = int(int(self.ymax)/100)
  49.  
  50.         q.put(self.converted_buf)
  51.  
  52.     def start(self):
  53.         for i in range(num_theads):
  54.             worker = Thread(target=self.crawl, args=(q,))
  55.             worker.setDaemon(True)    #setting threads as "daemon" allows main program to
  56.                                       #exit eventually even if these dont finish
  57.                                       #correctly.
  58.             worker.start()
  59.         self.mic.start()
  60.         self.sData = []
  61.         self.rform.plot.points = []
  62.         self.current_window = 0
  63.         self.xmax = 0
  64.         Clock.schedule_interval(self.readbuffer, self.mic.buffersize/self.SampleRate)
  65.  
  66.     def readbuffer(self, dt):
  67.         self.mic.poll()
  68.  
  69.     def stop(self):
  70.         Clock.unschedule(self.readbuffer)
  71.         self.mic.stop()
  72.         wf = wave.open(PATH, 'w')
  73.         wf.setnchannels(self.mic.channels)
  74.         wf.setsampwidth(2)
  75.         wf.setframerate(self.mic.rate)
  76.         wf.writeframes(b''.join(self.sData))
  77.         wf.close()
  78.         print(len(self.downsamples))
  79.         np.savetxt("/sdcard/kivyrecords/" + "test.txt", self.writetofile, fmt='%2.2i')
  80.         self.xmax = 0
  81.         self.ymax = 0
  82.         self.converted_buf = []
  83.         #self.rform.plot.points = []
  84.  
  85.     def crawl(self, q):
  86.         while not q.empty():
  87.             work = q.get()                      #fetch new work from the Queue
  88.             self.downsamples = np.append(self.downsamples, downsample(work, 4))
  89.             q.task_done()
  90.         return True
  91.  
  92.  
  93. def downsample(a, factor):
  94.     newlist = []
  95.     append = newlist.append
  96.     for i in xrange(0, len(a), factor):
  97.         try:
  98.             append(np.mean(a[i:i+factor]))
  99.         except IndexError:
  100.             break
  101.     return newlist
Advertisement
Add Comment
Please, Sign In to add comment