sound_openal.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. OpenAL support based on work by:
  5. Copyright (C) 2011 Sebastian 'Bahamada' Rühl
  6. Copyright (C) 2011 Cyriaque 'Cisoun' Skrapits <cysoun@gmail.com>
  7. Copyright (C) 2011 Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU Lesser General Public License as published by
  10. the Free Software Foundation; either version 2.1 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU Lesser General Public License for more details.
  16. You should have received a copy of the GNU Lesser General Public License along
  17. with this program; ifnot, write to the Free Software Foundation, Inc.,
  18. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "sound_openal.h"
  21. #if defined(_WIN32)
  22. #include <al.h>
  23. #include <alc.h>
  24. //#include <alext.h>
  25. #elif defined(__APPLE__)
  26. #include <OpenAL/al.h>
  27. #include <OpenAL/alc.h>
  28. //#include <OpenAL/alext.h>
  29. #else
  30. #include <AL/al.h>
  31. #include <AL/alc.h>
  32. #include <AL/alext.h>
  33. #endif
  34. #include <cmath>
  35. #include <vorbis/vorbisfile.h>
  36. #include <cassert>
  37. #include "log.h"
  38. #include "util/numeric.h" // myrand()
  39. #include "porting.h"
  40. #include <vector>
  41. #include <fstream>
  42. #include <unordered_map>
  43. #define BUFFER_SIZE 30000
  44. static const char *alcErrorString(ALCenum err)
  45. {
  46. switch (err) {
  47. case ALC_NO_ERROR:
  48. return "no error";
  49. case ALC_INVALID_DEVICE:
  50. return "invalid device";
  51. case ALC_INVALID_CONTEXT:
  52. return "invalid context";
  53. case ALC_INVALID_ENUM:
  54. return "invalid enum";
  55. case ALC_INVALID_VALUE:
  56. return "invalid value";
  57. case ALC_OUT_OF_MEMORY:
  58. return "out of memory";
  59. default:
  60. return "<unknown OpenAL error>";
  61. }
  62. }
  63. static const char *alErrorString(ALenum err)
  64. {
  65. switch (err) {
  66. case AL_NO_ERROR:
  67. return "no error";
  68. case AL_INVALID_NAME:
  69. return "invalid name";
  70. case AL_INVALID_ENUM:
  71. return "invalid enum";
  72. case AL_INVALID_VALUE:
  73. return "invalid value";
  74. case AL_INVALID_OPERATION:
  75. return "invalid operation";
  76. case AL_OUT_OF_MEMORY:
  77. return "out of memory";
  78. default:
  79. return "<unknown OpenAL error>";
  80. }
  81. }
  82. static ALenum warn_if_error(ALenum err, const char *desc)
  83. {
  84. if(err == AL_NO_ERROR)
  85. return err;
  86. warningstream<<desc<<": "<<alErrorString(err)<<std::endl;
  87. return err;
  88. }
  89. void f3_set(ALfloat *f3, v3f v)
  90. {
  91. f3[0] = v.X;
  92. f3[1] = v.Y;
  93. f3[2] = v.Z;
  94. }
  95. struct SoundBuffer
  96. {
  97. ALenum format;
  98. ALsizei freq;
  99. ALuint buffer_id;
  100. std::vector<char> buffer;
  101. };
  102. SoundBuffer *load_opened_ogg_file(OggVorbis_File *oggFile,
  103. const std::string &filename_for_logging)
  104. {
  105. int endian = 0; // 0 for Little-Endian, 1 for Big-Endian
  106. int bitStream;
  107. long bytes;
  108. char array[BUFFER_SIZE]; // Local fixed size array
  109. vorbis_info *pInfo;
  110. SoundBuffer *snd = new SoundBuffer;
  111. // Get some information about the OGG file
  112. pInfo = ov_info(oggFile, -1);
  113. // Check the number of channels... always use 16-bit samples
  114. if(pInfo->channels == 1)
  115. snd->format = AL_FORMAT_MONO16;
  116. else
  117. snd->format = AL_FORMAT_STEREO16;
  118. // The frequency of the sampling rate
  119. snd->freq = pInfo->rate;
  120. // Keep reading until all is read
  121. do
  122. {
  123. // Read up to a buffer's worth of decoded sound data
  124. bytes = ov_read(oggFile, array, BUFFER_SIZE, endian, 2, 1, &bitStream);
  125. if(bytes < 0)
  126. {
  127. ov_clear(oggFile);
  128. infostream << "Audio: Error decoding "
  129. << filename_for_logging << std::endl;
  130. delete snd;
  131. return NULL;
  132. }
  133. // Append to end of buffer
  134. snd->buffer.insert(snd->buffer.end(), array, array + bytes);
  135. } while (bytes > 0);
  136. alGenBuffers(1, &snd->buffer_id);
  137. alBufferData(snd->buffer_id, snd->format,
  138. &(snd->buffer[0]), snd->buffer.size(),
  139. snd->freq);
  140. ALenum error = alGetError();
  141. if(error != AL_NO_ERROR){
  142. infostream<<"Audio: OpenAL error: "<<alErrorString(error)
  143. <<"preparing sound buffer"<<std::endl;
  144. }
  145. infostream << "Audio file "
  146. << filename_for_logging << " loaded" << std::endl;
  147. // Clean up!
  148. ov_clear(oggFile);
  149. return snd;
  150. }
  151. SoundBuffer *load_ogg_from_file(const std::string &path)
  152. {
  153. OggVorbis_File oggFile;
  154. // Try opening the given file.
  155. // This requires libvorbis >= 1.3.2, as
  156. // previous versions expect a non-const char *
  157. if (ov_fopen(path.c_str(), &oggFile) != 0) {
  158. infostream << "Audio: Error opening " << path
  159. << " for decoding" << std::endl;
  160. return NULL;
  161. }
  162. return load_opened_ogg_file(&oggFile, path);
  163. }
  164. struct BufferSource {
  165. const char *buf;
  166. size_t cur_offset;
  167. size_t len;
  168. };
  169. size_t buffer_sound_read_func(void *ptr, size_t size, size_t nmemb, void *datasource)
  170. {
  171. BufferSource *s = (BufferSource *)datasource;
  172. size_t copied_size = MYMIN(s->len - s->cur_offset, size);
  173. memcpy(ptr, s->buf + s->cur_offset, copied_size);
  174. s->cur_offset += copied_size;
  175. return copied_size;
  176. }
  177. int buffer_sound_seek_func(void *datasource, ogg_int64_t offset, int whence)
  178. {
  179. BufferSource *s = (BufferSource *)datasource;
  180. if (whence == SEEK_SET) {
  181. if (offset < 0 || (size_t)MYMAX(offset, 0) >= s->len) {
  182. // offset out of bounds
  183. return -1;
  184. }
  185. s->cur_offset = offset;
  186. return 0;
  187. } else if (whence == SEEK_CUR) {
  188. if ((size_t)MYMIN(-offset, 0) > s->cur_offset
  189. || s->cur_offset + offset > s->len) {
  190. // offset out of bounds
  191. return -1;
  192. }
  193. s->cur_offset += offset;
  194. return 0;
  195. }
  196. // invalid whence param (SEEK_END doesn't have to be supported)
  197. return -1;
  198. }
  199. long BufferSourceell_func(void *datasource)
  200. {
  201. BufferSource *s = (BufferSource *)datasource;
  202. return s->cur_offset;
  203. }
  204. static ov_callbacks g_buffer_ov_callbacks = {
  205. &buffer_sound_read_func,
  206. &buffer_sound_seek_func,
  207. NULL,
  208. &BufferSourceell_func
  209. };
  210. SoundBuffer *load_ogg_from_buffer(const std::string &buf, const std::string &id_for_log)
  211. {
  212. OggVorbis_File oggFile;
  213. BufferSource s;
  214. s.buf = buf.c_str();
  215. s.cur_offset = 0;
  216. s.len = buf.size();
  217. if (ov_open_callbacks(&s, &oggFile, NULL, 0, g_buffer_ov_callbacks) != 0) {
  218. infostream << "Audio: Error opening " << id_for_log
  219. << " for decoding" << std::endl;
  220. return NULL;
  221. }
  222. return load_opened_ogg_file(&oggFile, id_for_log);
  223. }
  224. struct PlayingSound
  225. {
  226. ALuint source_id;
  227. bool loop;
  228. };
  229. class OpenALSoundManager: public ISoundManager
  230. {
  231. private:
  232. OnDemandSoundFetcher *m_fetcher;
  233. ALCdevice *m_device;
  234. ALCcontext *m_context;
  235. int m_next_id;
  236. std::unordered_map<std::string, std::vector<SoundBuffer*>> m_buffers;
  237. std::unordered_map<int, PlayingSound*> m_sounds_playing;
  238. v3f m_listener_pos;
  239. struct FadeState {
  240. FadeState() = default;
  241. FadeState(float step, float current_gain, float target_gain):
  242. step(step),
  243. current_gain(current_gain),
  244. target_gain(target_gain) {}
  245. float step;
  246. float current_gain;
  247. float target_gain;
  248. };
  249. std::unordered_map<int, FadeState> m_sounds_fading;
  250. float m_fade_delay;
  251. public:
  252. bool m_is_initialized;
  253. OpenALSoundManager(OnDemandSoundFetcher *fetcher):
  254. m_fetcher(fetcher),
  255. m_device(NULL),
  256. m_context(NULL),
  257. m_next_id(1),
  258. m_fade_delay(0),
  259. m_is_initialized(false)
  260. {
  261. ALCenum error = ALC_NO_ERROR;
  262. infostream<<"Audio: Initializing..."<<std::endl;
  263. m_device = alcOpenDevice(NULL);
  264. if(!m_device){
  265. infostream<<"Audio: No audio device available, audio system "
  266. <<"not initialized"<<std::endl;
  267. return;
  268. }
  269. m_context = alcCreateContext(m_device, NULL);
  270. if(!m_context){
  271. error = alcGetError(m_device);
  272. infostream<<"Audio: Unable to initialize audio context, "
  273. <<"aborting audio initialization ("<<alcErrorString(error)
  274. <<")"<<std::endl;
  275. alcCloseDevice(m_device);
  276. m_device = NULL;
  277. return;
  278. }
  279. if(!alcMakeContextCurrent(m_context) ||
  280. (error = alcGetError(m_device) != ALC_NO_ERROR))
  281. {
  282. infostream<<"Audio: Error setting audio context, aborting audio "
  283. <<"initialization ("<<alcErrorString(error)<<")"<<std::endl;
  284. alcDestroyContext(m_context);
  285. m_context = NULL;
  286. alcCloseDevice(m_device);
  287. m_device = NULL;
  288. return;
  289. }
  290. alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
  291. infostream<<"Audio: Initialized: OpenAL "<<alGetString(AL_VERSION)
  292. <<", using "<<alcGetString(m_device, ALC_DEVICE_SPECIFIER)
  293. <<std::endl;
  294. m_is_initialized = true;
  295. }
  296. ~OpenALSoundManager()
  297. {
  298. infostream<<"Audio: Deinitializing..."<<std::endl;
  299. // KABOOM!
  300. // TODO: Clear SoundBuffers
  301. alcMakeContextCurrent(NULL);
  302. alcDestroyContext(m_context);
  303. m_context = NULL;
  304. alcCloseDevice(m_device);
  305. m_device = NULL;
  306. for (auto &buffer : m_buffers) {
  307. for (SoundBuffer *sb : buffer.second) {
  308. delete sb;
  309. }
  310. buffer.second.clear();
  311. }
  312. m_buffers.clear();
  313. infostream<<"Audio: Deinitialized."<<std::endl;
  314. }
  315. void step(float dtime)
  316. {
  317. doFades(dtime);
  318. }
  319. void addBuffer(const std::string &name, SoundBuffer *buf)
  320. {
  321. std::unordered_map<std::string, std::vector<SoundBuffer*>>::iterator i =
  322. m_buffers.find(name);
  323. if(i != m_buffers.end()){
  324. i->second.push_back(buf);
  325. return;
  326. }
  327. std::vector<SoundBuffer*> bufs;
  328. bufs.push_back(buf);
  329. m_buffers[name] = bufs;
  330. }
  331. SoundBuffer* getBuffer(const std::string &name)
  332. {
  333. std::unordered_map<std::string, std::vector<SoundBuffer*>>::iterator i =
  334. m_buffers.find(name);
  335. if(i == m_buffers.end())
  336. return NULL;
  337. std::vector<SoundBuffer*> &bufs = i->second;
  338. int j = myrand() % bufs.size();
  339. return bufs[j];
  340. }
  341. PlayingSound* createPlayingSound(SoundBuffer *buf, bool loop,
  342. float volume, float pitch)
  343. {
  344. infostream<<"OpenALSoundManager: Creating playing sound"<<std::endl;
  345. assert(buf);
  346. PlayingSound *sound = new PlayingSound;
  347. assert(sound);
  348. warn_if_error(alGetError(), "before createPlayingSound");
  349. alGenSources(1, &sound->source_id);
  350. alSourcei(sound->source_id, AL_BUFFER, buf->buffer_id);
  351. alSourcei(sound->source_id, AL_SOURCE_RELATIVE, true);
  352. alSource3f(sound->source_id, AL_POSITION, 0, 0, 0);
  353. alSource3f(sound->source_id, AL_VELOCITY, 0, 0, 0);
  354. alSourcei(sound->source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
  355. volume = std::fmax(0.0f, volume);
  356. alSourcef(sound->source_id, AL_GAIN, volume);
  357. alSourcef(sound->source_id, AL_PITCH, pitch);
  358. alSourcePlay(sound->source_id);
  359. warn_if_error(alGetError(), "createPlayingSound");
  360. return sound;
  361. }
  362. PlayingSound* createPlayingSoundAt(SoundBuffer *buf, bool loop,
  363. float volume, v3f pos, float pitch)
  364. {
  365. infostream<<"OpenALSoundManager: Creating positional playing sound"
  366. <<std::endl;
  367. assert(buf);
  368. PlayingSound *sound = new PlayingSound;
  369. assert(sound);
  370. warn_if_error(alGetError(), "before createPlayingSoundAt");
  371. alGenSources(1, &sound->source_id);
  372. alSourcei(sound->source_id, AL_BUFFER, buf->buffer_id);
  373. alSourcei(sound->source_id, AL_SOURCE_RELATIVE, false);
  374. alSource3f(sound->source_id, AL_POSITION, pos.X, pos.Y, pos.Z);
  375. alSource3f(sound->source_id, AL_VELOCITY, 0, 0, 0);
  376. // Use alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED) and set reference
  377. // distance to clamp gain at <1 node distance, to avoid excessive
  378. // volume when closer
  379. alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 10.0f);
  380. alSourcei(sound->source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
  381. // Multiply by 3 to compensate for reducing AL_REFERENCE_DISTANCE from
  382. // the previous value of 30 to the new value of 10
  383. volume = std::fmax(0.0f, volume * 3.0f);
  384. alSourcef(sound->source_id, AL_GAIN, volume);
  385. alSourcef(sound->source_id, AL_PITCH, pitch);
  386. alSourcePlay(sound->source_id);
  387. warn_if_error(alGetError(), "createPlayingSoundAt");
  388. return sound;
  389. }
  390. int playSoundRaw(SoundBuffer *buf, bool loop, float volume, float pitch)
  391. {
  392. assert(buf);
  393. PlayingSound *sound = createPlayingSound(buf, loop, volume, pitch);
  394. if(!sound)
  395. return -1;
  396. int id = m_next_id++;
  397. m_sounds_playing[id] = sound;
  398. return id;
  399. }
  400. int playSoundRawAt(SoundBuffer *buf, bool loop, float volume, const v3f &pos,
  401. float pitch)
  402. {
  403. assert(buf);
  404. PlayingSound *sound = createPlayingSoundAt(buf, loop, volume, pos, pitch);
  405. if(!sound)
  406. return -1;
  407. int id = m_next_id++;
  408. m_sounds_playing[id] = sound;
  409. return id;
  410. }
  411. void deleteSound(int id)
  412. {
  413. std::unordered_map<int, PlayingSound*>::iterator i = m_sounds_playing.find(id);
  414. if(i == m_sounds_playing.end())
  415. return;
  416. PlayingSound *sound = i->second;
  417. alDeleteSources(1, &sound->source_id);
  418. delete sound;
  419. m_sounds_playing.erase(id);
  420. }
  421. /* If buffer does not exist, consult the fetcher */
  422. SoundBuffer* getFetchBuffer(const std::string &name)
  423. {
  424. SoundBuffer *buf = getBuffer(name);
  425. if(buf)
  426. return buf;
  427. if(!m_fetcher)
  428. return NULL;
  429. std::set<std::string> paths;
  430. std::set<std::string> datas;
  431. m_fetcher->fetchSounds(name, paths, datas);
  432. for (const std::string &path : paths) {
  433. loadSoundFile(name, path);
  434. }
  435. for (const std::string &data : datas) {
  436. loadSoundData(name, data);
  437. }
  438. return getBuffer(name);
  439. }
  440. // Remove stopped sounds
  441. void maintain()
  442. {
  443. verbosestream<<"OpenALSoundManager::maintain(): "
  444. <<m_sounds_playing.size()<<" playing sounds, "
  445. <<m_buffers.size()<<" sound names loaded"<<std::endl;
  446. std::set<int> del_list;
  447. for (auto &sp : m_sounds_playing) {
  448. int id = sp.first;
  449. PlayingSound *sound = sp.second;
  450. // If not playing, remove it
  451. {
  452. ALint state;
  453. alGetSourcei(sound->source_id, AL_SOURCE_STATE, &state);
  454. if(state != AL_PLAYING){
  455. del_list.insert(id);
  456. }
  457. }
  458. }
  459. if(!del_list.empty())
  460. verbosestream<<"OpenALSoundManager::maintain(): deleting "
  461. <<del_list.size()<<" playing sounds"<<std::endl;
  462. for (int i : del_list) {
  463. deleteSound(i);
  464. }
  465. }
  466. /* Interface */
  467. bool loadSoundFile(const std::string &name,
  468. const std::string &filepath)
  469. {
  470. SoundBuffer *buf = load_ogg_from_file(filepath);
  471. if (buf)
  472. addBuffer(name, buf);
  473. return false;
  474. }
  475. bool loadSoundData(const std::string &name,
  476. const std::string &filedata)
  477. {
  478. SoundBuffer *buf = load_ogg_from_buffer(filedata, name);
  479. if (buf)
  480. addBuffer(name, buf);
  481. return false;
  482. }
  483. void updateListener(v3f pos, v3f vel, v3f at, v3f up)
  484. {
  485. m_listener_pos = pos;
  486. alListener3f(AL_POSITION, pos.X, pos.Y, pos.Z);
  487. alListener3f(AL_VELOCITY, vel.X, vel.Y, vel.Z);
  488. ALfloat f[6];
  489. f3_set(f, at);
  490. f3_set(f+3, -up);
  491. alListenerfv(AL_ORIENTATION, f);
  492. warn_if_error(alGetError(), "updateListener");
  493. }
  494. void setListenerGain(float gain)
  495. {
  496. alListenerf(AL_GAIN, gain);
  497. }
  498. int playSound(const std::string &name, bool loop, float volume, float fade, float pitch)
  499. {
  500. maintain();
  501. if (name.empty())
  502. return 0;
  503. SoundBuffer *buf = getFetchBuffer(name);
  504. if(!buf){
  505. infostream<<"OpenALSoundManager: \""<<name<<"\" not found."
  506. <<std::endl;
  507. return -1;
  508. }
  509. int handle = -1;
  510. if (fade > 0) {
  511. handle = playSoundRaw(buf, loop, 0.0f, pitch);
  512. fadeSound(handle, fade, volume);
  513. } else {
  514. handle = playSoundRaw(buf, loop, volume, pitch);
  515. }
  516. return handle;
  517. }
  518. int playSoundAt(const std::string &name, bool loop, float volume, v3f pos, float pitch)
  519. {
  520. maintain();
  521. if (name.empty())
  522. return 0;
  523. SoundBuffer *buf = getFetchBuffer(name);
  524. if(!buf){
  525. infostream<<"OpenALSoundManager: \""<<name<<"\" not found."
  526. <<std::endl;
  527. return -1;
  528. }
  529. return playSoundRawAt(buf, loop, volume, pos, pitch);
  530. }
  531. void stopSound(int sound)
  532. {
  533. maintain();
  534. deleteSound(sound);
  535. }
  536. void fadeSound(int soundid, float step, float gain)
  537. {
  538. m_sounds_fading[soundid] = FadeState(step, getSoundGain(soundid), gain);
  539. }
  540. void doFades(float dtime)
  541. {
  542. m_fade_delay += dtime;
  543. if (m_fade_delay < 0.1f)
  544. return;
  545. float chkGain = 0;
  546. for (std::unordered_map<int, FadeState>::iterator i = m_sounds_fading.begin();
  547. i != m_sounds_fading.end();) {
  548. if (i->second.step < 0.f)
  549. chkGain = -(i->second.current_gain);
  550. else
  551. chkGain = i->second.current_gain;
  552. if (chkGain < i->second.target_gain) {
  553. i->second.current_gain += (i->second.step * m_fade_delay);
  554. i->second.current_gain = rangelim(i->second.current_gain, 0, 1);
  555. updateSoundGain(i->first, i->second.current_gain);
  556. ++i;
  557. } else {
  558. if (i->second.target_gain <= 0.f)
  559. stopSound(i->first);
  560. m_sounds_fading.erase(i++);
  561. }
  562. }
  563. m_fade_delay = 0;
  564. }
  565. bool soundExists(int sound)
  566. {
  567. maintain();
  568. return (m_sounds_playing.count(sound) != 0);
  569. }
  570. void updateSoundPosition(int id, v3f pos)
  571. {
  572. std::unordered_map<int, PlayingSound*>::iterator i = m_sounds_playing.find(id);
  573. if (i == m_sounds_playing.end())
  574. return;
  575. PlayingSound *sound = i->second;
  576. alSourcei(sound->source_id, AL_SOURCE_RELATIVE, false);
  577. alSource3f(sound->source_id, AL_POSITION, pos.X, pos.Y, pos.Z);
  578. alSource3f(sound->source_id, AL_VELOCITY, 0, 0, 0);
  579. alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 30.0);
  580. }
  581. bool updateSoundGain(int id, float gain)
  582. {
  583. std::unordered_map<int, PlayingSound*>::iterator i = m_sounds_playing.find(id);
  584. if (i == m_sounds_playing.end())
  585. return false;
  586. PlayingSound *sound = i->second;
  587. alSourcef(sound->source_id, AL_GAIN, gain);
  588. return true;
  589. }
  590. float getSoundGain(int id)
  591. {
  592. std::unordered_map<int, PlayingSound*>::iterator i = m_sounds_playing.find(id);
  593. if (i == m_sounds_playing.end())
  594. return 0;
  595. PlayingSound *sound = i->second;
  596. ALfloat gain;
  597. alGetSourcef(sound->source_id, AL_GAIN, &gain);
  598. return gain;
  599. }
  600. };
  601. ISoundManager *createOpenALSoundManager(OnDemandSoundFetcher *fetcher)
  602. {
  603. OpenALSoundManager *m = new OpenALSoundManager(fetcher);
  604. if(m->m_is_initialized)
  605. return m;
  606. delete m;
  607. return NULL;
  608. };