     with ConsAKD; use ConsAKD;
     with CursAKD;
     with InteAKD; use InteAKD;
     with FloaAKD; use FloaAKD;
     with MathAKD; use MathAKD;
     with SounAKD; use SounAKD;
     with TextAKD; use TextAKD;
     with TTY    ;


     package body HyperMus is
     ----------------------------------------------------------------------
     ----------------------------------------------------------------------

     procedure Music_Display (
       Notes       : in     Notes_Type;
       Note        : in     positive;
       Silent_Char : in     character;
       Opt         : in     Options_Type ) is
     ----------------------------------------------------------------------
       Last_Fired : integer := -1;
     begin
       for Neuron in reverse Notes'range loop
--       CursAKD.Move ( 14 + Neuron - 1, Note - 1 );
--       if Opt.Music ( Neuron ) ( Note ) = ' ' then
--         Put ( Silent_Char );
--       else
--         Put ( Opt.Music ( Neuron ) ( Note ) );
--       end if;
	 CursAKD.Move ( 24 - Notes'last + Neuron - 1, Note - 1 );
	 if Notes ( Neuron ) = ' ' then
	   Put ( Silent_Char );
	 else
	   Put ( Notes ( Neuron ) );
	   if ( Notes ( Neuron ) = '!' )
	     or ( Notes ( Neuron ) = '|' ) then
	       Last_Fired := Neuron;
	   end if;
	 end if;
       end loop;
       if Last_Fired >= 0 then
	 Spike_Sound ( Last_Fired, Opt.Tones_On );
       else
	 Speaker_Tone_Toggle ( false );
       end if;
     end Music_Display;

     procedure Music_Load (
       Music :    out Music_Type ) is
     ----------------------------------------------------------------------
       Music_File : File_Type;
       Temp       : string ( 1..( Song_Size + 1 ) );
       Last       : natural;
     begin
       Music := ( others => ( others => ' ' ) );
       Open ( Music_File, In_File, "Hypernet.Mus" );
       for Neuron in Music'range loop
	 exit when End_Of_File ( Music_File );
	 Get_Line ( Music_File, Temp, Last );
	 if Last > Song_Size then
	   Last := Song_Size;
	 end if;
	 Music ( Neuron ) ( 1..Last ) := Temp ( 1..Last );
       end loop;
       Close ( Music_File );
     end Music_Load;

     procedure Music_Set (
       Music : in out Music_Type ) is
     ----------------------------------------------------------------------
     begin
       Clear_Screen;
       for Neuron in Music'range loop
	 for Note in 1..Song_Size loop
	   Put ( Music ( Neuron ) ( Note ) );
	 end loop;
	 New_Line;
       end loop;
       New_Line;
       if Ask_Bool ( "Load the music file? ", false ) then
	 Music_Load ( Music );
       elsif Ask_Bool ( "Modify the music? ", false ) then
	 for Neuron in Music'range loop
	   Ask ( Music ( Neuron ), "Neuron" & integer'image ( Neuron )
	     & " ", Music ( Neuron ) );
	 end loop;
       end if;
     end Music_Set;

     procedure Spike_Sound (
       Neuron   : in     natural;
       Tones_On : in     boolean := Tones_On_Default ) is
     ----------------------------------------------------------------------
       Half_Step : constant float := 1.059463094;
       Freq_Base : constant float := 330.0; -- Hz
     begin
       if not Tones_On then
	 Speaker_Thump;
       else
	 Speaker_Tone_Toggle ( true );
	 Speaker_Tone_Set ( integer (
	   Freq_Base / ( Half_Step ** Neuron ) ) );
       end if;
     end Spike_Sound;

     procedure Voltage_Clamp (
       Quit     :    out boolean;
       Refresh  :    out boolean;
       Notes    : in out Notes_Type;
       Axons    : in out Axons_Type;
       Neurons  : in out Neurons_Type;
       Opt      : in     Options_Type ) is
     ----------------------------------------------------------------------
       Key : character;
       Neuron : integer;
     begin
       Quit := false;
       Refresh := false;
       while TTY.Char_Ready loop
	 Key := TTY.Get ( No_Echo => true );
	 if Key = ASCII.CR then
	   Quit := true;
	 elsif Key in '0'..'9' then
	   Neuron := character'pos ( Key ) - 48;
	   if Neuron <= Opt.N_Last then
	     Neurons ( Neuron ).Vm := Opt.Spike;
	     Notes ( Neuron ) := '!';
	   end if;
	 else
	   Refresh := true;
	 end if;
       end loop;
     end Voltage_Clamp;

     ----------------------------------------------------------------------
     ----------------------------------------------------------------------
     end HyperMus;
