Jesse Farmer <[log in to unmask]> writes:
> can someone spot the error in the code here?
> when you compile and run this you will notice that the time for the eighth
> note is way off. Any ideas as to why this is?
> We suspect some kind of decimal precision error but not positive about that.
> Any help would be greatly appreciated.
The problem is you are not resetting Next_Time after doing file IO, so
you are effectively timing the file IO.
Also, as a matter of style, any time you repeat the same code
structure, you should use a subprogram instead. And use Ada.Text_IO,
not Gnat.IO.
With these changes, I get as output:
Time Difference: 1.722298023 For one whole note
Time Difference: 1.721935966 For 2 Half note
Time Difference: 1.722189909 For 4 Quarter note Delay
Time Difference: 1.722186557 For 8 8th note Delay
Time Difference: 1.722149680 For 16 16th note Delay
Time Difference: 1.722237681 For 32 32th note Delay
Time Difference: 6.859441393 For 64 64th note Delay
Time Difference: 13.719359662 For 128 128th note Delay
On a Pentium Windows NT 4.0.
64th and 128th notes are too fine for this clock, as you noted in your
code.
Here's the new code:
with Ada.Real_Time;
with Ada.Text_IO;
procedure BPMPPQTest2 is
-- ----------------
-- WORK VARIABLES
-- ----------------
Time_Start : Ada.Real_Time.Time;
Time_End : Ada.Real_Time.Time;
Time_Difference : Ada.Real_Time.Time_Span;
Time_Delta : Ada.Real_Time.Time_Span;
Delta_Whole_Note : Ada.Real_Time.Time_Span;
Delta_Half_Note : Ada.Real_Time.Time_Span;
Delta_Quarter_Note : Ada.Real_Time.Time_Span;
Delta_Eighth_Note : Ada.Real_Time.Time_Span;
Delta_Sixteenth_Note : Ada.Real_Time.Time_Span;
Delta_ThirtySecond_Note : Ada.Real_Time.Time_Span;
Delta_SixtyFourth_Note : Ada.Real_Time.Time_Span;
Delta_128th_Note : Ada.Real_Time.Time_Span;
Time_Duration : Duration;
Time_Delta_Duration : Duration;
Next_Time : Ada.Real_Time.Time;
-- -----------
-- CONSTANTS
-- -----------
Ticks_Per_Minute : constant := 17_400;
Number_Of_Ticks : constant := 60.0;
Seconds_Per_Minute : constant := 60.0;
Pulse_Per_Quarter : constant := 480;
Beats_Per_Minutes : constant := 140.0;
-- ---------------------
-- FORMULAIC CONSTANTS
-- ---------------------
Whole_Note : constant := Pulse_Per_Quarter * 4;
Half_Note : constant := Pulse_Per_Quarter * 2;
Quarter_Note : constant := Pulse_Per_Quarter;
Eighth_Note : constant := Pulse_Per_Quarter / 2;
Sixteenth_Note : constant := Pulse_Per_Quarter / 4;
ThirtySecond_Note : constant := Pulse_Per_Quarter / 8;
SixtyFourth_Note : constant := Pulse_Per_Quarter / 16;
HundredTwentyEighth_Note : constant := Pulse_Per_Quarter / 32;
use type Ada.Real_Time.Time;
procedure Test
(Count : in Natural;
Note : in Ada.Real_Time.Time_Span;
Label : in String)
is
begin
Time_Start := Ada.Real_Time.Clock;
Next_Time := Time_Start + Note;
for J in 1 .. Count loop
delay until Next_Time;
Next_Time := Next_Time + Note;
end loop;
Time_End := Ada.Real_Time.Clock;
Time_Difference := Time_End - Time_Start;
Time_Duration := Ada.Real_Time.To_Duration(Time_Difference);
Ada.Text_IO.Put("Time Difference: ");
Ada.Text_IO.Put(Duration'Image (Time_Duration));
Ada.Text_IO.Put_Line (" " & Label);
end Test;
begin
-- -------------------------------------------------
-- Time Difference Calculations for note durations
-- -------------------------------------------------
Time_Delta :=
Ada.Real_Time.To_Time_Span(Duration((1.0/(Beats_Per_Minutes/Number_Of_Ticks)
)/Pulse_Per_Quarter));
Time_Delta_Duration := Ada.Real_Time.To_Duration(Time_Delta);
Delta_Whole_Note :=
Ada.Real_Time.To_Time_Span(Time_Delta_Duration * Whole_Note);
Delta_Half_Note :=
Ada.Real_Time.To_Time_Span(Time_Delta_Duration * Half_Note);
Delta_Quarter_Note :=
Ada.Real_Time.To_Time_Span(Time_Delta_Duration * Quarter_Note);
Delta_Eighth_Note :=
Ada.Real_Time.To_Time_Span(Time_Delta_Duration * Eighth_Note);
Delta_Sixteenth_Note :=
Ada.Real_Time.To_Time_Span(Time_Delta_Duration * Sixteenth_Note);
Delta_ThirtySecond_Note :=
Ada.Real_Time.To_Time_Span(Time_Delta_Duration * ThirtySecond_Note);
Delta_SixtyFourth_Note :=
Ada.Real_Time.To_Time_Span(Time_Delta_Duration * SixtyFourth_Note);
Delta_128th_Note :=
Ada.Real_Time.To_Time_Span(Time_Delta_Duration * HundredTwentyEighth_Note);
Test (1, Delta_Whole_Note, "For one whole note");
Test (2, Delta_Half_Note, "For 2 Half note");
Test (4, Delta_Quarter_Note, "For 4 Quarter note Delay");
Test (8, Delta_Eighth_Note, "For 8 8th note Delay");
Test (16, Delta_Sixteenth_Note, "For 16 16th note Delay");
Test (32, Delta_ThirtySecond_Note, "For 32 32th note Delay");
Test (64, Delta_Sixteenth_Note, "For 64 64th note Delay");
Test (128, Delta_Sixteenth_Note, "For 128 128th note Delay");
end BPMPPQTest2;
--
-- Stephe
|