/** Calculate the vertical position (white note key) where * the stem ends */ public WhiteNote CalculateEnd() { if (direction == Up) { WhiteNote w = top; w = w.Add(6); if (duration == NoteDuration.Sixteenth) { w = w.Add(2); } else if (duration == NoteDuration.ThirtySecond) { w = w.Add(4); } return(w); } else if (direction == Down) { WhiteNote w = bottom; w = w.Add(-6); if (duration == NoteDuration.Sixteenth) { w = w.Add(-2); } else if (duration == NoteDuration.ThirtySecond) { w = w.Add(-4); } return(w); } else { return(null); /* Shouldn't happen */ } }
/** Draw the black circle notes. * @param ytop The ylocation (in pixels) where the top of the staff starts. * @param topstaff The white note of the top of the staff. */ public void DrawNotes(Graphics g, Pen pen, int ytop, WhiteNote topstaff) { pen.Width = 1; foreach (NoteData note in notedata) { /* Get the x,y position to draw the note */ int ynote = ytop + topstaff.Dist(note.whitenote) * SheetMusic.NoteHeight / 2; int xnote = SheetMusic.LineSpace / 4; if (!note.leftside) { xnote += SheetMusic.NoteWidth; } /* Draw rotated ellipse. You must first translate (0,0) * to the center of the ellipse. */ g.TranslateTransform(xnote + SheetMusic.NoteWidth / 2 + 1, ynote - SheetMusic.LineWidth + SheetMusic.NoteHeight / 2); g.RotateTransform(-45); if (sheetmusic != null) { pen.Color = sheetmusic.NoteColor(note.number); } else { pen.Color = Color.Black; } if (note.duration == NoteDuration.Whole || note.duration == NoteDuration.Half || note.duration == NoteDuration.DottedHalf) { g.DrawEllipse(pen, -SheetMusic.NoteWidth / 2, -SheetMusic.NoteHeight / 2, SheetMusic.NoteWidth, SheetMusic.NoteHeight - 1); g.DrawEllipse(pen, -SheetMusic.NoteWidth / 2, -SheetMusic.NoteHeight / 2 + 1, SheetMusic.NoteWidth, SheetMusic.NoteHeight - 2); g.DrawEllipse(pen, -SheetMusic.NoteWidth / 2, -SheetMusic.NoteHeight / 2 + 1, SheetMusic.NoteWidth, SheetMusic.NoteHeight - 3); } else { Brush brush = Brushes.Black; if (pen.Color != Color.Black) { brush = new SolidBrush(pen.Color); } g.FillEllipse(brush, -SheetMusic.NoteWidth / 2, -SheetMusic.NoteHeight / 2, SheetMusic.NoteWidth, SheetMusic.NoteHeight - 1); if (pen.Color != Color.Black) { brush.Dispose(); } } pen.Color = Color.Black; g.DrawEllipse(pen, -SheetMusic.NoteWidth / 2, -SheetMusic.NoteHeight / 2, SheetMusic.NoteWidth, SheetMusic.NoteHeight - 1); g.RotateTransform(45); g.TranslateTransform(-(xnote + SheetMusic.NoteWidth / 2 + 1), -(ynote - SheetMusic.LineWidth + SheetMusic.NoteHeight / 2)); /* Draw a dot if this is a dotted duration. */ if (note.duration == NoteDuration.DottedHalf || note.duration == NoteDuration.DottedQuarter || note.duration == NoteDuration.DottedEighth) { g.FillEllipse(Brushes.Black, xnote + SheetMusic.NoteWidth + SheetMusic.LineSpace / 3, ynote + SheetMusic.LineSpace / 3, 4, 4); } /* Draw horizontal lines if note is above/below the staff */ WhiteNote top = topstaff.Add(1); int dist = note.whitenote.Dist(top); int y = ytop - SheetMusic.LineWidth; if (dist >= 2) { for (int i = 2; i <= dist; i += 2) { y -= SheetMusic.NoteHeight; g.DrawLine(pen, xnote - SheetMusic.LineSpace / 4, y, xnote + SheetMusic.NoteWidth + SheetMusic.LineSpace / 4, y); } } WhiteNote bottom = top.Add(-8); y = ytop + (SheetMusic.LineSpace + SheetMusic.LineWidth) * 4 - 1; dist = bottom.Dist(note.whitenote); if (dist >= 2) { for (int i = 2; i <= dist; i += 2) { y += SheetMusic.NoteHeight; g.DrawLine(pen, xnote - SheetMusic.LineSpace / 4, y, xnote + SheetMusic.NoteWidth + SheetMusic.LineSpace / 4, y); } } /* End drawing horizontal lines */ } }
LineUpStemEnds(ChordSymbol[] chords) { Stem firstStem = chords[0].Stem; Stem lastStem = chords[chords.Length - 1].Stem; Stem middleStem = chords[1].Stem; if (firstStem.Direction == Stem.Up) { /* Find the highest stem. The beam will either: * - Slant downwards (first stem is highest) * - Slant upwards (last stem is highest) * - Be straight (middle stem is highest) */ WhiteNote top = firstStem.End; foreach (ChordSymbol chord in chords) { top = WhiteNote.Max(top, chord.Stem.End); } if (top == firstStem.End && top.Dist(lastStem.End) >= 2) { firstStem.End = top; middleStem.End = top.Add(-1); lastStem.End = top.Add(-2); } else if (top == lastStem.End && top.Dist(firstStem.End) >= 2) { firstStem.End = top.Add(-2); middleStem.End = top.Add(-1); lastStem.End = top; } else { firstStem.End = top; middleStem.End = top; lastStem.End = top; } } else { /* Find the bottommost stem. The beam will either: * - Slant upwards (first stem is lowest) * - Slant downwards (last stem is lowest) * - Be straight (middle stem is highest) */ WhiteNote bottom = firstStem.End; foreach (ChordSymbol chord in chords) { bottom = WhiteNote.Min(bottom, chord.Stem.End); } if (bottom == firstStem.End && lastStem.End.Dist(bottom) >= 2) { middleStem.End = bottom.Add(1); lastStem.End = bottom.Add(2); } else if (bottom == lastStem.End && firstStem.End.Dist(bottom) >= 2) { middleStem.End = bottom.Add(1); firstStem.End = bottom.Add(2); } else { firstStem.End = bottom; middleStem.End = bottom; lastStem.End = bottom; } } /* All middle stems have the same end */ for (int i = 1; i < chords.Length - 1; i++) { Stem stem = chords[i].Stem; stem.End = middleStem.End; } }