Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # kivy imports
- from kivy.app import App
- from kivy.uix.boxlayout import BoxLayout
- from kivy.uix.anchorlayout import AnchorLayout
- from kivy.properties import ObjectProperty
- from kivy.clock import Clock
- from kivy.uix.screenmanager import ScreenManager, Screen
- from kivy.garden.graph import Graph, MeshLinePlot, SmoothLinePlot, LinePlot
- from kivy.core.window import Window
- from kivy.uix.widget import Widget
- # Import custom fonts
- from kivy.core.text import LabelBase
- from functools import partial
- KIVY_FONTS = [
- {
- "name": "RobotoCondensed",
- "fn_regular": "data/fonts/RobotoCondensed-Light.ttf",
- "fn_bold": "data/fonts/RobotoCondensed-Regular.ttf",
- "fn_italic": "data/fonts/RobotoCondensed-LightItalic.ttf",
- "fn_bolditalic": "data/fonts/RobotoCondensed-Italic.ttf"
- }
- ]
- # other imports
- from jnius import autoclass
- from audiostream import get_input
- import math
- import numpy as np
- import os
- import wave
- __author__ = 'Andre'
- if not os.path.isdir("/sdcard/kivyrecords/"):
- os.mkdir("/sdcard/kivyrecords/")
- PATH = "/sdcard/kivyrecords/rec_test.wav"
- recordtime = 5
- samples_per_second = 10
- screens = []
- # Load custom fonts
- for font in KIVY_FONTS:
- LabelBase.register(**font)
- # Screen that contains the Record Form Widget
- class RootScreen(BoxLayout): #
- user_profile = ObjectProperty()
- g_evaluation = ObjectProperty()
- show_results = ObjectProperty()
- go_settings = ObjectProperty()
- screen_manager = ObjectProperty(None)
- def __init__(self, **kwargs):
- super(RootScreen, self).__init__(**kwargs)
- self.tmp = ''
- # Initializing the button background normal images
- self.images = ['data/images/User.png', 'data/images/Microphone.png',
- 'data/images/AudioWave.png', 'data/images/Settings.png']
- self.buttons_array = [self.user_profile, self.g_evaluation, self.show_results, self.go_settings]
- for i in range(0, len(self.images)):
- self.buttons_array[i].background_normal = self.images[i]
- self.tmp = self.images[i]
- self.tmp = self.tmp.replace(".png", "d.png")
- self.buttons_array[i].background_down = self.tmp
- def select_screen(self):
- print("teste")
- print(self.ids)
- #sm.switch_to(screens[1])
- class RecordForm(BoxLayout): #
- # binding of record button on .kv
- b_record = ObjectProperty()
- # binding of the progressbar on .kv
- p_bar = ObjectProperty()
- # Instatiate the rform to pass later (Record Widget 'Layout')
- rform = ObjectProperty()
- # binding to the graph layout on .kv
- g_layout = ObjectProperty()
- def __init__(self, **kwargs):
- super(RecordForm, self).__init__(**kwargs)
- Clock.schedule_once(self.my_init)
- def my_init(self,dt):
- self.REC = Recorder(self.g_layout)
- def start_record(self):
- self.b_record.disabled = True
- self.p_bar.max = recordtime
- #print(self.ids)
- self.REC.start()
- Clock.schedule_once(self.stop_record, recordtime)
- Clock.schedule_interval(self.update_display, 1/samples_per_second)
- def stop_record(self, dt):
- Clock.unschedule(self.update_display)
- self.p_bar.value = 0
- self.REC.stop()
- self.b_record.disabled = False
- def update_display(self,dt):
- self.p_bar.value = self.p_bar.value + dt
- class GraphForm(AnchorLayout):
- def __init__(self, **kwargs):
- super(GraphForm, self).__init__(**kwargs)
- # Initializing the graph instance
- self.graph = Graph(xlabel='X', ylabel='Y', x_ticks_minor=5, x_ticks_major=10, y_ticks_major=20,
- y_grid_label=True, x_grid_label=True, padding=5, x_grid=True, y_grid=True, xmin=-0, xmax=120, ymin=-0, ymax=100)
- self.plot = SmoothLinePlot(color=[1, 0, 0, 1])
- self.graph.add_plot(self.plot)
- self.add_widget(self.graph)
- 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, dt, g_layout):
- 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.minBufferSize)
- self.rform = self.g_layout
- self.converted_buf = []
- self.xmax = 0
- self.ymax = 0
- 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)))
- # 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(self.rform.graph.y_ticks_major/5.)
- # New sample
- self.xmax += 1
- def start(self):
- self.mic.start()
- self.rform.plot.points = []
- 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 = []
- # screens.extend([RootScreen(name='Home'), RecordForm(name='RecordForm')])
- # for i in range(0,len(screens)-1):
- # sm.add_widget(screens[i])
- # print i
- class AirflowApp(App):
- def build(self):
- self.title = 'AirFlow Application'
- #Window.clearcolor = (1, 1, 1, 1)
- return RecordForm()#RootScreen()
- if __name__ == '__main__':
- AirflowApp().run()
Advertisement
Add Comment
Please, Sign In to add comment