Created
March 7, 2018 06:11
-
-
Save jsbain/572d3dca6e49d6c0ca0f48d9fae5d09e to your computer and use it in GitHub Desktop.
Untitled_187.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from objc_util import * | |
import ctypes | |
app=UIApplication.sharedApplication() | |
#NSURL *url = [NSURL fileURLWithPath:@"/dev/null"]; | |
url = nsurl('/dev/null') | |
import os | |
#url= nsurl(os.path.abspath('test.aac')) | |
settings=ns({ns('AVSampleRateKey'):ns(44100.0), | |
ns('AVFormatIDKey'): ns(1633772320), | |
ns('AVNumberOfChannelsKey'):ns(1)}) | |
err_ptr=c_void_p() | |
recorder=ObjCClass('AVAudioRecorder').alloc().initWithURL_settings_error_(url,settings,ctypes.byref(err_ptr)) | |
if err_ptr: | |
err=ObjCInstance(err_ptr) | |
print(err) | |
AVAudioSession = ObjCClass('AVAudioSession') | |
shared_session = AVAudioSession.sharedInstance() | |
category_set = shared_session.setCategory_error_(ns('AVAudioSessionCategoryPlayAndRecord'), None) | |
if (recorder): | |
recorder.prepareToRecord() | |
recorder.setMeteringEnabled_(True) | |
recorder.instantaneousMetering=True | |
recorder.record() | |
import ui | |
class audioView(ui.View): | |
def update(self): | |
recorder.updateMeters() | |
#here you could check if peakPowerForChannel is > some threshold, then run an action | |
self.name='peak:{:5.2f} average:{:5.2f}'.format( | |
recorder.peakPowerForChannel_(0), recorder.averagePowerForChannel_(0)) | |
self.set_needs_display() | |
def draw(self): | |
ui.set_color('red') | |
ui.Path.rect(0,0,160+recorder.peakPowerForChannel_(0),50).fill() | |
ui.Path.rect(0,50,160+recorder.averagePowerForChannel_(0),50).fill() | |
v=audioView() | |
v.update_interval=0.05 #adjustable. the peak power meter already seems to have a 1 second filter | |
v.present() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment