-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbase.sfx.soundmanager.base.bmx
1375 lines (1003 loc) · 36.2 KB
/
base.sfx.soundmanager.base.bmx
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
SuperStrict
Import Brl.Map
Import Brl.Audio 'for TChannel
Import Brl.LinkedList
Import brl.Map
Import Brl.Reflection
Import "base.util.logger.bmx"
Import "base.util.vector.bmx"
Import "base.util.time.bmx"
Type TSoundManager
Field soundFiles:TMap = CreateMap()
Field activeMusicChannel:TChannel = Null
Field inactiveMusicChannel:TChannel = Null
Field sfxVolume:Float = 1
Field defaulTSfxDynamicSettings:TSfxSettings = Null
Field sfxOn:Int = 1
Field musicOn:Int = 1
Field musicVolume:Float = 1
Field nextMusicVolume:Float = 1
Field lastTitleNumber:Int = 0
Field inactiveMusicStream:TDigAudioStream = Null
Field activeMusicStream:TDigAudioStream = Null
Field nextMusicStream:TDigAudioStream = Null
'do auto crossfade X milliseconds before song end, 0 disables
Field autoCrossFadeTime:Int = 1500
'disable to skip fading on next song switch
Field autoCrossFadeNextSong:Int = True
'crossfade time for the current crossfade
field crossFadeTime:int = 1500
Field defaultMusicVolume:Float = 1.0
Field forceNextMusic:Int = 0
Field fadeProcess:Int = 0 '0 = nicht aktiv 1 = aktiv
'time when fading started
Field fadeProcessTime:Long = 0
Field soundSources:TIntMap = new TIntMap
Field receiver:TSoundSourcePosition
Field _currentPlaylistName:String = "default"
'a named array of playlists, playlists contain available musicStreams
Field playlists:TMap = CreateMap()
'contains keys (IDs) and names (string representations and driver names)
'of supported audio engines
Field engineKeys:string[]
Field engineDriverNames:string[]
Field engineNames:string[]
Global instance:TSoundManager
Global PREFIX_MUSIC:String = "MUSIC_"
Global PREFIX_SFX:String = "SFX_"
Global audioEngineEnabled:int = True
Global audioEngine:String = "AUTOMATIC"
Global isRefillBufferRunning:Int = False
Function Create:TSoundManager()
Local manager:TSoundManager = New TSoundManager
'initialize sound system
manager.InitAudioEngine()
manager.defaulTSfxDynamicSettings = TSfxSettings.Create()
Return manager
End Function
Method GetAudioEngineDriverName:string(engineKey:string)
if not engineKeys or engineKeys.length = 0 then FillAudioEngines()
for local i:int = 0 until engineKeys.length
if engineKeys[i] = engineKey then return engineDriverNames[i]
Next
return "default"
End Method
Method GetAudioEngineKeys:String[]()
if not engineKeys or engineKeys.length = 0 then FillAudioEngines()
return engineKeys
End Method
Method GetAudioEngineDriverNames:String[]()
if not engineDriverNames or engineDriverNames.length = 0 then FillAudioEngines()
return engineDriverNames
End Method
Method GetAudioEngineNames:String[]()
if not engineNames or engineNames.length = 0 then FillAudioEngines()
return engineNames
End Method
Method FillAudioEngines:int()
End Method
Method SetAudioEngine:int(engine:String)
engine = engine.ToUpper()
if engine = audioEngine then return False
audioEngine = "AUTOMATIC"
local keys:string[] = GetAudioEngineKeys()
'local driverNames:string[] = GetAudioEngineDriverNames()
for local i:int = 0 until keys.length
if engine = keys[i]
audioEngine = keys[i]
exit
endif
Next
return True
End Method
Method InitSpecificAudioEngine:int(engineKey:string)
local engineDriver:string = GetAudioEngineDriverName(engineKey)
If Not SetAudioDriver(engineKey)
if engineKey = audioEngine
TLogger.Log("SoundManager.SetAudioEngine()", "audio engine ~q"+engineDriver+" [" + engineKey+"]~q (configured) failed.", LOG_ERROR)
else
TLogger.Log("SoundManager.SetAudioEngine()", "audio engine ~q"+engineDriver+" [" + engineKey+"]~q failed.", LOG_ERROR)
endif
Return False
Else
Return True
endif
End Method
Method InitAudioEngine:int()
local engines:String[] = [audioEngine]
local otherEngineKeys:String[] = GetAudioEngineKeys()
for local i:int = 0 until otherEngineKeys.length
if otherEngineKeys[i].ToLower() <> audioEngine.toLower()
engines :+ [otherEngineKeys[i]]
endif
Next
'try to init one of the engines, starting with the manually set
'audioEngine
local foundWorkingEngine:string = ""
if audioEngine <> "NONE"
For local engineKey:string = eachin engines
if InitSpecificAudioEngine(engineKey)
foundWorkingEngine = engineKey
exit
endif
Next
endif
'if no sound engine initialized successfully, use the dummy
'output (no sound)
if foundWorkingEngine = ""
TLogger.Log("SoundManager.SetAudioEngine()", "No working audio engine found. Disabling sound.", LOG_ERROR)
DisableAudioEngine()
Return False
endif
local workingDriver:string = GetAudioEngineDriverName(foundWorkingEngine)
TLogger.Log("SoundManager.SetAudioEngine()", "initialized with engine ~q"+workingDriver +" ["+foundWorkingEngine+"]~q.", LOG_DEBUG)
Return True
End Method
Function GetInstance:TSoundManager()
If Not instance Then instance = TSoundManager.Create()
Return instance
End Function
Method ApplyConfig(soundEngine:String="", musicVolume:Float=1.0, sfxVolume:Float=1.0, playlist:String="")
if soundEngine.ToLower() = "none"
MuteMusic(true)
MuteSfx(true)
audioEngineEnabled = False
elseif soundEngine <> ""
audioEngineEnabled = true
if SetAudioEngine(soundEngine.ToUpper())
'new and old engine differed, so initialize the engine
InitAudioEngine()
endif
endif
self.sfxVolume = sfxVolume
SetMusicvolume(musicVolume)
if audioEngineEnabled
MuteSfx(sfxVolume = 0.0)
MuteMusic(musicVolume = 0)
if not HasMutedMusic()
'if no music is played yet, try to get one from the "menu"-playlist
If Not isPlaying() and playlist
PlayMusicPlaylist(playlist)
endif
endif
endif
End Method
Function DisableAudioEngine:int()
audioEngineEnabled = False
End Function
'override for FreeAudio-specific checks
Method FixChannel:int(sfxChannel:TSfxChannel)
return False
End Method
'use 0 to disable feature
Method SetAutoCrossfadeTime:int(milliseconds:int = 0)
autoCrossFadeTime = milliseconds
End Method
Method GetDefaultReceiver:TSoundSourcePosition()
Return receiver
End Method
Method SetDefaultReceiver(_receiver:TSoundSourcePosition)
receiver = _receiver
End Method
'playlists is a comma separated string of playlists this music wants to
'be stored in
Method AddSound:Int(name:String, sound:Object, playlists:String="default")
Self.soundFiles.insert(Lower(name), sound)
Local playlistsArray:String[] = playlists.split(",")
For Local playlist:String = EachIn playlistsArray
playlist = playlist.Trim() 'remove whitespace
AddSoundToPlaylist(playlist, name, sound)
Next
End Method
Method AddSoundToPlaylist(playlist:String="default", name:String, sound:Object)
If TSound(sound)
playlist = PREFIX_SFX + Lower(playlist)
ElseIf TDigAudioStream(sound)
playlist = PREFIX_MUSIC + Lower(playlist)
EndIf
name = Lower(name)
'if not done yet - create a new playlist entry
'fetch the playlist
Local playlistContainer:TList
If Not playlists.contains(playlist)
playlistContainer = CreateList()
playlists.insert(playlist, playlistContainer)
Else
playlistContainer = TList(playlists.ValueForKey(playlist))
EndIf
playlistContainer.AddLast(sound)
End Method
Method GetCurrentPlaylist:String()
Return _currentPlaylistName:String
End Method
Method SetCurrentPlaylist(name:String="default")
_currentPlaylistName = name
End Method
'use this method if multiple sfx for a certain event are possible
'(so eg. multiple "door open/close"-sounds to make variations
Method GetRandomSfxFromPlaylist:TSound(playlist:String)
playlist = playlist.ToLower()
Local playlistContainer:TList = TList(playlists.ValueForKey(PREFIX_SFX + playlist))
If Not playlistContainer
TLogger.Log("SoundManager.GetRandomSfxFromPlaylist()", "Playlist ~q"+playlist+"~q not found.", LOG_WARNING)
Return Null
EndIf
If playlistContainer.count() = 0
TLogger.Log("SoundManager.GetRandomSfxFromPlaylist()", "Playlist ~q"+playlist+"~q is empty.", LOG_WARNING)
Return Null
EndIf
Return TSound(playlistContainer.ValueAtIndex(Rand(0, playlistContainer.count()-1)))
End Method
'if avoidMusic is set, the function tries to return another music (if possible)
Method GetRandomMusicFromPlaylist:TDigAudioStream(playlist:String, avoidMusic:TDigAudioStream = Null)
playlist = playlist.ToLower()
Local playlistContainer:TList = TList(playlists.ValueForKey(PREFIX_MUSIC + playlist))
If Not playlistContainer
'TLogger.Log("SoundManager.GetRandomMusicFromPlaylist()", "Playlist ~q"+playlist+"~q not found.", LOG_WARNING)
Return Null
EndIf
If playlistContainer.count() = 0
'TLogger.Log("SoundManager.GetRandomMusicFromPlaylist()", "Playlist ~q"+playlist+"~q is empty.", LOG_WARNING)
Return Null
EndIf
Local result:TDigAudioStream
'try to find another music file
If avoidMusic And playlistContainer.count()>1
Repeat
result = TDigAudioStream(playlistContainer.ValueAtIndex(Rand(0, playlistContainer .count()-1)))
Until result <> avoidMusic
Else
result = TDigAudioStream(playlistContainer.ValueAtIndex(Rand(0, playlistContainer .count()-1)))
EndIf
Return result
End Method
Method RemoveSoundSource:int(soundSource:TSoundSourceElement)
If Not soundSource then Return False
If Not soundSources.ValueForKey(soundSource.GetID()) then Return False
'stop playing all sfx - else "looped" continue playing
soundSource.StopAll()
soundSources.Remove(soundSource.GetID())
Return True
End Method
Method RegisterSoundSource:int(soundSource:TSoundSourceElement)
if not soundSource then return False
If Not soundSources.ValueForKey(soundSource.GetID())
soundSources.Insert(soundSource.GetID(), soundSource)
endif
End Method
Method GetSoundSource:TSoundSourceElement(ID:Int)
return TSoundSourceElement(soundSources.ValueForKey(ID))
End Method
Method IsPlaying:Int()
If Not activeMusicChannel Then Return False
Return activeMusicChannel.Playing()
End Method
Method StopSFX:Int()
'stop playing all sfx - else "looped" continue playing
For local soundSource:TSoundSourceElement = EachIn soundSources.Values()
soundSource.StopAll()
Next
soundSources.Clear()
End Method
Method Mute:Int(bool:Int=True)
If bool
TLogger.Log("TSoundManager.Mute()", "Muting all sounds", LOG_DEBUG)
Else
TLogger.Log("TSoundManager.Mute()", "Unmuting all sounds", LOG_DEBUG)
EndIf
MuteSfx(bool)
MuteMusic(bool)
End Method
Method MuteSfx:Int(bool:Int=True)
'already muted
if sfxOn = Not bool then return False
If bool
TLogger.Log("TSoundManager.MuteSfx()", "Muting all sound effects", LOG_DEBUG)
Else
TLogger.Log("TSoundManager.MuteSfx()", "Unmuting all sound effects", LOG_DEBUG)
EndIf
For Local element:TSoundSourceElement = EachIn soundSources.Values()
element.mute(bool)
Next
sfxOn = Not bool
return True
End Method
Method MuteMusic:Int(bool:Int=True)
'already muted
if musicOn = Not bool then return False
if not audioEngineEnabled then return False
If bool
TLogger.Log("TSoundManager.MuteMusic()", "Muting music", LOG_DEBUG)
Else
TLogger.Log("TSoundManager.MuteMusic()", "Unmuting music", LOG_DEBUG)
EndIf
If bool
If activeMusicChannel Then PauseChannel(activeMusicChannel)
If inactiveMusicChannel Then inactiveMusicChannel.Stop()
Else
If activeMusicChannel Then ResumeChannel(activeMusicChannel)
EndIf
musicOn = Not bool
return True
End Method
Method IsMuted:Int()
If sfxOn Or musicOn Then Return False
Return True
End Method
Method HasMutedMusic:Int()
Return Not musicOn
End Method
Method HasMutedSfx:Int()
Return Not sfxOn
End Method
Method RefillBuffers:int()
'currently executed?
if isRefillBufferRunning then return False
isRefillBufferRunning = True
If inactiveMusicStream then inactiveMusicStream.Update()
If activeMusicStream then activeMusicStream.Update()
isRefillBufferRunning = False
End Method
Method UpdateSFX()
If not sfxOn Then Return
For Local element:TSoundSourceElement = EachIn soundSources.Values()
element.Update()
Next
End Method
Method UpdateMusic:Int()
'nothing to do if no activated channel exists
If Not activeMusicChannel Then Return False
'also skip actions with muted music
If HasMutedMusic() then return False
'=== START PLAYING ===
'if nothing was playing yet, just start except we are playing already
If not activeMusicChannel.Playing() 'and not activeMusicStream ' and fadeProcess = 0
TLogger.Log("TSoundManager.Update()", "No active music channel playing, start new from current playlist", LOG_DEBUG)
PlayMusicPlaylist(GetCurrentPlaylist())
'enable autocrossfading for next song if disabled in the
'past (eg through playing the same song twice)
if autoCrossFadeTime > 0 then autoCrossFadeNextSong = True
EndIf
'=== AUTO CROSS FADE ===
'autocrossfade to the next song ?
'only start a new fade if not already fading
if autoCrossFadeNextSong and autoCrossFadeTime > 0 and activeMusicStream and fadeProcess = 0
local timeLeft:int = activeMusicStream.GetTimeLeft()
'if it is looped do not use the normal stream/track time but
'something taking loop amount into account
if activeMusicStream.loop then timeLeft = activeMusicStream.GetLoopedPlaytimeLeft()
if timeLeft < autoCrossFadeTime
PlayMusicPlaylist(GetCurrentPlaylist())
StartFadeOverToNextTitle(autoCrossFadeTime)
endif
endif
'=== FORCE NEXT MUSIC ===
'if something enforced next music, cross fade to it
If forceNextMusic And nextMusicStream
'TLogger.log("TSoundManager.Update()", "FadeOverToNextTitle. fadeProcess=" + fadeProcess, LOG_DEBUG)
StartFadeOverToNextTitle(autoCrossFadeTime)
EndIf
'=== UPDATE CROSS FADE ===
UpdateFadeOverToNextTitle()
return True
End Method
Method Update:Int()
'skip updates if muted
If isMuted() Then Return False
UpdateSFX()
UpdateMusic()
Return True
End Method
Method StartFadeOverToNextTitle:int(fadeTime:int = -1)
if not audioEngineEnabled then return False
?debug
print "StartFadeOverToNextTitle()"
?
If fadeProcess <> 0
?debug
print "StartFadeOverToNextTitle() called during other fading"
?
return False
EndIf
'set default if none was given
if fadeTime = -1 then
self.crossFadeTime = autoCrossFadeTime
else
self.crossFadeTime = fadeTime
endif
'maybe we missed the crossfade or "jumped beyond end"
'so this limits fade to it and in case of already reaching
'the end this means we instantly switch music
local timeLeft:int = activeMusicStream.GetTimeLeft()
if activeMusicStream.loop then timeLeft = activeMusicStream.GetLoopedPlaytimeLeft()
self.crossFadeTime = Max(0, Min(self.crossFadeTime, timeLeft))
?debug
if activeMusicStream.loop
print " crossfade current track looptime left: " + activeMusicStream.GetLoopedPlaytimeLeft() + " played="+activeMusicStream.GetLoopedTimePlayed() +" total="+activeMusicStream.GetLoopedPlaytime()
print " crossFadeTime: " + self.crossFadeTime
else
print " crossfade current track time Left: " + activeMusicStream.GetTimeLeft() + " played="+activeMusicStream.GetTimePlayed() +" total="+activeMusicStream.GetTimeTotal()
print " crossFadeTime: " + self.crossFadeTime
endif
?
'fade in the next channel
fadeProcess = 1
fadeProcessTime = Time.MillisecsLong()
'load next music "into" a new inactive channel and start playing
inactiveMusicChannel = nextMusicStream.CreateChannel(0)
ResumeChannel(inactiveMusicChannel)
'switches inactive channel/stream with active channel/stream
SwitchMusicChannels()
'set current next one
activeMusicStream = nextMusicStream
nextMusicStream = Null
?debug
print "FadeOverToNextTitle(): inactiveMusicStream.IsPlaying() = " + inactiveMusicStream.IsPlaying()
print "FadeOverToNextTitle(): activeMusicStream.IsPlaying() = " + activeMusicStream.IsPlaying()
?
forceNextMusic = False
End Method
Method UpdateFadeOverToNextTitle:int()
if not audioEngineEnabled then return False
'fade out of old (inactive) channel
If fadeProcess = 1
Local fadeValue:Float = 1.0
if self.crossFadeTime > 0
fadeValue = Max(0.0, Min(1.0, (Time.MillisecsLong() - fadeProcessTime) / float(crossFadeTime)))
endif
inactiveMusicChannel.SetVolume((1.0 - fadeValue) * musicVolume)
activeMusicChannel.SetVolume(fadeValue * nextMusicVolume)
if fadeValue >= 1.0 then fadeProcess = 2
EndIf
'fade finished
If fadeProcess = 2
?debug
print "FadeOverToNextTitle() finished"
?
fadeProcess = 0
musicVolume = nextMusicVolume
'stops the stream AND the channel (which might also remove it)
inactiveMusicStream.Stop()
'removes that channel!
inactiveMusicChannel.Stop()
'remove the whole stream (and the associated channel)
inactiveMusicStream = null
inactiveMusicChannel = null
'make sure active is unpaused
if activeMusicStream then activeMusicStream.SetPlaying(true)
EndIf
End Method
Method SetMusicVolume:int(volume:Float)
'disturbs fading!
if activeMusicChannel then activeMusicChannel.SetVolume(volume)
if inactiveMusicChannel then inactiveMusicChannel.SetVolume(volume)
musicVolume = volume
nextMusicVolume = volume
defaultMusicVolume = volume
End Method
Method SwitchMusicChannels()
Local channelTemp:TChannel = Self.activeMusicChannel
Local streamTemp:TDigAudioStream = Self.activeMusicStream
Self.activeMusicChannel = Self.inactiveMusicChannel
Self.activeMusicStream = Self.inactiveMusicStream
Self.inactiveMusicChannel = channelTemp
Self.inactiveMusicStream = streamTemp
End Method
Method PlaySfx:int(sfx:TSound, channel:TChannel)
if not audioEngineEnabled then return False
If Not HasMutedSfx() And sfx Then PlaySound(sfx, Channel)
End Method
Method PlayMusicPlaylist(playlist:String)
PlayMusicOrPlayList(playlist, True)
End Method
Method PlayMusic(music:String)
PlayMusicOrPlayList(music, False)
End Method
Method PlayMusicOrPlaylist:Int(name:String, fromPlaylist:Int=False)
if not audioEngineEnabled then return False
If HasMutedMusic() Then Return True
If fromPlaylist
nextMusicStream = GetMusicStream("", name)
nextMusicVolume = GetMusicVolume(name)
If nextMusicStream
SetCurrentPlaylist(name)
TLogger.Log("PlayMusicOrPlaylist", "GetMusicStream from Playlist ~q"+name+"~q. Also set current playlist to it.", LOG_DEBUG)
Else
TLogger.Log("PlayMusicOrPlaylist", "GetMusicStream from Playlist ~q"+name+"~q not possible. No Playlist.", LOG_DEBUG)
EndIf
Else
nextMusicStream = GetMusicStream(name, "")
nextMusicVolume = GetMusicVolume(name)
TLogger.Log("PlayMusicOrPlaylist", "GetMusicStream by name ~q"+name+"~q", LOG_DEBUG)
EndIf
if not nextMusicStream
TLogger.Log("PlayMusicOrPlaylist", "Music not found. Using random from default playlist", LOG_DEBUG)
nextMusicStream = GetRandomMusicFromPlaylist("default")
nextMusicVolume = defaultMusicVolume
endif
forceNextMusic = True
'start to play music if not done yet
If Not activeMusicChannel or Not activeMusicChannel.Playing()
If Not nextMusicStream
TLogger.Log("PlayMusicOrPlaylist", "could not start activeMusicChannel: no next music found", LOG_DEBUG)
Else
TLogger.Log("PlayMusicOrPlaylist", "start activeMusicChannel", LOG_DEBUG)
Local musicVolume:Float = nextMusicVolume
if activeMusicChannel then activeMusicChannel.Stop()
activeMusicChannel = nextMusicStream.CreateChannel(musicVolume)
activeMusicChannel.SetPaused(False)
inactiveMusicStream = activeMusicStream
activeMusicStream = nextMusicStream
forceNextMusic = False
EndIf
EndIf
'While playMusicPlayList will return the next song,
'there is no guarantee that it does not return the
'same song again
'the nature of the stream's buffer is, that you
'cannot play 1 stream in 2 different channels
'that is why fading needs a clone if songs are the same
if autoCrossFadeNextSong and nextMusicStream
if nextMusicStream = activeMusicStream
'print "playing same song: creating clone"
nextMusicStream = activeMusicStream.Clone()
nextMusicVolume = musicVolume
endif
endif
End Method
'returns if there would be a stream to play
'use this to avoid music changes if there is no new stream available
Method HasMusicStream:Int(music:String="", playlist:String="")
If playlist=""
Return Null <> TDigAudioStream(soundFiles.ValueForKey(Lower(music)))
Else
Return Null <> GetRandomMusicFromPlaylist(playlist, nextMusicStream)
EndIf
End Method
Method GetMusicStream:TDigAudioStream(music:String="", playlist:String="")
Local result:TDigAudioStream
If playlist=""
result = TDigAudioStream(soundFiles.ValueForKey(Lower(music)))
TLogger.Log("TSoundManager.GetMusicStream()", "Play music: " + music, LOG_DEBUG)
Else
'try to get a song differing to the current one
result = GetRandomMusicFromPlaylist(playlist, activeMusicStream)
'result = GetRandomMusicFromPlaylist(playlist, nextMusicStream)
Rem
if result
TLogger.log("TSoundManager.GetMusicStream()", "Play random music from playlist: ~q" + playlist +"~q file: ~q"+result.url+"~q", LOG_DEBUG)
else
TLogger.log("TSoundManager.GetMusicStream()", "Cannot play random music from playlist: ~q" + playlist +"~q, nothing found.", LOG_DEBUG)
endif
endrem
EndIf
Return result
End Method
Method GetSfx:TSound(sfx:String="", playlist:String="")
Local result:TSound
If playlist=""
result = TSound(soundFiles.ValueForKey(Lower(sfx)))
'TLogger.log("TSoundManager.GetSfx()", "Play sfx: " + sfx, LOG_DEBUG)
Else
result = GetRandomSfxFromPlaylist(playlist)
'TLogger.log("TSoundManager.GetSfx()", "Play random sfx from playlist: " + playlist, LOG_DEBUG)
EndIf
Return result
End Method
rem
'by default all sfx share the same volume
Method GetSfxVolume:Float(sfx:String)
Return 0.2
End Method
endrem
'by default all music share the same volume
Method GetMusicVolume:Float(music:String)
Return defaultMusicVolume
End Method
Method CreateDigAudioStreamOgg:TDigAudioStream(uri:string, loop:int)
return null
End Method
End Type
'===== CONVENIENCE ACCESSORS =====
'convenience instance getter
Function GetSoundManagerBase:TSoundManager()
return TSoundManager.GetInstance()
End Function
'base class
Type TDigAudioStream
Field playing:int = False
Field playtime:int = 0
Field loop:Int = False
Field lastChannelTime:Long
Method Clone:TDigAudioStream(deepClone:Int = False) abstract
Method CreateChannel:TChannel(volume:Float) abstract
Method GetChannel:TChannel() abstract
Method GetChannelPosition:int()
return 0
End Method
Method Stop()
SetPlaying(False)
End Method
Method Update()
'do stream updates here if needed
End Method
Method GetURI:object()
return null
End Method
Method IsPlaying:int()
return playing
End Method
Method SetPlaying:int(playing:int)
self.playing = playing
End Method
'for looped sounds...
Method SetLoopedPlaytime:int(playtime:int)
self.playtime = playtime
End Method
Method GetLoopedPlaytime:int()
return playtime
End Method
Method GetLoopedTimePlayed:int()
if lastChannelTime = 0 then return 0
return Time.MillisecsLong() - lastChannelTime
End Method
Method GetLoopedPlaytimeLeft:Int()
return GetLoopedPlaytime() - GetLoopedTimePlayed()
End Method
'returns time left in milliseconds
Method GetTimeLeft:Int()
Return GetLoopedPlaytimeLeft()
End Method
'returns milliseconds
Method GetTimePlayed:Int()
return GetLoopedTimePlayed()
End Method
'returns milliseconds
Method GetTimeBuffered:Int()
return 0
End Method
'returns milliseconds
Method GetTimeTotal:Int()
return GetLoopedPlaytime()
End Method
End Type
Type TDigAudioStreamManager
Field streams:TList = CreateList()
Method AddStream:Int(stream:TDigAudioStream)
streams.AddLast(stream)
Return True
End Method
Method RemoveStream:Int(stream:TDigAudioStream)
streams.Remove(stream)
Return True
End Method
Method Update:Int()
For Local stream:TDigAudioStream = EachIn streams
stream.Update()
Next
End Method
End Type
Global DigAudioStreamManager:TDigAudioStreamManager = New TDigAudioStreamManager
'base class wrapping a normal channel to add extended features
Type TSfxChannel
'preallocating channels returns invalid channels if done before the
'soundengine (eg. FreeAudio) is initialized
'-> channel.fa_channel is 0 then
Field _Channel:TChannel '= AllocChannel()
Field CurrentSfx:String
Field CurrentSettings:TSfxSettings
Field MuteAfterCurrentSfx:Int
Function Create:TSfxChannel()
Return New TSfxChannel
End Function
Method GetChannel:TChannel()
'Ask sound manager to check the channel
TSoundManager.GetInstance().FixChannel(self)
If not _channel
_channel = AllocChannel()
'print "AllocChannel() for ~q" + CurrentSfx+"~q. _channel="+_channel.ToString() + " time="+Millisecs()
EndIf
return _channel
End Method
Method PlaySfx(sfx:String, settings:TSfxSettings=Null)
'skip adjustments and loading the sound
if TSoundManager.GetInstance().HasMutedSfx() then return
CurrentSfx = sfx
CurrentSettings = settings
AdjustSettings(False)
Local sound:TSound = TSoundManager.GetInstance().GetSfx(sfx)
TSoundManager.GetInstance().PlaySfx(sound, GetChannel())
End Method
Method PlayRandomSfx(playlist:String, settings:TSfxSettings=Null)
'skip adjustments and loading the sound
if TSoundManager.GetInstance().HasMutedSfx() then return
CurrentSfx = playlist
CurrentSettings = settings
AdjustSettings(False)
Local sound:TSound = TSoundManager.GetInstance().GetSfx("", playlist)
if not sound or not GetChannel() then return
TSoundManager.GetInstance().PlaySfx(sound, GetChannel())
'if sound then PlaySound(sound, channel)
End Method
Method IsActive:Int()
If not _channel then return False
Return _channel.Playing()
End Method
Method Stop()
If not _channel then return