I've just finished waveform drawing code for my app. I'm pretty happy with it and on the simulator it looks great.
The problem I have is when I run it on an ipad it doesnt draw properly. On the simulator the drawing looks like a nice regular waveform drawing whereas on the ipad the waveform just looks like one big rectangle.
I'm very unsure how I could even begin to start trouble shooting and resolving something like this.
Can you offer any suggestions as to why its working on the simulator & not the ipad?
If I can submit anymore information that might help please let me know.
calculation
-(void) plotwaveform:(AudioSourceOBJ )source
{
int count =source->framecount;
int blocksize= count/resolution;
currentmaxvalue=0;
int readindex=0;
CGRect *addrects= malloc(resolution * sizeof(CGRect));
float *heights=malloc(resolution * sizeof(float));
for (int i=0; i<resolution;i++) {
AudioUnitSampleType *blockofaudio;
blockofaudio =malloc(blocksize * sizeof(AudioUnitSampleType));
memcpy(blockofaudio, &source->leftoutput[readindex],(blocksize * sizeof(AudioUnitSampleType)));
float sample= [self getRMS:blockofaudio blocksize:blocksize];
heights[i]=sample;
readindex+=blocksize;
}
for (int scale=0; scale<resolution; scale++) {
float h= heights[scale];
h= (h/currentmaxvalue)* 45;
addrects[scale]=CGRectMake(scale, 0, 1, h);
}
if (waveform) {
[waveform release];
[waveform removeFromSuperview];
waveform=nil;
}
CGMutablePathRef halfpath=CGPathCreateMutable();
CGPathAddRects(halfpath, NULL, addrects, resolution);
CGMutablePathRef path= CGPathCreateMutable();
CGAffineTransform xf = CGAffineTransformIdentity;
xf= CGAffineTransformTranslate(xf, 0.0,45);
CGPathAddPath(path,&xf, halfpath);
xf= CGAffineTransformIdentity;
xf= CGAffineTransformTranslate(xf, 0.0, 45);
xf=CGAffineTransformScale(xf, 1.0, -1);
CGPathAddPath(path, &xf, halfpath);
CGPathRelease(halfpath);
free(addrects);
waveform = [[Waveform alloc] initWithFrameAndPlotdata:CGRectMake(0, 0, 400,90) thepoints:path];
[self.view addSubview:waveform];
}
-(float ) getRMS:(AudioUnitSampleType *)blockofaudio blocksize:(int)blocksize
{
float output;
float sqsummed;
float sqrootofsum;
float val;
for (int i=0;i<blocksize; i++) {
val= blockofaudio[i];
sqsummed+= val* val;
}
sqrootofsum=sqsummed / blocksize;
output = sqrt(sqrootofsum);
// find the max
if(output> currentmaxvalue)
{
currentmaxvalue=output;
}
return output;
}
Drawing
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx= UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(ctx, 0, 0, 0, .5);
CGContextBeginPath(ctx);
CGContextAddPath(ctx, mutatablepath);
//CGContextStrokePath(ctx);
CGContextFillPath(ctx);
CFRelease(mutatablepath);
}
DESC EDIT
I pass a bunch of audio data to the plotwaveform function and divide it into chunks. For each chunk of audio I calculate the RMS for each chunk and keep a track of the maximum value. When all that is done I use the max value to scale my rms values to fit my view port.
I have noticed a strange thing. If I NSLog the values for the "output" variable in the getRMS function the waveform draws fine on the device. If I do not NSLog the values the waveform does not draw properly?!?
That to me is bizarre.