-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbase.sfx.soundmanager.sdlaudio.bmx
930 lines (701 loc) · 26.8 KB
/
base.sfx.soundmanager.sdlaudio.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
SuperStrict
Import brl.Map
'Import brl.WAVLoader
'Import brl.OGGLoader
Import sdl.SDLAudio
Import "base.util.logger.bmx"
Import "base.util.vector.bmx"
'the needed module files are located in "external/maxmod2_lite.mod.zip"
'Import MaxMod2.ogg
'Import MaxMod2.rtaudio
'Import MaxMod2.rtaudionopulse
'Import MaxMod2.WAV
'type to store music files (ogg) in it
'data is stored in bank
'Play-Method is adopted from maxmod2.bmx-Function "play"
Type TDigAudioStream
Field bank:TBank
Field loop:Int
Field url:String
Function Create:TDigAudioStream(url:Object, loop:Int=False)
Local obj:TDigAudioStream = New TDigAudioStream
obj.bank = LoadBank(url)
obj.loop = loop
obj.url = "unknown"
If String(url) Then obj.url=String(url)
Return obj
End Function
Method Clone:TDigAudioStream(deepClone:int = False)
local c:TDigAudioStream = new TDigAudioStream
c.bank = self.bank
c.loop = self.loop
c.url = self.url
return c
End Method
Method isValid:Int()
If Not Self.bank Then Return False
Return True
End Method
Method GetChannel:TChannel(volume:Float)
Local channel:TChannel = CueMusic(Self.bank, loop)
channel.SetVolume(volume)
Return channel
End Method
End Type
Type TDigAudioStreamOgg extends TDigAudioStream
Method CreateWithFile:TDigAudioStreamOgg(url:object, loop:int = False, useMemoryStream:int = False)
self.bank = LoadBank(url)
self.loop = loop
self.url = "unknown"
If String(url) Then self.url=String(url)
return self
End Method
Method Clone:TDigAudioStreamOgg(deepClone:int = False)
local c:TDigAudioStreamOgg = new TDigAudioStreamOgg
c.bank = self.bank
c.loop = self.loop
c.url = self.url
return c
End Method
End Type
Type TSoundManager
Field soundFiles:TMap = CreateMap()
Field musicChannel1:TChannel = Null
Field musicChannel2:TChannel = Null
Field activeMusicChannel:TChannel = Null
Field inactiveMusicChannel:TChannel = Null
Field sfxChannel_Elevator:TChannel = Null
Field sfxChannel_Elevator2:TChannel = Null
Field sfxVolume:Float = 1
Field defaulTSfxDynamicSettings:TSfxSettings = Null
Field sfxOn:Int = 1
Field musicOn:Int = 1
Field musicVolume:Float = 1
Field nextMusicTitleVolume:Float = 1
Field lastTitleNumber:Int = 0
Field currenTDigAudioStream:TDigAudioStream = Null
Field nextMusicTitleStream:TDigAudioStream = Null
Field currentMusic:TSound = Null
Field nextMusicTitle:TSound = Null
Field forceNextMusicTitle:Int = 0
Field fadeProcess:Int = 0 '0 = nicht aktiv 1 = aktiv
Field fadeOutVolume:Int = 1000
Field fadeInVolume:Int = 0
Field soundSources:TList = CreateList()
Field receiver:TSoundSourcePosition
Field _currentPlaylistName:String = "default"
Field playlists:TMap = CreateMap() 'a named array of playlists, playlists contain available musicStreams
Global instance:TSoundManager
Global PREFIX_MUSIC:String = "MUSIC_"
Global PREFIX_SFX:String = "SFX_"
Global audioEngineEnabled:int = True
Global audioEngine:String = "AUTOMATIC"
Function Create:TSoundManager()
Local manager:TSoundManager = New TSoundManager
'initialize sound system
InitAudioEngine()
manager.musicChannel1 = AllocChannel()
manager.musicChannel2 = AllocChannel()
manager.sfxChannel_Elevator = AllocChannel()
manager.sfxChannel_Elevator2 = AllocChannel()
manager.defaulTSfxDynamicSettings = TSfxSettings.Create()
Return manager
End Function
Function SetAudioEngine(engine:string)
'limit to allowed engines
Select engine.ToUpper()
case "NONE"
audioEngine = "NONE"
case "LINUX_ALSA"
audioEngine = "LINUX_ALSA"
case "LINUX_PULSE"
audioEngine = "LINUX_PULSE"
case "LINUX_OSS"
audioEngine = "LINUX_OSS"
'following are currently not compiled in the rtAudio module
'case "UNIX_JACK"
' audioEngine = "UNIX_JACK"
case "MACOSX_CORE"
audioEngine = "MACOSX_CORE"
case "WINDOWS_ASIO"
audioEngine = "WINDOWS_ASIO"
case "WINDOWS_DS"
audioEngine = "WINDOWS_DS"
default
audioEngine = "AUTOMATIC"
End Select
End Function
Function InitSpecificAudioEngine:int(engine:string)
SetAudioDriver("SDLAudio")
rem
TMaxModRtAudioDriver.Init(engine)
'
If Not SetAudioDriver("MaxMod RtAudio")
if engine = audioEngine
TLogger.Log("SoundManager.SetAudioEngine()", "audio engine ~q"+engine+"~q (configured) failed.", LOG_ERROR)
else
TLogger.Log("SoundManager.SetAudioEngine()", "audio engine ~q"+engine+"~q failed.", LOG_ERROR)
endif
Return False
Else
Return True
endif
endrem
End Function
Function InitAudioEngine:int()
rem
'reenable rtAudio-messages
TMaxModRtAudioDriver.showWarnings(False)
local engines:String[] = [audioEngine]
'add automatic-engine if manual setup is not already set to it
if audioEngine <> "AUTOMATIC" then engines :+ ["AUTOMATIC"]
?Linux
if audioEngine <> "LINUX_PULSE" then engines :+ ["LINUX_PULSE"]
if audioEngine <> "LINUX_ALSA" then engines :+ ["LINUX_ALSA"]
if audioEngine <> "LINUX_OSS" then engines :+ ["LINUX_OSS"]
'if audioEngine <> "UNIX_JACK" then engines :+ ["UNIX_JACK"]
?MacOS
'ATTENTION: WITHOUT ENABLED SOUNDCARD THIS CRASHES!
engines :+ ["MACOSX_CORE"]
?Win32
engines :+ ["WINDOWS_ASIO"]
engines :+ ["WINDOWS_DS"]
?
'try to init one of the engines, starting with the manually set
'audioEngine
local foundWorkingEngine:string = ""
if audioEngine <> "NONE"
For local engine:string = eachin engines
if InitSpecificAudioEngine(engine)
foundWorkingEngine = engine
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
'reenable rtAudio-messages
TMaxModRtAudioDriver.showWarnings(True)
TLogger.Log("SoundManager.SetAudioEngine()", "initialized with engine ~q"+foundWorkingEngine+"~q.", LOG_DEBUG)
endrem
Return True
End Function
Function GetInstance:TSoundManager()
If Not instance Then instance = TSoundManager.Create()
Return instance
End Function
Function DisableAudioEngine:int()
audioEngineEnabled = False
End Function
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)
Local playlistContainer:TList = TList(playlists.ValueForKey(PREFIX_SFX + playlist))
If Not playlistContainer Then Print "playlist: "+playlist+" not found."; Return Null
If playlistContainer.count() = 0 Then Print "empty list:"+PREFIX_SFX + playlist; Return Null
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)
Local playlistContainer:TList = TList(playlists.ValueForKey(PREFIX_MUSIC + playlist))
If Not playlistContainer
'TLogger.Log("GetRandomMusicFromPlaylist", "No playlist: "+playlist+" found.", LOG_WARNING)
Return Null
EndIf
If playlistContainer.count() = 0
'TLogger.Log("GetRandomMusicFromPlaylist", "playlist: "+playlist+" 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
' local playlistContainer:TDigAudioStream[] = TDigAudioStream[](playlists.ValueForKey("MUSIC_"+playlist))
' if playlistContainer.length = 0 then print "empty list:"+"MUSIC_"+playlist; return NULL
' return playlistContainer[Rand(0, playlistContainer.length-1)]
End Method
Method RegisterSoundSource(soundSource:TSoundSourceElement)
If Not soundSources.Contains(soundSource) Then soundSources.AddLast(soundSource)
End Method
Method IsPlaying:Int()
If Not activeMusicChannel Then Return False
Return activeMusicChannel.Playing()
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)
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
element.mute(bool)
Next
sfxOn = Not bool
End Method
Method MuteMusic:Int(bool:Int=True)
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
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 Update:Int()
'skip updates if muted
If isMuted() Then Return True
If sfxOn
For Local element:TSoundSourceElement = EachIn soundSources
element.Update()
Next
EndIf
If Not HasMutedMusic()
'Wenn der Musik-Channel nicht läuft, dann muss nichts gemacht werden
If Not activeMusicChannel Then Return True
'if the music didn't stop yet
If activeMusicChannel.Playing()
If (forceNextMusicTitle And nextMusicTitleStream) Or fadeProcess > 0
' TLogger.log("TSoundManager.Update()", "FadeOverToNextTitle", LOG_DEBUG)
FadeOverToNextTitle()
EndIf
'no music is playing, just start
Else
TLogger.Log("TSoundManager.Update()", "PlayMusicPlaylist", LOG_DEBUG)
PlayMusicPlaylist(GetCurrentPlaylist())
EndIf
EndIf
End Method
Method FadeOverToNextTitle:int()
if not audioEngineEnabled then return False
If (fadeProcess = 0) Then
fadeProcess = 1
inactiveMusicChannel = nextMusicTitleStream.GetChannel(0)
ResumeChannel(inactiveMusicChannel)
nextMusicTitleStream = Null
forceNextMusicTitle = False
fadeOutVolume = 1000
fadeInVolume = 0
EndIf
If (fadeProcess = 1) Then 'Das fade out des aktiven Channels
fadeOutVolume = fadeOutVolume - 15
activeMusicChannel.SetVolume(fadeOutVolume/1000.0 * musicVolume)
fadeInVolume = fadeInVolume + 15
inactiveMusicChannel.SetVolume(fadeInVolume/1000.0 * nextMusicTitleVolume)
EndIf
If fadeOutVolume <= 0 And fadeInVolume >= 1000 Then
fadeProcess = 0 'Prozess beendet
musicVolume = nextMusicTitleVolume
SwitchMusicChannels()
EndIf
End Method
Method SwitchMusicChannels()
Local channelTemp:TChannel = Self.activeMusicChannel
Self.activeMusicChannel = Self.inactiveMusicChannel
Self.inactiveMusicChannel = channelTemp
Self.inactiveMusicChannel.Stop()
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
nextMusicTitleStream = GeTDigAudioStream("", name)
nextMusicTitleVolume = GetMusicVolume(name)
If nextMusicTitleStream
SetCurrentPlaylist(name)
TLogger.Log("PlayMusicOrPlaylist", "GeTDigAudioStream from Playlist ~q"+name+"~q. Also set current playlist to it.", LOG_DEBUG)
Else
TLogger.Log("PlayMusicOrPlaylist", "GeTDigAudioStream from Playlist ~q"+name+"~q not possible. No Playlist.", LOG_DEBUG)
EndIf
Else
nextMusicTitleStream = GeTDigAudioStream(name, "")
nextMusicTitleVolume = GetMusicVolume(name)
TLogger.Log("PlayMusicOrPlaylist", "GeTDigAudioStream by name ~q"+name+"~q", LOG_DEBUG)
EndIf
forceNextMusicTitle = True
'Wenn der Musik-Channel noch nicht laeuft, dann jetzt starten
If Not activeMusicChannel Or Not activeMusicChannel.Playing()
If Not nextMusicTitleStream
TLogger.Log("PlayMusicOrPlaylist", "could not start activeMusicChannel: no next music found", LOG_DEBUG)
Else
TLogger.Log("PlayMusicOrPlaylist", "start activeMusicChannel", LOG_DEBUG)
Local musicVolume:Float = nextMusicTitleVolume
activeMusicChannel = nextMusicTitleStream.GetChannel(musicVolume)
ResumeChannel(activeMusicChannel)
forceNextMusicTitle = False
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, nextMusicTitleStream)
EndIf
End Method
Method GeTDigAudioStream:TDigAudioStream(music:String="", playlist:String="")
Local result:TDigAudioStream
If playlist=""
result = TDigAudioStream(soundFiles.ValueForKey(Lower(music)))
TLogger.Log("TSoundManager.GeTDigAudioStream()", "Play music: " + music, LOG_DEBUG)
Else
result = GetRandomMusicFromPlaylist(playlist, nextMusicTitleStream)
Rem
if result
TLogger.log("TSoundManager.GeTDigAudioStream()", "Play random music from playlist: ~q" + playlist +"~q file: ~q"+result.url+"~q", LOG_DEBUG)
else
TLogger.log("TSoundManager.GeTDigAudioStream()", "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
'by default all sfx share the same volume
Method GetSfxVolume:Float(sfx:String)
Return 0.2
End Method
'by default all music share the same volume
Method GetMusicVolume:Float(music:String)
Return 1.0
End Method
End Type
'===== CONVENIENCE ACCESSORS =====
'convenience instance getter
Function GetSoundManager:TSoundManager()
return TSoundManager.GetInstance()
End Function
'Diese Basisklasse ist ein Wrapper für einen normalen Channel mit erweiterten Funktionen
Type TSfxChannel
Field Channel:TChannel = AllocChannel()
Field CurrentSfx:String
Field CurrentSettings:TSfxSettings
Field MuteAfterCurrentSfx:Int
Function Create:TSfxChannel()
Return New TSfxChannel
End Function
Method PlaySfx(sfx:String, settings:TSfxSettings=Null)
CurrentSfx = sfx
CurrentSettings = settings
AdjustSettings(False)
Local sound:TSound = TSoundManager.GetInstance().GetSfx(sfx)
TSoundManager.GetInstance().PlaySfx(sound, Channel)
End Method
Method PlayRandomSfx(playlist:String, settings:TSfxSettings=Null)
CurrentSfx = playlist
CurrentSettings = settings
AdjustSettings(False)
Local sound:TSound = TSoundManager.GetInstance().GetSfx("", playlist)
TSoundManager.GetInstance().PlaySfx(sound, Channel)
'if sound then PlaySound(sound, channel)
End Method
Method IsActive:Int()
Return Channel.Playing()
End Method
Method Stop()
Channel.Stop()
End Method
Method Mute(bool:Int=True)
If bool
If MuteAfterCurrentSfx And IsActive()
AdjustSettings(True)
Else
Channel.SetVolume(0)
EndIf
Else
Channel.SetVolume(TSoundManager.GetInstance().sfxVolume)
EndIf
End Method
Method AdjustSettings(isUpdate:Int)
If Not isUpdate
channel.SetVolume(TSoundManager.GetInstance().sfxVolume * 0.75 * CurrentSettings.GetVolume()) '0.75 ist ein fixer Wert die Lautstärke der Sfx reduzieren soll
EndIf
End Method
End Type
'Der dynamische SfxChannel hat die Möglichkeit abhängig von der Position von Sound-Quelle und Empfänger dynamische Modifikationen an den Einstellungen vorzunehmen. Er wird bei jedem Update aktualisiert.
Type TDynamicSfxChannel Extends TSfxChannel
Field Source:TSoundSourceElement
Field Receiver:TSoundSourcePosition
Function CreateDynamicSfxChannel:TSfxChannel(source:TSoundSourceElement=Null)
Local sfxChannel:TDynamicSfxChannel = New TDynamicSfxChannel
sfxChannel.Source = source
Return sfxChannel
End Function
Method SetReceiver(_receiver:TSoundSourcePosition)
Self.Receiver = _receiver
End Method
Method AdjustSettings(isUpdate:Int)
Local sourcePoint:TVec3D = Source.GetCenter()
Local receiverPoint:TVec3D = Receiver.GetCenter() 'Meistens die Position der Spielfigur
If CurrentSettings.forceVolume
channel.SetVolume(CurrentSettings.defaultVolume)
'print "Volume:" + CurrentSettings.defaultVolume
Else
'Lautstärke ist Abhängig von der Entfernung zur Geräuschquelle
Local distanceVolume:Float = CurrentSettings.GetVolumeByDistance(Source, Receiver)
channel.SetVolume(TSoundManager.GetInstance().sfxVolume * distanceVolume) ''0.75 ist ein fixer Wert die Lautstärke der Sfx reduzieren soll
'print "Volume: " + (SoundManager.sfxVolume * distanceVolume)
EndIf
If (sourcePoint.z = 0) Then
'170 Grenzwert = Erst aber dem Abstand von 170 (gefühlt/geschätzt) hört man nur noch von einer Seite.
'Ergebnis sollte ungefähr zwischen -1 (links) und +1 (rechts) liegen.
If CurrentSettings.forcePan
channel.SetPan(CurrentSettings.defaultPan)
Else
channel.SetPan(Float(sourcePoint.x - receiverPoint.x) / 170)
EndIf
channel.SetDepth(0) 'Die Tiefe spielt keine Rolle, da elemenTVec2D.z = 0
Else
Local zDistance:Float = Abs(sourcePoint.z - receiverPoint.z)
If CurrentSettings.forcePan
channel.SetPan(CurrentSettings.defaultPan)
'print "Pan:" + CurrentSettings.defaultPan
Else
Local xDistance:Float = Abs(sourcePoint.x - receiverPoint.x)
Local yDistance:Float = Abs(sourcePoint.y - receiverPoint.y)
Local angleZX:Float = ATan(zDistance / xDistance) 'Winkelfunktion: Welchen Winkel hat der Hörer zur Soundquelle. 90° = davor/dahiner 0° = gleiche Ebene tan(alpha) = Gegenkathete / Ankathete
Local rawPan:Float = ((90 - angleZX) / 90)
Local panCorrection:Float = Max(0, Min(1, xDistance / 170)) 'Den r/l Effekt sollte noch etwas abgeschwächt werden, wenn die Quelle nah ist
Local correctPan:Float = rawPan * panCorrection
'0° => Aus einer Richtung / 90° => aus beiden Richtungen
If (sourcePoint.x < receiverPoint.x) Then 'von links
channel.SetPan(-correctPan)
'print "Pan:" + (-correctPan) + " - angleZX: " + angleZX + " (" + xDistance + "/" + zDistance + ") # " + rawPan + " / " + panCorrection
ElseIf (sourcePoint.x > receiverPoint.x) Then 'von rechts
channel.SetPan(correctPan)
'print "Pan:" + correctPan + " - angleZX: " + angleZX + " (" + xDistance + "/" + zDistance + ") # " + rawPan + " / " + panCorrection
Else
channel.SetPan(0)
EndIf
EndIf
If CurrentSettings.forceDepth
channel.SetDepth(CurrentSettings.defaultDepth)
'print "Depth:" + CurrentSettings.defaultDepth
Else
Local angleOfDepth:Float = ATan(receiverPoint.DistanceTo(sourcePoint, False) / zDistance) '0 = direkt hinter mir/vor mir, 90° = über/unter/neben mir
If sourcePoint.z < 0 Then 'Hintergrund
channel.SetDepth(-((90 - angleOfDepth) / 90)) 'Minuswert = Hintergrund / Pluswert = Vordergrund
'print "Depth:" + (-((90 - angleOfDepth) / 90)) + " - angle: " + angleOfDepth + " (" + receiverPoint.DistanceTo(sourcePoint, false) + "/" + zDistance + ")"
ElseIf sourcePoint.z > 0 Then 'Vordergrund
channel.SetDepth((90 - angleOfDepth) / 90) 'Minuswert = Hintergrund / Pluswert = Vordergrund
'print "Depth:" + ((90 - angleOfDepth) / 90) + " - angle: " + angleOfDepth + " (" + receiverPoint.DistanceTo(sourcePoint, false) + "/" + zDistance + ")"
EndIf
EndIf
EndIf
End Method
End Type
Type TSfxSettings
Field forceVolume:Float = False
Field forcePan:Float = False
Field forceDepth:Float = False
Field defaultVolume:Float = 1
Field defaultPan:Float = 0
Field defaultDepth:Float = 0
Field nearbyDistanceRange:Int = -1
' Field nearbyDistanceRangeTopY:int -1
' Field nearbyDistanceRangeBottomY:int -1 hier war ich
Field maxDistanceRange:Int = 1000
Field nearbyRangeVolume:Float = 1
Field midRangeVolume:Float = 0.8
Field minVolume:Float = 0
Function Create:TSfxSettings()
Return New TSfxSettings
End Function
Method GetVolume:Float()
Return defaultVolume
End Method
Method GetVolumeByDistance:Float(source:TSoundSourceElement, receiver:TSoundSourcePosition)
Local currentDistance:Int = source.GetCenter().DistanceTo(receiver.getCenter())
Local result:Float = midRangeVolume
If (currentDistance <> -1) Then
If currentDistance > Self.maxDistanceRange Then 'zu weit weg
result = Self.minVolume
ElseIf currentDistance < Self.nearbyDistanceRange Then 'sehr nah dran
result = Self.nearbyRangeVolume
Else 'irgendwo dazwischen
result = midRangeVolume * (Float(Self.maxDistanceRange) - Float(currentDistance)) / Float(Self.maxDistanceRange)
EndIf
EndIf
Return result
End Method
End Type
Type TSoundSourcePosition 'Basisklasse für verschiedene Wrapper
Method GetID:String() Abstract
Method GetCenter:TVec3D() Abstract
Method IsMovable:Int() Abstract
End Type
Type TSoundSourceElement Extends TSoundSourcePosition
Field SfxChannels:TMap = CreateMap()
Method GetIsHearable:Int() Abstract
Method GetChannelForSfx:TSfxChannel(sfx:String) Abstract
Method GetSfxSettings:TSfxSettings(sfx:String) Abstract
Method OnPlaySfx:Int(sfx:String) Abstract
Method GetReceiver:TSoundSourcePosition()
Return TSoundManager.GetInstance().GetDefaultReceiver()
End Method
Method PlayRandomSfx(playlist:String, sfxSettings:TSfxSettings=Null)
PlaySfxOrPlaylist(playlist, sfxSettings, True)
End Method
Method PlaySfx(sfx:String, sfxSettings:TSfxSettings=Null)
PlaySfxOrPlaylist(sfx, sfxSettings, False)
End Method
Method PlaySfxOrPlaylist(name:String, sfxSettings:TSfxSettings=Null, playlistMode:Int=False)
If Not GetIsHearable() Then Return
If Not OnPlaySfx(name) Then Return
'print GetID() + " # PlaySfx: " + sfx
TSoundManager.GetInstance().RegisterSoundSource(Self)
Local channel:TSfxChannel = GetChannelForSfx(name)
Local settings:TSfxSettings = sfxSettings
If settings = Null Then settings = GetSfxSettings(name)
If TDynamicSfxChannel(channel)
TDynamicSfxChannel(channel).SetReceiver(GetReceiver())
EndIf
If playlistMode
channel.PlayRandomSfx(name, settings)
Else
channel.PlaySfx(name, settings)
EndIf
'print GetID() + " # End PlaySfx: " + sfx
End Method
Method PlayOrContinueRandomSfx(playlist:String, sfxSettings:TSfxSettings=Null)
PlayOrContinueSfxOrPlaylist(playlist, sfxSettings, True)
End Method
Method PlayOrContinueSfx(sfx:String, sfxSettings:TSfxSettings=Null)
PlayOrContinueSfxOrPlaylist(sfx, sfxSettings, False)
End Method
Method PlayOrContinueSfxOrPlaylist(name:String, sfxSettings:TSfxSettings=Null, playlistMode:Int=False)
Local channel:TSfxChannel = GetChannelForSfx(name)
If Not channel.IsActive()
'Print "PlayOrContinueSfx: start"
PlaySfxOrPlaylist(name, sfxSettings, playlistMode)
Else
'Print "PlayOrContinueSfx: Continue"
EndIf
End Method
Method Stop(sfx:String)
Local channel:TSfxChannel = GetChannelForSfx(sfx)
channel.Stop()
End Method
Method Mute:Int(bool:Int=True)
For Local sfxChannel:TSfxChannel = EachIn MapValues(SfxChannels)
sfxChannel.Mute(bool)
Next
End Method
Method Update()
If GetIsHearable()
For Local sfxChannel:TSfxChannel = EachIn MapValues(SfxChannels)
If sfxChannel.IsActive() Then sfxChannel.AdjustSettings(True)
Next
Else
For Local sfxChannel:TSfxChannel = EachIn MapValues(SfxChannels)
sfxChannel.Mute()
Next
EndIf
End Method
Method AddDynamicSfxChannel:TSfxChannel(name:String, muteAfterSfx:Int=False)
Local sfxChannel:TSfxChannel = TDynamicSfxChannel.CreateDynamicSfxChannel(Self)
sfxChannel.MuteAfterCurrentSfx = muteAfterSfx
SfxChannels.insert(name, sfxChannel)
Return sfxChannel
End Method
Method GetSfxChannelByName:TSfxChannel(name:String)
Return TSfxChannel(MapValueForKey(SfxChannels, name))
End Method
End Type