2006-09-11 02:49:56 +00:00
|
|
|
//
|
|
|
|
// VLSheetViewNotes.mm
|
|
|
|
// Vocalese
|
|
|
|
//
|
|
|
|
// Created by Matthias Neeracher on 1/4/06.
|
|
|
|
// Copyright 2006 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "VLSheetView.h"
|
|
|
|
#import "VLSheetViewNotes.h"
|
|
|
|
#import "VLSheetViewInternal.h"
|
2006-12-30 09:57:40 +00:00
|
|
|
#import "VLDocument.h"
|
2006-09-11 02:49:56 +00:00
|
|
|
#import "VLSoundOut.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
@implementation VLSheetView (Notes)
|
|
|
|
|
2006-10-02 08:32:25 +00:00
|
|
|
- (void) addNoteAtCursor
|
2006-09-11 02:49:56 +00:00
|
|
|
{
|
2006-10-02 08:32:25 +00:00
|
|
|
if (fCursorMeasure > -1) {
|
2006-10-21 09:23:37 +00:00
|
|
|
VLNote newNote(1, fClickMode==' ' ? fCursorActualPitch : VLNote::kNoPitch);
|
2006-09-11 02:49:56 +00:00
|
|
|
|
2006-12-04 07:04:24 +00:00
|
|
|
[[self document] willChangeSong];
|
2006-10-16 09:01:25 +00:00
|
|
|
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
|
|
|
|
2006-10-16 09:01:25 +00:00
|
|
|
if (fClickMode == ' ')
|
|
|
|
VLSoundOut::Instance()->PlayNote(newNote);
|
|
|
|
else
|
|
|
|
fClickMode = ' ';
|
2006-09-11 02:49:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-02 08:32:25 +00:00
|
|
|
- (void) startKeyboardCursor
|
2006-09-11 02:49:56 +00:00
|
|
|
{
|
2006-10-02 08:32:25 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) drawNoteCursor
|
|
|
|
{
|
2006-10-16 09:01:25 +00:00
|
|
|
int cursorX;
|
|
|
|
int cursorY;
|
2006-10-21 09:23:37 +00:00
|
|
|
VLMusicElement accidental;
|
2006-10-16 09:01:25 +00:00
|
|
|
VLMusicElement cursorElt;
|
|
|
|
|
|
|
|
cursorX = [self noteXInMeasure:fCursorMeasure at:fCursorAt]-kNoteX;
|
2006-10-21 09:23:37 +00:00
|
|
|
switch (fClickMode) {
|
|
|
|
default:
|
2006-10-16 09:01:25 +00:00
|
|
|
cursorY =
|
2006-10-21 09:23:37 +00:00
|
|
|
[self noteYInMeasure:fCursorMeasure
|
|
|
|
withPitch:fCursorPitch
|
|
|
|
accidental:&accidental]
|
|
|
|
-kNoteY;
|
2006-10-16 09:01:25 +00:00
|
|
|
cursorElt = kMusicNoteCursor;
|
2006-10-21 09:23:37 +00:00
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
cursorY = [self noteYInMeasure:fCursorMeasure
|
|
|
|
withPitch:65
|
|
|
|
accidental:&accidental];
|
2006-10-16 09:01:25 +00:00
|
|
|
cursorElt = kMusicRestCursor;
|
2006-10-21 09:23:37 +00:00
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
cursorY = [self noteYInMeasure:fCursorMeasure
|
|
|
|
withPitch:fCursorPitch
|
|
|
|
accidental:&accidental];
|
|
|
|
cursorElt = kMusicKillCursor;
|
|
|
|
break;
|
2006-10-16 09:01:25 +00:00
|
|
|
}
|
|
|
|
|
2006-10-21 09:23:37 +00:00
|
|
|
NSPoint at = NSMakePoint(cursorX, cursorY);
|
2006-10-16 09:01:25 +00:00
|
|
|
[[self musicElement:cursorElt]
|
2006-10-21 09:23:37 +00:00
|
|
|
compositeToPoint:at
|
2006-09-11 02:49:56 +00:00
|
|
|
operation: NSCompositeSourceOver];
|
2006-10-21 09:23:37 +00:00
|
|
|
if (fCursorAccidental) {
|
|
|
|
at.y += kNoteY;
|
|
|
|
switch (cursorElt= fCursorAccidental) {
|
|
|
|
case kMusicFlatCursor:
|
|
|
|
at.x += kFlatW;
|
|
|
|
at.y += kFlatY;
|
|
|
|
break;
|
|
|
|
case kMusicSharpCursor:
|
|
|
|
at.x += kSharpW;
|
|
|
|
at.y += kSharpY;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
at.x += kNaturalW;
|
|
|
|
at.y += kNaturalY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
[[self musicElement:cursorElt]
|
|
|
|
compositeToPoint:at
|
|
|
|
operation: NSCompositeSourceOver];
|
|
|
|
}
|
2006-09-11 02:49:56 +00:00
|
|
|
}
|
|
|
|
|
2006-10-21 09:23:37 +00:00
|
|
|
- (void) drawNote:(VLFraction)dur at:(NSPoint)p
|
|
|
|
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;
|
|
|
|
switch (dur.fDenom) {
|
|
|
|
case 1:
|
|
|
|
head = [self musicElement:kMusicWholeNote];
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
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
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
if (dur.fDenom > 1) {
|
|
|
|
NSBezierPath * bz = [NSBezierPath bezierPath];
|
|
|
|
NSPoint s1 = NSMakePoint(s.x, s.y+kStemH);
|
|
|
|
NSImage * flag = nil;
|
|
|
|
switch (dur.fDenom) {
|
|
|
|
case 8:
|
|
|
|
flag = [self musicElement:kMusicEighthFlag];
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
flag = [self musicElement:kMusicSixteenthFlag];
|
|
|
|
s1.y += 5.0f;
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
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 =
|
2006-10-02 06:07:44 +00:00
|
|
|
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;
|
2006-10-02 06:07:44 +00:00
|
|
|
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];
|
|
|
|
}
|
2006-10-02 06:07:44 +00:00
|
|
|
fLastNoteCenter = c;
|
2006-09-11 02:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) drawRest:(VLFraction)dur at:(NSPoint)p
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Draw rest
|
|
|
|
//
|
|
|
|
NSImage * head = nil;
|
|
|
|
switch (dur.fDenom) {
|
|
|
|
case 1:
|
|
|
|
head = [self musicElement:kMusicWholeRest];
|
|
|
|
p.y += kWholeRestY;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
head = [self musicElement:kMusicHalfRest];
|
|
|
|
p.y += kHalfRestY;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
head = [self musicElement:kMusicQuarterRest];
|
|
|
|
p.x -= kNoteX;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
head = [self musicElement:kMusicEighthRest];
|
|
|
|
p.x -= kNoteX;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
head = [self musicElement:kMusicSixteenthRest];
|
|
|
|
p.x -= kNoteX;
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
head = [self musicElement:kMusicThirtysecondthRest];
|
|
|
|
p.x -= kNoteX;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
[head compositeToPoint:p
|
|
|
|
operation: NSCompositeSourceOver];
|
|
|
|
}
|
|
|
|
|
2006-10-02 08:32:25 +00:00
|
|
|
- (void) drawNotesForSystem:(int)system
|
2006-09-11 02:49:56 +00:00
|
|
|
{
|
|
|
|
const VLSong * song = [self song];
|
|
|
|
const VLProperties & prop = song->fProperties.front();
|
|
|
|
|
2006-10-02 08:32:25 +00:00
|
|
|
float kSystemY = [self systemY:system];
|
|
|
|
for (int m = 0; m<fMeasPerSystem; ++m) {
|
2006-10-21 09:23:37 +00:00
|
|
|
VLMusicElement accidentals[7];
|
|
|
|
memset(accidentals, 0, 7*sizeof(VLMusicElement));
|
2006-10-02 08:32:25 +00:00
|
|
|
int measIdx = m+system*fMeasPerSystem;
|
|
|
|
if (measIdx >= song->CountMeasures())
|
|
|
|
break;
|
|
|
|
const VLMeasure measure = song->fMeasures[measIdx];
|
|
|
|
const VLNoteList & melody = measure.fMelody;
|
|
|
|
VLFraction at(0);
|
2007-01-01 05:02:17 +00:00
|
|
|
VLFraction prevDur(0);
|
|
|
|
bool triplet = false;
|
2006-10-02 08:32:25 +00:00
|
|
|
for (VLNoteList::const_iterator note = melody.begin();
|
|
|
|
note != melody.end();
|
|
|
|
++note
|
|
|
|
) {
|
|
|
|
VLFraction dur = note->fDuration;
|
2007-01-01 05:02:17 +00:00
|
|
|
VLFraction nextDur(0);
|
|
|
|
VLNoteList::const_iterator next = note;
|
|
|
|
if (++next != melody.end())
|
|
|
|
nextDur = next->fDuration;
|
2006-10-02 08:32:25 +00:00
|
|
|
BOOL first = !m || !note->fTied;
|
|
|
|
int pitch = note->fPitch;
|
|
|
|
while (dur > 0) {
|
|
|
|
VLFraction partialDur; // Actual value of note drawn
|
2006-10-23 07:42:53 +00:00
|
|
|
VLFraction noteDur; // Visual value of note
|
2007-01-01 05:02:17 +00:00
|
|
|
prop.PartialNote(at, dur, dur==prevDur || dur==nextDur,
|
|
|
|
&partialDur);
|
|
|
|
prop.VisualNote(at, partialDur, triplet, ¬eDur, &triplet);
|
|
|
|
prevDur = partialDur;
|
2006-10-21 09:23:37 +00:00
|
|
|
if (pitch != VLNote::kNoPitch) {
|
|
|
|
VLMusicElement accidental;
|
|
|
|
NSPoint pos =
|
|
|
|
NSMakePoint([self noteXInMeasure:m at:at],
|
|
|
|
kSystemY+[self noteYWithPitch:pitch
|
|
|
|
accidental:&accidental]);
|
|
|
|
VLMusicElement acc = accidental;
|
|
|
|
int step= [self stepWithPitch:pitch];
|
|
|
|
if (acc == accidentals[step])
|
|
|
|
acc = kMusicNothing; // Don't repeat accidentals
|
|
|
|
else if (acc == kMusicNothing)
|
|
|
|
if (accidentals[step] == kMusicNatural) // Resume signature
|
|
|
|
acc = prop.fKey < 0 ? kMusicFlat : kMusicSharp;
|
|
|
|
else
|
|
|
|
acc = kMusicNatural;
|
2006-10-02 08:32:25 +00:00
|
|
|
[self drawNote:noteDur
|
2006-10-21 09:23:37 +00:00
|
|
|
at: pos
|
|
|
|
accidental: acc
|
2006-10-02 08:32:25 +00:00
|
|
|
tied:!first];
|
2006-10-21 09:23:37 +00:00
|
|
|
accidentals[step] = accidental;
|
|
|
|
} else {
|
|
|
|
VLMusicElement accidental;
|
|
|
|
NSPoint pos =
|
|
|
|
NSMakePoint([self noteXInMeasure:m at:at],
|
|
|
|
kSystemY+[self noteYWithPitch:65
|
|
|
|
accidental:&accidental]);
|
|
|
|
[self drawRest:noteDur at: pos];
|
|
|
|
}
|
2006-10-02 08:32:25 +00:00
|
|
|
dur -= partialDur;
|
|
|
|
at += partialDur;
|
|
|
|
first = NO;
|
2006-09-11 02:49:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-02 08:32:25 +00:00
|
|
|
if (fCursorPitch != VLNote::kNoPitch && fCursorMeasure/fMeasPerSystem == system)
|
2006-09-11 02:49:56 +00:00
|
|
|
[self drawNoteCursor];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|