-
Notifications
You must be signed in to change notification settings - Fork 13
/
godot_fmod.cpp
1746 lines (1559 loc) · 62.8 KB
/
godot_fmod.cpp
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
/*************************************************************************/
/* godot_fmod.cpp */
/*************************************************************************/
/* */
/* FMOD Studio module and bindings for the Godot game engine */
/* */
/*************************************************************************/
/* Copyright (c) 2020 Alex Fonseka */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "godot_fmod.h"
Mutex *Callbacks::mut;
Fmod *Fmod::singleton = nullptr;
void Fmod::init(int numOfChannels, int studioFlags, int flags) {
// initialize FMOD Studio and FMOD Core System with provided flags
if (checkErrors(system->initialize(numOfChannels, studioFlags, flags, nullptr))) {
print_line("FMOD Sound System: Successfully initialized");
if (studioFlags & FMOD_STUDIO_INIT_LIVEUPDATE)
print_line("FMOD Sound System: Live update enabled!");
} else
print_error("FMOD Sound System: Failed to initialize :|");
}
void Fmod::update() {
// clean up one shots
for (auto e = events.front(); e; e = e->next()) {
FMOD::Studio::EventInstance *eventInstance = e->get();
EventInfo *eventInfo = getEventInfo(eventInstance);
if (eventInfo->gameObj) {
if (isNull(eventInfo->gameObj)) {
FMOD_STUDIO_STOP_MODE m = FMOD_STUDIO_STOP_IMMEDIATE;
checkErrors(eventInstance->stop(m));
releaseOneEvent(eventInstance);
continue;
}
updateInstance3DAttributes(eventInstance, eventInfo->gameObj);
}
}
// clean up invalid channel references
clearChannelRefs();
// update listener position
setListenerAttributes();
// if events are subscribed to callbacks, update them
runCallbacks();
// finally, dispatch an update call to FMOD
checkErrors(system->update());
}
void Fmod::updateInstance3DAttributes(FMOD::Studio::EventInstance *instance, Object *o) {
// try to set 3D attributes
if (instance && !isNull(o)) {
CanvasItem *ci = Object::cast_to<CanvasItem>(o);
if (ci != nullptr) { // GameObject is 2D
Transform2D t2d = ci->get_transform();
Vector2 posVector = t2d.get_origin() / distanceScale;
// in 2D, the distance is measured in pixels
// TODO: Revise the set3DAttributes call. In 2D, the emitters must directly face the listener.
Vector3 pos(posVector.x, 0.0f, posVector.y),
up(0, 1, 0), forward(0, 0, 1), vel(0, 0, 0); // TODO: add doppler
FMOD_3D_ATTRIBUTES attr = get3DAttributes(toFmodVector(pos), toFmodVector(up), toFmodVector(forward), toFmodVector(vel));
checkErrors(instance->set3DAttributes(&attr));
} else { // GameObject is 3D
// needs testing
Spatial *s = Object::cast_to<Spatial>(o);
Transform t = s->get_transform();
Vector3 pos = t.get_origin() / distanceScale;
Vector3 up = t.get_basis().elements[1];
Vector3 forward = t.get_basis().elements[2];
Vector3 vel(0, 0, 0);
FMOD_3D_ATTRIBUTES attr = get3DAttributes(toFmodVector(pos), toFmodVector(up), toFmodVector(forward), toFmodVector(vel));
checkErrors(instance->set3DAttributes(&attr));
}
}
}
void Fmod::shutdown() {
checkErrors(system->unloadAll());
checkErrors(system->release());
}
void Fmod::setListenerAttributes() {
if (listeners.size() == 0) {
if (listenerWarning) {
print_error("FMOD Sound System: No listeners are set!");
listenerWarning = false;
}
return;
}
clearNullListeners();
for (int i = 0; i < listeners.size(); i++) {
auto listener = listeners[i];
CanvasItem *ci = Object::cast_to<CanvasItem>(listener.gameObj);
if (ci != nullptr) { // Listener is in 2D space
Transform2D t2d = ci->get_transform();
Vector2 posVector = t2d.get_origin() / distanceScale;
// in 2D, the distance is measured in pixels
// TODO: Revise the set3DAttributes call. In 2D, the listener must be a few units away from
// the emitters (or the screen) and must face them directly.
Vector3 pos(posVector.x, 0.0f, posVector.y),
up(0, 1, 0), forward(0, 0, 1), vel(0, 0, 0); // TODO: add doppler
FMOD_3D_ATTRIBUTES attr = get3DAttributes(toFmodVector(pos), toFmodVector(up), toFmodVector(forward), toFmodVector(vel));
if (!listener.listenerLock) checkErrors(system->setListenerAttributes(i, &attr));
} else { // Listener is in 3D space
// needs testing
Spatial *s = Object::cast_to<Spatial>(listener.gameObj);
Transform t = s->get_transform();
Vector3 pos = t.get_origin() / distanceScale;
Vector3 up = t.get_basis().elements[1];
Vector3 forward = t.get_basis().elements[2];
Vector3 vel(0, 0, 0);
FMOD_3D_ATTRIBUTES attr = get3DAttributes(toFmodVector(pos), toFmodVector(up), toFmodVector(forward), toFmodVector(vel));
if (!listener.listenerLock) checkErrors(system->setListenerAttributes(i, &attr));
}
}
}
void Fmod::addListener(Object *gameObj) {
if (listeners.size() == FMOD_MAX_LISTENERS) {
print_error("FMOD Sound System: Could not add listener. System already at max listeners.");
return;
}
Listener listener;
listener.gameObj = gameObj;
listeners.push_back(listener);
checkErrors(system->setNumListeners(listeners.size()));
}
void Fmod::removeListener(uint8_t index) {
if (index < 0 || index + 1 > listeners.size()) {
print_error("FMOD Sound System: Invalid listener ID");
return;
}
listeners.erase(listeners.begin() + index);
checkErrors(system->setNumListeners(listeners.size() == 0 ? 1 : listeners.size()));
std::string s = "FMOD Sound System: Listener at index " + std::to_string(index) + " was removed";
print_line(s.c_str());
}
void Fmod::setSoftwareFormat(int sampleRate, int speakerMode, int numRawSpeakers) {
auto m = static_cast<FMOD_SPEAKERMODE>(speakerMode);
checkErrors(coreSystem->setSoftwareFormat(sampleRate, m, numRawSpeakers));
}
void Fmod::setGlobalParameterByName(const String ¶meterName, float value) {
checkErrors(system->setParameterByName(parameterName.ascii().get_data(), value));
}
float Fmod::getGlobalParameterByName(const String ¶meterName) {
float value = 0.f;
checkErrors(system->getParameterByName(parameterName.ascii().get_data(), &value));
return value;
}
void Fmod::setGlobalParameterByID(const Array &idPair, float value) {
if (idPair.size() != 2) {
print_error("FMOD Sound System: Invalid parameter ID");
return;
}
FMOD_STUDIO_PARAMETER_ID id;
id.data1 = idPair[0];
id.data2 = idPair[1];
checkErrors(system->setParameterByID(id, value));
}
float Fmod::getGlobalParameterByID(const Array &idPair) {
if (idPair.size() != 2) {
print_error("FMOD Sound System: Invalid parameter ID");
return -1.f;
}
FMOD_STUDIO_PARAMETER_ID id;
id.data1 = idPair[0];
id.data2 = idPair[1];
float value = -1.f;
checkErrors(system->getParameterByID(id, &value));
return value;
}
Dictionary Fmod::getGlobalParameterDescByName(const String ¶meterName) {
Dictionary paramDesc;
FMOD_STUDIO_PARAMETER_DESCRIPTION pDesc;
if (checkErrors(system->getParameterDescriptionByName(parameterName.ascii().get_data(), &pDesc))) {
paramDesc["name"] = String(pDesc.name);
paramDesc["id_first"] = pDesc.id.data1;
paramDesc["id_second"] = pDesc.id.data2;
paramDesc["minimum"] = pDesc.minimum;
paramDesc["maximum"] = pDesc.maximum;
paramDesc["default_value"] = pDesc.defaultvalue;
}
return paramDesc;
}
Dictionary Fmod::getGlobalParameterDescByID(const Array &idPair) {
if (idPair.size() != 2) {
print_error("FMOD Sound System: Invalid parameter ID");
return Dictionary();
}
Dictionary paramDesc;
FMOD_STUDIO_PARAMETER_ID id;
id.data1 = idPair[0];
id.data2 = idPair[1];
FMOD_STUDIO_PARAMETER_DESCRIPTION pDesc;
if (checkErrors(system->getParameterDescriptionByID(id, &pDesc))) {
paramDesc["name"] = String(pDesc.name);
paramDesc["id_first"] = pDesc.id.data1;
paramDesc["id_second"] = pDesc.id.data2;
paramDesc["minimum"] = pDesc.minimum;
paramDesc["maximum"] = pDesc.maximum;
paramDesc["default_value"] = pDesc.defaultvalue;
}
return paramDesc;
}
uint32_t Fmod::getGlobalParameterDescCount() {
int count = 0;
checkErrors(system->getParameterDescriptionCount(&count));
return count;
}
Array Fmod::getGlobalParameterDescList() {
Array a;
FMOD_STUDIO_PARAMETER_DESCRIPTION descList[256];
int count = 0;
checkErrors(system->getParameterDescriptionList(descList, 256, &count));
for (int i = 0; i < count; i++) {
auto pDesc = descList[i];
Dictionary paramDesc;
paramDesc["name"] = String(pDesc.name);
paramDesc["id_first"] = pDesc.id.data1;
paramDesc["id_second"] = pDesc.id.data2;
paramDesc["minimum"] = pDesc.minimum;
paramDesc["maximum"] = pDesc.maximum;
paramDesc["default_value"] = pDesc.defaultvalue;
a.append(paramDesc);
}
return a;
}
Array Fmod::getAvailableDrivers() {
Array driverList;
int numDrivers = 0;
checkErrors(coreSystem->getNumDrivers(&numDrivers));
for (int i = 0; i < numDrivers; i++) {
char name[256];
int sampleRate;
FMOD_SPEAKERMODE speakerMode;
int speakerModeChannels;
checkErrors(coreSystem->getDriverInfo(i, name, 256, nullptr, &sampleRate, &speakerMode, &speakerModeChannels));
String nameStr(name);
Dictionary driverInfo;
driverInfo["id"] = i;
driverInfo["name"] = nameStr;
driverInfo["sample_rate"] = sampleRate;
driverInfo["speaker_mode"] = (int)speakerMode;
driverInfo["number_of_channels"] = speakerModeChannels;
driverList.push_back(driverInfo);
}
return driverList;
}
int Fmod::getDriver() {
int driverId = 0;
checkErrors(coreSystem->getDriver(&driverId));
return driverId;
}
void Fmod::setDriver(uint8_t id) {
checkErrors(coreSystem->setDriver(id));
}
Dictionary Fmod::getPerformanceData() {
Dictionary performanceData;
// get the CPU usage
FMOD_STUDIO_CPU_USAGE cpuUsage;
checkErrors(system->getCPUUsage(&cpuUsage));
Dictionary cpuPerfData;
cpuPerfData["dsp"] = cpuUsage.dspusage;
cpuPerfData["geometry"] = cpuUsage.geometryusage;
cpuPerfData["stream"] = cpuUsage.streamusage;
cpuPerfData["studio"] = cpuUsage.studiousage;
cpuPerfData["update"] = cpuUsage.updateusage;
performanceData["CPU"] = cpuPerfData;
// get the memory usage
int currentAlloc = 0;
int maxAlloc = 0;
checkErrors(FMOD::Memory_GetStats(¤tAlloc, &maxAlloc));
Dictionary memPerfData;
memPerfData["currently_allocated"] = currentAlloc;
memPerfData["max_allocated"] = maxAlloc;
performanceData["memory"] = memPerfData;
// get the file usage
long long sampleBytesRead = 0;
long long streamBytesRead = 0;
long long otherBytesRead = 0;
checkErrors(coreSystem->getFileUsage(&sampleBytesRead, &streamBytesRead, &otherBytesRead));
Dictionary filePerfData;
filePerfData["sample_bytes_read"] = (uint64_t)sampleBytesRead;
filePerfData["stream_bytes_read"] = (uint64_t)streamBytesRead;
filePerfData["other_bytes_read"] = (uint64_t)otherBytesRead;
performanceData["file"] = filePerfData;
return performanceData;
}
void Fmod::setListenerLock(uint8_t index, bool isLocked) {
if (index < 0 || index + 1 > listeners.size()) {
print_error("FMOD Sound System: Invalid listener ID");
return;
}
Listener *listener = &listeners[index];
listener->listenerLock = isLocked;
}
bool Fmod::getListenerLock(uint8_t index) {
if (index < 0 || index + 1 > listeners.size()) {
print_error("FMOD Sound System: Invalid listener ID");
return false;
}
return listeners[index].listenerLock;
}
void Fmod::waitForAllLoads() {
checkErrors(system->flushSampleLoading());
}
String Fmod::loadbank(const String &pathToBank, int flags) {
if (banks.has(pathToBank)) return pathToBank; // bank is already loaded
FMOD::Studio::Bank *bank = nullptr;
checkErrors(system->loadBankFile(pathToBank.ascii().get_data(), flags, &bank));
if (bank) {
banks.insert(pathToBank, bank);
return pathToBank;
}
return pathToBank;
}
void Fmod::unloadBank(const String &pathToBank) {
if (!banks.has(pathToBank)) return; // bank is not loaded
auto bank = banks.find(pathToBank);
if (bank->value()) {
checkErrors(bank->value()->unload());
banks.erase(pathToBank);
}
}
int Fmod::getBankLoadingState(const String &pathToBank) {
if (!banks.has(pathToBank)) return -1; // bank is not loaded
auto bank = banks.find(pathToBank);
if (bank->value()) {
FMOD_STUDIO_LOADING_STATE state;
checkErrors(bank->value()->getLoadingState(&state));
return state;
}
return -1;
}
int Fmod::getBankBusCount(const String &pathToBank) {
if (banks.has(pathToBank)) {
int count;
auto bank = banks.find(pathToBank);
if (bank->value()) checkErrors(bank->value()->getBusCount(&count));
return count;
}
return -1;
}
int Fmod::getBankEventCount(const String &pathToBank) {
if (banks.has(pathToBank)) {
int count;
auto bank = banks.find(pathToBank);
if (bank->value()) checkErrors(bank->value()->getEventCount(&count));
return count;
}
return -1;
}
int Fmod::getBankStringCount(const String &pathToBank) {
if (banks.has(pathToBank)) {
int count;
auto bank = banks.find(pathToBank);
if (bank->value()) checkErrors(bank->value()->getStringCount(&count));
return count;
}
return -1;
}
int Fmod::getBankVCACount(const String &pathToBank) {
if (banks.has(pathToBank)) {
int count;
auto bank = banks.find(pathToBank);
if (bank->value()) checkErrors(bank->value()->getVCACount(&count));
return count;
}
return -1;
}
uint64_t Fmod::descCreateInstance(uint64_t descHandle) {
if (!ptrToEventDescMap.has(descHandle)) return 0;
auto desc = ptrToEventDescMap.find(descHandle)->value();
auto instance = createInstance(desc, false, nullptr);
if (instance)
return (uint64_t)instance;
return 0;
}
int Fmod::descGetLength(uint64_t descHandle) {
if (!ptrToEventDescMap.has(descHandle)) return -1;
auto desc = ptrToEventDescMap.find(descHandle)->value();
int length = 0;
checkErrors(desc->getLength(&length));
return length;
}
String Fmod::descGetPath(uint64_t descHandle) {
if (!ptrToEventDescMap.has(descHandle)) return String("Invalid handle!");
auto desc = ptrToEventDescMap.find(descHandle)->value();
char path[256];
int retrived = 0;
checkErrors(desc->getPath(path, 256, &retrived));
return String(path);
}
Array Fmod::descGetInstanceList(uint64_t descHandle) {
Array array;
if (!ptrToEventDescMap.has(descHandle)) return array;
auto desc = ptrToEventDescMap.find(descHandle)->value();
FMOD::Studio::EventInstance *arr[128];
int count = 0;
checkErrors(desc->getInstanceList(arr, 128, &count));
for (int i = 0; i < count; i++) {
array.append((uint64_t)arr[i]);
}
return array;
}
int Fmod::descGetInstanceCount(uint64_t descHandle) {
if (!ptrToEventDescMap.has(descHandle)) return -1;
auto desc = ptrToEventDescMap.find(descHandle)->value();
int count = 0;
checkErrors(desc->getInstanceCount(&count));
return count;
}
void Fmod::descReleaseAllInstances(uint64_t descHandle) {
if (!ptrToEventDescMap.has(descHandle)) return;
auto desc = ptrToEventDescMap.find(descHandle)->value();
checkErrors(desc->releaseAllInstances());
}
void Fmod::descLoadSampleData(uint64_t descHandle) {
if (!ptrToEventDescMap.has(descHandle)) return;
auto desc = ptrToEventDescMap.find(descHandle)->value();
checkErrors(desc->loadSampleData());
}
void Fmod::descUnloadSampleData(uint64_t descHandle) {
if (!ptrToEventDescMap.has(descHandle)) return;
auto desc = ptrToEventDescMap.find(descHandle)->value();
checkErrors(desc->unloadSampleData());
}
int Fmod::descGetSampleLoadingState(uint64_t descHandle) {
if (!ptrToEventDescMap.has(descHandle)) return -1;
auto desc = ptrToEventDescMap.find(descHandle)->value();
FMOD_STUDIO_LOADING_STATE s;
checkErrors(desc->getSampleLoadingState(&s));
return s;
}
bool Fmod::descIs3D(uint64_t descHandle) {
if (!ptrToEventDescMap.has(descHandle)) return false;
auto desc = ptrToEventDescMap.find(descHandle)->value();
bool is3D = false;
checkErrors(desc->is3D(&is3D));
return is3D;
}
bool Fmod::descIsOneShot(uint64_t descHandle) {
if (!ptrToEventDescMap.has(descHandle)) return false;
auto desc = ptrToEventDescMap.find(descHandle)->value();
bool isOneShot = false;
checkErrors(desc->isOneshot(&isOneShot));
return isOneShot;
}
bool Fmod::descIsSnapshot(uint64_t descHandle) {
if (!ptrToEventDescMap.has(descHandle)) return false;
auto desc = ptrToEventDescMap.find(descHandle)->value();
bool isSnapshot = false;
checkErrors(desc->isSnapshot(&isSnapshot));
return isSnapshot;
}
bool Fmod::descIsStream(uint64_t descHandle) {
if (!ptrToEventDescMap.has(descHandle)) return false;
auto desc = ptrToEventDescMap.find(descHandle)->value();
bool isStream = false;
checkErrors(desc->isStream(&isStream));
return isStream;
}
bool Fmod::descHasCue(uint64_t descHandle) {
if (!ptrToEventDescMap.has(descHandle)) return false;
auto desc = ptrToEventDescMap.find(descHandle)->value();
bool hasCue = false;
checkErrors(desc->hasCue(&hasCue));
return hasCue;
}
float Fmod::descGetMaximumDistance(uint64_t descHandle) {
if (!ptrToEventDescMap.has(descHandle)) return 0.f;
auto desc = ptrToEventDescMap.find(descHandle)->value();
float maxDist = 0.f;
checkErrors(desc->getMaximumDistance(&maxDist));
return maxDist;
}
float Fmod::descGetMinimumDistance(uint64_t descHandle) {
if (!ptrToEventDescMap.has(descHandle)) return 0.f;
auto desc = ptrToEventDescMap.find(descHandle)->value();
float minDist = 0.f;
checkErrors(desc->getMinimumDistance(&minDist));
return minDist;
}
float Fmod::descGetSoundSize(uint64_t descHandle) {
if (!ptrToEventDescMap.has(descHandle)) return 0.f;
auto desc = ptrToEventDescMap.find(descHandle)->value();
float soundSize = 0.f;
checkErrors(desc->getSoundSize(&soundSize));
return soundSize;
}
Dictionary Fmod::descGetParameterDescriptionByName(uint64_t descHandle, const String &name) {
Dictionary paramDesc;
if (!ptrToEventDescMap.has(descHandle)) return paramDesc;
auto desc = ptrToEventDescMap.find(descHandle)->value();
FMOD_STUDIO_PARAMETER_DESCRIPTION pDesc;
if (checkErrors(desc->getParameterDescriptionByName(name.ascii().get_data(), &pDesc))) {
paramDesc["name"] = String(pDesc.name);
paramDesc["id_first"] = pDesc.id.data1;
paramDesc["id_second"] = pDesc.id.data2;
paramDesc["minimum"] = pDesc.minimum;
paramDesc["maximum"] = pDesc.maximum;
paramDesc["default_value"] = pDesc.defaultvalue;
}
return paramDesc;
}
Dictionary Fmod::descGetParameterDescriptionByID(uint64_t descHandle, const Array &idPair) {
Dictionary paramDesc;
if (!ptrToEventDescMap.has(descHandle) || idPair.size() != 2) return paramDesc;
auto desc = ptrToEventDescMap.find(descHandle)->value();
FMOD_STUDIO_PARAMETER_ID paramId;
paramId.data1 = (unsigned int)idPair[0];
paramId.data2 = (unsigned int)idPair[1];
FMOD_STUDIO_PARAMETER_DESCRIPTION pDesc;
if (checkErrors(desc->getParameterDescriptionByID(paramId, &pDesc))) {
paramDesc["name"] = String(pDesc.name);
paramDesc["id_first"] = pDesc.id.data1;
paramDesc["id_second"] = pDesc.id.data2;
paramDesc["minimum"] = pDesc.minimum;
paramDesc["maximum"] = pDesc.maximum;
paramDesc["default_value"] = pDesc.defaultvalue;
}
return paramDesc;
}
int Fmod::descGetParameterDescriptionCount(uint64_t descHandle) {
if (!ptrToEventDescMap.has(descHandle)) return 0;
auto desc = ptrToEventDescMap.find(descHandle)->value();
int count = 0;
checkErrors(desc->getParameterDescriptionCount(&count));
return count;
}
Dictionary Fmod::descGetParameterDescriptionByIndex(uint64_t descHandle, int index) {
Dictionary paramDesc;
if (!ptrToEventDescMap.has(descHandle)) return paramDesc;
auto desc = ptrToEventDescMap.find(descHandle)->value();
FMOD_STUDIO_PARAMETER_DESCRIPTION pDesc;
if (checkErrors(desc->getParameterDescriptionByIndex(index, &pDesc))) {
paramDesc["name"] = String(pDesc.name);
paramDesc["id_first"] = pDesc.id.data1;
paramDesc["id_second"] = pDesc.id.data2;
paramDesc["minimum"] = pDesc.minimum;
paramDesc["maximum"] = pDesc.maximum;
paramDesc["default_value"] = pDesc.defaultvalue;
}
return paramDesc;
}
Dictionary Fmod::descGetUserProperty(uint64_t descHandle, String name) {
Dictionary propDesc;
if (!ptrToEventDescMap.has(descHandle)) return propDesc;
auto desc = ptrToEventDescMap.find(descHandle)->value();
FMOD_STUDIO_USER_PROPERTY uProp;
if (checkErrors(desc->getUserProperty(name.ascii().get_data(), &uProp))) {
FMOD_STUDIO_USER_PROPERTY_TYPE fType = uProp.type;
if (fType == FMOD_STUDIO_USER_PROPERTY_TYPE_INTEGER)
propDesc[String(uProp.name)] = uProp.intvalue;
else if (fType == FMOD_STUDIO_USER_PROPERTY_TYPE_BOOLEAN)
propDesc[String(uProp.name)] = (bool)uProp.boolvalue;
else if (fType == FMOD_STUDIO_USER_PROPERTY_TYPE_FLOAT)
propDesc[String(uProp.name)] = uProp.floatvalue;
else if (fType == FMOD_STUDIO_USER_PROPERTY_TYPE_STRING)
propDesc[String(uProp.name)] = String(uProp.stringvalue);
}
return propDesc;
}
int Fmod::descGetUserPropertyCount(uint64_t descHandle) {
if (!ptrToEventDescMap.has(descHandle)) return -1;
auto desc = ptrToEventDescMap.find(descHandle)->value();
int count = 0;
checkErrors(desc->getUserPropertyCount(&count));
return count;
}
Dictionary Fmod::descUserPropertyByIndex(uint64_t descHandle, int index) {
Dictionary propDesc;
if (!ptrToEventDescMap.has(descHandle)) return propDesc;
auto desc = ptrToEventDescMap.find(descHandle)->value();
FMOD_STUDIO_USER_PROPERTY uProp;
if (checkErrors(desc->getUserPropertyByIndex(index, &uProp))) {
FMOD_STUDIO_USER_PROPERTY_TYPE fType = uProp.type;
if (fType == FMOD_STUDIO_USER_PROPERTY_TYPE_INTEGER)
propDesc[String(uProp.name)] = uProp.intvalue;
else if (fType == FMOD_STUDIO_USER_PROPERTY_TYPE_BOOLEAN)
propDesc[String(uProp.name)] = (bool)uProp.boolvalue;
else if (fType == FMOD_STUDIO_USER_PROPERTY_TYPE_FLOAT)
propDesc[String(uProp.name)] = uProp.floatvalue;
else if (fType == FMOD_STUDIO_USER_PROPERTY_TYPE_STRING)
propDesc[String(uProp.name)] = String(uProp.stringvalue);
}
return propDesc;
}
uint64_t Fmod::createEventInstance(const String &eventPath) {
FMOD::Studio::EventInstance *instance = createInstance(eventPath, false, nullptr);
if (instance) {
uint64_t instanceId = (uint64_t)instance;
events.insert(instanceId, instance);
return instanceId;
}
return 0;
}
FMOD::Studio::EventInstance *Fmod::createInstance(const String eventPath, const bool isOneShot, Object *gameObject) {
if (!eventDescriptions.has(eventPath)) {
FMOD::Studio::EventDescription *desc = nullptr;
auto res = checkErrors(system->getEvent(eventPath.ascii().get_data(), &desc));
if (!res) return 0;
eventDescriptions.insert(eventPath, desc);
}
auto desc = eventDescriptions.find(eventPath);
FMOD::Studio::EventInstance *instance;
checkErrors(desc->value()->createInstance(&instance));
if (instance && (!isOneShot || gameObject)) {
auto *eventInfo = new EventInfo();
eventInfo->gameObj = gameObject;
instance->setUserData(eventInfo);
auto instanceId = (uint64_t)instance;
events[instanceId] = instance;
}
return instance;
}
FMOD::Studio::EventInstance *Fmod::createInstance(FMOD::Studio::EventDescription *eventDesc, bool isOneShot, Object *gameObject) {
auto desc = eventDesc;
FMOD::Studio::EventInstance *instance;
checkErrors(desc->createInstance(&instance));
if (instance && (!isOneShot || gameObject)) {
auto *eventInfo = new EventInfo();
eventInfo->gameObj = gameObject;
instance->setUserData(eventInfo);
auto instanceId = (uint64_t)instance;
events[instanceId] = instance;
}
return instance;
}
float Fmod::getEventParameterByName(uint64_t instanceId, const String ¶meterName) {
float p = -1;
if (!events.has(instanceId)) return p;
auto i = events.find(instanceId);
if (i->value())
checkErrors(i->value()->getParameterByName(parameterName.ascii().get_data(), &p));
return p;
}
void Fmod::setEventParameterByName(uint64_t instanceId, const String ¶meterName, float value) {
if (!events.has(instanceId)) return;
auto i = events.find(instanceId);
if (i->value()) checkErrors(i->value()->setParameterByName(parameterName.ascii().get_data(), value));
}
float Fmod::getEventParameterByID(uint64_t instanceId, const Array &idPair) {
if (!events.has(instanceId) || idPair.size() != 2) return -1.0f;
auto i = events.find(instanceId);
if (i->value()) {
FMOD_STUDIO_PARAMETER_ID id;
id.data1 = idPair[0];
id.data2 = idPair[1];
float value;
checkErrors(i->value()->getParameterByID(id, &value));
return value;
}
return -1.0f;
}
void Fmod::setEventParameterByID(uint64_t instanceId, const Array &idPair, float value) {
if (!events.has(instanceId) || idPair.size() != 2) return;
auto i = events.find(instanceId);
if (i->value()) {
FMOD_STUDIO_PARAMETER_ID id;
id.data1 = idPair[0];
id.data2 = idPair[1];
checkErrors(i->value()->setParameterByID(id, value));
}
}
void Fmod::releaseEvent(uint64_t instanceId) {
if (!events.has(instanceId)) return;
auto i = events.find(instanceId);
FMOD::Studio::EventInstance *event = i->value();
if (event) {
releaseOneEvent(event);
}
}
void Fmod::releaseOneEvent(FMOD::Studio::EventInstance *eventInstance) {
Callbacks::mut->lock();
EventInfo *eventInfo = getEventInfo(eventInstance);
eventInstance->setUserData(nullptr);
events.erase((uint64_t)eventInstance);
checkErrors(eventInstance->release());
delete &eventInfo;
Callbacks::mut->unlock();
}
void Fmod::clearNullListeners() {
std::vector<uint32_t> queue;
for (int i = 0; i < listeners.size(); i++) {
if (isNull(listeners[i].gameObj)) {
queue.push_back(i);
std::string s = "FMOD Sound System: Listener at index " + std::to_string(i) + " was freed.";
print_line(s.c_str());
}
}
for (int i = 0; i < queue.size(); i++) {
int index = queue[i];
if (i != 0) index--;
listeners.erase(listeners.begin() + index);
}
checkErrors(system->setNumListeners(listeners.size() == 0 ? 1 : listeners.size()));
}
void Fmod::clearChannelRefs() {
if (channels.size() == 0) return;
std::vector<uint64_t> refs;
for (auto e = channels.front(); e; e = e->next()) {
// Check if the channel is valid by calling any of its getters
bool isPaused = false;
FMOD_RESULT res = e->get()->getPaused(&isPaused);
if (res != FMOD_OK)
refs.push_back(e->key());
}
for (auto ref : refs)
channels.erase(ref);
}
void Fmod::startEvent(uint64_t instanceId) {
if (!events.has(instanceId)) return;
auto i = events.find(instanceId);
if (i->value()) checkErrors(i->value()->start());
}
void Fmod::stopEvent(uint64_t instanceId, int stopMode) {
if (!events.has(instanceId)) return;
auto i = events.find(instanceId);
if (i->value()) {
auto m = static_cast<FMOD_STUDIO_STOP_MODE>(stopMode);
checkErrors(i->value()->stop(m));
}
}
void Fmod::triggerEventCue(uint64_t instanceId) {
if (!events.has(instanceId)) return;
auto i = events.find(instanceId);
if (i->value()) checkErrors(i->value()->triggerCue());
}
int Fmod::getEventPlaybackState(uint64_t instanceId) {
if (!events.has(instanceId))
return -1;
else {
auto i = events.find(instanceId);
if (i->value()) {
FMOD_STUDIO_PLAYBACK_STATE s;
checkErrors(i->value()->getPlaybackState(&s));
return s;
}
return -1;
}
}
bool Fmod::getEventPaused(uint64_t instanceId) {
if (!events.has(instanceId)) return false;
auto i = events.find(instanceId);
bool paused = false;
if (i->value()) checkErrors(i->value()->getPaused(&paused));
return paused;
}
void Fmod::setEventPaused(uint64_t instanceId, bool paused) {
if (!events.has(instanceId)) return;
auto i = events.find(instanceId);
if (i->value()) checkErrors(i->value()->setPaused(paused));
}
float Fmod::getEventPitch(uint64_t instanceId) {
if (!events.has(instanceId)) return 0.0f;
auto i = events.find(instanceId);
float pitch = 0.0f;
if (i->value()) checkErrors(i->value()->getPitch(&pitch));
return pitch;
}
void Fmod::setEventPitch(uint64_t instanceId, float pitch) {
if (!events.has(instanceId)) return;
auto i = events.find(instanceId);
if (i->value()) checkErrors(i->value()->setPitch(pitch));
}
float Fmod::getEventVolume(uint64_t instanceId) {
if (!events.has(instanceId)) return 0.0f;
auto i = events.find(instanceId);
float volume = 0.0f;
FMOD::Studio::EventInstance *event = i->value();
checkErrors(event->getVolume(&volume));
return volume;
}
void Fmod::setEventVolume(uint64_t instanceId, float volume) {
if (!events.has(instanceId)) return;
auto i = events.find(instanceId);
FMOD::Studio::EventInstance *event = i->value();
checkErrors(event->setVolume(volume));
}
int Fmod::getEventTimelinePosition(uint64_t instanceId) {
if (!events.has(instanceId)) return 0;
auto i = events.find(instanceId);
int tp = 0;
if (i->value()) checkErrors(i->value()->getTimelinePosition(&tp));
return tp;
}
void Fmod::setEventTimelinePosition(uint64_t instanceId, int position) {
if (!events.has(instanceId)) return;
auto i = events.find(instanceId);
if (i->value()) checkErrors(i->value()->setTimelinePosition(position));
}
float Fmod::getEventReverbLevel(uint64_t instanceId, int index) {
if (!events.has(instanceId)) return 0.0f;
auto i = events.find(instanceId);
float rvl = 0.0f;
if (i->value()) checkErrors(i->value()->getReverbLevel(index, &rvl));
return rvl;
}
void Fmod::setEventReverbLevel(uint64_t instanceId, int index, float level) {
if (!events.has(instanceId)) return;
auto i = events.find(instanceId);
if (i->value()) checkErrors(i->value()->setReverbLevel(index, level));
}
bool Fmod::isEventVirtual(uint64_t instanceId) {
if (!events.has(instanceId)) return false;
auto i = events.find(instanceId);
bool v = false;
if (i->value()) checkErrors(i->value()->isVirtual(&v));
return v;
}
bool Fmod::getBusMute(const String &busPath) {
loadBus(busPath);
if (!buses.has(busPath)) return false;
bool mute = false;
auto bus = buses.find(busPath);
checkErrors(bus->value()->getMute(&mute));
return mute;
}
bool Fmod::getBusPaused(const String &busPath) {
loadBus(busPath);
if (!buses.has(busPath)) return false;
bool paused = false;
auto bus = buses.find(busPath);
checkErrors(bus->value()->getPaused(&paused));
return paused;
}
float Fmod::getBusVolume(const String &busPath) {
loadBus(busPath);
if (!buses.has(busPath)) return 0.0f;
float volume = 0.0f;
auto bus = buses.find(busPath);
checkErrors(bus->value()->getVolume(&volume));
return volume;
}
void Fmod::setBusMute(const String &busPath, bool mute) {
loadBus(busPath);
if (!buses.has(busPath)) return;
auto bus = buses.find(busPath);
checkErrors(bus->value()->setMute(mute));
}
void Fmod::setBusPaused(const String &busPath, bool paused) {
loadBus(busPath);
if (!buses.has(busPath)) return;
auto bus = buses.find(busPath);
checkErrors(bus->value()->setPaused(paused));
}
void Fmod::setBusVolume(const String &busPath, float volume) {
loadBus(busPath);
if (!buses.has(busPath)) return;
auto bus = buses.find(busPath);
checkErrors(bus->value()->setVolume(volume));
}
void Fmod::stopAllBusEvents(const String &busPath, int stopMode) {
loadBus(busPath);
if (!buses.has(busPath)) return;
auto bus = buses.find(busPath);
auto m = static_cast<FMOD_STUDIO_STOP_MODE>(stopMode);
checkErrors(bus->value()->stopAllEvents(m));
}
bool Fmod::isNull(Object *o) {
CanvasItem *ci = Object::cast_to<CanvasItem>(o);
Spatial *s = Object::cast_to<Spatial>(o);
if (ci == nullptr && s == nullptr)
// an object cannot be 2D and 3D at the same time
// which means if the first cast returned null then the second cast also returned null
return true;
return false; // all g.
}
Fmod::EventInfo *Fmod::getEventInfo(FMOD::Studio::EventInstance *eventInstance) {
EventInfo *eventInfo;
eventInstance->getUserData((void **)&eventInfo);