├── .gitignore ├── LICENSE ├── README.md ├── look.kv └── main.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | 55 | # Sphinx documentation 56 | docs/_build/ 57 | 58 | # PyBuilder 59 | target/ 60 | 61 | #Ipython Notebook 62 | .ipynb_checkpoints 63 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Atul 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Kivy app for real time plotting of microphone Levels 2 | 3 | ![alt tag](http://s33.postimg.org/vccm7kz9r/ezgif_1353221528.gif) 4 | -------------------------------------------------------------------------------- /look.kv: -------------------------------------------------------------------------------- 1 | #:import MeshLinePlot kivy.garden.graph.MeshLinePlot 2 | Logic: 3 | BoxLayout: 4 | orientation: "vertical" 5 | BoxLayout: 6 | size_hint: [1, .8] 7 | Graph: 8 | id: graph 9 | xlabel: "" 10 | ylabel: "Level" 11 | BoxLayout: 12 | size_hint: [1, .2] 13 | orientation: "horizontal" 14 | Button: 15 | text: "START" 16 | bold: True 17 | on_press: root.start() 18 | Button: 19 | text: "STOP" 20 | bold: True 21 | on_press: root.stop() 22 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | """Real time plotting of Microphone level using kivy 2 | """ 3 | 4 | from kivy.lang import Builder 5 | from kivy.app import App 6 | from kivy.uix.boxlayout import BoxLayout 7 | from kivy.garden.graph import MeshLinePlot 8 | from kivy.clock import Clock 9 | from threading import Thread 10 | import audioop 11 | import pyaudio 12 | 13 | def get_microphone_level(): 14 | """ 15 | source: http://stackoverflow.com/questions/26478315/getting-volume-levels-from-pyaudio-for-use-in-arduino 16 | audioop.max alternative to audioop.rms 17 | """ 18 | chunk = 1024 19 | FORMAT = pyaudio.paInt16 20 | CHANNELS = 1 21 | RATE = 44100 22 | p = pyaudio.PyAudio() 23 | 24 | s = p.open(format=FORMAT, 25 | channels=CHANNELS, 26 | rate=RATE, 27 | input=True, 28 | frames_per_buffer=chunk) 29 | global levels 30 | while True: 31 | data = s.read(chunk) 32 | mx = audioop.rms(data, 2) 33 | if len(levels) >= 100: 34 | levels = [] 35 | levels.append(mx) 36 | 37 | 38 | class Logic(BoxLayout): 39 | def __init__(self,): 40 | super(Logic, self).__init__() 41 | self.plot = MeshLinePlot(color=[1, 0, 0, 1]) 42 | 43 | def start(self): 44 | self.ids.graph.add_plot(self.plot) 45 | Clock.schedule_interval(self.get_value, 0.001) 46 | 47 | def stop(self): 48 | Clock.unschedule(self.get_value) 49 | 50 | def get_value(self, dt): 51 | self.plot.points = [(i, j/5) for i, j in enumerate(levels)] 52 | 53 | 54 | class RealTimeMicrophone(App): 55 | def build(self): 56 | return Builder.load_file("look.kv") 57 | 58 | if __name__ == "__main__": 59 | levels = [] # store levels of microphone 60 | get_level_thread = Thread(target = get_microphone_level) 61 | get_level_thread.daemon = True 62 | get_level_thread.start() 63 | RealTimeMicrophone().run() 64 | 65 | --------------------------------------------------------------------------------