font_size 20pt#;
bassdepth#:=140/108pt#;     % depth of bass-clef below baseline = depth of comma in cmr
interline#:=140/36pt#;      % inter line space in staff = depth of descenders in cmr
framingspace#:=9/32interline#; % calculated from the bass-clef
long_axis#=1.5interline#-line_thick#;  % long axis in note-heads ellipse.
notehead_angle=26;          % the angle of rotation of the notehead ellipse.
horizontal_axis#=long_axis#*cosd(notehead_angle);
stemcorrection#=1/10interline#;
define_pixels(long_axis,horizontal_axis,stemcorrection);
short_axis=.65long_axis;
regular_width#=2framingspace#+horizontal_axis#;
define_pixels(bassdepth,interline,bassdepth,framingspace,regular_width);
curve_sidebar=round 10/36pt;
line_thick#=1/10interline#;
special_space#=1/9interline#;   % I used to use line_thick for some spaces, but it does not 
%                                   work when line_thick is changed. So, special_space is 
%                                   equal to the value of line_thick I used then.
staff_h#=3interline#;           % staff height
define_pixels(line_thick,staff_h,special_space)
museangle=75;                % angle for musecircles
stem_heigth=3.5; 
% Font-dimens:
font_normal_space:=0pt#;
font_normal_stretch:=100pt#;
font_normal_shrink:=0pt#;
font_quad:=horizontal_axis#;
font_x_height:=interline#;
font_extra_space:=0pt#;
% Width of different types of character
barline_sep:=1.5horizontal_axis;
numeric widths[];
def musecircle(suffix i,j,k,l) =    % center, edge, suffixes for new points. k=+, l=-
    numeric museradius, anglefrom;
    museradius=abs(length (z.i-z.j));
    anglefrom=angle (z.j-z.i);
    z.k-z.i=museradius*dir(anglefrom+museangle);
    z.l-z.i=museradius*dir(anglefrom-museangle);
    fill fullcircle scaled 2museradius shifted z.i
    enddef;
def note_head =
    fullcircle xscaled long_axis yscaled short_axis rotated notehead_angle
    enddef;
small_rests_radius:=1/2(interline-3line_thick);
def put_rest_unit(expr base, width) =
    if known x.circlecenter:
    else:
        x.circlecenter=x.circlebottom=-small_rests_radius; 
        y.circlecenter=y.circlebottom+small_rests_radius=y.restpoint-line_thick=1.5interline;
        x.restpoint-small_rests_radius=x.restbottom=0; 
        y.restbottom:=1/2line_thick;
        numeric rests_angle; 
        rests_angle=angle(z.restpoint-z.restbottom);
        penpos.restpoint(small_rests_radius,rests_angle);  
        z.circlebottom.l=z.circlebottom;
        z.circlebottom.r=z.circlepoint;
        z.restright.l=z.restpoint.l; z.restright.r=z.restpoint.r shifted(1/2line_thick,0);
        penpos.restbottom(width,0); 
    fi
    musecircle(circlecenter,circlebottom,circlepoint,secondcirclepoint) shifted base;
    penstroke (z.circlebottom.e{right}..z.restpoint.e--z.restright.e--z.restbottom.e)%
        shifted base;
    enddef;
def regular_note_head =
    note_axis[index] := max(note_axis[index],1/2regular_width);
    width[index] := max(width[index],1/2regular_width);
    stem_point[index][instr_] := (stem_dir[index][instr_]*(xpart(directionpoint up of (note_head))-.5line_thick),
        stem_dir[index][instr_]*ypart(directionpoint up of (note_head)));
    fill note_head
    enddef;
picture half_notehead;
begingroup; save x, y;
    clearit;
    path p[]; 
    p1=fullcircle xscaled long_axis yscaled .7long_axis;
    numeric t[];
    forsuffixes s=1,2,3,4:
        z.s=directionpoint dir(90+s*90) of p1;
    endfor;
    z5=z1+7/24short_axis*down; z6=z2+line_thick*right; 
    z7=z3+7/24short_axis*up; z8=z4+line_thick*left;
    p2=(superellipse(z8,z5,z6,z7,.8)) rotated notehead_angle;
    fill p1 rotated notehead_angle;
    unfill p2; 
    cullit;
half_notehead := currentpicture; clearit;
endgroup;

