【问题标题】:How to draw a Waveform?如何绘制波形?
【发布时间】:2015-10-14 21:07:40
【问题描述】:

我正在使用 Superpowered SDK 来播放声音。 它有一个返回 unsigned char** 的函数,称为 peakWaveForm。 我写了一个自定义 uiview 并尝试绘制这个值,我的视图看起来不太好。我的问题是,绘制波形的值应该如何? 以及什么样的变量。数组?。波形的正常大小应该是多少? SDK 返回一个 unsigned char** 我该如何继续?

- (void)drawRect:(CGRect)updateRect
{
    unsigned i, maxIndex;

    maxIndex = floor(CGRectGetMaxX(updateRect));
    i = floor(CGRectGetMinX(updateRect));
    float firstPoint = (float)mPeakWaveForm[0][i];

    UIBezierPath *path = [UIBezierPath bezierPath];
    path.lineWidth = 2;
   [[UIColor blackColor] setFill];
   [path moveToPoint:CGPointMake(i,firstPoint)];

   for(i; i <= maxIndex; i++)
   {
       float nextPoint = (float)mPeakWaveForm[0][i];
      [path addLineToPoint:CGPointMake(i, nextPoint)];
   }
   [path fill];
}

【问题讨论】:

    标签: ios objective-c audio waveform


    【解决方案1】:

    我的情况和你一样,就是这样解决的。 首先,你得到的波形只有一个一维数据数组,我们希望它是二维的,它在两个轴上都有。 所以我所做的是创建一个数组点,而不是直接绘制路径,然后绘制路径一次又一次镜像环绕 x 轴,如下所示:

        var points = Array<CGPoint>()
        for(i; i <= maxIndex; i++)
        {
           float nextPoint = (float)mPeakWaveForm[0][i];
           points.append(CGPoint(x: CGFloat(i), y: CGFloat(nextPoint)))
        }
        //Move to the center of the view
        var xf = CGAffineTransformIdentity;
        xf = CGAffineTransformTranslate(xf, 0, halfHeight)
        //Scale it as needed (you can avoid it)
        xf = CGAffineTransformScale(xf, xscale, yscale)
    
        let path = CGPathCreateMutable()
        //Draw the lines
        CGPathAddLines(path, &xf, points, points.count)
    
        //Mirror the drawing and draw them again
        xf = CGAffineTransformScale(xf, 1.0, -1.0);
        CGPathAddLines(path, &xf, points, points.count);
    
        CGPathCloseSubpath(path)
        //Draw the path
        let ctx = UIGraphicsGetCurrentContext();
        CGContextAddPath(ctx, path);
        CGContextSetStrokeColorWithColor(ctx,UIColor.whiteColor().CGColor);
        CGContextFillPath(ctx);
    

    对不起,代码在swift中,但功能与Objective-C中的相同

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      • 2018-12-03
      • 1970-01-01
      相关资源
      最近更新 更多