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
379
380
381
382
383
384
385
386 | -- MP: a Music Player
-- Copyright (C) 2021 by PragmAda Software Engineering. All rights reserved.
-- Released under the terms of the BSD 3-Clause license; see https://opensource.org/licenses
--
-- 2021-06-01 Use new Enpty_Options procedure from Gnoga 1.6a to clear Sel
-- 2021-04-01 Adapted to Ada-12 version of PragmARCs
-- 2020-09-15 Initial version
--
with Ada.Containers.Vectors;
with Ada.Directories;
with Ada.Exceptions;
with Ada.Numerics.Discrete_Random;
with Ada.Strings.Bounded;
with Ada.Strings.Unbounded;
with Gnoga.Application.Singleton;
with Gnoga.Gui.Base;
with Gnoga.Gui.Element.Common;
with Gnoga.Gui.Element.Form;
with Gnoga.Gui.Element.Multimedia;
with Gnoga.Gui.View;
with Gnoga.Gui.Window;
with Gnoga_File_Selection;
with PragmARC.Persistent_Skip_List_Unbounded;
package body MP.UI is
package B_Strings is new Ada.Strings.Bounded.Generic_Bounded_Length (Max => 400);
use B_Strings;
subtype Path_Name is Bounded_String;
function Less (Left : Path_Name; Right : Path_Name) return Boolean;
-- Files in the working directory are < files in subdirectories
package Path_Lists is new PragmARC.Persistent_Skip_List_Unbounded (Element => Path_Name, "<" => Less);
Window : Gnoga.Gui.Window.Window_Type;
View : Gnoga.Gui.View.View_Type;
Player : Gnoga.Gui.Element.Multimedia.Audio_Type;
Form : Gnoga.Gui.Element.Form.Form_Type;
Sel : Gnoga.Gui.Element.Form.Selection_Type;
Count : Gnoga.Gui.Element.Form.Number_Type;
Cnt_Lbl : Gnoga.Gui.Element.Form.Label_Type;
Delete : Gnoga.Gui.Element.Common.Button_Type;
Path : Gnoga.Gui.Element.Form.Text_Type;
Browse : Gnoga.Gui.Element.Common.Button_Type;
Add : Gnoga.Gui.Element.Common.Button_Type;
Play : Gnoga.Gui.Element.Common.Button_Type;
Skip : Gnoga.Gui.Element.Common.Button_Type;
Quit : Gnoga.Gui.Element.Common.Button_Type;
Quit_After : Gnoga.Gui.Element.Form.Check_Box_Type;
QA_LbL : Gnoga.Gui.Element.Form.Label_Type;
function Less (Left : Path_Name; Right : Path_Name) return Boolean is
function Slash_Count (Path : Path_Name) return Natural;
function Slash_Count (Path : Path_Name) return Natural is
Result : Natural := 0;
Path_S : constant String := To_String (Path);
begin -- Slash_Count
All_Chars : for I in Path_S'Range loop
if Path_S (I) = '/' then
Result := Result + 1;
end if;
end loop All_Chars;
return Result;
end Slash_Count;
Count_L : constant Natural := Slash_Count (Left);
Count_R : constant Natural := Slash_Count (Right);
begin -- Less
if Count_L = 0 and Count_R > 0 then
return True;
end if;
if Count_R = 0 and Count_L > 0 then
return False;
end if;
return Left < Right;
end Less;
task DJ is
entry Start;
entry Play;
entry Skip;
entry Quit;
end DJ;
procedure Play_Selected (Object : in out Gnoga.Gui.Base.Base_Type'Class) is
-- Empty;
begin -- Play_Selected
DJ.Play;
exception -- Play_Selected
when E : others =>
Gnoga.Log (Message => "Play_Selected: " & Ada.Exceptions.Exception_Information (E) );
end Play_Selected;
procedure Skip_To_Next (Object : in out Gnoga.Gui.Base.Base_Type'Class) is
-- Empty;
begin -- Skip_To_Next
DJ.Skip;
exception -- Skip_To_Next
when E : others =>
Gnoga.Log (Message => "Skip_To_Next: " & Ada.Exceptions.Exception_Information (E) );
end Skip_To_Next;
procedure Quit_Now (Object : in out Gnoga.Gui.Base.Base_Type'Class) is
-- Empty;
begin -- Quit_Now
DJ.Quit;
delay 0.1;
Gnoga.Application.Singleton.End_Application;
exception -- Quit_Now
when E : others =>
Gnoga.Log (Message => "Quit_Now: " & Ada.Exceptions.Exception_Information (E) );
end Quit_Now;
type Song_Info is record
Position : Positive;
Path : Path_Name;
end record;
package Song_Lists is new Ada.Containers.Vectors (Index_Type => Positive, Element_Type => Song_Info);
List : Path_Lists.Persistent_Skip_List := Path_Lists.Open_List ("playlist.mpl");
Song : Song_Lists.Vector;
procedure Make_Song_List (List : in out Song_Lists.Vector) is
Position : Natural := 0;
procedure Add_One (Item : in Path_Name) is
-- Empty;
begin -- Add_One
Position := Position + 1;
List.Append (New_Item => (Position => Position, Path => Item) );
Sel.Add_Option (Value => To_String (Item), Text => To_String (Item) );
end Add_One;
procedure Add_All is new Path_Lists.Iterate (Action => Add_One);
begin -- Make_Song_List
List.Clear;
Add_All (List => UI.List);
end Make_Song_List;
procedure Shuffle (List : in out Song_Lists.Vector) is
subtype Index is Integer range 1 .. List.Last_Index;
package Index_Random is new Ada.Numerics.Discrete_Random (Result_Subtype => Index);
Gen : Index_Random.Generator;
J : Index;
T : Song_Info;
begin -- Shuffle
Index_Random.Reset (Gen => Gen);
Swap_All : for I in 1 .. List.Last_Index loop
J := Index_Random.Random (Gen);
T := List.Element (I);
List.Replace_Element (Index => I, New_Item => List.Element (J) );
List.Replace_Element (Index => J, New_Item => T);
end loop Swap_All;
end Shuffle;
procedure Refresh is
-- Empty;
begin -- Refresh
Sel.Empty_Options;
Make_Song_List (List => Song);
Count.Value (Value => Sel.Length);
Shuffle (List => Song);
end Refresh;
procedure Delete_Song (Object : in out Gnoga.Gui.Base.Base_Type'Class) is
Index : constant Natural := Sel.Selected_Index;
Path : Path_Name;
Paused : Boolean := False;
begin -- Delete_Song
if Index = 0 then
return;
end if;
if not Player.Playback_Ended then
Paused := True;
Player.Pause;
end if;
Path := To_Bounded_String (Sel.Text (Index) );
List.Delete (Item => Path);
Refresh;
if Paused then
Player.Play;
end if;
exception -- Delete_Song
when E : others =>
Gnoga.Log (Message => "Delete_Song: " & Ada.Exceptions.Exception_Information (E) );
end Delete_Song;
Current_Directory : constant String := Ada.Directories.Current_Directory;
procedure Browse_Songs (Object : in out Gnoga.Gui.Base.Base_Type'Class) is
Result : constant Gnoga_File_Selection.Result_Info :=
Gnoga_File_Selection.Select_File (Window, Current_Directory);
begin -- Browse_Songs
if Result.Picked then
Get_Name : declare
Name : constant String := Ada.Strings.Unbounded.To_String (Result.Value);
begin -- Get_Name
if Name'Length > Current_Directory'Length and then
Name (Name'First .. Name'First + Current_Directory'Length - 1) = Current_Directory
then
Path.Value (Value => Name (Name'First + Current_Directory'Length + 1 .. Name'Last) );
end if;
end Get_Name;
end if;
end Browse_Songs;
procedure Add_Song (Object : in out Gnoga.Gui.Base.Base_Type'Class) is
Name : constant String := Path.Value;
Path : Path_Name;
Paused : Boolean := False;
begin -- Add_Song
if Name'Length = 0 then
return;
end if;
if not Player.Playback_Ended then
Paused := True;
Player.Pause;
end if;
Path := To_Bounded_String (Name);
List.Insert (Item => Path);
Refresh;
UI.Path.Value (Value => "");
if Paused then
Player.Play;
end if;
exception -- Add_Song
when E : others =>
Gnoga.Log (Message => "Add_Song: " & Ada.Exceptions.Exception_Information (E) );
end Add_Song;
Title : constant String := "MP";
task body DJ is
function Start (Song : in String) return Boolean is -- Returns True if Song started; False otherwise
-- Empty;
begin -- Start
Player.Media_Source (Source => Song);
Window.Document.Title (Value => Title & ' ' & Song);
Wait_For_Ready : for I in 1 .. 10 loop
if Player.Ready_To_Play then
Player.Play;
return True;
end if;
delay 0.01;
end loop Wait_For_Ready;
return False;
end Start;
Current : Song_Info;
Index : Positive := 1;
Run : Boolean := True;
begin -- DJ
Wait_For_Song : loop
exit Wait_For_Song when Song.Last_Index > 0;
select
accept Quit;
Window.Document.Title (Value => Title);
Run := False;
or
delay 1.0;
end select;
end loop Wait_For_Song;
if Run then
accept Start;
Forever : loop
Current := Song.Element (Index);
Sel.Selected (Index => Current.Position);
if Start (To_String (Current.Path) ) then
Wait_For_End : loop
select
accept Play;
exit Wait_For_End when not Start (Sel.Text (Sel.Selected_Index) );
or
accept Skip;
Window.Document.Title (Value => Title);
exit Wait_For_End;
or
accept Quit;
Window.Document.Title (Value => Title);
exit Forever;
or
delay 1.0;
exit Wait_For_End when Player.Playback_Ended;
end select;
end loop Wait_For_End;
end if;
exit Forever when Quit_After.Checked;
Index := Index + 1;
if Index > Song.Last_Index then
Index := 1;
end if;
end loop Forever;
if Quit_After.Checked then
Window.Document.Title (Value => Title);
Gnoga.Application.Singleton.End_Application;
end if;
end if;
exception -- DJ
when E : others =>
Gnoga.Log (Message => "DJ: " & Ada.Exceptions.Exception_Information (E) );
end DJ;
begin -- MP.UI
Gnoga.Application.Title (Name => Title);
Gnoga.Application.HTML_On_Close (HTML => Title & " ended.");
Gnoga.Application.Open_URL (url => "http://localhost:8089/");
Gnoga.Application.Singleton.Initialize (Main_Window => Window, Port => 8089);
View.Create (Parent => Window);
View.Text_Alignment (Value => Gnoga.Gui.Element.Center);
Player.Create (Parent => View, Preload => True);
View.New_Line;
Form.Create (Parent => View);
Form.Text_Alignment (Value => Gnoga.Gui.Element.Center);
Sel.Create (Form => Form, Visible_Lines => 30);
Form.New_Line;
Count.Create (Form => Form);
Count.Editable (Value => False);
Count.Read_Only;
Cnt_Lbl.Create (Form => Form, Label_For => Count, Content => "Number of songs:");
Form.New_Line;
Delete.Create (Parent => Form, Content => "Delete");
Delete.On_Click_Handler (Handler => Delete_Song'Access);
Path.Create (Form => Form, Size => 100);
Browse.Create (Parent => Form, Content => "Browse");
Browse.On_Click_Handler (Handler => Browse_Songs'Access);
Add.Create (Parent => Form, Content => "Add");
Add.On_Click_Handler (Handler => Add_Song'Access);
Form.New_Line;
Play.Create (Parent => Form, Content => "Play");
Play.On_Click_Handler (Handler => Play_Selected'Access);
Skip.Create (Parent => Form, Content => "Skip");
Skip.On_Click_Handler (Handler => Skip_To_Next'Access);
Quit.Create (Parent => Form, Content => "Quit");
Quit.On_Click_Handler (Handler => Quit_Now'Access);
Quit_After.Create (Form => Form);
QA_LbL.Create (Form => Form, Label_For => Quit_After, Content => "Quit after this song", Auto_Place => False);
Refresh;
DJ.Start;
Gnoga.Application.Singleton.Message_Loop;
exception -- MP.UI
when E : others =>
Gnoga.Log (Message => Ada.Exceptions.Exception_Information (E) );
end MP.UI;
|