Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Recorder(object):
- def __init__(self, g_layout):
- # get the needed Java classes
- Clock.schedule_once(partial(self.my_init, g_layout))
- #
- def my_init(self, g_layout, dt):
- self.buffer_size = 4096
- if platform() == 'android':
- self.MediaRecorder = autoclass('android.media.MediaRecorder')
- self.AudioSource = autoclass('android.media.MediaRecorder$AudioSource')
- self.AudioFormat = autoclass('android.media.AudioFormat')
- self.AudioRecord = autoclass('android.media.AudioRecord')
- # set the system config
- self.SampleRate = 44100
- self.ChannelConfig = self.AudioFormat.CHANNEL_IN_MONO
- self.AudioEncoding = self.AudioFormat.ENCODING_PCM_16BIT
- self.minBufferSize = self.AudioRecord.getMinBufferSize(self.SampleRate, self.ChannelConfig, self.AudioEncoding)
- self.sData = []
- self.mic = get_input(callback=self.mic_callback, source='mic', buffersize=self.buffer_size)#self.minBufferSize)
- self.rform = g_layout
- self.converted_buf = []
- self.writetofile = []
- self.allsamples = []
- self.downsamples = []
- self.LPCg = []
- self.xmax = 0
- self.ymax = 0
- #self.LPCg=getLPCpwr(self.converted_buf, self.SampleRate,[0, 0, 0, 0, 1])
- def mic_callback(self, buf):
- self.sData.append(str(buf))
- self.converted_buf = np.frombuffer(buf, dtype=np.int16)
- self.allsamples = np.append(self.allsamples, self.converted_buf)
- self.writetofile.append(self.converted_buf)
- if np.amax(self.converted_buf) > self.ymax:
- self.ymax = np.amax(self.converted_buf)
- self.rform.plot.points.append((self.xmax, np.amax(self.converted_buf)))
- self.xmax += 1
- # Auto-Scale Y-Axis
- self.rform.graph.ymax = int(self.ymax)
- # Scale X and Y Ticks
- self.rform.graph.y_ticks_major = int(int(self.ymax)/5)
- self.rform.graph.y_ticks_minor = int(int(self.ymax)/100)
- q.put(self.converted_buf)
- def start(self):
- for i in range(num_theads):
- worker = Thread(target=self.crawl, args=(q,))
- worker.setDaemon(True) #setting threads as "daemon" allows main program to
- #exit eventually even if these dont finish
- #correctly.
- worker.start()
- self.mic.start()
- self.sData = []
- self.rform.plot.points = []
- self.current_window = 0
- self.xmax = 0
- Clock.schedule_interval(self.readbuffer, self.mic.buffersize/self.SampleRate)
- def readbuffer(self, dt):
- self.mic.poll()
- def stop(self):
- Clock.unschedule(self.readbuffer)
- self.mic.stop()
- wf = wave.open(PATH, 'w')
- wf.setnchannels(self.mic.channels)
- wf.setsampwidth(2)
- wf.setframerate(self.mic.rate)
- wf.writeframes(b''.join(self.sData))
- wf.close()
- print(len(self.downsamples))
- np.savetxt("/sdcard/kivyrecords/" + "test.txt", self.writetofile, fmt='%2.2i')
- self.xmax = 0
- self.ymax = 0
- self.converted_buf = []
- #self.rform.plot.points = []
- def crawl(self, q):
- while not q.empty():
- work = q.get() #fetch new work from the Queue
- self.downsamples = np.append(self.downsamples, downsample(work, 4))
- q.task_done()
- return True
- def downsample(a, factor):
- newlist = []
- append = newlist.append
- for i in xrange(0, len(a), factor):
- try:
- append(np.mean(a[i:i+factor]))
- except IndexError:
- break
- return newlist
Advertisement
Add Comment
Please, Sign In to add comment