オシロスコープで表示できるようなリサージュ波形を描画するには、
x = sin(右チャンネルのオーディオデータ);
y = sin(左チャンネルのオーディオデータ);
という感じでサンプルごとに座標を求めて、線で繋ぐ。
NSViewのサブクラスでこのようなインスタンス変数があるとして、
NSUInteger length; //オーディオデータのサンプル数 float *lPtr; //左チャンネルのオーディオデータ float *rPtr; //右チャンネルのオーディオデータ
以下のようにdrawRectメソッドを記述する。(※2008/7/8 描画する位置をrectではなくboundsから求めるように変更しました)
- (void)drawRect:(NSRect)rect {
NSRect viewRect = [self bounds];
double halfWidth = viewRect.size.width / 2.0;
double halfHeight = viewRect.size.height / 2.0;
[[NSColor blackColor] set];
NSRectFill(
NSMakeRect(0, 0, viewRect.size.width, viewRect.size.height));
NSBezierPath *path = [NSBezierPath bezierPath];
[[NSColor greenColor] set];
[path setLineWidth:1.0];
for (NSUInteger i = 0; i < length; i++) {
double x = sin(rPtr[i]);
double y = sin(lPtr[i]);
NSPoint point =
NSMakePoint(x * halfWidth + halfWidth ,
y * halfHeight + halfHeight);
if ([path isEmpty]) {
[path moveToPoint:point];
}
[path lineToPoint:point];
}
[path stroke];
}