PageRenderTime 103ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llaudio/llaudioengine_fmod.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 781 lines | 478 code | 132 blank | 171 comment | 76 complexity | 8f1a6483280033767596cd8b3e71a6e2 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file audioengine_fmod.cpp
  3. * @brief Implementation of LLAudioEngine class abstracting the audio support as a FMOD 3D implementation
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "llstreamingaudio.h"
  28. #include "llstreamingaudio_fmod.h"
  29. #include "llaudioengine_fmod.h"
  30. #include "lllistener_fmod.h"
  31. #include "llerror.h"
  32. #include "llmath.h"
  33. #include "llrand.h"
  34. #include "fmod.h"
  35. #include "fmod_errors.h"
  36. #include "lldir.h"
  37. #include "llapr.h"
  38. #include "sound_ids.h"
  39. extern "C" {
  40. void * F_CALLBACKAPI windCallback(void *originalbuffer, void *newbuffer, int length, void* userdata);
  41. }
  42. LLAudioEngine_FMOD::LLAudioEngine_FMOD()
  43. {
  44. mInited = false;
  45. mWindGen = NULL;
  46. mWindDSP = NULL;
  47. }
  48. LLAudioEngine_FMOD::~LLAudioEngine_FMOD()
  49. {
  50. }
  51. bool LLAudioEngine_FMOD::init(const S32 num_channels, void* userdata)
  52. {
  53. LLAudioEngine::init(num_channels, userdata);
  54. // Reserve one extra channel for the http stream.
  55. if (!FSOUND_SetMinHardwareChannels(num_channels + 1))
  56. {
  57. LL_WARNS("AppInit") << "FMOD::init[0](), error: " << FMOD_ErrorString(FSOUND_GetError()) << LL_ENDL;
  58. }
  59. LL_DEBUGS("AppInit") << "LLAudioEngine_FMOD::init() initializing FMOD" << LL_ENDL;
  60. F32 version = FSOUND_GetVersion();
  61. if (version < FMOD_VERSION)
  62. {
  63. LL_WARNS("AppInit") << "Error : You are using the wrong FMOD version (" << version
  64. << ")! You should be using FMOD " << FMOD_VERSION << LL_ENDL;
  65. //return false;
  66. }
  67. U32 fmod_flags = 0x0;
  68. #if LL_WINDOWS
  69. // Windows needs to know which window is frontmost.
  70. // This must be called before FSOUND_Init() per the FMOD docs.
  71. // This could be used to let FMOD handle muting when we lose focus,
  72. // but we don't actually want to do that because we want to distinguish
  73. // between minimized and not-focused states.
  74. if (!FSOUND_SetHWND(userdata))
  75. {
  76. LL_WARNS("AppInit") << "Error setting FMOD window: "
  77. << FMOD_ErrorString(FSOUND_GetError()) << LL_ENDL;
  78. return false;
  79. }
  80. // Play audio when we don't have focus.
  81. // (For example, IM client on top of us.)
  82. // This means we also try to play audio when minimized,
  83. // so we manually handle muting in that case. JC
  84. fmod_flags |= FSOUND_INIT_GLOBALFOCUS;
  85. #endif
  86. #if LL_LINUX
  87. // initialize the FMOD engine
  88. // This is a hack to use only FMOD's basic FPU mixer
  89. // when the LL_VALGRIND environmental variable is set,
  90. // otherwise valgrind will fall over on FMOD's MMX detection
  91. if (getenv("LL_VALGRIND")) /*Flawfinder: ignore*/
  92. {
  93. LL_INFOS("AppInit") << "Pacifying valgrind in FMOD init." << LL_ENDL;
  94. FSOUND_SetMixer(FSOUND_MIXER_QUALITY_FPU);
  95. }
  96. // If we don't set an output method, Linux FMOD always
  97. // decides on OSS and fails otherwise. So we'll manually
  98. // try ESD, then OSS, then ALSA.
  99. // Why this order? See SL-13250, but in short, OSS emulated
  100. // on top of ALSA is ironically more reliable than raw ALSA.
  101. // Ack, and ESD has more reliable failure modes - but has worse
  102. // latency - than all of them, so wins for now.
  103. bool audio_ok = false;
  104. if (!audio_ok)
  105. {
  106. if (NULL == getenv("LL_BAD_FMOD_ESD")) /*Flawfinder: ignore*/
  107. {
  108. LL_DEBUGS("AppInit") << "Trying ESD audio output..." << LL_ENDL;
  109. if(FSOUND_SetOutput(FSOUND_OUTPUT_ESD) &&
  110. FSOUND_Init(44100, num_channels, fmod_flags))
  111. {
  112. LL_DEBUGS("AppInit") << "ESD audio output initialized OKAY"
  113. << LL_ENDL;
  114. audio_ok = true;
  115. } else {
  116. LL_WARNS("AppInit") << "ESD audio output FAILED to initialize: "
  117. << FMOD_ErrorString(FSOUND_GetError()) << LL_ENDL;
  118. }
  119. } else {
  120. LL_DEBUGS("AppInit") << "ESD audio output SKIPPED" << LL_ENDL;
  121. }
  122. }
  123. if (!audio_ok)
  124. {
  125. if (NULL == getenv("LL_BAD_FMOD_OSS")) /*Flawfinder: ignore*/
  126. {
  127. LL_DEBUGS("AppInit") << "Trying OSS audio output..." << LL_ENDL;
  128. if(FSOUND_SetOutput(FSOUND_OUTPUT_OSS) &&
  129. FSOUND_Init(44100, num_channels, fmod_flags))
  130. {
  131. LL_DEBUGS("AppInit") << "OSS audio output initialized OKAY" << LL_ENDL;
  132. audio_ok = true;
  133. } else {
  134. LL_WARNS("AppInit") << "OSS audio output FAILED to initialize: "
  135. << FMOD_ErrorString(FSOUND_GetError()) << LL_ENDL;
  136. }
  137. } else {
  138. LL_DEBUGS("AppInit") << "OSS audio output SKIPPED" << LL_ENDL;
  139. }
  140. }
  141. if (!audio_ok)
  142. {
  143. if (NULL == getenv("LL_BAD_FMOD_ALSA")) /*Flawfinder: ignore*/
  144. {
  145. LL_DEBUGS("AppInit") << "Trying ALSA audio output..." << LL_ENDL;
  146. if(FSOUND_SetOutput(FSOUND_OUTPUT_ALSA) &&
  147. FSOUND_Init(44100, num_channels, fmod_flags))
  148. {
  149. LL_DEBUGS("AppInit") << "ALSA audio output initialized OKAY" << LL_ENDL;
  150. audio_ok = true;
  151. } else {
  152. LL_WARNS("AppInit") << "ALSA audio output FAILED to initialize: "
  153. << FMOD_ErrorString(FSOUND_GetError()) << LL_ENDL;
  154. }
  155. } else {
  156. LL_DEBUGS("AppInit") << "OSS audio output SKIPPED" << LL_ENDL;
  157. }
  158. }
  159. if (!audio_ok)
  160. {
  161. LL_WARNS("AppInit") << "Overall audio init failure." << LL_ENDL;
  162. return false;
  163. }
  164. // On Linux, FMOD causes a SIGPIPE for some netstream error
  165. // conditions (an FMOD bug); ignore SIGPIPE so it doesn't crash us.
  166. // NOW FIXED in FMOD 3.x since 2006-10-01.
  167. //signal(SIGPIPE, SIG_IGN);
  168. // We're interested in logging which output method we
  169. // ended up with, for QA purposes.
  170. switch (FSOUND_GetOutput())
  171. {
  172. case FSOUND_OUTPUT_NOSOUND: LL_DEBUGS("AppInit") << "Audio output: NoSound" << LL_ENDL; break;
  173. case FSOUND_OUTPUT_OSS: LL_DEBUGS("AppInit") << "Audio output: OSS" << LL_ENDL; break;
  174. case FSOUND_OUTPUT_ESD: LL_DEBUGS("AppInit") << "Audio output: ESD" << LL_ENDL; break;
  175. case FSOUND_OUTPUT_ALSA: LL_DEBUGS("AppInit") << "Audio output: ALSA" << LL_ENDL; break;
  176. default: LL_INFOS("AppInit") << "Audio output: Unknown!" << LL_ENDL; break;
  177. };
  178. #else // LL_LINUX
  179. // initialize the FMOD engine
  180. if (!FSOUND_Init(44100, num_channels, fmod_flags))
  181. {
  182. LL_WARNS("AppInit") << "Error initializing FMOD: "
  183. << FMOD_ErrorString(FSOUND_GetError()) << LL_ENDL;
  184. return false;
  185. }
  186. #endif
  187. // set up our favourite FMOD-native streaming audio implementation if none has already been added
  188. if (!getStreamingAudioImpl()) // no existing implementation added
  189. setStreamingAudioImpl(new LLStreamingAudio_FMOD());
  190. LL_DEBUGS("AppInit") << "LLAudioEngine_FMOD::init() FMOD initialized correctly" << LL_ENDL;
  191. mInited = true;
  192. return true;
  193. }
  194. std::string LLAudioEngine_FMOD::getDriverName(bool verbose)
  195. {
  196. if (verbose)
  197. {
  198. F32 version = FSOUND_GetVersion();
  199. return llformat("FMOD version %f", version);
  200. }
  201. else
  202. {
  203. return "FMOD";
  204. }
  205. }
  206. void LLAudioEngine_FMOD::allocateListener(void)
  207. {
  208. mListenerp = (LLListener *) new LLListener_FMOD();
  209. if (!mListenerp)
  210. {
  211. llwarns << "Listener creation failed" << llendl;
  212. }
  213. }
  214. void LLAudioEngine_FMOD::shutdown()
  215. {
  216. if (mWindDSP)
  217. {
  218. FSOUND_DSP_SetActive(mWindDSP,false);
  219. FSOUND_DSP_Free(mWindDSP);
  220. }
  221. stopInternetStream();
  222. LLAudioEngine::shutdown();
  223. llinfos << "LLAudioEngine_FMOD::shutdown() closing FMOD" << llendl;
  224. FSOUND_Close();
  225. llinfos << "LLAudioEngine_FMOD::shutdown() done closing FMOD" << llendl;
  226. delete mListenerp;
  227. mListenerp = NULL;
  228. }
  229. LLAudioBuffer * LLAudioEngine_FMOD::createBuffer()
  230. {
  231. return new LLAudioBufferFMOD();
  232. }
  233. LLAudioChannel * LLAudioEngine_FMOD::createChannel()
  234. {
  235. return new LLAudioChannelFMOD();
  236. }
  237. bool LLAudioEngine_FMOD::initWind()
  238. {
  239. if (!mWindGen)
  240. {
  241. bool enable;
  242. switch (FSOUND_GetMixer())
  243. {
  244. case FSOUND_MIXER_MMXP5:
  245. case FSOUND_MIXER_MMXP6:
  246. case FSOUND_MIXER_QUALITY_MMXP5:
  247. case FSOUND_MIXER_QUALITY_MMXP6:
  248. enable = (typeid(MIXBUFFERFORMAT) == typeid(S16));
  249. break;
  250. case FSOUND_MIXER_BLENDMODE:
  251. enable = (typeid(MIXBUFFERFORMAT) == typeid(S32));
  252. break;
  253. case FSOUND_MIXER_QUALITY_FPU:
  254. enable = (typeid(MIXBUFFERFORMAT) == typeid(F32));
  255. break;
  256. default:
  257. // FSOUND_GetMixer() does not return a valid mixer type on Darwin
  258. LL_INFOS("AppInit") << "Unknown FMOD mixer type, assuming default" << LL_ENDL;
  259. enable = true;
  260. break;
  261. }
  262. if (enable)
  263. {
  264. mWindGen = new LLWindGen<MIXBUFFERFORMAT>(FSOUND_GetOutputRate());
  265. }
  266. else
  267. {
  268. LL_WARNS("AppInit") << "Incompatible FMOD mixer type, wind noise disabled" << LL_ENDL;
  269. }
  270. }
  271. mNextWindUpdate = 0.0;
  272. if (mWindGen && !mWindDSP)
  273. {
  274. mWindDSP = FSOUND_DSP_Create(&windCallback, FSOUND_DSP_DEFAULTPRIORITY_CLEARUNIT + 20, mWindGen);
  275. }
  276. if (mWindDSP)
  277. {
  278. FSOUND_DSP_SetActive(mWindDSP, true);
  279. return true;
  280. }
  281. return false;
  282. }
  283. void LLAudioEngine_FMOD::cleanupWind()
  284. {
  285. if (mWindDSP)
  286. {
  287. FSOUND_DSP_SetActive(mWindDSP, false);
  288. FSOUND_DSP_Free(mWindDSP);
  289. mWindDSP = NULL;
  290. }
  291. delete mWindGen;
  292. mWindGen = NULL;
  293. }
  294. //-----------------------------------------------------------------------
  295. void LLAudioEngine_FMOD::updateWind(LLVector3 wind_vec, F32 camera_height_above_water)
  296. {
  297. LLVector3 wind_pos;
  298. F64 pitch;
  299. F64 center_freq;
  300. if (!mEnableWind)
  301. {
  302. return;
  303. }
  304. if (mWindUpdateTimer.checkExpirationAndReset(LL_WIND_UPDATE_INTERVAL))
  305. {
  306. // wind comes in as Linden coordinate (+X = forward, +Y = left, +Z = up)
  307. // need to convert this to the conventional orientation DS3D and OpenAL use
  308. // where +X = right, +Y = up, +Z = backwards
  309. wind_vec.setVec(-wind_vec.mV[1], wind_vec.mV[2], -wind_vec.mV[0]);
  310. // cerr << "Wind update" << endl;
  311. pitch = 1.0 + mapWindVecToPitch(wind_vec);
  312. center_freq = 80.0 * pow(pitch,2.5*(mapWindVecToGain(wind_vec)+1.0));
  313. mWindGen->mTargetFreq = (F32)center_freq;
  314. mWindGen->mTargetGain = (F32)mapWindVecToGain(wind_vec) * mMaxWindGain;
  315. mWindGen->mTargetPanGainR = (F32)mapWindVecToPan(wind_vec);
  316. }
  317. }
  318. /*
  319. //-----------------------------------------------------------------------
  320. void LLAudioEngine_FMOD::setSourceMinDistance(U16 source_num, F64 distance)
  321. {
  322. if (!mInited)
  323. {
  324. return;
  325. }
  326. if (mBuffer[source_num])
  327. {
  328. mMinDistance[source_num] = (F32) distance;
  329. if (!FSOUND_Sample_SetMinMaxDistance(mBuffer[source_num],mMinDistance[source_num], mMaxDistance[source_num]))
  330. {
  331. llwarns << "FMOD::setSourceMinDistance(" << source_num << "), error: " << FMOD_ErrorString(FSOUND_GetError()) << llendl;
  332. }
  333. }
  334. }
  335. //-----------------------------------------------------------------------
  336. void LLAudioEngine_FMOD::setSourceMaxDistance(U16 source_num, F64 distance)
  337. {
  338. if (!mInited)
  339. {
  340. return;
  341. }
  342. if (mBuffer[source_num])
  343. {
  344. mMaxDistance[source_num] = (F32) distance;
  345. if (!FSOUND_Sample_SetMinMaxDistance(mBuffer[source_num],mMinDistance[source_num], mMaxDistance[source_num]))
  346. {
  347. llwarns << "FMOD::setSourceMaxDistance(" << source_num << "), error: " << FMOD_ErrorString(FSOUND_GetError()) << llendl;
  348. }
  349. }
  350. }
  351. //-----------------------------------------------------------------------
  352. void LLAudioEngine_FMOD::get3DParams(S32 source_num, S32 *volume, S32 *freq, S32 *inside, S32 *outside, LLVector3 *orient, S32 *out_volume, F32 *min_dist, F32 *max_dist)
  353. {
  354. *volume = 0;
  355. *freq = 0;
  356. *inside = 0;
  357. *outside = 0;
  358. *orient = LLVector3::zero;
  359. *out_volume = 0;
  360. *min_dist = 0.f;
  361. *max_dist = 0.f;
  362. }
  363. */
  364. //-----------------------------------------------------------------------
  365. void LLAudioEngine_FMOD::setInternalGain(F32 gain)
  366. {
  367. if (!mInited)
  368. {
  369. return;
  370. }
  371. gain = llclamp( gain, 0.0f, 1.0f );
  372. FSOUND_SetSFXMasterVolume( llround( 255.0f * gain ) );
  373. LLStreamingAudioInterface *saimpl = getStreamingAudioImpl();
  374. if ( saimpl )
  375. {
  376. // fmod likes its streaming audio channel gain re-asserted after
  377. // master volume change.
  378. saimpl->setGain(saimpl->getGain());
  379. }
  380. }
  381. //
  382. // LLAudioChannelFMOD implementation
  383. //
  384. LLAudioChannelFMOD::LLAudioChannelFMOD() : LLAudioChannel(), mChannelID(0), mLastSamplePos(0)
  385. {
  386. }
  387. LLAudioChannelFMOD::~LLAudioChannelFMOD()
  388. {
  389. cleanup();
  390. }
  391. bool LLAudioChannelFMOD::updateBuffer()
  392. {
  393. if (LLAudioChannel::updateBuffer())
  394. {
  395. // Base class update returned true, which means that we need to actually
  396. // set up the channel for a different buffer.
  397. LLAudioBufferFMOD *bufferp = (LLAudioBufferFMOD *)mCurrentSourcep->getCurrentBuffer();
  398. // Grab the FMOD sample associated with the buffer
  399. FSOUND_SAMPLE *samplep = bufferp->getSample();
  400. if (!samplep)
  401. {
  402. // This is bad, there should ALWAYS be a sample associated with a legit
  403. // buffer.
  404. llerrs << "No FMOD sample!" << llendl;
  405. return false;
  406. }
  407. // Actually play the sound. Start it off paused so we can do all the necessary
  408. // setup.
  409. mChannelID = FSOUND_PlaySoundEx(FSOUND_FREE, samplep, FSOUND_DSP_GetSFXUnit(), true);
  410. //llinfos << "Setting up channel " << std::hex << mChannelID << std::dec << llendl;
  411. }
  412. // If we have a source for the channel, we need to update its gain.
  413. if (mCurrentSourcep)
  414. {
  415. // SJB: warnings can spam and hurt framerate, disabling
  416. if (!FSOUND_SetVolume(mChannelID, llround(getSecondaryGain() * mCurrentSourcep->getGain() * 255.0f)))
  417. {
  418. // llwarns << "LLAudioChannelFMOD::updateBuffer error: " << FMOD_ErrorString(FSOUND_GetError()) << llendl;
  419. }
  420. if (!FSOUND_SetLoopMode(mChannelID, mCurrentSourcep->isLoop() ? FSOUND_LOOP_NORMAL : FSOUND_LOOP_OFF))
  421. {
  422. // llwarns << "Channel " << mChannelID << "Source ID: " << mCurrentSourcep->getID()
  423. // << " at " << mCurrentSourcep->getPositionGlobal() << llendl;
  424. // llwarns << "LLAudioChannelFMOD::updateBuffer error: " << FMOD_ErrorString(FSOUND_GetError()) << llendl;
  425. }
  426. }
  427. return true;
  428. }
  429. void LLAudioChannelFMOD::update3DPosition()
  430. {
  431. if (!mChannelID)
  432. {
  433. // We're not actually a live channel (i.e., we're not playing back anything)
  434. return;
  435. }
  436. LLAudioBufferFMOD *bufferp = (LLAudioBufferFMOD *)mCurrentBufferp;
  437. if (!bufferp)
  438. {
  439. // We don't have a buffer associated with us (should really have been picked up
  440. // by the above if.
  441. return;
  442. }
  443. if (mCurrentSourcep->isAmbient())
  444. {
  445. // Ambient sound, don't need to do any positional updates.
  446. bufferp->set3DMode(false);
  447. }
  448. else
  449. {
  450. // Localized sound. Update the position and velocity of the sound.
  451. bufferp->set3DMode(true);
  452. LLVector3 float_pos;
  453. float_pos.setVec(mCurrentSourcep->getPositionGlobal());
  454. if (!FSOUND_3D_SetAttributes(mChannelID, float_pos.mV, mCurrentSourcep->getVelocity().mV))
  455. {
  456. LL_DEBUGS("FMOD") << "LLAudioChannelFMOD::update3DPosition error: " << FMOD_ErrorString(FSOUND_GetError()) << LL_ENDL;
  457. }
  458. }
  459. }
  460. void LLAudioChannelFMOD::updateLoop()
  461. {
  462. if (!mChannelID)
  463. {
  464. // May want to clear up the loop/sample counters.
  465. return;
  466. }
  467. //
  468. // Hack: We keep track of whether we looped or not by seeing when the
  469. // sample position looks like it's going backwards. Not reliable; may
  470. // yield false negatives.
  471. //
  472. U32 cur_pos = FSOUND_GetCurrentPosition(mChannelID);
  473. if (cur_pos < (U32)mLastSamplePos)
  474. {
  475. mLoopedThisFrame = true;
  476. }
  477. mLastSamplePos = cur_pos;
  478. }
  479. void LLAudioChannelFMOD::cleanup()
  480. {
  481. if (!mChannelID)
  482. {
  483. //llinfos << "Aborting cleanup with no channelID." << llendl;
  484. return;
  485. }
  486. //llinfos << "Cleaning up channel: " << mChannelID << llendl;
  487. if (!FSOUND_StopSound(mChannelID))
  488. {
  489. LL_DEBUGS("FMOD") << "LLAudioChannelFMOD::cleanup error: " << FMOD_ErrorString(FSOUND_GetError()) << llendl;
  490. }
  491. mCurrentBufferp = NULL;
  492. mChannelID = 0;
  493. }
  494. void LLAudioChannelFMOD::play()
  495. {
  496. if (!mChannelID)
  497. {
  498. llwarns << "Playing without a channelID, aborting" << llendl;
  499. return;
  500. }
  501. if (!FSOUND_SetPaused(mChannelID, false))
  502. {
  503. llwarns << "LLAudioChannelFMOD::play error: " << FMOD_ErrorString(FSOUND_GetError()) << llendl;
  504. }
  505. getSource()->setPlayedOnce(true);
  506. }
  507. void LLAudioChannelFMOD::playSynced(LLAudioChannel *channelp)
  508. {
  509. LLAudioChannelFMOD *fmod_channelp = (LLAudioChannelFMOD*)channelp;
  510. if (!(fmod_channelp->mChannelID && mChannelID))
  511. {
  512. // Don't have channels allocated to both the master and the slave
  513. return;
  514. }
  515. U32 position = FSOUND_GetCurrentPosition(fmod_channelp->mChannelID) % mCurrentBufferp->getLength();
  516. // Try to match the position of our sync master
  517. if (!FSOUND_SetCurrentPosition(mChannelID, position))
  518. {
  519. llwarns << "LLAudioChannelFMOD::playSynced unable to set current position" << llendl;
  520. }
  521. // Start us playing
  522. play();
  523. }
  524. bool LLAudioChannelFMOD::isPlaying()
  525. {
  526. if (!mChannelID)
  527. {
  528. return false;
  529. }
  530. return FSOUND_IsPlaying(mChannelID) && (!FSOUND_GetPaused(mChannelID));
  531. }
  532. //
  533. // LLAudioBufferFMOD implementation
  534. //
  535. LLAudioBufferFMOD::LLAudioBufferFMOD()
  536. {
  537. mSamplep = NULL;
  538. }
  539. LLAudioBufferFMOD::~LLAudioBufferFMOD()
  540. {
  541. if (mSamplep)
  542. {
  543. // Clean up the associated FMOD sample if it exists.
  544. FSOUND_Sample_Free(mSamplep);
  545. mSamplep = NULL;
  546. }
  547. }
  548. bool LLAudioBufferFMOD::loadWAV(const std::string& filename)
  549. {
  550. // Try to open a wav file from disk. This will eventually go away, as we don't
  551. // really want to block doing this.
  552. if (filename.empty())
  553. {
  554. // invalid filename, abort.
  555. return false;
  556. }
  557. if (!LLAPRFile::isExist(filename, NULL, LL_APR_RPB))
  558. {
  559. // File not found, abort.
  560. return false;
  561. }
  562. if (mSamplep)
  563. {
  564. // If there's already something loaded in this buffer, clean it up.
  565. FSOUND_Sample_Free(mSamplep);
  566. mSamplep = NULL;
  567. }
  568. // Load up the wav file into an fmod sample
  569. #if LL_WINDOWS
  570. // MikeS. - Loading the sound file manually and then handing it over to FMOD,
  571. // since FMOD uses posix IO internally,
  572. // which doesn't work with unicode file paths.
  573. LLFILE* sound_file = LLFile::fopen(filename,"rb"); /* Flawfinder: ignore */
  574. if (sound_file)
  575. {
  576. fseek(sound_file,0,SEEK_END);
  577. U32 file_length = ftell(sound_file); //Find the length of the file by seeking to the end and getting the offset
  578. size_t read_count;
  579. fseek(sound_file,0,SEEK_SET); //Seek back to the beginning
  580. char* buffer = new char[file_length];
  581. llassert(buffer);
  582. read_count = fread((void*)buffer,file_length,1,sound_file);//Load it..
  583. if(ferror(sound_file)==0 && (read_count == 1)){//No read error, and we got 1 chunk of our size...
  584. unsigned int mode_flags = FSOUND_LOOP_NORMAL | FSOUND_LOADMEMORY;
  585. //FSOUND_16BITS | FSOUND_MONO | FSOUND_LOADMEMORY | FSOUND_LOOP_NORMAL;
  586. mSamplep = FSOUND_Sample_Load(FSOUND_UNMANAGED, buffer, mode_flags , 0, file_length);
  587. }
  588. delete[] buffer;
  589. fclose(sound_file);
  590. }
  591. #else
  592. mSamplep = FSOUND_Sample_Load(FSOUND_UNMANAGED, filename.c_str(), FSOUND_LOOP_NORMAL, 0, 0);
  593. #endif
  594. if (!mSamplep)
  595. {
  596. // We failed to load the file for some reason.
  597. llwarns << "Could not load data '" << filename << "': "
  598. << FMOD_ErrorString(FSOUND_GetError()) << llendl;
  599. //
  600. // If we EVER want to load wav files provided by end users, we need
  601. // to rethink this!
  602. //
  603. // file is probably corrupt - remove it.
  604. LLFile::remove(filename);
  605. return false;
  606. }
  607. // Everything went well, return true
  608. return true;
  609. }
  610. U32 LLAudioBufferFMOD::getLength()
  611. {
  612. if (!mSamplep)
  613. {
  614. return 0;
  615. }
  616. return FSOUND_Sample_GetLength(mSamplep);
  617. }
  618. void LLAudioBufferFMOD::set3DMode(bool use3d)
  619. {
  620. U16 current_mode = FSOUND_Sample_GetMode(mSamplep);
  621. if (use3d)
  622. {
  623. if (!FSOUND_Sample_SetMode(mSamplep, (current_mode & (~FSOUND_2D))))
  624. {
  625. llwarns << "LLAudioBufferFMOD::set3DMode error: " << FMOD_ErrorString(FSOUND_GetError()) << llendl;
  626. }
  627. }
  628. else
  629. {
  630. if (!FSOUND_Sample_SetMode(mSamplep, current_mode | FSOUND_2D))
  631. {
  632. llwarns << "LLAudioBufferFMOD::set3DMode error: " << FMOD_ErrorString(FSOUND_GetError()) << llendl;
  633. }
  634. }
  635. }
  636. void * F_CALLBACKAPI windCallback(void *originalbuffer, void *newbuffer, int length, void* userdata)
  637. {
  638. // originalbuffer = fmod's original mixbuffer.
  639. // newbuffer = the buffer passed from the previous DSP unit.
  640. // length = length in samples at this mix time.
  641. // userdata = user parameter passed through in FSOUND_DSP_Create.
  642. LLWindGen<LLAudioEngine_FMOD::MIXBUFFERFORMAT> *windgen =
  643. (LLWindGen<LLAudioEngine_FMOD::MIXBUFFERFORMAT> *)userdata;
  644. newbuffer = windgen->windGenerate((LLAudioEngine_FMOD::MIXBUFFERFORMAT *)newbuffer, length);
  645. return newbuffer;
  646. }