25

I am looking to draw a music staff on a .NET (C#) form. I am using Microsoft Visual C# 2010 Express. I was wondering if anyone knew of existing code or existing free .NET libraries that can help with that. I am looking at drawing both the treble and bass clef staff and adding one quarter note to some where in the staff. I am making a Piano Tester app using C# for my son. If I code it myself, I will proably just override the onPaint method. But I thought I would see if anyone has seen some free code or library available to get me started.

Dale K
  • 25,246
  • 15
  • 42
  • 71
JeffJak
  • 2,008
  • 5
  • 28
  • 40

6 Answers6

14

There are the required primitives to generate musical output in the Unicode code set (starting at U+1D100). For example, U+1D11A is a 5-line staff, U+1D158 is a closed notehead.

See http://www.unicode.org/charts/PDF/U1D100.pdf

..then the issue becomes making sure that you have a typeface with the appropriate glyphs included (and dealing with the issues of spacing things correctly, etc.)

IF you're looking to generate printed output, you should look at Lilypond, which is an OSS music notation package that uses a text file format to define the musical content and then generates gorgeous output.

bgporter
  • 35,114
  • 8
  • 59
  • 65
  • 1
    I like this solution. I installed the Symbola font from this site.http://www.fileformat.info/info/unicode/char/1d11e/fontsupport.htm. But I don't know how to draw the symbol. I have tried: Font f = new Font("Symbola", 72); g.DrawString("U+1D11A", f, _noteBrush, 50, 4); I have also tried "\u1D11A". Any ideas on what the code would look like? – JeffJak Nov 03 '10 at 19:00
  • 3
    Never mind...Just figured it out..finally. The above site had a link to this http://www.fileformat.info/info/unicode/char/1d11a/index.htm which showed that this would be the code: g.DrawString("\uD834\uDD1A", f, _noteBrush, 50, 4); – JeffJak Nov 03 '10 at 19:10
  • one big plus, for Lilypond – Saeed Neamati Dec 03 '16 at 15:23
5

You might look at a music editing program written in C# a few years ago. I looks somewhat promising: Music Editing Program

JeremyDWill
  • 3,132
  • 21
  • 18
3

This will be a difficult project. Finale uses a custom font for notes and other symbols. That might be an efficient way to get you started.

You might also check out Niffty. It is open source and written in Java. You could probably translate the important parts over, or borrow concepts.

Edit: This may also be useful: http://www.c-sharpcorner.com/UploadFile/mgold/musicmaker09242005015433AM/musicmaker.aspx

Brad
  • 159,648
  • 54
  • 349
  • 530
  • You might want to note that Niffty is licensed under the GPL. So I'm not sure about the `translate the important parts over` part if he has f.e. a closed-source application. – Bobby Oct 29 '10 at 14:21
  • If he is making this software for his son then he is probably not distributing the executable. In which case, he doesn't need to publish the code, either. – Eyal Jul 31 '11 at 13:43
3

If you think about it, the number of primitives needed to draw music notation is fairly small, especially if you don't get too fancy. All you basically need is:

  • Vertical lines (note stems)
  • Horizontal lines (staff lines)
  • Ovals full and outlined (representing notes)
  • Sharp and Flats are already provided for you with # and b

There are some fancier symbols that i omitted such as treble, bass clef marks but those you could cheese with T and B or find a fancier font that might work.

Very simple, sample code to get you started:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

public partial class MusicForm : Form
{
    public MusicForm()
    {
        InitializeComponent();
    }

    private int _staffHght = 15;
    private int _noteHght = 12;
    private int _noteWdth = 20;
    private Pen _notePen = new Pen(Color.Black, 2);
    private Brush _noteBrush = Brushes.Black;

    private void musicPanel_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.SmoothingMode = SmoothingMode.HighQuality;

        // draw some staff lines
        for (int i = 1; i < 6; i++)
            g.DrawLine(Pens.Black, 0, i * _staffHght, musicPanel.Width, i * _staffHght);

        // draw four semi-random full and quarter notes
        g.DrawEllipse(_notePen, 10, 2 * _staffHght, _noteWdth, _noteHght);
        g.DrawEllipse(_notePen, 50, 4 * _staffHght, _noteWdth, _noteHght);

        g.FillEllipse(_noteBrush, 100, 2 * _staffHght, _noteWdth, _noteHght);
        g.FillEllipse(_noteBrush, 150, 4 * _staffHght, _noteWdth, _noteHght);
    }
}

This paint function would of course have to be much more dynamic: full of loops on your collections of primitives...

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • 1
    # and b aren't actually the sharp and flat symbols. But they are available in Unicode fonts. Even Tahoma has them (sharp is U+266F and flat is U+266D). – siride Oct 29 '10 at 14:54
  • I think you have way over-simplified what is needed. Even at the basic level, we need dynamics, slurs, etc. – Brad Oct 29 '10 at 15:00
  • @Brad: I have definitely oversimplified. The sample code has only six or seven lines that do anything. And dynamics and slurs, if actually needed, would simply be two more primitives that need to be added to the mix. My notes and code sample are literally the starting point for a programmer to begin thinking about how an application like this might come together. – Paul Sasik Oct 29 '10 at 16:05
0

A previous commentator mentioned porting java's Niffty tool to c# for this purpose.

Turns out it's already been done, works nicely.

0

If you like to use the Unicode Musical Symbols as descibed in the 1D100.pdf in a custom C# application. The following will get you started:

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84