def half_note_head =
    note_axis[index] := max(note_axis[index],1/2regular_width);
    width[index] := max(width[index],1/2regular_width);
    stem_point[index][instr_] := (stem_dir[index][instr_]*(xpart(directionpoint up of (note_head))-.5line_thick),
        stem_dir[index][instr_]*ypart(directionpoint up of (note_head)));
    addto currentpicture also half_notehead
    enddef;
    
vardef whole_test(expr scale) = 
    ypart(directionpoint left of p)-%
    ypart(directionpoint left of (p scaled scale rotated (90+notehead_angle)))%
    <line_thick enddef;

picture whole_notehead;
    path p;
    p=fullcircle xscaled (1.5interline+line_thick) yscaled (interline+1/2line_thick);
    numeric true_scale;
    true_scale=solve whole_test(1,0);
    fill p; unfill p scaled true_scale rotated (90+notehead_angle);
    whole_notehead=currentpicture; clearit; 

def whole_note = 
    note_axis[index] := max(note_axis[index],1.5interline+1/2line_thick);
    width[index] := max(note_axis[index],1.5interline+1/2line_thick);
    stem_point[index][instr_] := (stem_dir[index][instr_]*1.5interline,
        stem_dir[index][instr_]*.5interline);
    addto currentpicture also whole_notehead
    enddef;    

picture rest_[];
    pickup penrazor xscaled line_thick rotated 90;
    draw (-1/regular_width, 2interline)--(1/2regular_width, 2interline);
    pickup penrazor xscaled 1/2interline rotated 90;
    draw (-1/horizontal_axis, 1.75interline)--(1/2horizontal_axis, 1.75interline);
rest_[-2]:=currentpicture; clearit; rest_width[-2]:= 1/2regular_width + framingspace;
    draw (-1/2horizontal_axis, 1.25interline)--(1/2horizontal_axis, 1.25interline);
    pickup penrazor xscaled line_thick rotated 90;
    draw (-1/2regular_width, interline)--(1/2regular_width, interline);
rest_[-1]:=currentpicture; clearit; rest_width[-1]:= 1/2regular_width + framingspace;
    z1=(.65short_axis,line_thick);
    z2=(x1,2interline-.5line_thick);
    z3=(0,interline-.5line_thick);
    path p[];
    p1=z2{z3-z2}..{down}z3..{z1-z3}z1;
    z4=point .2 of p1;
    y5=interline+.5line_thick;
    vardef tooleft(expr x) =
        length((x,y5)-z3)>.5interline enddef;
    x5=solve tooleft(-short_axis,short_axis);
    z6-z4=z5-z3;
    x7r-x5=x5+.75short_axis;
    z7r-z2=whatever*(z5-z1);
    penpos7(line_thick,90+angle(z7r-z4));
    penpos1(-1.5line_thick,90+angle(z1-z5));
    fill z7l--z4{z3-z2}..{down}z3..{z1l-z3}z1l--z1r---z5{z6-z5}..{up}z6..{z7r-z6}z7r--cycle;
    z8=(-line_thick/2,-.5interline);
    penpos8(line_thick,90+angle(z1-z5));
    z9l=(-.65short_axis,y1);
    penpos9(-.4interline,angle(z5-z3));
    penstroke z1e{z5-z1l}..z9e{down}..{z1l-z5}z8e;
    cullit;
rest_[0]:=currentpicture; clearit; rest_width[0] := 1/2regular_width + framingspace;
begingroup; save x,y;
    z0=(3/2small_rests_radius,0); 
    put_rest_unit(z0, line_thick);
rest_[1] := currentpicture; clearit; rest_width[1] := 3/2small_rests_radius + framingspace;
endgroup;
begingroup; save x,y;
    x0-1/2(5small_rests_radius+framingspace)=1/2(5small_rests_radius+framingspace)-x1=%
        1/2cosd(rests_angle)/sind(rests_angle)*interline;
    y0=0; y1=-interline; 
    put_rest_unit(z0, 1/3line_thick);
    put_rest_unit(z1, line_thick);
