在NSView中绘制波形

发布于 2024-12-13 06:37:49 字数 250 浏览 0 评论 0原文

我需要在 NSView 中绘制波形。 (我将所有样本都放在一个数组中)。绘图必须高效且快速,没有剪切、闪烁等,而且必须平滑。波形将根据歌曲位置“移动”,样本的一些变化(DSP 处理)将实时显示在 NSView 上。

我熟悉在画布对象上绘制线条、弧线等,并且我已经开发了执行此类操作的应用程序,但不是在 Mac OS X 上......

我想问是否有人可以指导我从哪里开始绘制!核心动画、OpenGL、简单覆盖绘图方法等。哪个是最佳实践 - 使用 API?

I need to draw a waveform in an NSView. (I have all the samples in an array). The drawing must be efficient and really fast without clipping, flickering, etc, it must be smooth. The waveform will be "moving" according to the song position and some changes to the samples (DSP processing) will be shown as visual representation onto NSView in realtime.

I'm familiar drawing lines, arcs, etc onto canvas objects and I have developed apps doing such things but not on Mac OS X ...

I want to ask if anyone can guide me where to start drawing! Core Animation, OpenGL, simple override drawing methods, ??, etc. Which would be the best practice - API to use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城 2024-12-20 06:37:49

我会保持简单,并使用使用 Cocoa 绘图的 audioData 属性创建一个 NSView 子类。您可以调用[view setAudioData:waveArray],它又会调用[self setNeedsDisplay:YES]

然后,您可以在 drawRect: 方法中迭代示例并相应地使用 NSRectFill()。这里 samplevalue 介于 0 和 1 之间。

- (void)drawRect:(NSRect)dirtyRect {
    [[NSColor blueColor]set];
    for (id sample in self.waveArray) {
        NSRect drawingRect = NSZeroRect;
        drawingRect.origin.x = [self bounds].origin.x;
        drawingRect.origin.y = [self bounds].origin.y + ([self.waveArray indexOfObject:sample]/([self.waveArray count] - 1.0));
        drawingRect.size.width = [self bounds].size.width/[self.waveArray count];
        drawingRect.size.height = [self bounds].size.height * [sample value];
        NSRectFill(drawingRect);
    }
}

此代码并不准确,您应该确保仅在 内绘制样本来提高其效率脏矩形。

I would keep it simple and create an NSView subclass with an audioData property that uses Cocoa drawing. You could call [view setAudioData:waveArray] which would in turn call [self setNeedsDisplay:YES].

In your drawRect: method you could then iterate through the samples and use NSRectFill() accordingly. Here sample's value is between 0 and 1.

- (void)drawRect:(NSRect)dirtyRect {
    [[NSColor blueColor]set];
    for (id sample in self.waveArray) {
        NSRect drawingRect = NSZeroRect;
        drawingRect.origin.x = [self bounds].origin.x;
        drawingRect.origin.y = [self bounds].origin.y + ([self.waveArray indexOfObject:sample]/([self.waveArray count] - 1.0));
        drawingRect.size.width = [self bounds].size.width/[self.waveArray count];
        drawingRect.size.height = [self bounds].size.height * [sample value];
        NSRectFill(drawingRect);
    }
}

This code isn't exact, and you should be sure to make it more efficent by only drawing samples inside dirtyRect.

有深☉意 2024-12-20 06:37:49

我将从一个非常长且薄的图像开始来表示波形的单个条/列。

我的计划是有一个 NSTimer,每 0.01 秒将波形的所有条形向左移动一格。

所以在循环中是这样的。

for (int x; x < [WaveArray count] ; x++)
{
    UIImageView * Bar = [WaveArray ObjectAtIndex: x];
    [Bar setCenter:CGPointMake(Bar.center.x-1,Bar.center.y)];
}

现在您所要做的就是在正确的高度创建对象并将它们添加到 WaveArray,它们都会移动到左侧。

I would start with a really long and thin image to represent a single bar/column for the waveform.

My plan would be to have a NSTimer that moves all bars of the wave one to the left every 0.01 seconds.

So something like this in the loop.

for (int x; x < [WaveArray count] ; x++)
{
    UIImageView * Bar = [WaveArray ObjectAtIndex: x];
    [Bar setCenter:CGPointMake(Bar.center.x-1,Bar.center.y)];
}

Now all you have to do is create the objects at the correct hight and add them to the WaveArray and they all will be moved to the left.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文