Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
log.py
###### starting#####
Memory used: 346,796.0 K delaT=0.3418772083314252
Memory used: 346,752.0 K delaT=0.34547562499938067
Memory used: 332,104.0 K delaT=0.33316375000140397
Memory used: 332,168.0 K delaT=0.3367614583330578
Memory used: 332,364.0 K delaT=0.34773424999730196
Memory used: 332,428.0 K delaT=0.3353516249990207
Memory used: 332,624.0 K delaT=0.33892854166333564
Memory used: 332,820.0 K delaT=0.34268920832982985
Memory used: 333,016.0 K delaT=0.33042024999303976
playing
Memory used: 333,212.0 K delaT=0.33429491666174727
Memory used: 335,284.0 K delaT=0.34478800000215415
Memory used: 335,484.0 K delaT=0.34858670833637007
Memory used: 333,992.0 K delaT=0.3362730000008014
Memory used: 334,188.0 K delaT=0.33989112499693874
Memory used: 334,384.0 K delaT=0.34364574999926845
Memory used: 334,580.0 K delaT=0.33129462500073714
Memory used: 334,792.0 K delaT=0.3353379583277274
Memory used: 334,988.0 K delaT=0.3457879999987199
Memory used: 335,184.0 K delaT=0.34977325000363635
Memory used: 335,380.0 K delaT=0.33734491666109534
Memory used: 335,576.0 K delaT=0.34176520833716495
Memory used: 335,772.0 K delaT=0.35200670833000913
Memory used: 335,968.0 K delaT=0.33250129166845
Memory used: 336,164.0 K delaT=0.3429983333335258
Memory used: 336,360.0 K delaT=0.346681750001153
Memory used: 336,556.0 K delaT=0.3345457916657324
Memory used: 336,752.0 K delaT=0.33838462499988964
Memory used: 336,948.0 K delaT=0.3489675000018906
Memory used: 338,832.0 K delaT=0.3298709999944549
Memory used: 337,340.0 K delaT=0.3332736666634446
Memory used: 337,536.0 K delaT=0.34397954167070566
Memory used: 337,732.0 K delaT=0.33161254166043364
Memory used: 337,928.0 K delaT=0.335492999998678
Memory used: 338,108.0 K delaT=0.33908299999893643
Memory used: 338,304.0 K delaT=0.3499989166666637
Memory used: 338,500.0 K delaT=0.33057687500695465
Memory used: 338,696.0 K delaT=0.34125387500535
Memory used: 341,072.0 K delaT=0.34481833333848044
Memory used: 339,096.0 K delaT=0.33262187500076834
Memory used: 339,292.0 K delaT=0.3364639583378448
Memory used: 339,488.0 K delaT=0.34718066667119274
Memory used: 339,684.0 K delaT=0.3513797500054352
Memory used: 339,880.0 K delaT=0.33874620833375957
Memory used: 340,076.0 K delaT=0.3422484583352343
Memory used: 340,272.0 K delaT=0.329829875001451
Memory used: 340,468.0 K delaT=0.33405541666434146
Memory used: 340,664.0 K delaT=0.33769049999682466
Memory used: 340,860.0 K delaT=0.34820475000014994
Memory used: 343,004.0 K delaT=0.3360745833342662
Memory used: 343,344.0 K delaT=0.3400354583354783
Memory used: 341,448.0 K delaT=0.34320124999794643
Memory used: 341,644.0 K delaT=0.33075749999989057
Memory used: 341,840.0 K delaT=0.3355730833281996
Memory used: 342,036.0 K delaT=0.34536829166609095
Memory used: 342,232.0 K delaT=0.349099875005777
Memory used: 343,636.0 K delaT=0.33669479166565
Memory used: 343,372.0 K delaT=0.3403339583383058
Memory used: 343,376.0 K delaT=0.3514275416673627
Memory used: 341,960.0 K delaT=0.3316948333304026
Memory used: 341,960.0 K delaT=0.33566037499986123
Memory used: 343,376.0 K delaT=0.3467822916645673
Memory used: 343,376.0 K delaT=0.33375858332874486
Memory used: 343,376.0 K delaT=0.3374480416678125
Memory used: 341,960.0 K delaT=0.34125320833118167
Memory used: 341,960.0 K delaT=0.35256987500179093
Memory used: 341,964.0 K delaT=0.3326677916702465
Memory used: 341,964.0 K delaT=0.34356074999959674
Memory used: 341,964.0 K delaT=0.3473011249952833
Memory used: 343,380.0 K delaT=0.33515433333377587
Memory used: 343,380.0 K delaT=0.3385201249984675
Memory used: 343,380.0 K delaT=0.3497809583277558
Memory used: 341,964.0 K delaT=0.329880500001309
Memory used: 341,964.0 K delaT=0.34095004166738363
Memory used: 343,380.0 K delaT=0.3446236666641198
Memory used: 343,380.0 K delaT=0.3321044999975129
Memory used: 343,380.0 K delaT=0.3357895416629617
Memory used: 342,040.0 K delaT=0.33952870833309134
Memory used: 342,040.0 K delaT=0.3507361666634097
Memory used: 342,040.0 K delaT=0.33085312500043074
Memory used: 342,040.0 K delaT=0.3417183749988908
Memory used: 341,960.0 K delaT=0.3454838749967166
Memory used: 343,376.0 K delaT=0.33311108333145967
Memory used: 343,376.0 K delaT=0.3368107083297218
Memory used: 343,376.0 K delaT=0.3477340833269409
Memory used: 341,960.0 K delaT=0.35134820832900004
Memory used: 341,964.0 K delaT=0.33182762499927776
Memory used: 343,380.0 K delaT=0.34316695832967525
Memory used: 343,380.0 K delaT=0.3301217500047642
Memory used: 343,524.0 K delaT=0.3338702499968349
Memory used: 341,964.0 K delaT=0.33764545833400916
Memory used: 341,964.0 K delaT=0.348588374996325
Memory used: 341,964.0 K delaT=0.3523396250020596
Memory used: 341,968.0 K delaT=0.3399332083281479
Memory used: 341,968.0 K delaT=0.34367525000561727
Memory used: 343,384.0 K delaT=0.33158800000092015
Memory used: 343,384.0 K delaT=0.3350091250031255
Memory used: 343,384.0 K delaT=0.3458165416668635
Memory used: 341,968.0 K delaT=0.3497586249941378
Memory used: 341,968.0 K delaT=0.3303687916704803
Memory used: 341,968.0 K delaT=0.3411365833308082
Memory used: 343,384.0 K delaT=0.34484341667121043
Memory used: 343,384.0 K delaT=0.3320774999956484
Memory used: 343,380.0 K delaT=0.33588562499789987
Memory used: 341,964.0 K delaT=0.34673612500046147
Memory used: 341,964.0 K delaT=0.35047162500268314
Memory used: 341,964.0 K delaT=0.33822450000297977
Memory used: 341,964.0 K delaT=0.34181550000357674
Memory used: 343,380.0 K delaT=0.32937979167036247
Memory used: 343,380.0 K delaT=0.3332877083303174
Memory used: 343,380.0 K delaT=0.3440307916607708
Memory used: 341,964.0 K delaT=0.3477515000049607
Memory used: 341,964.0 K delaT=0.3355864166660467
Memory used: 343,380.0 K delaT=0.3392877916630823
Memory used: 343,380.0 K delaT=0.3497846250029397
Memory used: 343,380.0 K delaT=0.330264583339158
Memory used: 341,964.0 K delaT=0.334324249997735
Memory used: 341,968.0 K delaT=0.3452067500038538
Memory used: 341,968.0 K delaT=0.33258699999714736
Memory used: 341,968.0 K delaT=0.33630820833786856
Memory used: 341,968.0 K delaT=0.34056220833736006
#stopping engine
###### starting#####
Memory used: 358,040.0 K delaT=0.3419129583344329
Memory used: 345,132.0 K delaT=0.3456880416633794
Memory used: 344,628.0 K delaT=0.33331824999913806
Memory used: 344,628.0 K delaT=0.33659875000012107
Memory used: 345,684.0 K delaT=0.3475862916675396
Memory used: 342,816.0 K delaT=0.35120545833342476
Memory used: 342,816.0 K delaT=0.3392019166640239
Memory used: 344,632.0 K delaT=0.34285841666860506
Memory used: 344,632.0 K delaT=0.33029304166848306
playing
Memory used: 342,816.0 K delaT=0.33390545833390206
Memory used: 345,688.0 K delaT=0.3376887499980512
Memory used: 342,816.0 K delaT=0.34860425000078976
Memory used: 342,816.0 K delaT=0.35234387499804143
Memory used: 342,816.0 K delaT=0.3399577500022133
Memory used: 342,816.0 K delaT=0.3437232500000391
Memory used: 344,632.0 K delaT=0.33131787500315113
Memory used: 344,628.0 K delaT=0.3350414583328529
Memory used: 344,628.0 K delaT=0.3385362500048359
Memory used: 344,628.0 K delaT=0.34979575000033947
Memory used: 342,812.0 K delaT=0.33027945833600825
Memory used: 342,812.0 K delaT=0.3410694166668691
Memory used: 344,628.0 K delaT=0.3447142916629673
Memory used: 344,628.0 K delaT=0.33212341666512657
Memory used: 342,812.0 K delaT=0.3358408749991213
Memory used: 342,812.0 K delaT=0.34685570833971724
Memory used: 342,812.0 K delaT=0.3505840416692081
Memory used: 342,812.0 K delaT=0.3311185833372292
Memory used: 342,812.0 K delaT=0.34198241666308604
Memory used: 344,628.0 K delaT=0.34636183333350345
Memory used: 344,628.0 K delaT=0.3331702499999665
Memory used: 344,628.0 K delaT=0.33690154166833963
Memory used: 342,812.0 K delaT=0.34797058333060704
Memory used: 342,816.0 K delaT=0.3514794166694628
Memory used: 344,632.0 K delaT=0.33967470833158586
Memory used: 344,632.0 K delaT=0.34324891666619806
Memory used: 344,632.0 K delaT=0.33059570832847385
Memory used: 344,632.0 K delaT=0.3340677500018501
Memory used: 342,816.0 K delaT=0.3449721249999129
Memory used: 342,816.0 K delaT=0.34867370833671885
Memory used: 342,816.0 K delaT=0.33633425000152783
#stopping engine
# -*- coding: utf-8 -*-
from objc_util import *
import ctypes,time,os,struct,array,console
def getfile():
import os
import requests
if not os.path.exists('Allegro.mp3'):
r=requests.get('http://www.stephaniequinn.com/Music/Allegro%20from%20Duet%20in%20C%20Major.mp3')
with open('Allegro.mp3','bw') as f:
f.write(r.content)
getfile()
bufsize = 4410 # Audio Tap out
input_type = 2
play_filename ='Allegro.mp3'
global Srv_Ctime,dWhen
Srv_Ctime,dWhen = 0,0
#
# Get iOS System Memory Info
#
NSProcessInfo = ObjCClass('NSProcessInfo')
NSByteCountFormatter = ObjCClass('NSByteCountFormatter')
class c_vm_statistics(Structure):
_fields_ = [('free_count', c_uint),
('active_count', c_uint),
('inactive_count', c_uint),
('wire_count', c_uint),
('zero_fill_count', c_uint),
('reactivations', c_uint),
('pageins', c_uint),
('pageouts', c_uint),
('faults', c_uint),
('cow_faults', c_uint),
('lookups', c_uint),
('hits', c_uint),
('purgeable_count', c_uint),
('purges', c_uint),
('speculative_count', c_uint)]
c = ctypes.cdll.LoadLibrary(None)
mach_host_self = c.mach_host_self
mach_host_self.restype = c_uint
mach_host_self.argtypes = [c_void_p]
host_page_size = c.host_page_size
host_page_size.restype = c_int
host_page_size.argtypes = [c_uint, POINTER(c_uint)]
host_statistics = c.host_statistics
host_statistics.restype = c_int
host_statistics.argtypes = [c_uint, c_int, POINTER(c_int), POINTER(c_uint)]
host_port = c_uint()
host_size = c_uint()
page_size = c_uint()
host_port = mach_host_self(None)
host_size = c_uint(int(sizeof(c_vm_statistics) / sizeof(c_int)))
host_page_size(host_port, byref(page_size))
vm_stat = c_vm_statistics()
HOST_VM_INFO = c_int(2) # This is a c macro
# Return System Used/Free memory (bytes)
def Get_mem():
get_host_statistics = host_statistics(host_port, HOST_VM_INFO, ctypes.cast(byref(vm_stat),ctypes.POINTER(c_int)), ctypes.byref(host_size))
mem_used = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * int(page_size.value)
mem_free = vm_stat.free_count * int(page_size.value)
return mem_used,mem_free
# Get System Time
curTime = ctypes.cdll.LoadLibrary(None)
curTime.CACurrentMediaTime.restype=c_double
# AVAudio Define
AVAudioEngine=ObjCClass('AVAudioEngine')
AVAudioSession=ObjCClass('AVAudioSession')
AVAudioPlayerNode=ObjCClass('AVAudioPlayerNode')
AVAudioFile=ObjCClass('AVAudioFile')
AVAudioUnitEQ=ObjCClass('AVAudioUnitEQ')
AVAudioMixerNode=ObjCClass('AVAudioMixerNode')
AVAudioPCMBuffer=ObjCClass('AVAudioPCMBuffer')
AVAudioFormat=ObjCClass('AVAudioFormat')
AVAudioUnitEQFilterParameters=ObjCClass('AVAudioUnitEQFilterParameters')
AVAudioSessionPortDescription=ObjCClass('AVAudioSessionPortDescription')
#AVAudioCompressedBuffer=ObjCClass('AVAudioCompressedBuffer')
#AVAudioConverter=ObjCClass('AVAudioConverter')
AVAudioTime=ObjCClass('AVAudioTime')
class AudioStreamBasicDescription(ctypes.Structure):
_fields_=[('mSampleRate',ctypes.c_double),('mFormatID',ctypes.c_uint32),('mFormatFlags',ctypes.c_uint32),('mBytesPerPacket',ctypes.c_uint32),('mFramesPerPacket',ctypes.c_uint32),('mBytesPerFrame',ctypes.c_uint32),('mChannelsPerFrame',ctypes.c_uint32),('mBitsPerChannel',ctypes.c_uint32),('mReserved',ctypes.c_uint32)]
# create AVAudio engine
def setup():
error=ctypes.c_void_p(0)
session=AVAudioSession.sharedInstance()
session.setCategory('AVAudioSessionCategoryPlayAndRecord',error=ctypes.pointer(error))
if error:
raise Exception('error setting up category')
session.setActive(True, error=ctypes.pointer(error))
if error:
raise Exception('error setting up session active')
engine=AVAudioEngine.new()
return engine
#
# Audio Tap
#
def processBuffer(self,buffer,when,cmd):
global Srv_Ctime,dWhen
# Record audio when time
t_when = AVAudioTime.secondsForHostTime(ObjCInstance(when).hostTime())
# Record audio tap out time,and conver to 8 bytes
Srv_Ctime = curTime.CACurrentMediaTime()
dWhen = Srv_Ctime - t_when
# get buffer
cbuf = ObjCInstance(buffer)
# ios 9 need this line,because installTapOnBus buffsize not effect,but ios 10 it's work.
cbuf.frameLength = bufsize
cbuf._cached_methods.clear()
cbuf=[]
process_block=ObjCBlock(processBuffer,restype=None,argtypes=[c_void_p,c_void_p,c_void_p,c_void_p])
#
# Audio Init
#
# open a mp3 player
fileurl = nsurl(os.path.abspath(play_filename))
file = AVAudioFile.alloc().initForReading_error_(fileurl,None)
bd=file.processingFormat().streamDescription()
# AVAudioFormat: 0-other,1-PCM float32,2 PCM float64,3-PCM int16,4-PCM int32
#audioFormat=AVAudioFormat.alloc().initWithCommonFormat_sampleRate_channels_interleaved_(1,44100,2,False)
audioFormat = file.processingFormat()
# save file to buffer for loop player
audioFrameCount = file.length()
audioFileBuffer = AVAudioPCMBuffer.alloc().initWithPCMFormat_frameCapacity_(audioFormat,audioFrameCount)
file.readIntoBuffer_error_(audioFileBuffer,None)
# init Audio Engine
engine=setup()
# Mic
ainput = engine.inputNode()
# Player for mp3
player = AVAudioPlayerNode.new()
engine.attachNode(player)
# loop play audio files (so need using buff)
#player.scheduleFile_atTime_completionHandler_(file,None,None)
player.scheduleBuffer_atTime_options_completionHandler_(audioFileBuffer,None,1,None)
# Mixer
mixer = engine.mainMixerNode()
# Connect devices
if input_type == 1 or input_type == 3:
engine.connect_to_format_(ainput,mixer,ainput.inputFormatForBus_(0))
if input_type == 2 or input_type == 3:
engine.connect_to_format_(player,mixer,audioFormat)
# install Tap on mixer
mixer.installTapOnBus(0,bufferSize=bufsize,format=audioFormat,block=process_block)
print('###### starting#####')
# start audio engin
engine.prepare()
engine.startAndReturnError_(None)
# play mp3 music
# UI Display Loop
ui_cnt = 0
try:
while 1:
time.sleep(0.05)
ui_cnt += 1
mem_used,mem_free = Get_mem()
if ui_cnt==100:
if input_type == 2 or input_type ==3:
print('playing')
player.play()
if (ui_cnt % 10) == 0:
print( 'Memory used:','{:,} K delaT={}'.format(mem_used/1024,dWhen))
except KeyboardInterrupt:
print('#stopping engine')
engine.pause()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.