rest_[2] := currentpicture shifted(-2small_rests_radius,0); 
clearit; rest_width[2] := 2small_rests_radius + 2framingspace;
endgroup;
%transform u;
%xpart u=0;
%xxpart u=1;
%xypart u=0;
%ypart u=0;
%yxpart u=sind(17)/cosd(17);
%yypart u=1;    
%vardef higher(expr y) =
%    ypart((bot (short_axis-line_thick,y)) transformed u)>interline enddef;
picture sharp;
begingroup;
    save x, y;
    transform t;
    (0, .5interline) transformed t = (0,.5interline);
    (0, 1) transformed t = (0, 1);
    (horizontal_axis, .5interline) transformed t = (horizontal_axis, .8interline);
    pickup penrazor xscaled .3interline rotated 90;
    z1 = (-1/2horizontal_axis, .5interline); z2 = (1/2horizontal_axis, .5interline);
    draw (z1--z2) transformed t;
    pickup penrazor xscaled line_thick;
    ypart(z3 transformed t) = 1.5interline;
    x3=x2-.35interline;
    draw (z3--(x3, -y3)) transformed t;
    addto currentpicture also currentpicture rotatedabout ((0,0),180);
endgroup;
sharp := (currentpicture shifted (-1/2horizontal_axis,0));
clearit;
picture natural;
begingroup;
    save x, y;
    transform t;
    (0, .5interline) transformed t = (0,.5interline);
    (0, 1) transformed t = (0, 1);
    (horizontal_axis, .5interline) transformed t = (horizontal_axis, .8interline);
    z1 = (-1/2horizontal_axis, .5interline); z2 = (1/2horizontal_axis, .5interline);
%    draw (z1--z2) transformed t;
    ypart(z3 transformed t) = 1.5interline;
    x3 = x2-.35interline;
    pickup penrazor xscaled .3interline rotated 90;
    z4 = (z1--z2) intersectionpoint (z3--(x3,-y3));
    draw ((-x4, y4)--(x4+1/2line_thick, y4)) transformed t;
    pickup penrazor xscaled line_thick;
    draw (z4--(x3, -y3)) transformed t;
    addto currentpicture also currentpicture rotatedabout ((0,0),180);
endgroup;
natural := (currentpicture shifted (.15interline-1/2horizontal_axis,0));
clearit;
picture flat;
begingroup;
    save x, y;
    numeric scratch_angle; scratch_angle=25;
    penpos1(line_thick,-90);
    x1=1/2framingspace+line_thick; y1=-1/2line_thick;
    penpos2(6/14interline,1/2scratch_angle);
    y2r-y1r=1/2interline+sind(scratch_angle)/cosd(scratch_angle)*(long_axis-line_thick-2framingspace);
    x2r=long_axis-line_thick-1/2framingspace;
    penpos3(line_thick,90+scratch_angle);
    x3=1/2[x2l,x1]; y3=interline+1/2sind(scratch_angle)/cosd(scratch_angle)*(x3-x1);
    penpos4(2/7interline,90);
    x4=x1; 
    y4r=interline-1/2sind(scratch_angle)/cosd(scratch_angle)*(x3-x1);
    penstroke z1e{dir scratch_angle}..z2e{up}..z3e{dir (180+scratch_angle)}..z4e;
    pickup penrazor scaled line_thick;
    draw (x1-1/2line_thick, y1r)--(x1-1/2line_thick,2interline+line_thick);
endgroup;
flat := (currentpicture shifted (-horizontal_axis,-1/2interline));
clearit;

picture treble_clef; 
picture small_treble_clef;
clearit;
begingroup
save x,y,u;
    u:=1/10staff_h; % bottom circle radius
    numeric edgeshift, scratchangle, scratchdistance;
    edgeshift:=1/4interline;
    path p[];
    z10l=(framingspace,edgeshift);
    penpos10(2line_thick,0);
    y1=-2interline; 
    z2-z1=1.2u*dir-135;
    trebleaxis = x1+u;
    z3=(trebleaxis,-2.5interline); z4l=(x1+3u,y1);
    z5l=(trebleaxis,2.5interline);
    z17=(x10r+2u+interline,line_thick); % treble center
    x17-x5l=x4l-x17; 
    z4r-z4l=whatever*(z17-z4l) rotated 90; 
    penpos4(line_thick,angle(z17-z4l)+90);
    z5r-z5l=z4r-z4l;
    penpos3(line_thick,90);
    musecircle(1,2,15,16);
    z2l=z2; z2r=z15;
    penstroke z2e..{right}z3e..{up}z4e{z17-z4l}..z5e; 
    scratchangle=-45;
    z8l=z5r; penpos8(1/2interline,scratchangle);
    z9l=(1/2[x5r,x10],2interline); penpos9(1/2interline,scratchangle);
    z11=(trebleaxis,-interline); penpos11(line_thick,90);
    z13l=(x17,interline+line_thick); penpos13(1/2interline,-90);
    penstroke z8e..{z9-z8}z9e..z10e..z11e{right}; 
    p1=(x11,y11-1/2special_space){right}..{left}z13l; p2=fullcircle rotated -90 scaled 2interline shifted z17;
    pickup pencircle scaled .1pt;
    numeric t,v;
    (t,v)=p1 intersectiontimes p2; 
    z12=p1 intersectionpoint p2; penpos12(line_thick,angle direction v of p2 + 90);
    penstroke z11e{right}..z12e..{left}z13e; 
