Created
March 7, 2018 06:01
-
-
Save jsbain/e4e51761dece46e75844abc73f6acb91 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 | |
#if update interval is small enough, you may even be able to detect some sort of tones in averagepowerorchannel | |
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