VocalEasel/Sources/VLSheetViewNotes.mm

385 lines
9.6 KiB
Plaintext
Raw Normal View History

2006-09-11 02:49:56 +00:00
//
2007-04-27 06:41:34 +00:00
// File: VLSheetViewNotes.mm - Melody editing functionality
2006-09-11 02:49:56 +00:00
//
2007-04-27 06:41:34 +00:00
// Author(s):
//
// (MN) Matthias Neeracher
//
2008-05-29 18:54:30 +00:00
// Copyright © 2005-2008 Matthias Neeracher
2006-09-11 02:49:56 +00:00
//
#import "VLSheetView.h"
#import "VLSheetViewNotes.h"
#import "VLSheetViewInternal.h"
#import "VLDocument.h"
2006-09-11 02:49:56 +00:00
#import "VLSoundOut.h"
#include <algorithm>
@implementation VLSheetView (Notes)
- (void) addNoteAtCursor
2006-09-11 02:49:56 +00:00
{
if (fCursorMeasure > -1 && fCursorActualPitch) {
2006-10-21 09:23:37 +00:00
VLNote newNote(1, fClickMode==' ' ? fCursorActualPitch : VLNote::kNoPitch);
2008-05-29 18:54:30 +00:00
switch (fCursorAccidental) {
case kMusicFlatCursor:
newNote.fVisual |= VLNote::kWantFlat;
break;
case kMusicSharpCursor:
newNote.fVisual |= VLNote::kWantSharp;
break;
}
2006-12-04 07:04:24 +00:00
[[self document] willChangeSong];
2007-04-22 02:59:52 +00:00
if (fCursorAccidental == kMusicExtendCursor)
[self song]->ExtendNote(fCursorMeasure, fCursorAt);
else if (fClickMode == 'k')
[self song]->DelNote(fCursorMeasure, fCursorAt);
else
2006-11-27 07:07:45 +00:00
[self song]->AddNote(VLLyricsNote(newNote), fCursorMeasure, fCursorAt);
2006-12-04 07:04:24 +00:00
[[self document] didChangeSong];
2006-09-11 02:49:56 +00:00
if (fClickMode == ' ')
VLSoundOut::Instance()->PlayNote(newNote);
else
fClickMode = ' ';
2006-09-11 02:49:56 +00:00
}
}
- (void) startKeyboardCursor
2006-09-11 02:49:56 +00:00
{
if (fCursorMeasure < 0) {
2006-10-21 09:23:37 +00:00
fCursorMeasure = 0;
fCursorPitch = VLNote::kMiddleC;
fCursorActualPitch = fCursorPitch;
fCursorAt = VLFraction(0);
2006-09-11 02:49:56 +00:00
}
}
2008-05-29 18:54:30 +00:00
- (void) drawLedgerLinesInSection:(int)section withPitch:(int)pitch visual:(int)visual at:(NSPoint)p
2007-04-21 22:34:55 +00:00
{
p.x += kLedgerX;
int octave = (pitch / 12) - 5;
2008-05-29 18:54:30 +00:00
int step = (octave*7
+ [self stepInSection:section withPitch:pitch visual:visual]
- 2
) / 2;
2007-04-21 22:34:55 +00:00
for (int i=0; i-- > step; ) {
NSPoint p0 = p;
p0.y += i*kLineH;
NSPoint p1 = p0;
p1.x += kLedgerW;
[NSBezierPath strokeLineFromPoint:p0 toPoint:p1];
}
for (int i=4; i++ < step; ) {
NSPoint p0 = p;
p0.y += i*kLineH;
NSPoint p1 = p0;
p1.x += kLedgerW;
[NSBezierPath strokeLineFromPoint:p0 toPoint:p1];
}
}
2008-05-29 18:54:30 +00:00
- (void) drawNoteCursor:(int)pitch inMeasure:(size_t)measure at:(VLFract)at
accidental:(VLMusicElement)accidental
mode:(char)mode
2006-09-11 02:49:56 +00:00
{
int cursorX;
int cursorY;
int cursorSect;
2008-05-29 18:54:30 +00:00
int cursorVisual = 0;
VLMusicElement acc;
VLMusicElement cursorElt;
2008-05-29 18:54:30 +00:00
cursorX = [self noteXInMeasure:measure at:at];
if (accidental == kMusicExtendCursor) {
cursorY =
2008-05-29 18:54:30 +00:00
[self noteYInMeasure:measure
withPitch:pitch
visual:0
accidental:&acc];
cursorElt = accidental;
2007-04-22 02:59:52 +00:00
} else
2008-05-29 18:54:30 +00:00
switch (mode) {
2007-04-22 02:59:52 +00:00
default:
2008-05-29 18:54:30 +00:00
switch (accidental) {
case kMusicSharp:
cursorVisual = VLNote::kWantSharp;
break;
case kMusicFlat:
cursorVisual = VLNote::kWantFlat;
break;
}
2007-04-22 02:59:52 +00:00
cursorY =
2008-05-29 18:54:30 +00:00
[self noteYInMeasure:measure withPitch:pitch
visual:cursorVisual accidental:&acc] - kNoteY;
cursorSect = [self song]->fMeasures[measure].fPropIdx;
[self drawLedgerLinesInSection:cursorSect withPitch:pitch
visual:cursorVisual at:NSMakePoint(cursorX,
[self systemY:fLayout->SystemForMeasure(measure)])];
2007-04-22 02:59:52 +00:00
cursorElt = kMusicNoteCursor;
break;
case 'r':
2008-05-29 18:54:30 +00:00
cursorY = [self noteYInMeasure:measure
withPitch:65 visual:0 accidental:&acc];
2007-04-22 02:59:52 +00:00
cursorElt = kMusicRestCursor;
break;
case 'k':
2008-05-29 18:54:30 +00:00
cursorY = [self noteYInMeasure:measure
withPitch:pitch visual:0
accidental:&acc];
2007-04-22 02:59:52 +00:00
cursorElt = kMusicKillCursor;
break;
}
2008-05-29 18:54:30 +00:00
NSPoint xy = NSMakePoint(cursorX-kNoteX, cursorY);
[[self musicElement:cursorElt]
2008-05-29 18:54:30 +00:00
compositeToPoint:xy
2006-09-11 02:49:56 +00:00
operation: NSCompositeSourceOver];
2008-05-29 18:54:30 +00:00
if (accidental && accidental != kMusicExtendCursor) {
xy.y += kNoteY;
switch (cursorElt= accidental) {
2006-10-21 09:23:37 +00:00
case kMusicFlatCursor:
2008-05-29 18:54:30 +00:00
xy.x += kFlatW;
xy.y += kFlatY;
2006-10-21 09:23:37 +00:00
break;
case kMusicSharpCursor:
2008-05-29 18:54:30 +00:00
xy.x += kSharpW;
xy.y += kSharpY;
2006-10-21 09:23:37 +00:00
break;
default:
2008-05-29 18:54:30 +00:00
xy.x += kNaturalW;
xy.y += kNaturalY;
2006-10-21 09:23:37 +00:00
break;
}
[[self musicElement:cursorElt]
2008-05-29 18:54:30 +00:00
compositeToPoint:xy
2006-10-21 09:23:37 +00:00
operation: NSCompositeSourceOver];
}
2006-09-11 02:49:56 +00:00
}
2008-05-29 18:54:30 +00:00
- (void) drawNoteCursor:(int)pitch inMeasure:(size_t)measure at:(VLFract)at
{
[self drawNoteCursor:pitch inMeasure:measure at:at
accidental:kMusicNothing mode:' '];
}
- (void) drawNoteCursor
{
[self drawNoteCursor:fCursorPitch inMeasure:fCursorMeasure at:fCursorAt
2008-08-16 20:43:19 +00:00
accidental:fCursorAccidental mode:fClickMode];
2008-05-29 18:54:30 +00:00
}
2007-05-21 08:18:58 +00:00
- (void) drawNote:(int)visual at:(NSPoint)p
2006-10-21 09:23:37 +00:00
accidental:(VLMusicElement)accidental tied:(BOOL)tied
2006-09-11 02:49:56 +00:00
{
NSPoint s = p;
NSPoint c = p;
p.x -= kNoteX;
p.y -= kNoteY;
s.x += kNoteX+kStemX;
s.y += kStemY;
//
// Draw note head
//
NSImage * head;
2007-05-21 08:18:58 +00:00
switch (visual & VLNote::kNoteHead) {
case VLNote::kWhole:
2006-09-11 02:49:56 +00:00
head = [self musicElement:kMusicWholeNote];
break;
2007-05-21 08:18:58 +00:00
case VLNote::kHalf:
2006-09-11 02:49:56 +00:00
head = [self musicElement:kMusicHalfNote];
s.x -= 1.0f;
break;
default:
head = [self musicElement:kMusicNote];
s.x -= 2.0f;
break;
}
[head compositeToPoint:p
2006-10-21 09:23:37 +00:00
operation: NSCompositePlusDarker];
//
// Draw accidental
//
if (accidental) {
NSPoint at = p;
at.y += kNoteY;
switch (accidental) {
case kMusicSharp:
at.x += kSharpW;
at.y += kSharpY;
break;
case kMusicFlat:
at.x += kFlatW;
at.y += kFlatY;
break;
case kMusicNatural:
at.x += kNaturalW;
at.y += kNaturalY;
break;
}
[[self musicElement:accidental]
compositeToPoint:at operation: NSCompositeSourceOver];
}
2006-09-11 02:49:56 +00:00
//
// Draw stem
//
//
//
2007-05-21 08:18:58 +00:00
if (visual > 0) {
2006-09-11 02:49:56 +00:00
NSBezierPath * bz = [NSBezierPath bezierPath];
NSPoint s1 = NSMakePoint(s.x, s.y+kStemH);
NSImage * flag = nil;
2007-05-21 08:18:58 +00:00
switch (visual) {
case VLNote::kEighth:
2006-09-11 02:49:56 +00:00
flag = [self musicElement:kMusicEighthFlag];
break;
2007-05-21 08:18:58 +00:00
case VLNote::k16th:
2006-09-11 02:49:56 +00:00
flag = [self musicElement:kMusicSixteenthFlag];
s1.y += 5.0f;
break;
2007-05-21 08:18:58 +00:00
case VLNote::k32nd:
2006-09-11 02:49:56 +00:00
flag = [self musicElement:kMusicThirtysecondthFlag];
s1.y += 13.0f;
break;
}
[[NSColor blackColor] set];
[bz setLineWidth:2.0f];
[bz moveToPoint:s];
[bz lineToPoint:s1];
[bz stroke];
if (flag)
[flag compositeToPoint:s
operation: NSCompositePlusDarker];
}
//
// Draw tie
//
if (tied) {
NSPoint mid =
NSMakePoint(0.5f*(fLastNoteCenter.x+c.x),
0.5f*(fLastNoteCenter.y+c.y));
2006-09-11 02:49:56 +00:00
NSPoint dir = NSMakePoint(c.y-mid.y, c.x-mid.x);
float n = dir.x*dir.x+dir.y*dir.y;
float r = (kTieDepth*kTieDepth+n) / (2.0f*kTieDepth);
float l = (r-kTieDepth) / sqrtf(n);
mid.x += dir.x*l;
mid.y += dir.y*l;
float a1 = atan2(fLastNoteCenter.y-mid.y, fLastNoteCenter.x-mid.x);
2006-09-11 02:49:56 +00:00
float a2 = atan2(c.y-mid.y, c.x-mid.x);
NSBezierPath * bz = [NSBezierPath bezierPath];
[bz appendBezierPathWithArcWithCenter:mid radius:r
startAngle:a1*180.0f/M_PI endAngle:a2*180.0f/M_PI];
[bz stroke];
}
fLastNoteCenter = c;
2006-09-11 02:49:56 +00:00
}
2007-05-21 08:18:58 +00:00
- (void) drawRest:(int)visual at:(NSPoint)p
2006-09-11 02:49:56 +00:00
{
//
// Draw rest
//
NSImage * head = nil;
2007-05-21 08:18:58 +00:00
switch (visual) {
case VLNote::kWhole:
2006-09-11 02:49:56 +00:00
head = [self musicElement:kMusicWholeRest];
p.y += kWholeRestY;
break;
2007-05-21 08:18:58 +00:00
case VLNote::kHalf:
2006-09-11 02:49:56 +00:00
head = [self musicElement:kMusicHalfRest];
p.y += kHalfRestY;
break;
2007-05-21 08:18:58 +00:00
case VLNote::kQuarter:
2006-09-11 02:49:56 +00:00
head = [self musicElement:kMusicQuarterRest];
p.x -= kNoteX;
break;
2007-05-21 08:18:58 +00:00
case VLNote::kEighth:
2006-09-11 02:49:56 +00:00
head = [self musicElement:kMusicEighthRest];
p.x -= kNoteX;
break;
2007-05-21 08:18:58 +00:00
case VLNote::k16th:
2006-09-11 02:49:56 +00:00
head = [self musicElement:kMusicSixteenthRest];
p.x -= kNoteX;
break;
2007-05-21 08:18:58 +00:00
case VLNote::k32nd:
2006-09-11 02:49:56 +00:00
head = [self musicElement:kMusicThirtysecondthRest];
p.x -= kNoteX;
break;
}
[head compositeToPoint:p
operation: NSCompositeSourceOver];
}
- (void) drawNotesForSystem:(int)system
2006-09-11 02:49:56 +00:00
{
2007-12-23 12:45:17 +00:00
const int kFirstMeas = fLayout->FirstMeasure(system);
const VLSong * song = [self song];
2007-12-23 13:14:09 +00:00
const VLProperties & kProp = song->Properties(kFirstMeas);
2007-12-23 12:45:17 +00:00
const VLSystemLayout & kLayout = (*fLayout)[system];
2006-09-11 02:49:56 +00:00
float kSystemY = [self systemY:system];
2007-12-23 12:45:17 +00:00
for (int m = 0; m<kLayout.NumMeasures(); ++m) {
2006-10-21 09:23:37 +00:00
VLMusicElement accidentals[7];
memset(accidentals, 0, 7*sizeof(VLMusicElement));
2007-12-23 12:45:17 +00:00
int measIdx = m+kFirstMeas;
if (measIdx >= song->CountMeasures())
break;
2007-05-21 08:18:58 +00:00
const VLMeasure & measure = song->fMeasures[measIdx];
VLNoteList melody;
2007-12-23 12:45:17 +00:00
measure.DecomposeNotes(kProp, melody);
VLFraction at(0);
for (VLNoteList::const_iterator note = melody.begin();
note != melody.end();
++note
) {
2007-05-21 08:18:58 +00:00
BOOL tied = (note != melody.begin() || m)
&& note->fTied & VLNote::kTiedWithPrev;
int pitch = note->fPitch;
if (pitch != VLNote::kNoPitch) {
[self drawLedgerLinesInSection:measure.fPropIdx withPitch:pitch
2008-05-29 18:54:30 +00:00
visual:note->fVisual
at:NSMakePoint([self noteXInMeasure:measIdx at:at],
kSystemY)];
2007-05-21 08:18:58 +00:00
VLMusicElement accidental;
NSPoint pos =
2007-12-23 23:07:27 +00:00
NSMakePoint([self noteXInMeasure:measIdx at:at],
kSystemY+[self noteYInSection:measure.fPropIdx
withPitch:pitch
2008-05-29 18:54:30 +00:00
visual:note->fVisual
2007-05-21 08:18:58 +00:00
accidental:&accidental]);
VLMusicElement acc = accidental;
int step= [self stepInSection:measure.fPropIdx
2008-05-29 18:54:30 +00:00
withPitch:pitch
visual:note->fVisual];
2007-05-21 08:18:58 +00:00
if (acc == accidentals[step])
acc = kMusicNothing; // Don't repeat accidentals
else if (acc == kMusicNothing)
if (accidentals[step] == kMusicNatural) // Resume signature
2007-12-23 12:45:17 +00:00
acc = kProp.fKey < 0 ? kMusicFlat : kMusicSharp;
2007-05-21 08:18:58 +00:00
else
acc = kMusicNatural;
[self drawNote:note->fVisual & VLNote::kNoteHead
at: pos
accidental: acc
tied:tied];
accidentals[step] = accidental;
} else {
VLMusicElement accidental;
NSPoint pos =
2007-12-23 23:07:27 +00:00
NSMakePoint([self noteXInMeasure:measIdx at:at],
kSystemY+[self noteYInSection:measure.fPropIdx
2008-05-29 18:54:30 +00:00
withPitch:65 visual:0
2007-05-21 08:18:58 +00:00
accidental:&accidental]);
[self drawRest:note->fVisual & VLNote::kNoteHead at: pos];
2006-09-11 02:49:56 +00:00
}
2007-05-21 08:18:58 +00:00
at += note->fDuration;
2006-09-11 02:49:56 +00:00
}
}
2007-12-23 12:45:17 +00:00
if (fCursorPitch != VLNote::kNoPitch && fLayout->SystemForMeasure(fCursorMeasure) == system)
2006-09-11 02:49:56 +00:00
[self drawNoteCursor];
}
@end