% point 14, the end of the central ball, is as far from 17 as it is from the curve downthere.
% And that distance should be the one called scratchdistance.
    z18=point 1/3 of (z9r{z9-z8}..{down}z10r); % to get the distance between the ball and the stem
    scratchdistance=length(z18-z17)-interline;
    p3=fullcircle scaled 4scratchdistance shifted z17; 
    p4=z10r{down}..z11r{right};
    z19=p3 intersectionpoint p4; z14=1/2[z19,z17];
    treblewidth = x14;
    numeric t,v; (t,v)=p3 intersectiontimes p4; 
    penpos14(.5line_thick, angle direction v of p4+90);
    penstroke z13e{left}..{direction v of p4}z14e;
%   crown
    z6l=(xpart(p1 intersectionpoint (z4l--z5l)),4interline); penpos6(1/2interline,95);
    pair interpoint;
    interpoint=(whatever,3.1interline);
    interpoint-z5r=whatever*(z5l-z4l);
    fill z5l{z5l-z4l}..{right}z6l--z6r{z8l-z5l}..{down}interpoint..{z4l-z5l}z5r--cycle;
    x7l-x17=x17-x5r; y7l=3.6interline; penpos7(2special_space,-30);
    fill z6l{right}..{down}z7l..tension1.6..z8l--z8r..{up}z7r..{curl2}z6r--cycle;
endgroup;
treble_clef:=currentpicture; clearit;
begingroup
save x,y,u;
    u:=1/10staff_h; % bottom circle radius
    numeric edgeshift, scratchangle, scratchdistance;
    edgeshift:=0/4interline;
    path p[];
    z10l=(1framingspace,edgeshift);
    penpos10(1.7line_thick,0);
    y1=-1.5interline; 
    z2-z1=u*dir-155;
    trebleaxis:=x1+.85u;
    z3=(trebleaxis,-2interline); z4l=(x1+2.5u,y1);
    z5l=(trebleaxis,2.2interline);
    z17=(x10r+1.2u+interline,0); % treble center
    x17-x5l=x4l-x17; 
    z4r-z4l=whatever*(z17-z4l) rotated 90; 
    penpos4(line_thick,angle(z17-z4l)+90);
    z5r-z5l=z4r-z4l;
    penpos3(line_thick,90);
    musecircle(1,2,15,16);
    z2l=z2; z2r=z15;
    penstroke z2e..{right}z3e..{up}z4e{z17-z4l}..z5e; 
    scratchangle=-45;
    z8l=z5r; penpos8(2/5interline,scratchangle);
    z9l=(1/2[x5r,x10],1.7interline); penpos9(2/5interline,scratchangle);
    z11=(trebleaxis,-.85interline); penpos11(line_thick,90);
    z13l=(x17,.85interline); penpos13(2/5interline,-90);
    penstroke z8e..{z9-z8}z9e..z10e..z11e{right}; 
    p1=z11l{right}..{left}z13l; p2=fullcircle rotated -90 scaled 1.8interline shifted z17;
    x12 =  x17; y12=y11; penpos12(line_thick, 90);
    penstroke z11e..z12e{right}..{left}z13e; 
    z18=point 1/3 of (z9r{z9-z8}..{down}z10r); % to get the distance between the ball and the stem
    scratchdistance=length(z18-z17)-.8interline;
    p3=fullcircle scaled 4scratchdistance shifted z17; 
    p4=z10r{down}..z11r{right};
    z19=p3 intersectionpoint p4; z14=1/2[z19,z17];
    numeric t,v; (t,v)=p3 intersectiontimes p4; 
    penpos14(.5line_thick, angle direction v of p4+90);
    penstroke z13e{left}..{direction v of p4}z14e;
    z6l=(xpart(p1 intersectionpoint (z4l--z5l)),3.5interline); penpos6(2/5interline,95);
    pair interpoint;
    interpoint=(whatever,2.8interline);
    interpoint-z5r=whatever*(z5l-z4l);
    fill z5l{z5l-z4l}..{right}z6l--z6r{z8l-z5l}..{down}interpoint..{z4l-z5l}z5r--cycle;
    x7l-x17=x17-x5r; y7l=3.2interline; penpos7(1.7special_space,-30);
    fill z6l{right}..{down}z7l..tension1.6..z8l--z8r..{up}z7r..{curl2}z6r--cycle;
