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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
|
/**
* @file audioengine.cpp
* @brief implementation of LLAudioEngine class abstracting the Open
* AL audio support
*
* $LicenseInfo:firstyear=2000&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2010, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#include "linden_common.h"
#include "llaudioengine.h"
#include "llstreamingaudio.h"
#include "llerror.h"
#include "llmath.h"
#include "sound_ids.h" // temporary hack for min/max distances
#include "llfilesystem.h"
#include "lldir.h"
#include "llaudiodecodemgr.h"
#include "llassetstorage.h"
// necessary for grabbing sounds from sim (implemented in viewer)
extern void request_sound(const LLUUID &sound_guid);
LLAudioEngine* gAudiop = NULL;
//
// LLAudioEngine implementation
//
LLAudioEngine::LLAudioEngine()
{
setDefaults();
}
LLAudioEngine::~LLAudioEngine()
{
}
LLStreamingAudioInterface* LLAudioEngine::getStreamingAudioImpl()
{
return mStreamingAudioImpl;
}
void LLAudioEngine::setStreamingAudioImpl(LLStreamingAudioInterface *impl)
{
mStreamingAudioImpl = impl;
}
void LLAudioEngine::setDefaults()
{
mMaxWindGain = 1.f;
mListenerp = NULL;
mMuted = false;
mUserData = NULL;
mLastStatus = 0;
mNumChannels = 0;
mEnableWind = false;
S32 i;
for (i = 0; i < MAX_CHANNELS; i++)
{
mChannels[i] = NULL;
}
for (i = 0; i < MAX_BUFFERS; i++)
{
mBuffers[i] = NULL;
}
mMasterGain = 1.f;
// Setting mInternalGain to an out of range value fixes the issue reported in STORM-830.
// There is an edge case in setMasterGain during startup which prevents setInternalGain from
// being called if the master volume setting and mInternalGain both equal 0, so using -1 forces
// the if statement in setMasterGain to execute when the viewer starts up.
mInternalGain = -1.f;
mNextWindUpdate = 0.f;
mStreamingAudioImpl = NULL;
for (U32 i = 0; i < LLAudioEngine::AUDIO_TYPE_COUNT; i++)
mSecondaryGain[i] = 1.0f;
}
bool LLAudioEngine::init(const S32 num_channels, void* userdata, const std::string &app_title)
{
setDefaults();
mNumChannels = num_channels;
mUserData = userdata;
allocateListener();
// Initialize the decode manager
gAudioDecodeMgrp = new LLAudioDecodeMgr;
LL_INFOS("AudioEngine") << "LLAudioEngine::init() AudioEngine successfully initialized" << LL_ENDL;
return true;
}
void LLAudioEngine::shutdown()
{
// Clean up decode manager
delete gAudioDecodeMgrp;
gAudioDecodeMgrp = NULL;
// Clean up wind source
cleanupWind();
// Clean up audio sources
source_map::iterator iter_src;
for (iter_src = mAllSources.begin(); iter_src != mAllSources.end(); iter_src++)
{
delete iter_src->second;
}
// Clean up audio data
data_map::iterator iter_data;
for (iter_data = mAllData.begin(); iter_data != mAllData.end(); iter_data++)
{
delete iter_data->second;
}
// Clean up channels
S32 i;
for (i = 0; i < MAX_CHANNELS; i++)
{
delete mChannels[i];
mChannels[i] = NULL;
}
// Clean up buffers
for (i = 0; i < MAX_BUFFERS; i++)
{
delete mBuffers[i];
mBuffers[i] = NULL;
}
}
// virtual
void LLAudioEngine::startInternetStream(const std::string& url)
{
if (mStreamingAudioImpl)
mStreamingAudioImpl->start(url);
}
// virtual
void LLAudioEngine::stopInternetStream()
{
if (mStreamingAudioImpl)
mStreamingAudioImpl->stop();
}
// virtual
void LLAudioEngine::pauseInternetStream(S32 pause)
{
if (mStreamingAudioImpl)
mStreamingAudioImpl->pause(pause);
}
// virtual
void LLAudioEngine::updateInternetStream()
{
if (mStreamingAudioImpl)
mStreamingAudioImpl->update();
}
// virtual
LLAudioEngine::LLAudioPlayState LLAudioEngine::isInternetStreamPlaying()
{
if (mStreamingAudioImpl)
return (LLAudioEngine::LLAudioPlayState) mStreamingAudioImpl->isPlaying();
return LLAudioEngine::AUDIO_STOPPED; // Stopped
}
// virtual
void LLAudioEngine::setInternetStreamGain(F32 vol)
{
if (mStreamingAudioImpl)
mStreamingAudioImpl->setGain(vol);
}
// virtual
std::string LLAudioEngine::getInternetStreamURL()
{
if (mStreamingAudioImpl)
return mStreamingAudioImpl->getURL();
else return std::string();
}
void LLAudioEngine::updateChannels()
{
S32 i;
for (i = 0; i < MAX_CHANNELS; i++)
{
if (mChannels[i])
{
mChannels[i]->updateBuffer();
mChannels[i]->update3DPosition();
mChannels[i]->updateLoop();
}
}
}
static const F32 default_max_decode_time = .002f; // 2 ms
void LLAudioEngine::idle(F32 max_decode_time)
{
if (max_decode_time <= 0.f)
{
max_decode_time = default_max_decode_time;
}
// "Update" all of our audio sources, clean up dead ones.
// Primarily does position updating, cleanup of unused audio sources.
// Also does regeneration of the current priority of each audio source.
S32 i;
for (i = 0; i < MAX_BUFFERS; i++)
{
if (mBuffers[i])
{
mBuffers[i]->mInUse = false;
}
}
F32 max_priority = -1.f;
LLAudioSource *max_sourcep = NULL; // Maximum priority source without a channel
source_map::iterator iter;
for (iter = mAllSources.begin(); iter != mAllSources.end();)
{
LLAudioSource *sourcep = iter->second;
// Update this source
sourcep->update();
sourcep->updatePriority();
if (sourcep->isDone())
{
// The source is done playing, clean it up.
delete sourcep;
iter = mAllSources.erase(iter);
continue;
}
if (sourcep->isMuted())
{
++iter;
continue;
}
if (!sourcep->getChannel() && sourcep->getCurrentBuffer())
{
// We could potentially play this sound if its priority is high enough.
if (sourcep->getPriority() > max_priority)
{
max_priority = sourcep->getPriority();
max_sourcep = sourcep;
}
}
// Move on to the next source
iter++;
}
// Now, do priority-based organization of audio sources.
// All channels used, check priorities.
// Find channel with lowest priority
if (max_sourcep)
{
LLAudioChannel *channelp = getFreeChannel(max_priority);
if (channelp)
{
//LL_INFOS() << "Replacing source in channel due to priority!" << LL_ENDL;
max_sourcep->setChannel(channelp);
channelp->setSource(max_sourcep);
if (max_sourcep->isSyncSlave())
{
// A sync slave, it doesn't start playing until it's synced up with the master.
// Flag this channel as waiting for sync, and return true.
channelp->setWaiting(true);
}
else
{
channelp->setWaiting(false);
channelp->play();
}
}
}
// Do this BEFORE we update the channels
// Update the channels to sync up with any changes that the source made,
// such as changing what sound was playing.
updateChannels();
// Update queued sounds (switch to next queued data if the current has finished playing)
for (iter = mAllSources.begin(); iter != mAllSources.end(); ++iter)
{
// This is lame, instead of this I could actually iterate through all the sources
// attached to each channel, since only those with active channels
// can have anything interesting happen with their queue? (Maybe not true)
LLAudioSource *sourcep = iter->second;
if (!sourcep->mQueuedDatap || sourcep->isMuted())
{
// Muted, or nothing queued, so we don't care.
continue;
}
LLAudioChannel *channelp = sourcep->getChannel();
if (!channelp)
{
// This sound isn't playing, so we just process move the queue
sourcep->mCurrentDatap = sourcep->mQueuedDatap;
sourcep->mQueuedDatap = NULL;
// Reset the timer so the source doesn't die.
sourcep->mAgeTimer.reset();
// Make sure we have the buffer set up if we just decoded the data
if (sourcep->mCurrentDatap)
{
updateBufferForData(sourcep->mCurrentDatap);
}
// Actually play the associated data.
sourcep->setupChannel();
channelp = sourcep->getChannel();
if (channelp)
{
channelp->updateBuffer();
sourcep->getChannel()->play();
}
continue;
}
else
{
// Check to see if the current sound is done playing, or looped.
if (!channelp->isPlaying())
{
sourcep->mCurrentDatap = sourcep->mQueuedDatap;
sourcep->mQueuedDatap = NULL;
// Reset the timer so the source doesn't die.
sourcep->mAgeTimer.reset();
// Make sure we have the buffer set up if we just decoded the data
if (sourcep->mCurrentDatap)
{
updateBufferForData(sourcep->mCurrentDatap);
}
// Actually play the associated data.
sourcep->setupChannel();
channelp->updateBuffer();
sourcep->getChannel()->play();
}
else if (sourcep->isLoop())
{
// It's a loop, we need to check and see if we're done with it.
if (channelp->mLoopedThisFrame)
{
sourcep->mCurrentDatap = sourcep->mQueuedDatap;
sourcep->mQueuedDatap = NULL;
// Actually, should do a time sync so if we're a loop master/slave
// we don't drift away.
sourcep->setupChannel();
sourcep->getChannel()->play();
}
}
}
}
// Lame, update the channels AGAIN.
// Update the channels to sync up with any changes that the source made,
// such as changing what sound was playing.
updateChannels();
// Hack! For now, just use a global sync master;
LLAudioSource *sync_masterp = NULL;
LLAudioChannel *master_channelp = NULL;
F32 max_sm_priority = -1.f;
for (iter = mAllSources.begin(); iter != mAllSources.end(); ++iter)
{
LLAudioSource *sourcep = iter->second;
if (sourcep->isMuted())
{
continue;
}
if (sourcep->isSyncMaster())
{
if (sourcep->getPriority() > max_sm_priority)
{
sync_masterp = sourcep;
master_channelp = sync_masterp->getChannel();
max_sm_priority = sourcep->getPriority();
}
}
}
if (master_channelp && master_channelp->mLoopedThisFrame)
{
// Synchronize loop slaves with their masters
// Update queued sounds (switch to next queued data if the current has finished playing)
for (iter = mAllSources.begin(); iter != mAllSources.end(); ++iter)
{
LLAudioSource *sourcep = iter->second;
if (!sourcep->isSyncSlave())
{
// Not a loop slave, we don't need to do anything
continue;
}
LLAudioChannel *channelp = sourcep->getChannel();
if (!channelp)
{
// Not playing, don't need to bother.
continue;
}
if (!channelp->isPlaying())
{
// Now we need to check if our loop master has just looped, and
// start playback if that's the case.
if (sync_masterp->getChannel())
{
channelp->playSynced(master_channelp);
channelp->setWaiting(false);
}
}
}
}
// Sync up everything that the audio engine needs done.
commitDeferredChanges();
// Flush unused buffers that are stale enough
for (i = 0; i < MAX_BUFFERS; i++)
{
if (mBuffers[i])
{
if (!mBuffers[i]->mInUse && mBuffers[i]->mLastUseTimer.getElapsedTimeF32() > 30.f)
{
//LL_INFOS() << "Flushing unused buffer!" << LL_ENDL;
mBuffers[i]->mAudioDatap->mBufferp = NULL;
delete mBuffers[i];
mBuffers[i] = NULL;
}
}
}
// Clear all of the looped flags for the channels
for (i = 0; i < MAX_CHANNELS; i++)
{
if (mChannels[i])
{
mChannels[i]->mLoopedThisFrame = false;
}
}
// Decode audio files
gAudioDecodeMgrp->processQueue(max_decode_time);
// Call this every frame, just in case we somehow
// missed picking it up in all the places that can add
// or request new data.
startNextTransfer();
updateInternetStream();
}
bool LLAudioEngine::updateBufferForData(LLAudioData *adp, const LLUUID &audio_uuid)
{
if (!adp)
{
return false;
}
// Update the audio buffer first - load a sound if we have it.
// Note that this could potentially cause us to waste time updating buffers
// for sounds that actually aren't playing, although this should be mitigated
// by the fact that we limit the number of buffers, and we flush buffers based
// on priority.
if (!adp->getBuffer())
{
if (adp->hasDecodedData())
{
adp->load();
}
else if (adp->hasLocalData())
{
if (audio_uuid.notNull())
{
gAudioDecodeMgrp->addDecodeRequest(audio_uuid);
}
}
else
{
return false;
}
}
return true;
}
void LLAudioEngine::enableWind(bool enable)
{
if (enable && (!mEnableWind))
{
mEnableWind = initWind();
}
else if (mEnableWind && (!enable))
{
mEnableWind = false;
cleanupWind();
}
}
LLAudioBuffer * LLAudioEngine::getFreeBuffer()
{
S32 i;
for (i = 0; i < MAX_BUFFERS; i++)
{
if (!mBuffers[i])
{
mBuffers[i] = createBuffer();
return mBuffers[i];
}
}
// Grab the oldest unused buffer
F32 max_age = -1.f;
S32 buffer_id = -1;
for (i = 0; i < MAX_BUFFERS; i++)
{
if (mBuffers[i])
{
if (!mBuffers[i]->mInUse)
{
if (mBuffers[i]->mLastUseTimer.getElapsedTimeF32() > max_age)
{
max_age = mBuffers[i]->mLastUseTimer.getElapsedTimeF32();
buffer_id = i;
}
}
}
}
if (buffer_id >= 0)
{
LL_DEBUGS() << "Taking over unused buffer " << buffer_id << LL_ENDL;
//LL_INFOS() << "Flushing unused buffer!" << LL_ENDL;
mBuffers[buffer_id]->mAudioDatap->mBufferp = NULL;
delete mBuffers[buffer_id];
mBuffers[buffer_id] = createBuffer();
return mBuffers[buffer_id];
}
return NULL;
}
LLAudioChannel * LLAudioEngine::getFreeChannel(const F32 priority)
{
S32 i;
for (i = 0; i < mNumChannels; i++)
{
if (!mChannels[i])
{
// No channel allocated here, use it.
mChannels[i] = createChannel();
return mChannels[i];
}
else
{
// Channel is allocated but not playing right now, use it.
if (!mChannels[i]->isPlaying() && !mChannels[i]->isWaiting())
{
mChannels[i]->cleanup();
if (mChannels[i]->getSource())
{
mChannels[i]->getSource()->setChannel(NULL);
}
return mChannels[i];
}
}
}
// All channels used, check priorities.
// Find channel with lowest priority and see if we want to replace it.
F32 min_priority = 10000.f;
LLAudioChannel *min_channelp = NULL;
for (i = 0; i < mNumChannels; i++)
{
LLAudioChannel *channelp = mChannels[i];
LLAudioSource *sourcep = channelp->getSource();
if (sourcep->getPriority() < min_priority)
{
min_channelp = channelp;
min_priority = sourcep->getPriority();
}
}
if (min_priority > priority || !min_channelp)
{
// All playing channels have higher priority, return.
return NULL;
}
// Flush the minimum priority channel, and return it.
min_channelp->cleanup();
min_channelp->getSource()->setChannel(NULL);
return min_channelp;
}
void LLAudioEngine::cleanupBuffer(LLAudioBuffer *bufferp)
{
S32 i;
for (i = 0; i < MAX_BUFFERS; i++)
{
if (mBuffers[i] == bufferp)
{
delete mBuffers[i];
mBuffers[i] = NULL;
}
}
}
bool LLAudioEngine::preloadSound(const LLUUID &uuid)
{
LL_DEBUGS("AudioEngine")<<"( "<<uuid<<" )"<<LL_ENDL;
getAudioData(uuid); // We don't care about the return value, this is just to make sure
// that we have an entry, which will mean that the audio engine knows about this
if (gAudioDecodeMgrp->addDecodeRequest(uuid))
{
// This means that we do have a local copy, and we're working on decoding it.
return true;
}
return false;
}
bool LLAudioEngine::isWindEnabled()
{
return mEnableWind;
}
void LLAudioEngine::setMuted(bool muted)
{
if (muted != mMuted)
{
mMuted = muted;
setMasterGain(mMasterGain);
}
enableWind(!mMuted);
}
void LLAudioEngine::setMasterGain(const F32 gain)
{
mMasterGain = gain;
F32 internal_gain = getMuted() ? 0.f : gain;
if (internal_gain != mInternalGain)
{
mInternalGain = internal_gain;
setInternalGain(mInternalGain);
}
}
F32 LLAudioEngine::getMasterGain()
{
return mMasterGain;
}
void LLAudioEngine::setSecondaryGain(S32 type, F32 gain)
{
llassert(type < LLAudioEngine::AUDIO_TYPE_COUNT);
mSecondaryGain[type] = gain;
}
F32 LLAudioEngine::getSecondaryGain(S32 type)
{
return mSecondaryGain[type];
}
F32 LLAudioEngine::getInternetStreamGain()
{
if (mStreamingAudioImpl)
return mStreamingAudioImpl->getGain();
else
return 1.0f;
}
void LLAudioEngine::setMaxWindGain(F32 gain)
{
mMaxWindGain = gain;
}
F64 LLAudioEngine::mapWindVecToGain(LLVector3 wind_vec)
{
F64 gain = 0.0;
gain = wind_vec.magVec();
if (gain)
{
if (gain > 20)
{
gain = 20;
}
gain = gain/20.0;
}
return (gain);
}
F64 LLAudioEngine::mapWindVecToPitch(LLVector3 wind_vec)
{
LLVector3 listen_right;
F64 theta;
// Wind frame is in listener-relative coordinates
LLVector3 norm_wind = wind_vec;
norm_wind.normVec();
listen_right.setVec(1.0,0.0,0.0);
// measure angle between wind vec and listener right axis (on 0,PI)
theta = acos(norm_wind * listen_right);
// put it on 0, 1
theta /= F_PI;
// put it on [0, 0.5, 0]
if (theta > 0.5) theta = 1.0-theta;
if (theta < 0) theta = 0;
return (theta);
}
F64 LLAudioEngine::mapWindVecToPan(LLVector3 wind_vec)
{
LLVector3 listen_right;
F64 theta;
// Wind frame is in listener-relative coordinates
listen_right.setVec(1.0,0.0,0.0);
LLVector3 norm_wind = wind_vec;
norm_wind.normVec();
// measure angle between wind vec and listener right axis (on 0,PI)
theta = acos(norm_wind * listen_right);
// put it on 0, 1
theta /= F_PI;
return (theta);
}
void LLAudioEngine::triggerSound(const LLUUID &audio_uuid, const LLUUID& owner_id, const F32 gain,
const S32 type, const LLVector3d &pos_global)
{
// Create a new source (since this can't be associated with an existing source.
//LL_INFOS() << "Localized: " << audio_uuid << LL_ENDL;
if (mMuted)
{
return;
}
LLUUID source_id;
source_id.generate();
LLAudioSource *asp = new LLAudioSource(source_id, owner_id, gain, type);
addAudioSource(asp);
if (pos_global.isExactlyZero())
{
// For sound preview and UI
asp->setForcedPriority(true);
}
else
{
asp->setPositionGlobal(pos_global);
}
asp->updatePriority();
asp->play(audio_uuid);
}
void LLAudioEngine::triggerSound(SoundData& soundData)
{
triggerSound(soundData.audio_uuid, soundData.owner_id, soundData.gain, soundData.type, soundData.pos_global);
}
void LLAudioEngine::setListenerPos(LLVector3 aVec)
{
mListenerp->setPosition(aVec);
}
LLVector3 LLAudioEngine::getListenerPos()
{
if (mListenerp)
{
return(mListenerp->getPosition());
}
else
{
return(LLVector3::zero);
}
}
void LLAudioEngine::setListenerVelocity(LLVector3 aVec)
{
mListenerp->setVelocity(aVec);
}
void LLAudioEngine::translateListener(LLVector3 aVec)
{
mListenerp->translate(aVec);
}
void LLAudioEngine::orientListener(LLVector3 up, LLVector3 at)
{
mListenerp->orient(up, at);
}
void LLAudioEngine::setListener(LLVector3 pos, LLVector3 vel, LLVector3 up, LLVector3 at)
{
mListenerp->set(pos,vel,up,at);
}
void LLAudioEngine::setDopplerFactor(F32 factor)
{
if (mListenerp)
{
mListenerp->setDopplerFactor(factor);
}
}
F32 LLAudioEngine::getDopplerFactor()
{
if (mListenerp)
{
return mListenerp->getDopplerFactor();
}
else
{
return 0.f;
}
}
void LLAudioEngine::setRolloffFactor(F32 factor)
{
if (mListenerp)
{
mListenerp->setRolloffFactor(factor);
}
}
F32 LLAudioEngine::getRolloffFactor()
{
if (mListenerp)
{
return mListenerp->getRolloffFactor();
}
else
{
return 0.f;
}
}
void LLAudioEngine::commitDeferredChanges()
{
mListenerp->commitDeferredChanges();
}
LLAudioSource * LLAudioEngine::findAudioSource(const LLUUID &source_id)
{
source_map::iterator iter;
iter = mAllSources.find(source_id);
if (iter == mAllSources.end())
{
return NULL;
}
else
{
return iter->second;
}
}
LLAudioData * LLAudioEngine::getAudioData(const LLUUID &audio_uuid)
{
data_map::iterator iter;
iter = mAllData.find(audio_uuid);
if (iter == mAllData.end())
{
// Create the new audio data
LLAudioData *adp = new LLAudioData(audio_uuid);
mAllData[audio_uuid] = adp;
return adp;
}
else
{
return iter->second;
}
}
void LLAudioEngine::addAudioSource(LLAudioSource *asp)
{
mAllSources[asp->getID()] = asp;
}
void LLAudioEngine::cleanupAudioSource(LLAudioSource *asp)
{
source_map::iterator iter;
iter = mAllSources.find(asp->getID());
if (iter == mAllSources.end())
{
LL_WARNS() << "Cleaning up unknown audio source!" << LL_ENDL;
return;
}
else
{
LL_DEBUGS("AudioEngine") << "Cleaning up audio sources for "<< asp->getID() <<LL_ENDL;
delete asp;
mAllSources.erase(iter);
}
}
bool LLAudioEngine::hasDecodedFile(const LLUUID &uuid)
{
std::string uuid_str;
uuid.toString(uuid_str);
std::string wav_path;
wav_path = gDirUtilp->getExpandedFilename(LL_PATH_CACHE,uuid_str);
wav_path += ".dsf";
if (gDirUtilp->fileExists(wav_path))
{
return true;
}
else
{
return false;
}
}
bool LLAudioEngine::hasLocalFile(const LLUUID &uuid)
{
// See if it's in the cache.
bool have_local = LLFileSystem::getExists(uuid, LLAssetType::AT_SOUND);
LL_DEBUGS("AudioEngine") << "sound uuid " << uuid << " exists in cache" << LL_ENDL;
return have_local;
}
void LLAudioEngine::startNextTransfer()
{
//LL_INFOS() << "LLAudioEngine::startNextTransfer()" << LL_ENDL;
if (mCurrentTransfer.notNull() || getMuted())
{
//LL_INFOS() << "Transfer in progress, aborting" << LL_ENDL;
return;
}
// Get the ID for the next asset that we want to transfer.
// Pick one in the following order:
LLUUID asset_id;
S32 i;
LLAudioSource *asp = NULL;
LLAudioData *adp = NULL;
data_map::iterator data_iter;
// Check all channels for currently playing sounds.
F32 max_pri = -1.f;
for (i = 0; i < MAX_CHANNELS; i++)
{
if (!mChannels[i])
{
continue;
}
asp = mChannels[i]->getSource();
if (!asp)
{
continue;
}
if (asp->getPriority() <= max_pri)
{
continue;
}
if (asp->getPriority() <= max_pri)
{
continue;
}
adp = asp->getCurrentData();
if (!adp)
{
continue;
}
if (!adp->hasLocalData() && adp->hasValidData())
{
asset_id = adp->getID();
max_pri = asp->getPriority();
}
}
// Check all channels for currently queued sounds.
if (asset_id.isNull())
{
max_pri = -1.f;
for (i = 0; i < MAX_CHANNELS; i++)
{
if (!mChannels[i])
{
continue;
}
LLAudioSource *asp;
asp = mChannels[i]->getSource();
if (!asp)
{
continue;
}
if (asp->getPriority() <= max_pri)
{
continue;
}
adp = asp->getQueuedData();
if (!adp)
{
continue;
}
if (!adp->hasLocalData() && adp->hasValidData())
{
asset_id = adp->getID();
max_pri = asp->getPriority();
}
}
}
// Check all live channels for other sounds (preloads).
if (asset_id.isNull())
{
max_pri = -1.f;
for (i = 0; i < MAX_CHANNELS; i++)
{
if (!mChannels[i])
{
continue;
}
LLAudioSource *asp;
asp = mChannels[i]->getSource();
if (!asp)
{
continue;
}
if (asp->getPriority() <= max_pri)
{
continue;
}
for (data_iter = asp->mPreloadMap.begin(); data_iter != asp->mPreloadMap.end(); data_iter++)
{
LLAudioData *adp = data_iter->second;
if (!adp)
{
continue;
}
if (!adp->hasLocalData() && adp->hasValidData())
{
asset_id = adp->getID();
max_pri = asp->getPriority();
}
}
}
}
// Check all sources
if (asset_id.isNull())
{
max_pri = -1.f;
source_map::iterator source_iter;
for (source_iter = mAllSources.begin(); source_iter != mAllSources.end(); source_iter++)
{
asp = source_iter->second;
if (!asp)
{
continue;
}
if (asp->getPriority() <= max_pri)
{
continue;
}
adp = asp->getCurrentData();
if (adp && !adp->hasLocalData() && adp->hasValidData())
{
asset_id = adp->getID();
max_pri = asp->getPriority();
continue;
}
adp = asp->getQueuedData();
if (adp && !adp->hasLocalData() && adp->hasValidData())
{
asset_id = adp->getID();
max_pri = asp->getPriority();
continue;
}
for (data_iter = asp->mPreloadMap.begin(); data_iter != asp->mPreloadMap.end(); data_iter++)
{
LLAudioData *adp = data_iter->second;
if (!adp)
{
continue;
}
if (!adp->hasLocalData() && adp->hasValidData())
{
asset_id = adp->getID();
max_pri = asp->getPriority();
break;
}
}
}
}
if (asset_id.notNull())
{
LL_INFOS() << "Getting asset data for: " << asset_id << LL_ENDL;
mCurrentTransfer = asset_id;
mCurrentTransferTimer.reset();
gAssetStorage->getAssetData(asset_id, LLAssetType::AT_SOUND,
assetCallback, NULL);
}
else
{
//LL_INFOS() << "No pending transfers?" << LL_ENDL;
}
}
// static
void LLAudioEngine::assetCallback(const LLUUID &uuid, LLAssetType::EType type, void *user_data, S32 result_code, LLExtStat ext_status)
{
if (!gAudiop)
{
LL_WARNS("AudioEngine") << "LLAudioEngine instance doesn't exist!" << LL_ENDL;
return;
}
if (result_code)
{
LL_INFOS() << "Boom, error in audio file transfer: " << LLAssetStorage::getErrorString( result_code ) << " (" << result_code << ")" << LL_ENDL;
// Need to mark data as bad to avoid constant rerequests.
LLAudioData *adp = gAudiop->getAudioData(uuid);
if (adp)
{ // Make sure everything is cleared
adp->setHasValidData(false);
adp->setHasLocalData(false);
adp->setHasDecodedData(false);
adp->setHasCompletedDecode(true);
}
}
else
{
LLAudioData *adp = gAudiop->getAudioData(uuid);
if (!adp)
{
// Should never happen
LL_WARNS() << "Got asset callback without audio data for " << uuid << LL_ENDL;
}
else
{
// LL_INFOS() << "Got asset callback with good audio data for " << uuid << ", making decode request" << LL_ENDL;
adp->setHasValidData(true);
adp->setHasLocalData(true);
gAudioDecodeMgrp->addDecodeRequest(uuid);
}
}
gAudiop->mCurrentTransfer = LLUUID::null;
gAudiop->startNextTransfer();
}
//
// LLAudioSource implementation
//
LLAudioSource::LLAudioSource(const LLUUID& id, const LLUUID& owner_id, const F32 gain, const S32 type)
: mID(id),
mOwnerID(owner_id),
mPriority(0.f),
mGain(gain),
mSourceMuted(false),
mForcedPriority(false),
mLoop(false),
mSyncMaster(false),
mSyncSlave(false),
mQueueSounds(false),
mPlayedOnce(false),
mCorrupted(false),
mType(type),
mChannelp(NULL),
mCurrentDatap(NULL),
mQueuedDatap(NULL)
{
}
LLAudioSource::~LLAudioSource()
{
if (mChannelp)
{
// Stop playback of this sound
mChannelp->setSource(NULL);
mChannelp = NULL;
}
}
void LLAudioSource::setChannel(LLAudioChannel *channelp)
{
if (channelp == mChannelp)
{
return;
}
mChannelp = channelp;
}
void LLAudioSource::update()
{
if(mCorrupted)
{
return ; //no need to update
}
if (!getCurrentBuffer())
{
LLAudioData *adp = getCurrentData();
if (adp)
{
// Hack - try and load the sound. Will do this as a callback
// on decode later.
if (adp->load() && adp->getBuffer())
{
play(adp->getID());
}
else if (adp->hasCompletedDecode()) // Only mark corrupted after decode is done
{
LL_WARNS() << "Marking LLAudioSource corrupted for " << adp->getID() << LL_ENDL;
mCorrupted = true ;
}
}
}
}
void LLAudioSource::updatePriority()
{
if (isForcedPriority())
{
mPriority = 1.f;
}
else if (isMuted())
{
mPriority = 0.f;
}
else
{
// Priority is based on distance
LLVector3 dist_vec;
dist_vec.setVec(getPositionGlobal());
if (gAudiop)
{
dist_vec -= gAudiop->getListenerPos();
}
F32 dist_squared = llmax(1.f, dist_vec.magVecSquared());
mPriority = mGain / dist_squared;
}
}
bool LLAudioSource::setupChannel()
{
LLAudioData *adp = getCurrentData();
if (!adp->getBuffer())
{
// We're not ready to play back the sound yet, so don't try and allocate a channel for it.
//LL_WARNS() << "Aborting, no buffer" << LL_ENDL;
return false;
}
if (!gAudiop)
{
LL_WARNS("AudioEngine") << "LLAudioEngine instance doesn't exist!" << LL_ENDL;
return false;
}
if (!mChannelp)
{
// Update the priority, in case we need to push out another channel.
updatePriority();
setChannel(gAudiop->getFreeChannel(getPriority()));
}
if (!mChannelp)
{
// Ugh, we don't have any free channels.
// Now we have to reprioritize.
// For now, just don't play the sound.
//LL_WARNS() << "Aborting, no free channels" << LL_ENDL;
return false;
}
mChannelp->setSource(this);
return true;
}
void LLAudioSource::stop()
{
play(LLUUID::null);
if (mCurrentDatap)
{
// always reset data if something wants us to stop
mCurrentDatap = nullptr;
}
}
bool LLAudioSource::play(const LLUUID &audio_uuid)
{
// Special abuse of play(); don't play a sound, but kill it.
if (audio_uuid.isNull())
{
if (getChannel())
{
getChannel()->setSource(NULL);
setChannel(NULL);
if (!isMuted())
{
mCurrentDatap = NULL;
}
}
return false;
}
// Reset our age timeout if someone attempts to play the source.
mAgeTimer.reset();
if (!gAudiop)
{
LL_WARNS("AudioEngine") << "LLAudioEngine instance doesn't exist!" << LL_ENDL;
return false;
}
LLAudioData *adp = gAudiop->getAudioData(audio_uuid);
addAudioData(adp);
if (isMuted())
{
return false;
}
bool has_buffer = gAudiop->updateBufferForData(adp, audio_uuid);
if (!has_buffer)
{
// Don't bother trying to set up a channel or anything, we don't have an audio buffer.
return false;
}
if (!setupChannel())
{
return false;
}
if (isSyncSlave())
{
// A sync slave, it doesn't start playing until it's synced up with the master.
// Flag this channel as waiting for sync, and return true.
getChannel()->setWaiting(true);
return true;
}
getChannel()->play();
return true;
}
bool LLAudioSource::isDone() const
{
if(mCorrupted)
{
return true ;
}
const F32 MAX_AGE = 60.f;
const F32 MAX_UNPLAYED_AGE = 15.f;
const F32 MAX_MUTED_AGE = 11.f;
if (isLoop())
{
// Looped sources never die on their own.
return false;
}
if (hasPendingPreloads())
{
return false;
}
if (mQueuedDatap)
{
// Don't kill this sound if we've got something queued up to play.
return false;
}
F32 elapsed = mAgeTimer.getElapsedTimeF32();
// This is a single-play source
if (!mChannelp)
{
if ((elapsed > (mSourceMuted ? MAX_MUTED_AGE : MAX_UNPLAYED_AGE)) || mPlayedOnce)
{
// We don't have a channel assigned, and it's been
// over 15 seconds since we tried to play it. Don't bother.
//LL_INFOS() << "No channel assigned, source is done" << LL_ENDL;
return true;
}
else
{
return false;
}
}
if (mChannelp->isPlaying())
{
if (elapsed > MAX_AGE)
{
// Arbitarily cut off non-looped sounds when they're old.
return true;
}
else
{
// Sound is still playing and we haven't timed out, don't kill it.
return false;
}
}
if ((elapsed > MAX_UNPLAYED_AGE) || mPlayedOnce)
{
// The sound isn't playing back after 15 seconds or we're already done playing it, kill it.
return true;
}
return false;
}
void LLAudioSource::addAudioData(LLAudioData *adp, const bool set_current)
{
// Only handle a single piece of audio data associated with a source right now,
// until I implement prefetch.
if (!gAudiop)
{
LL_WARNS("AudioEngine") << "LLAudioEngine instance doesn't exist!" << LL_ENDL;
return;
}
if (set_current)
{
if (!mCurrentDatap)
{
mCurrentDatap = adp;
if (mChannelp)
{
mChannelp->updateBuffer();
mChannelp->play();
}
// Make sure the audio engine knows that we want to request this sound.
gAudiop->startNextTransfer();
return;
}
else if (mQueueSounds)
{
// If we have current data, and we're queuing, put
// the object onto the queue.
if (mQueuedDatap)
{
// We only queue one sound at a time, and it's a FIFO.
// Don't put it onto the queue.
return;
}
if (adp == mCurrentDatap && isLoop())
{
// No point in queueing the same sound if
// we're looping.
return;
}
mQueuedDatap = adp;
// Make sure the audio engine knows that we want to request this sound.
gAudiop->startNextTransfer();
}
else
{
if (mCurrentDatap != adp)
{
// Right now, if we're currently playing this sound in a channel, we
// update the buffer that the channel's associated with
// and play it. This may not be the correct behavior.
mCurrentDatap = adp;
if (mChannelp)
{
mChannelp->updateBuffer();
mChannelp->play();
}
// Make sure the audio engine knows that we want to request this sound.
gAudiop->startNextTransfer();
}
}
}
else
{
// Add it to the preload list.
mPreloadMap[adp->getID()] = adp;
gAudiop->startNextTransfer();
}
}
bool LLAudioSource::hasPendingPreloads() const
{
// Check to see if we've got any preloads on deck for this source
data_map::const_iterator iter;
for (iter = mPreloadMap.begin(); iter != mPreloadMap.end(); iter++)
{
LLAudioData *adp = iter->second;
// note: a bad UUID will forever be !hasDecodedData()
// but also !hasValidData(), hence the check for hasValidData()
if (!adp)
{
continue;
}
if (!adp->hasDecodedData() && adp->hasValidData())
{
// This source is still waiting for a preload
return true;
}
}
return false;
}
LLAudioData * LLAudioSource::getCurrentData()
{
return mCurrentDatap;
}
LLAudioData * LLAudioSource::getQueuedData()
{
return mQueuedDatap;
}
LLAudioBuffer * LLAudioSource::getCurrentBuffer()
{
if (!mCurrentDatap)
{
return NULL;
}
return mCurrentDatap->getBuffer();
}
//
// LLAudioChannel implementation
//
LLAudioChannel::LLAudioChannel() :
mCurrentSourcep(NULL),
mCurrentBufferp(NULL),
mLoopedThisFrame(false),
mWaiting(false),
mSecondaryGain(1.0f)
{
}
LLAudioChannel::~LLAudioChannel()
{
// Need to disconnect any sources which are using this channel.
//LL_INFOS() << "Cleaning up audio channel" << LL_ENDL;
if (mCurrentSourcep)
{
mCurrentSourcep->setChannel(NULL);
}
mCurrentBufferp = NULL;
}
void LLAudioChannel::setSource(LLAudioSource *sourcep)
{
//LL_INFOS() << this << ": setSource(" << sourcep << ")" << LL_ENDL;
if (!sourcep)
{
// Clearing the source for this channel, don't need to do anything.
//LL_INFOS() << "Clearing source for channel" << LL_ENDL;
cleanup();
mCurrentSourcep = NULL;
mWaiting = false;
}
else
{
LL_DEBUGS("AudioEngine") << "( id: " << sourcep->getID() << ")" << LL_ENDL;
if (sourcep == mCurrentSourcep)
{
// Don't reallocate the channel, this will make FMOD goofy.
//LL_INFOS() << "Calling setSource with same source!" << LL_ENDL;
}
mCurrentSourcep = sourcep;
updateBuffer();
update3DPosition();
}
}
bool LLAudioChannel::updateBuffer()
{
if (!gAudiop)
{
LL_WARNS("AudioEngine") << "LLAudioEngine instance doesn't exist!" << LL_ENDL;
return false;
}
if (!mCurrentSourcep)
{
// This channel isn't associated with any source, nothing
// to be updated
return false;
}
// Initialize the channel's gain setting for this sound.
setSecondaryGain(gAudiop->getSecondaryGain(mCurrentSourcep->getType()));
LLAudioBuffer *bufferp = mCurrentSourcep->getCurrentBuffer();
if (bufferp == mCurrentBufferp)
{
if (bufferp)
{
// The source hasn't changed what buffer it's playing
bufferp->mLastUseTimer.reset();
bufferp->mInUse = true;
}
return false;
}
//
// The source changed what buffer it's playing. We need to clean up
// the existing channel
//
cleanup();
mCurrentBufferp = bufferp;
if (bufferp)
{
bufferp->mLastUseTimer.reset();
bufferp->mInUse = true;
}
if (!mCurrentBufferp)
{
// There's no new buffer to be played, so we just abort.
return false;
}
return true;
}
//
// LLAudioData implementation
//
LLAudioData::LLAudioData(const LLUUID &uuid) :
mID(uuid),
mBufferp(NULL),
mHasLocalData(false),
mHasDecodedData(false),
mHasCompletedDecode(false),
mHasValidData(true)
{
if (uuid.isNull())
{
// This is a null sound.
return;
}
if (!gAudiop)
{
LL_WARNS("AudioEngine") << "LLAudioEngine instance doesn't exist!" << LL_ENDL;
return;
}
if (gAudiop->hasDecodedFile(uuid))
{
// Already have a decoded version, don't need to decode it.
setHasLocalData(true);
setHasDecodedData(true);
setHasCompletedDecode(true);
}
else if (gAssetStorage && gAssetStorage->hasLocalAsset(uuid, LLAssetType::AT_SOUND))
{
setHasLocalData(true);
}
}
//return false when the audio file is corrupted.
bool LLAudioData::load()
{
// For now, just assume we're going to use one buffer per audiodata.
if (mBufferp)
{
// We already have this sound in a buffer, don't do anything.
LL_INFOS() << "Already have a buffer for this sound, don't bother loading!" << LL_ENDL;
return true;
}
if (!gAudiop)
{
LL_WARNS("AudioEngine") << "LLAudioEngine instance doesn't exist!" << LL_ENDL;
return false;
}
mBufferp = gAudiop->getFreeBuffer();
if (!mBufferp)
{
// No free buffers, abort.
LL_INFOS() << "Not able to allocate a new audio buffer, aborting." << LL_ENDL;
return true;
}
std::string uuid_str;
std::string wav_path;
mID.toString(uuid_str);
wav_path= gDirUtilp->getExpandedFilename(LL_PATH_CACHE,uuid_str) + ".dsf";
if (!mBufferp->loadWAV(wav_path))
{
// Hrm. Right now, let's unset the buffer, since it's empty.
gAudiop->cleanupBuffer(mBufferp);
mBufferp = NULL;
return false;
}
mBufferp->mAudioDatap = this;
return true;
}
|