在NSView中绘制波形
我需要在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会保持简单,并使用使用 Cocoa 绘图的
audioData
属性创建一个NSView
子类。您可以调用[view setAudioData:waveArray]
,它又会调用[self setNeedsDisplay:YES]
。然后,您可以在
drawRect:
方法中迭代示例并相应地使用NSRectFill()
。这里sample
的value
介于 0 和 1 之间。此代码并不准确,您应该确保仅在
内绘制样本来提高其效率脏矩形。
I would keep it simple and create an
NSView
subclass with anaudioData
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 useNSRectFill()
accordingly. Heresample
'svalue
is between 0 and 1.This code isn't exact, and you should be sure to make it more efficent by only drawing samples inside
dirtyRect
.我将从一个非常长且薄的图像开始来表示波形的单个条/列。
我的计划是有一个 NSTimer,每 0.01 秒将波形的所有条形向左移动一格。
所以在循环中是这样的。
现在您所要做的就是在正确的高度创建对象并将它们添加到 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.
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.