ios - Core Graphics Drawing, Bad Performance Degredation -
i using following core graphics code draw waveform of audio data recording. apologize tons of code, here (i found here):
// // waveformview.m // // created edward majcher on 7/17/14. // #import "waveformview.h" //gain applied incoming samples static cgfloat kgain = 10.; //number of samples displayed static int kmaxwaveforms = 80.; @interface waveformview () @property (nonatomic) bool addtobuffer; //holds kmaxwaveforms number of incoming samples, //80 based on half width of iphone, adding 1 pixel line between samples @property (strong, nonatomic) nsmutablearray* bufferarray; + (float)rms:(float *)buffer length:(int)buffersize; @end @implementation waveformview - (void)awakefromnib { [super awakefromnib]; self.bufferarray = [nsmutablearray array]; } -(void)updatebuffer:(float *)buffer withbuffersize:(uint32)buffersize { if (!self.addtobuffer) { self.addtobuffer = yes; return; } else { self.addtobuffer = no; } float rms = [waveformview rms:buffer length:buffersize]; if ([self.bufferarray count] == kmaxwaveforms) { //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ // [self.bufferarray removeobjectatindex:0]; } [self.bufferarray addobject:@(rms * kgain)]; [self setneedsdisplay]; } + (float)rms:(float *)buffer length:(int)buffersize { float sum = 0.0; for(int = 0; < buffersize; i++) { sum += buffer[i] * buffer[i]; } return sqrtf( sum / buffersize ); } // ***************************************************** - (void)drawrect:(cgrect)rect { cgfloat midx = cgrectgetmidx(rect); cgfloat maxx = cgrectgetmaxx(rect); cgfloat midy = cgrectgetmidy(rect); cgcontextref context = uigraphicsgetcurrentcontext(); // draw out center line cgcontextsetstrokecolorwithcolor(context, [uicolor whitecolor].cgcolor); cgcontextsetlinewidth(context, 1.); cgcontextmovetopoint(context, 0., midy); cgcontextaddlinetopoint(context, maxx, midy); cgcontextstrokepath(context); cgfloat x = 0.; (nsnumber* n in self.bufferarray) { cgfloat height = 20 * [n floatvalue]; cgcontextmovetopoint(context, x, midy - height); cgcontextaddlinetopoint(context, x, midy + height); cgcontextstrokepath(context); x += 2; } if ([self.bufferarray count] >= kmaxwaveforms) { [self addmarkerincontext:context forx:midx forrect:rect]; } else { [self addmarkerincontext:context forx:x forrect:rect]; } } - (void)addmarkerincontext:(cgcontextref)context forx:(cgfloat)x forrect:(cgrect)rect { cgfloat maxy = cgrectgetmaxy(rect); cgcontextsetstrokecolorwithcolor(context, [uicolor greencolor].cgcolor); cgcontextsetfillcolorwithcolor(context, [uicolor greencolor].cgcolor); cgcontextfillellipseinrect(context, cgrectmake(x - 1.5, 0, 3, 3)); cgcontextmovetopoint(context, x, 0 + 3); cgcontextaddlinetopoint(context, x, maxy - 3); cgcontextstrokepath(context); cgcontextfillellipseinrect(context, cgrectmake(x - 1.5, maxy - 3, 3, 3)); } @end
so recording audio, waveform drawing gets more , more jittery, kind of game has bad frame rates. tried contacting owner of piece of code, no luck. have never used core graphics, i'm trying figure out why performance bad. performance starts degrade @ around 2-3 seconds worth of audio (the waveform doesn't fill screen).
my first question is, redrawing entire audio history every time drawrect called? if in drawrect function (marked asterisks), there variable called cgrect x. seems affect position @ waveform being drawn (if set 60 instead of 0, starts @ x=60 pixels instead of x=0 pixels).
from viewcontroller, pass in audiodata gets stored in self.bufferarray property. when loop goes through draw data, seems it's starting @ 0 , working way every time drawrect getting called, means every new piece of audio data added, drawrect gets called, , redraws entire waveform plus new piece of audio data.
if problem, know how can optimize piece of code? tried emptying bufferarray after loop contained new data, didn't work.
if not problem, there core graphics experts can figure out problem is?
i should mention commented out piece of code (marked @@@ @ signs) because need entire waveform. don't want remove pieces of waveform @ beginning. ios voice memos app can hold waveform of audio without performance degradation.
Comments
Post a Comment