endgroup;
small_treble_clef:=currentpicture; clearit;
picture bass_clef, small_bass_clef;
begingroup
save x, y, u;
    u=1/16staff_h;
    y1=y4r=2interline;
    x1=3u; z2-z1=2u*dir190; 
    musecircle(1,2,7,6);
    scratchradius=x1-x2;
    bassaxis=scratchradius/2;
    penpos3(line_thick,-100);
    penpos4(1.4scratchradius,170);
    x5=0; y5=-bassdepth;
    x4r=x1+3.1scratchradius;
    y3l=staff_h; x3r=1/2[x4r,x1];
    basswidth = x4l - bassaxis;
    pickup pencircle scaled .1pt;
    fill z2{dir100}..{right}z3l..{down}z4l..{dir-170}z5%
        --(x5,y5+1){dir10}..{up}z4r..{left}z3r..{down}z6..cycle;
    fill fullcircle scaled scratchradius %
        shifted (staff_h-framingspace-.5scratchradius, 2.5interline);
    fill fullcircle scaled scratchradius %
        shifted (staff_h-framingspace-.5scratchradius, 1.5interline);
endgroup;
bass_clef:=currentpicture; clearit;
begingroup
save x, y, u;
    u:=1/6interline;
    y1=y4r=2interline;
    x1=3u; z2-z1=2u*dir190; 
    musecircle(1,2,7,6);
    scratchradius:=x1-x2;
    penpos3(line_thick,-100);
    penpos4(1.4scratchradius,170);
    x5=0; y5=0;
    x4r=x1+3.1scratchradius;
    y3l=2.9interline; x3r=1/2[x4r,x1];
    pickup pencircle scaled .1pt;
    fill z2{dir100}..{right}z3l..{down}z4l..{dir-170}z5%
        --(x5,y5+1){dir10}..{up}z4r..{left}z3r..{down}z6..cycle;
    fill fullcircle scaled scratchradius %
        shifted (2.75interline-framingspace-.5scratchradius, 2.5interline);
    fill fullcircle scaled scratchradius %
        shifted (2.75interline-framingspace-.5scratchradius, 1.5interline);
endgroup;
small_bass_clef:=currentpicture; clearit;
def universal_flag(expr flag_height,stemmed_length,extreme_shift_ratio,collision_angle) =
    path q[]; 
    q1=note_head;
    numeric flag_time;
    pair flag_top, flag_extreme, flag_bottom, flag_middle;
    pair extremel, extremer;
    flag_extreme=(extreme_shift_ratio*short_axis)*dir(notehead_angle+90);
    flag_top=(xpart(directionpoint up of q1)-1/2line_thick,ypart(flag_extreme)+flag_height);
    flag_extreme=1/2[extremel,extremer];
    extremer=extremel+(.5special_space,0) rotated notehead_angle;
    flag_top-flag_bottom=(0,stemmed_length-1/2line_thick);
    q2=extremer{dir(90+notehead_angle)}..%
        {dir(collision_angle)}flag_bottom-(1/2line_thick,0)--flag_bottom-(7/16line_thick,0);
    flag_time=directiontime dir(90-notehead_angle) of q2;
    flag_middle-directionpoint dir(90-notehead_angle) of q2= %
        line_thick*dir(180-notehead_angle);
    q3=extremel{dir(90+notehead_angle)}..{dir(90-notehead_angle)}flag_middle%
        ..{dir(88)}flag_top-(1/2line_thick,0)--flag_top-(7/16line_thick,0);
    fill (q2--reverse q3--cycle)
enddef;
def eighth_flag(expr notehead_center) =
    universal_flag(3interline-line_thick,interline,1/2,10) shifted(notehead_center)
enddef;