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;
开发者_如何学Go }
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.
One major error I see is that you never initialize sqsummed
inside the getRMS:blocksize:
method, so its initial value is garbage. What the garbage happens to be depends on the details of the surrounding code, how the compiler allocates registers for variables, and so on. Adding an NSLog statement could well change what the garbage is next time around the loop.
If the garbage happens to always correspond to a very small float value you'll get expected behavior, while if it happens to always correspond to some extremely large float value (large enough to swamp the actual samples) you'll get one big rectangle, while if it happens to vary you'll get a noise-like output.
In any case, please remember that the simulator has your entire mac ram and cpu power to work with. Process capacity is sadly not emulated in the iphone/ipad simulator.
精彩评论