PROGRAM harmonica;
const
chromatic =1; diatonic=0;







global
freq[100];
shift;
gamestate;
monica_id;
struct hole_data[20]
note[3];
note_id;
end


    hole_id;
BEGIN


    set_mode(600800);
    make_notes();
    monica_id=make_harmonica(100,740,30,50,10,chromatic,0);
    mouth();
    gamestate=0;


    signal(type hole,s_sleep);
    signal(type mouth,s_sleep);
    delete_text(all_text);
    delete_draw(all_drawing);

    LOOP

                if(key(_0))
                gamestate=0;

                end

                if(key(_1))
                gamestate=1;
                end
                if(key(_2))
                gamestate=2;
                end

        switch(gamestate)

            case 0:
                write(0,100,100,0,"Harmonica Hero Simulator");
                write(0,100,120,0,"Press <1> for free play");
                write(0,100,140,0,"Press <2> to compose");
            end
            case 1:
                delete_text(all_text);
                signal(type hole,s_wakeup);
                signal(type mouth,s_wakeup);
                gamestate=10;
            end

            case 10:

            end
            case 2:

            end



            default:
            end
        end

        FRAME;
    END
END

process make_harmonica(x,y,sx,sy,holes, model,n0)
private
i; j;
begin
for(i=0; i<holes;i+=1);
hole(i,x+i*(sx+2),y,sx,sy);
end

loop
frame;
end
end


process hole(index,x,y,sx,sy)
private
label;
dr_label;
i;
begin
z=index;

for(i=0; i<=3; i+=1)

    hole_data[z].note[i]=freq[z*4+i];
end

graph=new_map(sx,sy,0,0,(index+1)*16-8);
label=index-1;
    if(label>0)
        dr_label=write_int(0,x+sx/2,y-20,1,&label);
    end

loop

frame;
end
end



process mouth()
private
    volume[1]=128,128;
    frequency[1]=256,256;
    id_sound;
    playing[1];
    breath100;
    breath;
    p;
    hole1;
begin
    graph=new_map(13,7,6,3,31);
    z=-20;
    id_sound = load_sound("note512.wav", 1);
    for(p=0;p<=1;p+=1)
     playing[p] = sound(id_sound, volume, frequency);
   end
loop



    x=mouse.x;
    y=mouse.y;
    shift=key(_space);
    IF (mouse.left xor mouse.right)
        hole1=0;
        for(p=0;p<=1;p+=1)
            hole_id=collision(type hole);
            if(hole_id<>0 and hole_id<>hole1)
                hole1=hole_id;

                volume[p]=20+y-hole_id.y;
                if(abs(breath)>90)
                    if(abs(breath)>100)
                        volume[p]=0;
                    else
                        volume[p]-=(abs(breath)-90)*10;
                    end
                end

                if(mouse.left)
                    frequency[p]=hole_data[hole_id.z].note[0+shift];
                else
                    frequency[p]=hole_data[hole_id.z].note[2+shift];
                end

            else
                volume[p]=0;
            end
        end
        breath100+=(mouse.right-mouse.left)*volume*2;
    else
        volume=0;
        volume[1]=0;

        breath100=near_angle(breath100,0,500);

    end

        change_sound(playing,volume,frequency);
        change_sound(playing[1],volume[1],frequency[1]);

        breath=breath100/100;
        breathmeter(monica_id.x+400,monica_id.y,breath);
        shift_bar(monica_id.x+360,monica_id.y,shift);




frame;
end
end



process shift_bar(x,y,shift)
begin
if(shift)
    graph=new_map(50,20,35,0,15);
else
    graph=new_map(50,20,15,0,15);
end
frame;
unload_map(graph);
end



process breathmeter(x,y,breath);
begin
if(breath>0)
    graph=new_map(20,abs(breath),0,breath-1,15);
else
    if(breath<0)
        graph=new_map(20,abs(breath),0,0,15);
    end
end
frame;
unload_map(graph);
end



function max(a,b)
begin
if(a>b)
return (a);
else
return (b);
 end
end

function min(a,b)
begin
if(a<b)
return (a);
else
return (b);
 end
end






function make_notes()

begin
for(x=0; x<40; x+=1)
freq[x]=32+16*x;
end
freq[0]=199;
freq[1]=210;
freq[2]=220;
freq[3]=235;

freq[4]=264;
freq[5]=280;
freq[6]=249;
freq[7]=264;

freq[8]=264;
freq[9]=280;
freq[10]=296;
freq[11]=314;

freq[12]=333;
freq[13]=352;
freq[14]=352;
freq[15]=373;

freq[16]=395;
freq[17]=419;
freq[18]=440;
freq[19]=470;

freq[20]=528;
freq[21]=559;
freq[22]=498;
freq[23]=528;

freq[24]=528;
freq[25]=559;
freq[26]=593;
freq[27]=628;

freq[28]=665;
freq[29]=703;
freq[30]=703;
freq[31]=747;

freq[32]=791;
freq[33]=838;
freq[34]=880;
freq[35]=941;

freq[36]=1056;
freq[37]=1218;
freq[38]=997;
freq[39]=1056;


for(x=0; x<40; x+=1)
freq[x]/=2;
end


end


