You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

132 regels
2.6 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2016 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #include <lol/engine-internal.h>
  13. #if LOL_USE_SDL_MIXER
  14. # if HAVE_SDL_SDL_H
  15. # include <SDL/SDL.h>
  16. # include <SDL/SDL_mixer.h>
  17. # elif HAVE_SDL2_SDL_H
  18. # include <SDL2/SDL.h>
  19. # include <SDL2/SDL_mixer.h>
  20. # else
  21. # include <SDL.h>
  22. # include <SDL_mixer.h>
  23. # endif
  24. #endif
  25. namespace lol
  26. {
  27. struct audio_streamer
  28. {
  29. int m_channel;
  30. std::function<void(void *, int)> m_callback;
  31. #if defined LOL_USE_SDL_MIXER
  32. array<uint8_t> m_empty; // SDL keeps a reference to this
  33. Mix_Chunk *m_chunk;
  34. #endif
  35. };
  36. array<audio_streamer *> g_streamers;
  37. /*
  38. * Public audio class
  39. */
  40. void audio::init()
  41. {
  42. #if defined LOL_USE_SDL_MIXER
  43. Mix_OpenAudio(22050, AUDIO_S16, 2, 1024);
  44. set_channels(8);
  45. #endif
  46. }
  47. void audio::set_channels(int channels)
  48. {
  49. #if defined LOL_USE_SDL_MIXER
  50. Mix_AllocateChannels(channels);
  51. #else
  52. UNUSED(channels);
  53. #endif
  54. }
  55. void audio::set_volume(int channel, int volume)
  56. {
  57. #if defined LOL_USE_SDL_MIXER
  58. Mix_Volume(channel,volume);
  59. #else
  60. UNUSED(channel);
  61. #endif
  62. }
  63. void audio::mute_all()
  64. {
  65. #if defined LOL_USE_SDL_MIXER
  66. Mix_Volume(-1,0);
  67. #endif
  68. }
  69. void audio::unmute_all()
  70. {
  71. #if defined LOL_USE_SDL_MIXER
  72. Mix_Volume(-1,MIX_MAX_VOLUME);
  73. #endif
  74. }
  75. int audio::start_streaming(std::function<void(void *, int)> const &f)
  76. {
  77. #if defined LOL_USE_SDL_MIXER
  78. static auto trampoline = [](int, void *stream, int bytes, void *udata)
  79. {
  80. auto s = (audio_streamer *)udata;
  81. s->m_callback(stream, bytes);
  82. };
  83. audio_streamer *s = new audio_streamer();
  84. g_streamers.push(s);
  85. s->m_empty.resize(1024);
  86. s->m_chunk = Mix_QuickLoad_RAW(s->m_empty.data(), s->m_empty.bytes());
  87. s->m_channel = Mix_PlayChannel(-1, s->m_chunk, -1);
  88. s->m_callback = f;
  89. Mix_RegisterEffect(s->m_channel, trampoline, nullptr, s);
  90. return s->m_channel;
  91. #else
  92. UNUSED(f);
  93. return -1;
  94. #endif
  95. }
  96. void audio::stop_streaming(int channel)
  97. {
  98. #if defined LOL_USE_SDL_MIXER
  99. for (int i = 0; i < g_streamers.count(); ++i)
  100. {
  101. if (g_streamers[i]->m_channel == channel)
  102. {
  103. Mix_HaltChannel(channel);
  104. delete g_streamers[i];
  105. g_streamers.remove(i);
  106. return;
  107. }
  108. }
  109. #else
  110. UNUSED(channel);
  111. #endif
  112. }
  113. } /* namespace lol */