sound_openal.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. #define OPENAL_DEPRECATED
  27. #include <OpenAL/al.h>
  28. #include <OpenAL/alc.h>
  29. //#include <OpenAL/alext.h>
  30. #else
  31. #include <AL/al.h>
  32. #include <AL/alc.h>
  33. #include <AL/alext.h>
  34. #endif
  35. #include <cmath>
  36. #include <vorbis/vorbisfile.h>
  37. #include <cassert>
  38. #include "log.h"
  39. #include "util/numeric.h" // myrand()
  40. #include "porting.h"
  41. #include <vector>
  42. #include <fstream>
  43. #include <unordered_map>
  44. #include <unordered_set>
  45. #define BUFFER_SIZE 30000
  46. std::shared_ptr<SoundManagerSingleton> g_sound_manager_singleton;
  47. typedef std::unique_ptr<ALCdevice, void (*)(ALCdevice *p)> unique_ptr_alcdevice;
  48. typedef std::unique_ptr<ALCcontext, void(*)(ALCcontext *p)> unique_ptr_alccontext;
  49. static void delete_alcdevice(ALCdevice *p)
  50. {
  51. if (p)
  52. alcCloseDevice(p);
  53. }
  54. static void delete_alccontext(ALCcontext *p)
  55. {
  56. if (p) {
  57. alcMakeContextCurrent(nullptr);
  58. alcDestroyContext(p);
  59. }
  60. }
  61. static const char *alErrorString(ALenum err)
  62. {
  63. switch (err) {
  64. case AL_NO_ERROR:
  65. return "no error";
  66. case AL_INVALID_NAME:
  67. return "invalid name";
  68. case AL_INVALID_ENUM:
  69. return "invalid enum";
  70. case AL_INVALID_VALUE:
  71. return "invalid value";
  72. case AL_INVALID_OPERATION:
  73. return "invalid operation";
  74. case AL_OUT_OF_MEMORY:
  75. return "out of memory";
  76. default:
  77. return "<unknown OpenAL error>";
  78. }
  79. }
  80. static ALenum warn_if_error(ALenum err, const char *desc)
  81. {
  82. if(err == AL_NO_ERROR)
  83. return err;
  84. warningstream<<desc<<": "<<alErrorString(err)<<std::endl;
  85. return err;
  86. }
  87. void f3_set(ALfloat *f3, v3f v)
  88. {
  89. f3[0] = v.X;
  90. f3[1] = v.Y;
  91. f3[2] = v.Z;
  92. }
  93. struct SoundBuffer
  94. {
  95. ALenum format;
  96. ALsizei freq;
  97. ALuint buffer_id;
  98. std::vector<char> buffer;
  99. };
  100. SoundBuffer *load_opened_ogg_file(OggVorbis_File *oggFile,
  101. const std::string &filename_for_logging)
  102. {
  103. int endian = 0; // 0 for Little-Endian, 1 for Big-Endian
  104. int bitStream;
  105. long bytes;
  106. char array[BUFFER_SIZE]; // Local fixed size array
  107. vorbis_info *pInfo;
  108. SoundBuffer *snd = new SoundBuffer;
  109. // Get some information about the OGG file
  110. pInfo = ov_info(oggFile, -1);
  111. // Check the number of channels... always use 16-bit samples
  112. if(pInfo->channels == 1)
  113. snd->format = AL_FORMAT_MONO16;
  114. else
  115. snd->format = AL_FORMAT_STEREO16;
  116. // The frequency of the sampling rate
  117. snd->freq = pInfo->rate;
  118. // Keep reading until all is read
  119. do
  120. {
  121. // Read up to a buffer's worth of decoded sound data
  122. bytes = ov_read(oggFile, array, BUFFER_SIZE, endian, 2, 1, &bitStream);
  123. if(bytes < 0)
  124. {
  125. ov_clear(oggFile);
  126. infostream << "Audio: Error decoding "
  127. << filename_for_logging << std::endl;
  128. delete snd;
  129. return nullptr;
  130. }
  131. // Append to end of buffer
  132. snd->buffer.insert(snd->buffer.end(), array, array + bytes);
  133. } while (bytes > 0);
  134. alGenBuffers(1, &snd->buffer_id);
  135. alBufferData(snd->buffer_id, snd->format,
  136. &(snd->buffer[0]), snd->buffer.size(),
  137. snd->freq);
  138. ALenum error = alGetError();
  139. if(error != AL_NO_ERROR){
  140. infostream << "Audio: OpenAL error: " << alErrorString(error)
  141. << "preparing sound buffer" << std::endl;
  142. }
  143. //infostream << "Audio file "
  144. // << filename_for_logging << " loaded" << std::endl;
  145. // Clean up!
  146. ov_clear(oggFile);
  147. return snd;
  148. }
  149. SoundBuffer *load_ogg_from_file(const std::string &path)
  150. {
  151. OggVorbis_File oggFile;
  152. // Try opening the given file.
  153. // This requires libvorbis >= 1.3.2, as
  154. // previous versions expect a non-const char *
  155. if (ov_fopen(path.c_str(), &oggFile) != 0) {
  156. infostream << "Audio: Error opening " << path
  157. << " for decoding" << std::endl;
  158. return nullptr;
  159. }
  160. return load_opened_ogg_file(&oggFile, path);
  161. }
  162. struct BufferSource {
  163. const char *buf;
  164. size_t cur_offset;
  165. size_t len;
  166. };
  167. size_t buffer_sound_read_func(void *ptr, size_t size, size_t nmemb, void *datasource)
  168. {
  169. BufferSource *s = (BufferSource *)datasource;
  170. size_t copied_size = MYMIN(s->len - s->cur_offset, size);
  171. memcpy(ptr, s->buf + s->cur_offset, copied_size);
  172. s->cur_offset += copied_size;
  173. return copied_size;
  174. }
  175. int buffer_sound_seek_func(void *datasource, ogg_int64_t offset, int whence)
  176. {
  177. BufferSource *s = (BufferSource *)datasource;
  178. if (whence == SEEK_SET) {
  179. if (offset < 0 || (size_t)MYMAX(offset, 0) >= s->len) {
  180. // offset out of bounds
  181. return -1;
  182. }
  183. s->cur_offset = offset;
  184. return 0;
  185. } else if (whence == SEEK_CUR) {
  186. if ((size_t)MYMIN(-offset, 0) > s->cur_offset
  187. || s->cur_offset + offset > s->len) {
  188. // offset out of bounds
  189. return -1;
  190. }
  191. s->cur_offset += offset;
  192. return 0;
  193. }
  194. // invalid whence param (SEEK_END doesn't have to be supported)
  195. return -1;
  196. }
  197. long BufferSourceell_func(void *datasource)
  198. {
  199. BufferSource *s = (BufferSource *)datasource;
  200. return s->cur_offset;
  201. }
  202. static ov_callbacks g_buffer_ov_callbacks = {
  203. &buffer_sound_read_func,
  204. &buffer_sound_seek_func,
  205. nullptr,
  206. &BufferSourceell_func
  207. };
  208. SoundBuffer *load_ogg_from_buffer(const std::string &buf, const std::string &id_for_log)
  209. {
  210. OggVorbis_File oggFile;
  211. BufferSource s;
  212. s.buf = buf.c_str();
  213. s.cur_offset = 0;
  214. s.len = buf.size();
  215. if (ov_open_callbacks(&s, &oggFile, nullptr, 0, g_buffer_ov_callbacks) != 0) {
  216. infostream << "Audio: Error opening " << id_for_log
  217. << " for decoding" << std::endl;
  218. return nullptr;
  219. }
  220. return load_opened_ogg_file(&oggFile, id_for_log);
  221. }
  222. struct PlayingSound
  223. {
  224. ALuint source_id;
  225. bool loop;
  226. };
  227. class SoundManagerSingleton
  228. {
  229. public:
  230. unique_ptr_alcdevice m_device;
  231. unique_ptr_alccontext m_context;
  232. public:
  233. SoundManagerSingleton() :
  234. m_device(nullptr, delete_alcdevice),
  235. m_context(nullptr, delete_alccontext)
  236. {
  237. }
  238. bool init()
  239. {
  240. if (!(m_device = unique_ptr_alcdevice(alcOpenDevice(nullptr), delete_alcdevice))) {
  241. errorstream << "Audio: Global Initialization: Failed to open device" << std::endl;
  242. return false;
  243. }
  244. if (!(m_context = unique_ptr_alccontext(
  245. alcCreateContext(m_device.get(), nullptr), delete_alccontext))) {
  246. errorstream << "Audio: Global Initialization: Failed to create context" << std::endl;
  247. return false;
  248. }
  249. if (!alcMakeContextCurrent(m_context.get())) {
  250. errorstream << "Audio: Global Initialization: Failed to make current context" << std::endl;
  251. return false;
  252. }
  253. alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
  254. if (alGetError() != AL_NO_ERROR) {
  255. errorstream << "Audio: Global Initialization: OpenAL Error " << alGetError() << std::endl;
  256. return false;
  257. }
  258. infostream << "Audio: Global Initialized: OpenAL " << alGetString(AL_VERSION)
  259. << ", using " << alcGetString(m_device.get(), ALC_DEVICE_SPECIFIER)
  260. << std::endl;
  261. return true;
  262. }
  263. ~SoundManagerSingleton()
  264. {
  265. infostream << "Audio: Global Deinitialized." << std::endl;
  266. }
  267. };
  268. class OpenALSoundManager: public ISoundManager
  269. {
  270. private:
  271. OnDemandSoundFetcher *m_fetcher;
  272. ALCdevice *m_device;
  273. ALCcontext *m_context;
  274. int m_next_id;
  275. std::unordered_map<std::string, std::vector<SoundBuffer*>> m_buffers;
  276. std::unordered_map<int, PlayingSound*> m_sounds_playing;
  277. struct FadeState {
  278. FadeState() = default;
  279. FadeState(float step, float current_gain, float target_gain):
  280. step(step),
  281. current_gain(current_gain),
  282. target_gain(target_gain) {}
  283. float step;
  284. float current_gain;
  285. float target_gain;
  286. };
  287. std::unordered_map<int, FadeState> m_sounds_fading;
  288. public:
  289. OpenALSoundManager(SoundManagerSingleton *smg, OnDemandSoundFetcher *fetcher):
  290. m_fetcher(fetcher),
  291. m_device(smg->m_device.get()),
  292. m_context(smg->m_context.get()),
  293. m_next_id(1)
  294. {
  295. infostream << "Audio: Initialized: OpenAL " << std::endl;
  296. }
  297. ~OpenALSoundManager()
  298. {
  299. infostream << "Audio: Deinitializing..." << std::endl;
  300. std::unordered_set<int> source_del_list;
  301. for (const auto &sp : m_sounds_playing)
  302. source_del_list.insert(sp.first);
  303. for (const auto &id : source_del_list)
  304. deleteSound(id);
  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 nullptr;
  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 = std::fmax(0.0f, 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. // Use alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED) and set reference
  376. // distance to clamp gain at <1 node distance, to avoid excessive
  377. // volume when closer
  378. alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 10.0f);
  379. alSourcei(sound->source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
  380. // Multiply by 3 to compensate for reducing AL_REFERENCE_DISTANCE from
  381. // the previous value of 30 to the new value of 10
  382. volume = std::fmax(0.0f, volume * 3.0f);
  383. alSourcef(sound->source_id, AL_GAIN, volume);
  384. alSourcef(sound->source_id, AL_PITCH, pitch);
  385. alSourcePlay(sound->source_id);
  386. warn_if_error(alGetError(), "createPlayingSoundAt");
  387. return sound;
  388. }
  389. int playSoundRaw(SoundBuffer *buf, bool loop, float volume, float pitch)
  390. {
  391. assert(buf);
  392. PlayingSound *sound = createPlayingSound(buf, loop, volume, pitch);
  393. if(!sound)
  394. return -1;
  395. int id = m_next_id++;
  396. m_sounds_playing[id] = sound;
  397. return id;
  398. }
  399. int playSoundRawAt(SoundBuffer *buf, bool loop, float volume, const v3f &pos,
  400. float pitch)
  401. {
  402. assert(buf);
  403. PlayingSound *sound = createPlayingSoundAt(buf, loop, volume, pos, pitch);
  404. if(!sound)
  405. return -1;
  406. int id = m_next_id++;
  407. m_sounds_playing[id] = sound;
  408. return id;
  409. }
  410. void deleteSound(int id)
  411. {
  412. std::unordered_map<int, PlayingSound*>::iterator i = m_sounds_playing.find(id);
  413. if(i == m_sounds_playing.end())
  414. return;
  415. PlayingSound *sound = i->second;
  416. alDeleteSources(1, &sound->source_id);
  417. delete sound;
  418. m_sounds_playing.erase(id);
  419. }
  420. /* If buffer does not exist, consult the fetcher */
  421. SoundBuffer* getFetchBuffer(const std::string &name)
  422. {
  423. SoundBuffer *buf = getBuffer(name);
  424. if(buf)
  425. return buf;
  426. if(!m_fetcher)
  427. return nullptr;
  428. std::set<std::string> paths;
  429. std::set<std::string> datas;
  430. m_fetcher->fetchSounds(name, paths, datas);
  431. for (const std::string &path : paths) {
  432. loadSoundFile(name, path);
  433. }
  434. for (const std::string &data : datas) {
  435. loadSoundData(name, data);
  436. }
  437. return getBuffer(name);
  438. }
  439. // Remove stopped sounds
  440. void maintain()
  441. {
  442. if (!m_sounds_playing.empty()) {
  443. verbosestream << "OpenALSoundManager::maintain(): "
  444. << m_sounds_playing.size() <<" playing sounds, "
  445. << m_buffers.size() <<" sound names loaded"<<std::endl;
  446. }
  447. std::unordered_set<int> del_list;
  448. for (const auto &sp : m_sounds_playing) {
  449. int id = sp.first;
  450. PlayingSound *sound = sp.second;
  451. // If not playing, remove it
  452. {
  453. ALint state;
  454. alGetSourcei(sound->source_id, AL_SOURCE_STATE, &state);
  455. if(state != AL_PLAYING){
  456. del_list.insert(id);
  457. }
  458. }
  459. }
  460. if(!del_list.empty())
  461. verbosestream<<"OpenALSoundManager::maintain(): deleting "
  462. <<del_list.size()<<" playing sounds"<<std::endl;
  463. for (int i : del_list) {
  464. deleteSound(i);
  465. }
  466. }
  467. /* Interface */
  468. bool loadSoundFile(const std::string &name,
  469. const std::string &filepath)
  470. {
  471. SoundBuffer *buf = load_ogg_from_file(filepath);
  472. if (buf)
  473. addBuffer(name, buf);
  474. return !!buf;
  475. }
  476. bool loadSoundData(const std::string &name,
  477. const std::string &filedata)
  478. {
  479. SoundBuffer *buf = load_ogg_from_buffer(filedata, name);
  480. if (buf)
  481. addBuffer(name, buf);
  482. return !!buf;
  483. }
  484. void updateListener(const v3f &pos, const v3f &vel, const v3f &at, const v3f &up)
  485. {
  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. // Ignore the command if step isn't valid.
  539. if (step == 0)
  540. return;
  541. float current_gain = getSoundGain(soundid);
  542. step = gain - current_gain > 0 ? abs(step) : -abs(step);
  543. if (m_sounds_fading.find(soundid) != m_sounds_fading.end()) {
  544. auto current_fade = m_sounds_fading[soundid];
  545. // Do not replace the fade if it's equivalent.
  546. if (current_fade.target_gain == gain && current_fade.step == step)
  547. return;
  548. m_sounds_fading.erase(soundid);
  549. }
  550. gain = rangelim(gain, 0, 1);
  551. m_sounds_fading[soundid] = FadeState(step, current_gain, gain);
  552. }
  553. void doFades(float dtime)
  554. {
  555. for (auto i = m_sounds_fading.begin(); i != m_sounds_fading.end();) {
  556. FadeState& fade = i->second;
  557. assert(fade.step != 0);
  558. fade.current_gain += (fade.step * dtime);
  559. if (fade.step < 0.f)
  560. fade.current_gain = std::max(fade.current_gain, fade.target_gain);
  561. else
  562. fade.current_gain = std::min(fade.current_gain, fade.target_gain);
  563. if (fade.current_gain <= 0.f)
  564. stopSound(i->first);
  565. else
  566. updateSoundGain(i->first, fade.current_gain);
  567. // The increment must happen during the erase call, or else it'll segfault.
  568. if (fade.current_gain == fade.target_gain)
  569. m_sounds_fading.erase(i++);
  570. else
  571. i++;
  572. }
  573. }
  574. bool soundExists(int sound)
  575. {
  576. maintain();
  577. return (m_sounds_playing.count(sound) != 0);
  578. }
  579. void updateSoundPosition(int id, v3f pos)
  580. {
  581. auto i = m_sounds_playing.find(id);
  582. if (i == m_sounds_playing.end())
  583. return;
  584. PlayingSound *sound = i->second;
  585. alSourcei(sound->source_id, AL_SOURCE_RELATIVE, false);
  586. alSource3f(sound->source_id, AL_POSITION, pos.X, pos.Y, pos.Z);
  587. alSource3f(sound->source_id, AL_VELOCITY, 0, 0, 0);
  588. alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 30.0);
  589. }
  590. bool updateSoundGain(int id, float gain)
  591. {
  592. auto i = m_sounds_playing.find(id);
  593. if (i == m_sounds_playing.end())
  594. return false;
  595. PlayingSound *sound = i->second;
  596. alSourcef(sound->source_id, AL_GAIN, gain);
  597. return true;
  598. }
  599. float getSoundGain(int id)
  600. {
  601. auto i = m_sounds_playing.find(id);
  602. if (i == m_sounds_playing.end())
  603. return 0;
  604. PlayingSound *sound = i->second;
  605. ALfloat gain;
  606. alGetSourcef(sound->source_id, AL_GAIN, &gain);
  607. return gain;
  608. }
  609. };
  610. std::shared_ptr<SoundManagerSingleton> createSoundManagerSingleton()
  611. {
  612. auto smg = std::make_shared<SoundManagerSingleton>();
  613. if (!smg->init()) {
  614. smg.reset();
  615. }
  616. return smg;
  617. }
  618. ISoundManager *createOpenALSoundManager(SoundManagerSingleton *smg, OnDemandSoundFetcher *fetcher)
  619. {
  620. return new OpenALSoundManager(smg, fetcher);
  621. };