program piano;

uses crt,time;

type noottype=(bm1,c,ck,d,dk,e,f,fk,g,gk,a,ak,b,c1,c1k,
                  d1,d1k,e1,f1,f1k,g1,g1k,a1,a1k,b1,c2,c2k,
                  d2,d2k,e2,f2,f2k,g2,g2k,a2,a2k,b2,c3,c3k,
                  d3,d3k,e3);
type oktaaftype=-5..5;
type toon=object
             noot:noottype;
             oktaaf:oktaaftype;
             procedure kruis;
             procedure mol;
             function  letter:string;
             end;
procedure toon.kruis;
begin
if noot=b then begin noot:=c; inc(oktaaf) end else inc(noot);
end;

procedure toon.mol;
begin
if noot=c then begin noot:=b; dec(oktaaf) end else dec(noot);
end;

function toon.letter:string;
begin
if noot = bm1 then letter:='b-1';
if noot = a then letter:='a';
if noot = ak then letter:='a#';
if noot = b then letter:='b';
if noot = c then letter:='c';
if noot = ck then letter:='c#';
if noot = d then letter:='d';
if noot = dk then letter:='d#';
if noot = e then letter:='e';
if noot = f then letter:='f';
if noot = fk then letter:='f#';
if noot = g then letter:='g';
if noot = gk then letter:='g#';
if noot = a1 then letter:='a+1';
if noot = a1k then letter:='a+1#';
if noot = b1 then letter:='b+1';
if noot = c1 then letter:='c+1';
if noot = c1k then letter:='c+1#';
if noot = d1 then letter:='d+1';
if noot = d1k then letter:='d+1#';
if noot = e1 then letter:='e+1';
if noot = f1 then letter:='f+1';
if noot = f1k then letter:='f+1#';
if noot = g1 then letter:='g+1';
if noot = g1k then letter:='g+1#';
if noot = c2 then letter:='c+2';
if noot = c2k then letter:='c+2#';
if noot = d2 then letter:='d+2';
if noot = d2k then letter:='d+2#';
if noot = e2 then letter:='e+2';
if noot = f2 then letter:='f+2';
if noot = f2k then letter:='f+2#';
if noot = g2 then letter:='g+2';
if noot = g2k then letter:='g+2#';
if noot = a2 then letter:='a+2';
if noot = a2k then letter:='a+2#';
if noot = b2 then letter:='b+2';
if noot = c3 then letter:='c+3';
if noot = c3k then letter:='c+3#';
if noot = d3 then letter:='d+3';
if noot = d3k then letter:='d+3#';
if noot = e3 then letter:='e+3';
end;



function frequentie(n:toon):integer;
 begin
  frequentie:=round(440 * exp((12*n.oktaaf+ord(n.noot)-10)*ln(2)/12))
 end;

 var aa:char;
     bb:toon;
     speel:boolean;
     defile:text;
     t:integer;
     begintijd:real;

const lengte:integer=500;

begin
{clrscr;}
begintijd:=0;
bb.noot:=c;
bb.oktaaf:=0;
assign(defile,'piano.out');
rewrite(defile);
repeat
repeat aa:=readkey;
until aa<>'';
speel:=true;
case aa of
'\':bb.noot:=bm1;
'z':bb.noot:=c;
's':bb.noot:=ck;
'x':bb.noot:=d;
'd':bb.noot:=dk;
'c':bb.noot:=e;

'v':bb.noot:=f;
'g':bb.noot:=fk;
'b':bb.noot:=g;
'h':bb.noot:=gk;
'n':bb.noot:=a;
'j':bb.noot:=ak;
'm':bb.noot:=b;
',':bb.noot:=c1;
'l':bb.noot:=c1k;
'.':bb.noot:=d1;
';':bb.noot:=d1k;
'/':bb.noot:=e1;

#9 :bb.noot:=f1;
'1':bb.noot:=f1k;
'q':bb.noot:=g1;
'2':bb.noot:=g1k;
'w':bb.noot:=a1;
'3':bb.noot:=a1k;
'e':bb.noot:=b1;
'r':bb.noot:=c2;
'5':bb.noot:=c2k;
't':bb.noot:=d2;
'6':bb.noot:=d2k;
'y':bb.noot:=e2;

'u':bb.noot:=f2;
'8':bb.noot:=f2k;
'i':bb.noot:=g2;
'9':bb.noot:=g2k;
'o':bb.noot:=a2;
'0':bb.noot:=a2k;
'p':bb.noot:=b2;

'[':bb.noot:=c3;
'=':bb.noot:=c3k;
']':bb.noot:=d3;
#8:bb.noot:=d3k;
#13:bb.noot:=e3;

else speel:=false
end;

if speel then begin
               sound(frequentie(bb));
               if begintijd>0 then write(defile,round((gettijd-begintijd)*15),' ');
               begintijd:=gettijd;
               write(defile,bb.letter);
               write(bb.letter,' ');
              end;
{gotoxy(1,1);}
{(',frequentie(bb),' Hz)  ');}
for t:=1 to lengte do if keypressed then t:=lengte;
nosound
until ord(aa)=27;
close(defile)

end.
