aicwl_3.24.1_73939c9e/sources/test_aicwl/test_generator.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
--                                                                    --
--  package Test_Generator          Copyright (c)  Dmitry A. Kazakov  --
--  Test waveform generator                        Luebeck            --
--                                                 Spring, 2011       --
--                                                                    --
--                                Last revision :  13:15 14 Sep 2019  --
--                                                                    --
--  This  library  is  free software; you can redistribute it and/or  --
--  modify it under the terms of the GNU General Public  License  as  --
--  published by the Free Software Foundation; either version  2  of  --
--  the License, or (at your option) any later version. This library  --
--  is distributed in the hope that it will be useful,  but  WITHOUT  --
--  ANY   WARRANTY;   without   even   the   implied   warranty   of  --
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  --
--  General  Public  License  for  more  details.  You  should  have  --
--  received  a  copy  of  the GNU General Public License along with  --
--  this library; if not, write to  the  Free  Software  Foundation,  --
--  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.    --
--                                                                    --
--  As a special exception, if other files instantiate generics from  --
--  this unit, or you link this unit with other files to produce  an  --
--  executable, this unit does not by  itself  cause  the  resulting  --
--  executable to be covered by the GNU General Public License. This  --
--  exception  does not however invalidate any other reasons why the  --
--  executable file might be covered by the GNU Public License.       --
--____________________________________________________________________--

with Ada.Real_Time;         use Ada.Real_Time;
with GLib;                  use GLib;
with GLib.Properties;       use GLib.Properties;
with Gtk.Enums;             use Gtk.Enums;
with Gtk.Label;             use Gtk.Label;
with Gtk.Layered.Waveform;  use Gtk.Layered.Waveform;
with Gtk.Missed;            use Gtk.Missed;
with Strings_Edit.Floats;   use Strings_Edit.Floats;

with Ada.Numerics.Long_Elementary_Functions;
with Ada.Unchecked_Deallocation;

