4

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.

dubbeat
  • 7,706
  • 18
  • 70
  • 122
  • Could you boil that code down a bit? There's a lot of code that isn't directly related to drawing which makes it quite hard to understand. – ryyst Jul 26 '11 at 09:14
  • Summary explanation of code added. Also a bizarre observation added – dubbeat Jul 26 '11 at 09:27
  • Can you post some sample code? – Richard Baxter Jul 26 '11 at 08:52
  • I am having this exact problem as well, and cannot for the life of me figure it out. The drawing works 100% fine in the simulator, but comes out as laggy/jagged lines on the iPad. – AlexFZ Jul 26 '11 at 18:42
  • @AlexFZ : I'm searching for a solution for this. If I find something useful I will post back here – dubbeat Jul 27 '11 at 09:15
  • after reading this thread by @iforce2d _ http://stackoverflow.com/questions/380062/iphone-device-vs-iphone-simulator -- I'm going to experiment with the order of the function calls in the waveform calculation function – dubbeat Jul 27 '11 at 11:51

2 Answers2

3

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.

Anomie
  • 92,546
  • 13
  • 126
  • 145
  • thats an interesting observation. In the morning I will try initializing the variable to 0 & report back. I've noticed that other parts of my application are also not working when archieved and built to an ipa for adhoc testing. Prehapsit could be a related matter. There are obvioulsy some critical difference between how a debug build and a release build are compiled that I am utterly ignorant about because when I hit the run button with a device plugged in it works fine..... just not when built for distribution. – dubbeat Jul 28 '11 at 21:39
0

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.

Nils Munch
  • 8,805
  • 11
  • 51
  • 103