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
- 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.xmax = 0
- self.ymax = 0
- self.testbuff = []
- def mic_callback(self, buf):
- self.sData.append(str(buf))
- self.converted_buf = np.frombuffer(buf, dtype=np.int16)
- 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)))
- print(self.mic.rate)
- print(self.mic.channels)
- print(self.mic.encoding)
- #print(self.converted_buf)
- # Auto-Scale Y-Axis
- self.rform.graph.ymax = int(self.ymax)
- # Scale Y Ticks
- self.rform.graph.y_ticks_major = int(int(self.ymax)/5)
- self.rform.graph.y_ticks_minor = int(int(self.ymax)/100)
- #print(str(self.rform.graph.y_ticks_minor))
- # New sample
- self.xmax += 1
- def start(self):
- self.mic.start()
- self.rform.plot.points = [(1,80),(2,90)]
- Clock.schedule_interval(self.readbuffer, 1/60.)
- 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()
- #np.savetxt("/sdcard/kivyrecords/" + "test.txt", self.newpoints, fmt='%2.2i')
- self.sData = []
- self.xmax = 0
- self.ymax = 0
- self.converted_buf = []
Advertisement
Add Comment
Please, Sign In to add comment