views:

222

answers:

4

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.

Thanks.

+3  A: 

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
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
+4  A: 

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

JeremyDWill
+3  A: 

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
# 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
I think you have way over-simplified what is needed. Even at the basic level, we need dynamics, slurs, etc.
Brad
@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
Of course! I need more coffee. Sorry, +1 for you!
Brad
+5  A: 

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
+1 Nice find on the symbols
Paul Sasik