/
sgAudio.pas
1589 lines (1336 loc) · 50.7 KB
/
sgAudio.pas
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//=============================================================================
// sgAudio.pas
//=============================================================================
//
// The Audio unit is responsible for managing SDL audio for music and sound
// effects. This includes initialisation, loading, freeing, playing, and
// checking if music or sound is playing.
//
// Change History:
//
// Version 3:
// - 2011-05-12: Josh : Added PauseMusic and Resume Music
// - 2010-12-08: Andrew : Changed to RaiseWarning, and checked audio open
// : Added interacting with sounds and music by name
// - 2010-01-28: David : Changed LoadSoundEffectNamed to use an already
// loaded sound if found
// - 2009-11-10: Andrew : Added sn and csn tags to code
// - 2009-11-06: Andrew : Returned loading code
// : Added extra comments and tracing
// - 2009-07-29: Andrew : Open Audio now indicates if audio has been opened.
// - 2009-07-14: Andrew : Removed loading and freeing code.
// - 2009-06-16: Clinton: Commenting/format tweaks
// - 2009-06-04: Andrew : Finished processing comments.
// Added fading capabilities.
// Fixed comments in implementation.
// Version 2.1:
// - 2009-05-19: Andrew: Added PlaySoundEffect with volume
// Added meta comments
// Version 2.0:
// - 2008-12-17: Andrew: Moved all integers to Longint
// - 2008-12-16: Andrew: Added volume controls
//
// Version 1.1:
// - 2008-03-09: Andrew: Added extra exception handling
// - 2008-01-17: Aki + Andrew: Refactor
//
// Version 1.0:
// - Various
//=============================================================================
/// SwinGame's Audio is responsible for loading and playing music and sound
/// effects. The main functionality exists in `LoadMusic`, `PlayMusic`,
/// `LoadSoundEffect`, and `PlaySoundEffect`. Associated with these are the
/// `Music` and `SoundEffect` types.
///
/// @module Audio
/// @static
///
/// @doc_types SoundEffect, Music
unit sgAudio;
//=============================================================================
interface
uses sgTypes;
//=============================================================================
//----------------------------------------------------------------------------
// Opening and Closing Audio support
//----------------------------------------------------------------------------
/// `TryOpenAudio` attempts to open the audio device for SwinGame to use.
/// If this fails `TryOpenAudio` returns false to indicate that the audio
/// device has not opened correctly and audio cannot be played.
///
/// @lib
function TryOpenAudio(): Boolean;
/// `OpenAudio` is used to initialise the SwinGame audio code. This should be
/// called at the start of your programs code, and is usually coded into the
/// starting project templates. After initialising the audio code you can
/// load and play `Music` using `LoadMusic` and `PlayMusic`, load and play
/// `SoundEffect`s using `LoadSoundEffect` and `PlaySoundEffect`. At the end
/// of the program you need to call `CloseAudio` to ensure that the audio
/// code is correctly terminated.
///
/// @lib
procedure OpenAudio();
/// `AudioReady` indicates if SwinGame's audio has been opened. Sound effects
/// and Music can only be played with the audio is "ready".
///
/// @lib
function AudioReady(): Boolean;
/// `CloseAudio` is used to clean up the resources used by SwinGame audio. If
/// `OpenAudio` is called, this must be called to return the resources used
/// before the program terminates.
///
/// @lib
procedure CloseAudio();
//----------------------------------------------------------------------------
// Loading & Releasing Sound Effects
//----------------------------------------------------------------------------
/// Loads the `SoundEffect` from the supplied filename. The sound will be loaded
/// from the Resources/sounds folder unless a full path to the file is passed
/// in. If you are passing in the full path and you want to ensure that your game
/// is able to work across multiple platforms correctly then use
/// `PathToResource` to get the full path to the files in the projects
/// resources folder.
///
/// LoadSoundEffect can load wav and ogg audio files.
///
/// `FreeSoundEffect` should be called to free the resources used by the
/// `SoundEffect` data once the resource is no longer needed.
///
/// @param filename the filename of the sound effect file to load.
///
/// @lib
///
/// @class SoundEffect
/// @constructor
/// @csn initFromFile:%s
function LoadSoundEffect(filename: String): SoundEffect;
/// Loads and returns a sound effect. The supplied ``filename`` is used to
/// locate the sound effect to load. The supplied ``name`` indicates the
/// name to use to refer to this SoundEffect. The `SoundEffect` can then be
/// retrieved by passing this ``name`` to the `SoundEffectNamed` function.
///
/// @lib
/// @sn loadSoundEffectNamed:%s fromFile:%s
///
/// @class SoundEffect
/// @constructor
/// @csn initWithName:%s fromFile:%s
function LoadSoundEffectNamed(name, filename: String): SoundEffect;
/// Determines if SwinGame has a sound effect loaded for the supplied name.
/// This checks against all sounds loaded, those loaded without a name
/// are assigned the filename as a default
///
/// @lib
function HasSoundEffect(name: String): Boolean;
/// Returns the `SoundEffect` that has been loaded with the specified name,
/// see `LoadSoundEffectNamed`.
///
/// @lib
function SoundEffectNamed(name: String): SoundEffect;
/// Releases the SwinGame resources associated with the sound effect of the
/// specified ``name``.
///
/// @lib
procedure ReleaseSoundEffect(name: String);
/// Releases all of the sound effects that have been loaded.
///
/// @lib
procedure ReleaseAllSoundEffects();
/// Frees the resources used by a `SoundEffect` resource. All loaded
/// `SoundEffect`s should be freed once they are no longer needed.
///
/// @lib
///
/// @class SoundEffect
/// @dispose
procedure FreeSoundEffect(var effect: SoundEffect);
//----------------------------------------------------------------------------
// Loading & Releasing Music
//----------------------------------------------------------------------------
/// Loads and returns a music value. The supplied ``filename`` is used to
/// locate the music file to load. The supplied ``name`` indicates the
/// name to use to refer to this Music value. The `Music` can then be
/// retrieved by passing this ``name`` to the `MusicNamed` function.
///
/// @lib
/// @sn loadMusicNamed:%s fromFile:%s
///
/// @class Music
/// @constructor
/// @csn initWithName:%s fromFile:%s
function LoadMusicNamed(name, filename: String): Music;
/// Determines if SwinGame has a music value loaded for the supplied name.
/// This checks against all music values loaded using `LoadMusicNamed`.
///
/// @lib
function HasMusic(name: String): Boolean;
/// Returns the `Music` that has been loaded with the specified name.
/// This works with music data loaded using `LoadMusicNamed`.
///
/// @lib
function MusicNamed(name: String): Music;
/// Releases the music that have been loaded with the supplied name.
///
/// @lib
procedure ReleaseMusic(name: String);
/// Releases all of the music data that have been loaded.
///
/// @lib
procedure ReleaseAllMusic();
/// Loads the `Music` from the supplied filename. The music will be loaded
/// from the Resources/sounds folder unless a full path to the file is passed
/// in. If you are passing in the full path and you want toensure that your game
/// is able to work across multiple platforms correctly ensure that you use
/// `PathToResource` to get the full path to the files in the projects
/// resources folder.
///
/// LoadMusic can load mp3, wav and ogg audio files.
///
/// `FreeMusic` should be called to free the resources used by the
/// `Music` data once the resource is no longer needed.
///
/// @param filename the filename to the music file to load.
///
/// @lib
///
/// @class Music
/// @constructor
/// @csn initFromFile:%s
function LoadMusic(filename: String): Music;
/// Frees the resources used by a `Music` resource. All loaded
/// `Music` should be freed once it is no longer needed.
///
/// @lib
///
/// @class Music
/// @dispose
procedure FreeMusic(var mus: Music);
//----------------------------------------------------------------------------
// Playing Sound Effects
//----------------------------------------------------------------------------
/// There are several versions of PlaySoundEffect that can be used to control
/// the way the sound effect plays, allowing you to control its volume and
/// the number of times the code loops. In all cases the started sound effect
/// is mixed with the currently playing sound effects and music.
///
/// With this version of PlaySoundEffect, the started sound effect will be
/// played at full volume.
///
/// @param effect The effect indicates which sound effect to start playing. This
/// effect is played once at its full volume.
///
/// @lib PlaySoundEffectWithLoopAndVolume(effect,1,1.0)
/// @uname PlaySoundEffect
///
/// @class SoundEffect
/// @method Play
///
/// @doc_idx 1
procedure PlaySoundEffect(effect: SoundEffect); overload;
/// This version of PlaySoundEffect allows you to indicate the number of times
/// the sound effect is repeated. Setting the loops parameter to -1 will cause
/// the sound effect to be looped infinitely, setting it to a value larger than
/// 0 plays the sound effect the number of times indicated, calling with a
/// value of 0 means the sound effect is not played.
///
/// @param effect The effect indicates which sound effect to start playing. This
/// effect is played once at its full volume.
///
/// @param loops Controls the number of times the sound effect is played. -1
/// means the sound effect is repeated infinitely.
///
/// @lib PlaySoundEffectWithLoopAndVolume(effect, loops, 1.0)
/// @uname PlaySoundEffectWithLoop
/// @sn playSoundEffect:%s looped:%s
///
/// @class SoundEffect
/// @overload Play PlayWithLoops
/// @csn playLooped:%s
///
/// @doc_details
procedure PlaySoundEffect(effect: SoundEffect; loops: Longint); overload;
/// This version of PlaySoundEffect allows you to control the volume of the
/// sounds playback. The vol parameter will take a value between 0 and 1
/// indicating the percentage of full volume to play at.
/// For example, 0.1 plays the sound effect at 10% of its original volume.
///
/// @param effect The effect indicates which sound effect to start playing.
/// @param vol Indicates the percentage of the original volume to play the
/// `SoundEffect` at. This must be between 0 and 1.
///
/// @lib PlaySoundEffectWithLoopAndVolume(effect, 1, vol)
/// @uname PlaySoundEffectWithVolume
/// @sn playSoundEffect:%s atVolume:%s
/// @version 2.1
///
/// @class SoundEffect
/// @overload Play PlayWithVolume
/// @csn playVolume:%s
///
/// @doc_details
procedure PlaySoundEffect(effect: SoundEffect; vol: Single); overload;
/// This version of PlaySoundEffect allows you to control both the number
/// of times the `SoundEffect` is repeated, and its playback volume.
///
/// @param effect The effect indicates which sound effect to start playing.
/// @param loops Controls the number of times the sound effect is played.
/// @param vol Indicates the percentage of the original volume to play the
/// `SoundEffect` at. This must be between 0 and 1.
///
/// @lib PlaySoundEffectWithLoopAndVolume
/// @sn playSoundEffect:%s looped:%s atVolume:%s
/// @version 2.0
///
/// @class SoundEffect
/// @overload Play PlayWithLoopsAndVolume
/// @csn playLooped:%s vol:%s
///
/// @doc_details
procedure PlaySoundEffect(effect: SoundEffect; loops: Longint; vol: Single); overload;
/// This version of PlaySoundEffect allows you to control both the number
/// of times the `SoundEffect` is repeated, and its playback volume.
///
/// @param name The name of the sound effect to start playing.
/// @param loops Controls the number of times the sound effect is played.
/// @param vol Indicates the percentage of the original volume to play the
/// `SoundEffect` at. This must be between 0 and 1.
///
/// @lib PlaySoundEffectNamedWithLoopAndVolume
/// @sn playSoundEffectNamed:%s looped:%s atVolume:%s
///
/// @doc_details
procedure PlaySoundEffect(name: String; loops: Longint; vol: Single); overload;
/// This version of PlaySoundEffect allows you to indicate the number of times
/// the sound effect is repeated. Setting the loops parameter to -1 will cause
/// the sound effect to be looped infinitely, setting it to a value larger than
/// 0 plays the sound effect the number of times indicated, calling with a
/// value of 0 means the sound effect is not played.
///
/// @param name The name of the sound effect to start playing.
///
/// @param loops Controls the number of times the sound effect is played. -1
/// means the sound effect is repeated infinitely.
///
/// @lib PlaySoundEffectNamedWithLoopAndVolume(name, loops, 1.0)
/// @uname PlaySoundEffectNamedWithLoop
/// @sn playSoundEffectNamed:%s looped:%s
///
/// @doc_details
procedure PlaySoundEffect(name: String; loops: Longint); overload;
/// There are several versions of PlaySoundEffect that can be used to control
/// the way the sound effect plays, allowing you to control its volume and
/// the number of times the code loops. In all cases the started sound effect
/// is mixed with the currently playing sound effects and music.
///
/// With this version of PlaySoundEffect, the started sound effect will be
/// played at full volume.
///
/// @param name The name of the sound effect to play
///
/// @lib PlaySoundEffectNamedWithLoopAndVolume(name,1,1.0)
/// @uname PlaySoundEffectNamed
procedure PlaySoundEffect(name: String); overload;
/// This version of PlaySoundEffect allows you to control the volume of the
/// sounds playback. The vol parameter will take a value between 0 and 1
/// indicating the percentage of full volume to play at.
/// For example, 0.1 plays the sound effect at 10% of its original volume.
///
/// @param name The name of the sound effect to play.
/// @param vol Indicates the percentage of the original volume to play the
/// `SoundEffect` at. This must be between 0 and 1.
///
/// @lib PlaySoundEffectNamedWithLoopAndVolume(name, 1, vol)
/// @uname PlaySoundEffectNamedWithVolume
/// @sn playSoundEffectNamed:%s atVolume:%s
///
/// @doc_details
procedure PlaySoundEffect(name: String; vol: Single); overload;
//----------------------------------------------------------------------------
// Playing Music
//----------------------------------------------------------------------------
/// PlayMusic starts playing a `Music` resource. SwinGame only allows one
/// music resource to be played at a time. Starting to play a new music
/// resource will stop the currently playing music track. You can also stop
/// the music by calling `StopMusic`.
///
/// By default SwinGame starts playing music at its full volume. This can be
/// controlled by calling `SetMusicVolume`. The current volume can be checked
/// with `MusicVolume`.
///
/// To test if a `Music` resource is currently playing you can use the
/// `MusicPlaying` function.
///
/// This version of PlayMusic can be used to play background music that is
/// looped infinitely. The currently playing music is stopped and the new
/// music resource will start playing, and will repeat until `StopMusic` is
/// called, or another resource is played.
///
/// @param mus The `Music` resource to play.
///
/// @lib PlayMusicWithLoops(mus, -1)
/// @uname PlayMusic
///
/// @class Music
/// @method Play
procedure PlayMusic(mus: Music); overload;
/// This version of PlayMusic allows you to control the number of times the
/// `Music` resource is repeated. It starts playing the supplied `Music`
/// resource, repeating it the numder of times specified in the loops
/// parameter. Setting loops to -1 repeats the music infinitely, other values
/// larger than 0 indicate the number of times that the music should be
/// played.
///
/// @param mus The `Music` resource to be played.
/// @param loops The number of times that the music should be played, -1 for
/// repeat infinitely
///
/// @lib PlayMusicWithLoops
/// @sn playMusic:%s looped:%s
///
/// @class Music
/// @overload Play PlayWithLoops
procedure PlayMusic(mus: Music; loops: Longint); overload;
/// Fades the music in over a number of milliseconds, and then continues to
/// play the music repeatedly until the program ends or the music is stopped.
/// The music fades from 0 volume up to the currently set music volume.
///
/// @param mus The `Music` resource to be played.
/// @param ms The number of milliseconds over which to fade the music in to
// the current music volume.
///
/// @lib FadeMusicIn
/// @sn playMusic:%s fadeIn:%s
///
/// @class Music
/// @method FadeIn
/// @csn playFadeIn:%s
procedure FadeMusicIn(mus: Music; ms: Longint); overload;
/// This version of FadeMusicIn fades the music in then plays the 'Music'
/// for a given number of loops.Setting loops to -1 repeats the music
/// infinitely, other values larger than 0 indicate the number of times that
/// the music should be played.
///
/// @param mus The `Music` resource to be played.
/// @param loops The number of times that the music should be played, -1 for
/// repeat infinitely
/// @param ms The number of milliseconds over which to fade the music in to
/// the current music volume.
///
/// @lib FadeMusicInWithLoops
/// @sn playMusic:%s looped:%s fadeIn:%s
///
/// @class Music
/// @overload FadeIn FadeInWithLoops
/// @csn playLooped:%s fadeIn:%s
procedure FadeMusicIn(mus: Music; loops, ms: Longint); overload;
/// This version of PlayMusic allows you to control the number of times the
/// `Music` resource is repeated. It starts playing the supplied `Music`
/// resource, repeating it the numder of times specified in the loops
/// parameter. Setting loops to -1 repeats the music infinitely, other values
/// larger than 0 indicate the number of times that the music should be
/// played.
///
/// @param name The name of the `Music` resource to be played.
/// @param loops The number of times that the music should be played, -1 for
/// repeat infinitely
///
/// @lib PlayMusicNamedWithLoops
/// @sn playMusicNamed:%s looped:%s
procedure PlayMusic(name: String; loops: Longint); overload;
/// PlayMusic starts playing a `Music` resource. SwinGame only allows one
/// music resource to be played at a time. Starting to play a new music
/// resource will stop the currently playing music track. You can also stop
/// the music by calling `StopMusic`.
///
/// By default SwinGame starts playing music at its full volume. This can be
/// controlled by calling `SetMusicVolume`. The current volume can be checked
/// with `MusicVolume`.
///
/// To test if a `Music` resource is currently playing you can use the
/// `MusicPlaying` function.
///
/// This version of PlayMusic can be used to play background music that is
/// looped infinitely. The currently playing music is stopped and the new
/// music resource will start playing, and will repeat until `StopMusic` is
/// called, or another resource is played.
///
/// @param name The name of the `Music` resource to play.
///
/// @lib PlayMusicNamedWithLoops(name, -1)
/// @uname PlayMusicNamed
procedure PlayMusic(name: String); overload;
/// This version of FadeMusicIn fades the music in then plays the 'Music'
/// for a given number of loops.Setting loops to -1 repeats the music
/// infinitely, other values larger than 0 indicate the number of times that
/// the music should be played.
///
/// @param name The name of the `Music` resource to be played.
/// @param loops The number of times that the music should be played, -1 for
/// repeat infinitely
/// @param ms The number of milliseconds over which to fade the music in to
/// the current music volume.
///
/// @lib FadeMusicNamedInWithLoops
/// @sn playMusicNamed:%s looped:%s fadeIn:%s
procedure FadeMusicIn(name: String; loops, ms: Longint); overload;
/// Fades the music in over a number of milliseconds, and then continues to
/// play the music repeatedly until the program ends or the music is stopped.
/// The music fades from 0 volume up to the currently set music volume.
///
/// @param name The name of the `Music` resource to be played.
/// @param ms The number of milliseconds over which to fade the music in to
// the current music volume.
///
/// @lib FadeMusicNamedIn
/// @sn playMusicNamed:%s fadeIn:%s
procedure FadeMusicIn(name: String; ms: Longint); overload;
/// Pauses the currently playing music. See `ResumeMusic`.
///
/// @lib
procedure PauseMusic();
/// Resume currently paused music. See `PauseMusic`.
///
/// @lib
procedure ResumeMusic();
//----------------------------------------------------------------------------
// Query music
//----------------------------------------------------------------------------
/// This procedure allows you to set the volume of the currently playing
/// music. The vol parameter indicates the percentage of the original volume,
/// for example, 0.1 sets the playback volume to 10% of its full volume.
///
/// @param value Indicates the percentage of the original volume to play the
/// `Music` at. This must be between 0 and 1, e.g. 0.1 is 10%.
///
/// @lib SetMusicVolume
///
/// @class Music
/// @static
/// @setter Volume
procedure SetMusicVolume(value: Single);
/// This function returns the current volume of the music. This will be a
/// value between 0 and 1, with 1 indicating 100% of the `Music` resources
/// volume.
///
/// @returns The volume of the currently playing music.
///
/// @lib MusicVolume
///
/// @class Music
/// @static
/// @getter Volume
function MusicVolume(): Single;
/// This function indicates if music is currently playing. As only one music
/// resource can be playing at a time this does not need to be told which
/// music resource to check for.
///
/// @returns true if the music is playing
///
/// @lib MusicPlaying
///
/// @class Music
/// @static
/// @method IsPlaying
function MusicPlaying(): Boolean;
/// Returns the name that SwinGame uses to refer to this music data. This
/// name can be used to fetch and release this music resource.
///
/// @lib
///
/// @class Music
/// @getter Name
function MusicName(mus: Music): String;
/// Returns the filename that SwinGame uses to load to this music data.
///
/// @lib
///
/// @class Music
/// @getter Filename
function MusicFilename(mus: Music): String;
//----------------------------------------------------------------------------
// Query Sound Effects
//----------------------------------------------------------------------------
/// This function can be used to check if a sound effect is currently
/// playing.
///
/// @param effect The sound effect to check.
/// @returns true if the effect `SoundEffect` is playing.
///
/// @lib SoundEffectPlaying
///
/// @class SoundEffect
/// @method IsPlaying
function SoundEffectPlaying(effect: SoundEffect): Boolean; overload;
/// This function can be used to check if a sound effect is currently
/// playing.
///
/// @param name The name of the sound effect to check.
/// @returns true if the effect `SoundEffect` is playing.
///
/// @lib SoundEffectNamedPlaying
function SoundEffectPlaying(name: String): Boolean; overload;
/// Returns the name that SwinGame uses to refer to this sound effect. This
/// name can be used to fetch and release this sound effect resource.
///
/// @lib
///
/// @class SoundEffect
/// @getter Name
function SoundEffectName(effect: SoundEffect): String;
/// Returns the filename that SwinGame used to load to this sound effect.
///
/// @lib
///
/// @class SoundEffect
/// @getter Filename
function SoundEffectFilename(effect: SoundEffect): String;
//----------------------------------------------------------------------------
// Stop Music & Sound Effects
//----------------------------------------------------------------------------
/// Stops all occurances of the effect `SoundEffect` that is currently playing.
///
/// @param effect The sound to stop.
///
/// @lib StopSoundEffect
///
/// @class SoundEffect
/// @method Stop
procedure StopSoundEffect(effect: SoundEffect);
/// Stops all occurances of the named `SoundEffect` that are currently playing.
///
/// @param name The name of the sound effect to stop.
///
/// @lib StopSoundEffectNamed
procedure StopSoundEffect(name: String);
/// Stops playing the current music resource.
///
/// @lib StopMusic
///
/// @class Music
/// @static
/// @method Stop
procedure StopMusic();
/// Fades the currently playing music out over a number of milli seconds.
///
/// @param ms The number of milliseconds over which to fade the music to 0 volume.
///
/// @lib FadeMusicOut
///
/// @class Music
/// @static
/// @method FadeOut
procedure FadeMusicOut(ms: Longint);
//=============================================================================
implementation
uses
SysUtils, Classes,
stringhash, // libsrc
sgShared, sgResources, sgTrace, sgDriverAudio;
//=============================================================================
var
_SoundEffects: TStringHash;
_Music: TStringHash;
_firstLoad: Boolean = false;
function TryOpenAudio(): Boolean;
begin
{$IFDEF TRACE}
TraceEnter('sgAudio', 'TryOpenAudio', '');
{$ENDIF}
sgShared.AudioOpen := AudioDriver.OpenAudio();
result := sgShared.AudioOpen;
{$IFDEF TRACE}
TraceExit('sgAudio', 'TryOpenAudio', BoolToStr(result, true));
{$ENDIF}
end;
procedure OpenAudio();
begin
{$IFDEF TRACE}
TraceEnter('sgAudio', 'OpenAudio', '');
{$ENDIF}
if not TryOpenAudio() then RaiseException('Error opening audio device: ' + string(AudioDriver.GetError()));
{$IFDEF TRACE}
TraceExit('sgAudio', 'OpenAudio');
{$ENDIF}
end;
function AudioReady(): Boolean;
begin
{$IFDEF TRACE}
TraceEnter('sgAudio', 'AudioReady', '');
{$ENDIF}
if not _firstLoad then
begin
OpenAudio();
_firstLoad := true;
end;
result := sgShared.AudioOpen;
{$IFDEF TRACE}
TraceExit('sgAudio', 'AudioReady', BoolToStr(result, true));
{$ENDIF}
end;
procedure CloseAudio();
begin
{$IFDEF TRACE}
TraceEnter('sgAudio', 'CloseAudio', '');
{$ENDIF}
AudioOpen := False;
AudioDriver.CloseAudio();
{$IFDEF TRACE}
TraceExit('sgAudio', 'CloseAudio');
{$ENDIF}
end;
procedure SetMusicVolume(value: Single);
begin
{$IFDEF TRACE}
TraceEnter('sgAudio', 'SetMusicVolume', FloatToStr(value));
{$ENDIF}
if (value < 0) then value := 0
else if value > 1 then value := 1;
AudioDriver.SetMusicVolume(value);
{$IFDEF TRACE}
TraceExit('sgAudio', 'SetMusicVolume');
{$ENDIF}
end;
function MusicVolume(): Single;
begin
{$IFDEF TRACE}
TraceEnter('sgAudio', 'MusicVolume', '');
{$ENDIF}
result := AudioDriver.GetMusicVolume();
{$IFDEF TRACE}
TraceExit('sgAudio', 'MusicVolume', FloatToStr(result));
{$ENDIF}
end;
//----------------------------------------------------------------------------
// Private:
// Called by LoadSoundEffectNamed
function DoLoadSoundEffect(filename, name: String): SoundEffect;
begin
{$IFDEF TRACE}
TraceEnter('sgAudio', 'DoLoadSoundEffect', name + ' = ' + filename);
{$ENDIF}
if not FileExists(filename) then
begin
filename := PathToResource(filename, SoundResource);
if not FileExists(filename) then
begin
RaiseWarning('Unable to locate ' + name + ' sound effect file at ' + filename);
result := nil;
exit;
end;
end;
result := AudioDriver.LoadSoundEffect(filename, name);
{$IFDEF TRACE}
TraceExit('sgAudio', 'DoLoadSoundEffect', HexStr(result));
{$ENDIF}
end;
function LoadSoundEffect(filename: String): SoundEffect;
begin
{$IFDEF TRACE}
TraceEnter('sgAudio', 'LoadSoundEffect', filename);
{$ENDIF}
result := LoadSoundEffectNamed(filename, filename);
{$IFDEF TRACE}
TraceExit('sgAudio', 'LoadSoundEffect');
{$ENDIF}
end;
function LoadSoundEffectNamed(name, filename: String): SoundEffect;
var
obj: tResourceContainer;
snd: SoundEffect;
begin
{$IFDEF TRACE}
TraceEnter('sgAudio', 'LoadSoundEffectNamed', name + ' = ' + filename);
{$ENDIF}
if not AudioReady() then
begin
{$IFDEF TRACE}
TraceExit('sgAudio', 'LoadSoundEffectNamed', 'Audio Closed');
{$ENDIF}
result := nil;
exit;
end;
if _SoundEffects.containsKey(name) then
begin
result := SoundEffectNamed(name);
exit;
end;
snd := DoLoadSoundEffect(filename, name);
if not assigned(snd) then
begin
result := nil;
exit;
end;
obj := tResourceContainer.Create(snd);
if not _SoundEffects.setValue(name, obj) then
begin
RaiseWarning('** Leaking: Caused by Sound Effect resource loading twice, ' + name);
result := nil;
exit;
end;
result := snd;
{$IFDEF TRACE}
TraceExit('sgAudio', 'LoadSoundEffectNamed');
{$ENDIF}
end;
// private:
// Called to actually free the resource
procedure DoFreeSoundEffect(var effect: SoundEffect);
begin
{$IFDEF TRACE}
TraceEnter('sgAudio', 'DoFreeSoundEffect', 'effect = ' + HexStr(effect));
{$ENDIF}
if assigned(effect) then
begin
CallFreeNotifier(effect);
AudioDriver.FreeSoundEffect(effect);
Dispose(effect);
end;
effect := nil;
{$IFDEF TRACE}
TraceExit('sgAudio', 'DoFreeSoundEffect');
{$ENDIF}
end;
procedure FreeSoundEffect(var effect: SoundEffect);
begin
{$IFDEF TRACE}
TraceEnter('sgAudio', 'FreeSoundEffect', 'effect = ' + HexStr(effect));
{$ENDIF}
if(assigned(effect)) then
begin
ReleaseSoundEffect(effect^.name);
end;
effect := nil;
{$IFDEF TRACE}
TraceExit('sgAudio', 'FreeSoundEffect');
{$ENDIF}
end;
procedure ReleaseSoundEffect(name: String);
var
snd: SoundEffect;
begin
{$IFDEF TRACE}
TraceEnter('sgAudio', 'ReleaseSoundEffect', 'effect = ' + name);
{$ENDIF}
snd := SoundEffectNamed(name);
if (assigned(snd)) then
begin
_SoundEffects.remove(name).Free();
DoFreeSoundEffect(snd);
end;
{$IFDEF TRACE}
TraceExit('sgAudio', 'ReleaseSoundEffect');
{$ENDIF}
end;
procedure ReleaseAllSoundEffects();
begin
{$IFDEF TRACE}
TraceEnter('sgAudio', 'ReleaseAllSoundEffects', '');
{$ENDIF}
ReleaseAll(_SoundEffects, @ReleaseSoundEffect);
{$IFDEF TRACE}
TraceExit('sgAudio', 'ReleaseAllSoundEffects');
{$ENDIF}
end;
//----------------------------------------------------------------------------
function DoLoadMusic(filename, name: String): Music;
begin
{$IFDEF TRACE}
TraceEnter('sgAudio', 'DoLoadMusic', name + ' = ' + filename);
{$ENDIF}
if not FileExists(filename) then
begin
filename := PathToResource(filename, SoundResource);
if not FileExists(filename) then
begin
RaiseWarning('Unable to locate ' + name + ' music file at ' + filename);
result := nil;
exit;
end;
end;
result := AudioDriver.LoadMusic(filename, name);
{$IFDEF TRACE}
TraceExit('sgAudio', 'DoLoadMusic');
{$ENDIF}
end;
function LoadMusic(filename: String): Music;
begin
{$IFDEF TRACE}
TraceEnter('sgAudio', 'LoadMusic', filename);
{$ENDIF}
result := LoadMusicNamed(filename, filename);
{$IFDEF TRACE}
TraceExit('sgAudio', 'LoadMusic');
{$ENDIF}
end;
function LoadMusicNamed(name, filename: String): Music;
var
obj: tResourceContainer;