package body Test_Generator is
   use Ada.Numerics.Long_Elementary_Functions;

   procedure Changed
             (  Widget : access Gtk_Widget_Record'Class;
                Wave   : Wave_Generator
             )  is
   begin
      Wave.Worker.Set
      (  Duration (Value (Get_Text (Wave.Period_Edit))),
         Duration (Value (Get_Text (Wave.Refresh_Edit)))
      );
   exception
      when others =>
         null;
   end Changed;

   procedure Changed_Shape
             (  Widget : access Gtk_Widget_Record'Class;
                Wave   : Wave_Generator
             )  is
   begin
      Wave.Shape :=
         Wave_Shape'Val (Wave.Shape_Combo.Get_Active);
   exception
      when others =>
         null;
   end Changed_Shape;

   procedure Destroy
             (  Widget : access Gtk_Widget_Record'Class;
                Wave   : Wave_Generator
             )  is
      procedure Free is
         new Ada.Unchecked_Deallocation
             (  Generator,
                Generator_Ptr
             );
   begin
      if Wave.Worker /= null then
         Wave.Worker.Stop;
         while not Wave.Worker'Terminated loop
            delay 0.001;
         end loop;
         Free (Wave.Worker);
      end if;
      Wave.Buffer.Unref;
   end Destroy;

   function Get_Source
            (  Wave : not null access Wave_Generator_Record
            )  return not null access Waveform_Data_Source'Class is
   begin
      return Wave.Buffer.all'Unchecked_Access;
   end Get_Source;

   function Get_Buffer
            (  Wave : not null access Wave_Generator_Record
            )  return not null access
                      Gtk_Wavefrom_Ring_Data_Buffer_Record'Class is
   begin
      return Wave.Buffer;
   end Get_Buffer;

   procedure Gtk_New
             (  Wave   : out Wave_Generator;
                Shape  : Wave_Shape;
                Cycle  : Duration;
                Size   : Positive := 10_100;
                Period : Duration := 0.01
             )  is
   begin
      Wave := new Wave_Generator_Record;
      Initialize (Wave, Shape, Cycle, Size, Period);
   end Gtk_New;

   procedure Gtk_New
             (  Wave   : out Wave_Generator;
                Shape  : Wave_Shape;
                Cycle  : Duration;
                Buffer : not null access
                         Gtk_Wavefrom_Ring_Data_Buffer_Record'Class;
                Period : Duration := 0.01
             )  is
   begin
      Wave := new Wave_Generator_Record;
      Initialize (Wave, Shape, Cycle, Buffer, Period);
   end Gtk_New;

   procedure Init
             (  Wave   : not null access Wave_Generator_Record'Class;
                Shape  : Wave_Shape;
                Cycle  : Duration;
                Period : Duration
             )  is
      Label : Gtk_Label;
   begin
      Initialize (Wave, 3, 3, False);
      Wave.Set_Row_Spacings (3);
      Wave.Set_Col_Spacings (3);
         -- Row 1
      Gtk_New (Label, "Shape");
      Label.Set_Halign (Align_End);
      Label.Set_Valign (Align_Center);
      Attach
      (  Wave,
         Label,
         0, 1, 0, 1,
         XOptions => Fill,
         YOptions => Shrink
      );
      Gtk_New (Wave.Shape_Combo);
      Wave.Shape_Combo.Append_Text ("Constant");
      Wave.Shape_Combo.Append_Text ("Modulated");
      Wave.Shape_Combo.Append_Text ("Sine");
      Wave.Shape_Combo.Append_Text ("Saw");
      Wave.Shape_Combo.Append_Text ("Square");
      Wave.Shape_Combo.Append_Text ("Triangle");
      Wave.Shape_Combo.Set_Active (Wave_Shape'Pos (Shape));
      Attach
      (  Wave,
         Wave.Shape_Combo,
         1, 3, 0, 1,
         XOptions => Fill,
         YOptions => Shrink
      );
         -- Row 2
      Gtk_New (Label, "Cycle");
      Label.Set_Halign (Align_End);
      Label.Set_Valign (Align_Center);
      Attach
      (  Wave,
         Label,
         0, 1, 1, 2,
         XOptions => Fill,
         YOptions => Shrink
      );
      Gtk_New (Wave.Period_Edit);
      Wave.Period_Edit.Set_Width_Chars (6);
      if Find_Property (Wave.Period_Edit, "max-width-chars") /= null
      then
         Set_Property
         (  Wave.Period_Edit,
            Build ("max-width-chars"),
            GInt'(6)
         );
      end if;
      Set_Text (Wave.Period_Edit, Image (Float (Cycle)));
      Attach
      (  Wave,
         Wave.Period_Edit,
         1, 2, 1, 2,
         YOptions => Shrink
      );
      Gtk_New (Label, "s");
      Attach
      (  Wave,
         Label,
         2, 3, 1, 2,
         XOptions => Shrink,
         YOptions => Shrink
      );
         -- Row 3
      Gtk_New (Label, "Rate");
      Label.Set_Halign (Align_End);
      Label.Set_Valign (Align_Center);
      Attach
      (  Wave,
         Label,
         0, 1, 2, 3,
         XOptions => Fill,
         YOptions => Shrink
      );
      Gtk_New (Wave.Refresh_Edit);
      Wave.Refresh_Edit.Set_Width_Chars (6);
      if Find_Property (Wave.Refresh_Edit, "max-width-chars") /= null
      then
         Set_Property
         (  Wave.Refresh_Edit,
            Build ("max-width-chars"),
            GInt'(6)
         );
      end if;
      Set_Text (Wave.Refresh_Edit, Image (Float (Period)));
      Attach
      (  Wave,
         Wave.Refresh_Edit,
         1, 2, 2, 3,
         YOptions => Shrink
      );
      Gtk_New (Label, "s");
      Attach
      (  Wave,
         Label,
         2, 3, 2, 3,
         XOptions => Shrink,
         YOptions => Shrink
      );

      Handlers.Connect
      (  Wave,
         "destroy",
         Destroy'Access,
         Wave.all'Unchecked_Access
      );
      Handlers.Connect
      (  Wave.Period_Edit,
         "changed",
         Changed'Access,
         Wave.all'Unchecked_Access
      );
      Handlers.Connect
      (  Wave.Refresh_Edit,
         "changed",
         Changed'Access,
         Wave.all'Unchecked_Access
      );
      Handlers.Connect
      (  Wave.Shape_Combo,
         "changed",
         Changed_Shape'Access,
         Wave.all'Unchecked_Access
      );
      Wave.Shape := Shape;
   end Init;

   procedure Initialize
             (  Wave   : not null access Wave_Generator_Record'Class;
                Shape  : Wave_Shape;
                Cycle  : Duration;
                Size   : Positive;
                Period : Duration
             )  is
   begin
      Init (Wave, Shape, Cycle, Period);
      Gtk_New (Wave.Buffer, Size);
      Wave.Worker := new Generator (Wave.all'Unchecked_Access);
      Wave.Worker.Set (Cycle, Period);
   end Initialize;

   procedure Initialize
             (  Wave   : not null access Wave_Generator_Record'Class;
                Shape  : Wave_Shape;
                Cycle  : Duration;
                Buffer : not null access
                         Gtk_Wavefrom_Ring_Data_Buffer_Record'Class;
                Period : Duration
             )  is
   begin
      Init (Wave, Shape, Cycle, Period);
      Wave.Buffer := Buffer.all'Unchecked_Access;
      Wave.Buffer.Ref;
      Wave.Worker := new Generator (Wave.all'Unchecked_Access);
      Wave.Worker.Set (Cycle, Period);
   end Initialize;

   task body Generator is
      Cycle  : Long_Float := 1.0;
      Period : Duration   := 100.0;
      X      : GDouble;
   begin
      loop
         select
            accept Stop;
            exit;
         or accept Set (Cycle, Period : Duration) do
               Generator.Cycle  := Long_Float (Cycle);
               Generator.Period := Period;
            end Set;
            if Period < 0.001 then
               Period := 0.001;
            end if;
            if Cycle < 0.001 then
               Cycle := 0.001;
            end if;
         or delay Period;
            X := To_Double (Clock);
            case Wave.Shape is
               when Modulated =>
                  Wave.Buffer.Put
                  (  T => X_Axis (X),
                     V => Y_Axis
                          (  sin (Long_Float (X), Cycle / 5.7)
                          *  sin (Long_Float (X), Cycle * 4.3)
                  )       );
               when Sine =>
                  Wave.Buffer.Put
                  (  T => X_Axis (X),
                     V => Y_Axis (sin (Long_Float (X), Cycle))
                  );
               when Saw =>
                  Wave.Buffer.Put
                  (  T => X_Axis (X),
                     V => (  2.0
                          *  Y_Axis'Remainder
                             (  Y_Axis (X),
                                Y_Axis (Cycle)
                             )
                          /  Y_Axis (Cycle)
                  )       );
               when Triangle =>
                  Wave.Buffer.Put
                  (  T => X_Axis (X),
                     V => (  4.0
                          *  abs Y_Axis'Remainder
                                 (  Y_Axis (X),
                                    Y_Axis (Cycle)
                                 )
                          /  Y_Axis (Cycle)
                          -  1.0
                  )       );
               when Square =>
                  if (  Y_Axis'Remainder (Y_Axis (X), Y_Axis (Cycle))
                     >= 0.0
                     )
                  then
                     Wave.Buffer.Put (T => X_Axis (X), V => 1.0);
                  else
                     Wave.Buffer.Put (T => X_Axis (X), V => 0.0);
                  end if;
               when Const =>
                  Wave.Buffer.Put
                  (  T => X_Axis (X),
                     V => Y_Axis (0.0)
                  );
            end case;
         end select;
      end loop;
   end Generator;

   procedure Set_Shape
             (  Wave  : not null access Wave_Generator_Record;
                Shape : Wave_Shape
             )  is
   begin
      Wave.Shape_Combo.Set_Active (Wave_Shape'Pos (Shape));
   end Set_Shape;

end Test_Generator;