sound_openal.cpp 17 KB

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