🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
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.

224 lines
6.6 KiB

  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 2018 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include "sdl2.h"
  22. #include <cassert>
  23. #include <cstdlib>
  24. #include <cstring>
  25. #include <string>
  26. #include "almalloc.h"
  27. #include "alnumeric.h"
  28. #include "core/device.h"
  29. #include "core/logging.h"
  30. _Pragma("GCC diagnostic push")
  31. _Pragma("GCC diagnostic ignored \"-Wold-style-cast\"")
  32. #include "SDL.h"
  33. _Pragma("GCC diagnostic pop")
  34. namespace {
  35. #ifdef _WIN32
  36. #define DEVNAME_PREFIX "OpenAL Soft on "
  37. #else
  38. #define DEVNAME_PREFIX ""
  39. #endif
  40. constexpr char defaultDeviceName[] = DEVNAME_PREFIX "Default Device";
  41. struct Sdl2Backend final : public BackendBase {
  42. Sdl2Backend(DeviceBase *device) noexcept : BackendBase{device} { }
  43. ~Sdl2Backend() override;
  44. void audioCallback(Uint8 *stream, int len) noexcept;
  45. static void audioCallbackC(void *ptr, Uint8 *stream, int len) noexcept
  46. { static_cast<Sdl2Backend*>(ptr)->audioCallback(stream, len); }
  47. void open(const char *name) override;
  48. bool reset() override;
  49. void start() override;
  50. void stop() override;
  51. SDL_AudioDeviceID mDeviceID{0u};
  52. uint mFrameSize{0};
  53. uint mFrequency{0u};
  54. DevFmtChannels mFmtChans{};
  55. DevFmtType mFmtType{};
  56. uint mUpdateSize{0u};
  57. DEF_NEWDEL(Sdl2Backend)
  58. };
  59. Sdl2Backend::~Sdl2Backend()
  60. {
  61. if(mDeviceID)
  62. SDL_CloseAudioDevice(mDeviceID);
  63. mDeviceID = 0;
  64. }
  65. void Sdl2Backend::audioCallback(Uint8 *stream, int len) noexcept
  66. {
  67. const auto ulen = static_cast<unsigned int>(len);
  68. assert((ulen % mFrameSize) == 0);
  69. mDevice->renderSamples(stream, ulen / mFrameSize, mDevice->channelsFromFmt());
  70. }
  71. void Sdl2Backend::open(const char *name)
  72. {
  73. SDL_AudioSpec want{}, have{};
  74. want.freq = static_cast<int>(mDevice->Frequency);
  75. switch(mDevice->FmtType)
  76. {
  77. case DevFmtUByte: want.format = AUDIO_U8; break;
  78. case DevFmtByte: want.format = AUDIO_S8; break;
  79. case DevFmtUShort: want.format = AUDIO_U16SYS; break;
  80. case DevFmtShort: want.format = AUDIO_S16SYS; break;
  81. case DevFmtUInt: /* fall-through */
  82. case DevFmtInt: want.format = AUDIO_S32SYS; break;
  83. case DevFmtFloat: want.format = AUDIO_F32; break;
  84. }
  85. want.channels = (mDevice->FmtChans == DevFmtMono) ? 1 : 2;
  86. want.samples = static_cast<Uint16>(minu(mDevice->UpdateSize, 8192));
  87. want.callback = &Sdl2Backend::audioCallbackC;
  88. want.userdata = this;
  89. /* Passing nullptr to SDL_OpenAudioDevice opens a default, which isn't
  90. * necessarily the first in the list.
  91. */
  92. SDL_AudioDeviceID devid;
  93. if(!name || strcmp(name, defaultDeviceName) == 0)
  94. devid = SDL_OpenAudioDevice(nullptr, SDL_FALSE, &want, &have, SDL_AUDIO_ALLOW_ANY_CHANGE);
  95. else
  96. {
  97. const size_t prefix_len = strlen(DEVNAME_PREFIX);
  98. if(strncmp(name, DEVNAME_PREFIX, prefix_len) == 0)
  99. devid = SDL_OpenAudioDevice(name+prefix_len, SDL_FALSE, &want, &have,
  100. SDL_AUDIO_ALLOW_ANY_CHANGE);
  101. else
  102. devid = SDL_OpenAudioDevice(name, SDL_FALSE, &want, &have, SDL_AUDIO_ALLOW_ANY_CHANGE);
  103. }
  104. if(!devid)
  105. throw al::backend_exception{al::backend_error::NoDevice, "%s", SDL_GetError()};
  106. DevFmtChannels devchans{};
  107. if(have.channels >= 2)
  108. devchans = DevFmtStereo;
  109. else if(have.channels == 1)
  110. devchans = DevFmtMono;
  111. else
  112. {
  113. SDL_CloseAudioDevice(devid);
  114. throw al::backend_exception{al::backend_error::DeviceError,
  115. "Unhandled SDL channel count: %d", int{have.channels}};
  116. }
  117. DevFmtType devtype{};
  118. switch(have.format)
  119. {
  120. case AUDIO_U8: devtype = DevFmtUByte; break;
  121. case AUDIO_S8: devtype = DevFmtByte; break;
  122. case AUDIO_U16SYS: devtype = DevFmtUShort; break;
  123. case AUDIO_S16SYS: devtype = DevFmtShort; break;
  124. case AUDIO_S32SYS: devtype = DevFmtInt; break;
  125. case AUDIO_F32SYS: devtype = DevFmtFloat; break;
  126. default:
  127. SDL_CloseAudioDevice(devid);
  128. throw al::backend_exception{al::backend_error::DeviceError, "Unhandled SDL format: 0x%04x",
  129. have.format};
  130. }
  131. if(mDeviceID)
  132. SDL_CloseAudioDevice(mDeviceID);
  133. mDeviceID = devid;
  134. mFrameSize = BytesFromDevFmt(devtype) * have.channels;
  135. mFrequency = static_cast<uint>(have.freq);
  136. mFmtChans = devchans;
  137. mFmtType = devtype;
  138. mUpdateSize = have.samples;
  139. mDevice->DeviceName = name ? name : defaultDeviceName;
  140. }
  141. bool Sdl2Backend::reset()
  142. {
  143. mDevice->Frequency = mFrequency;
  144. mDevice->FmtChans = mFmtChans;
  145. mDevice->FmtType = mFmtType;
  146. mDevice->UpdateSize = mUpdateSize;
  147. mDevice->BufferSize = mUpdateSize * 2; /* SDL always (tries to) use two periods. */
  148. setDefaultWFXChannelOrder();
  149. return true;
  150. }
  151. void Sdl2Backend::start()
  152. { SDL_PauseAudioDevice(mDeviceID, 0); }
  153. void Sdl2Backend::stop()
  154. { SDL_PauseAudioDevice(mDeviceID, 1); }
  155. } // namespace
  156. BackendFactory &SDL2BackendFactory::getFactory()
  157. {
  158. static SDL2BackendFactory factory{};
  159. return factory;
  160. }
  161. bool SDL2BackendFactory::init()
  162. { return (SDL_InitSubSystem(SDL_INIT_AUDIO) == 0); }
  163. bool SDL2BackendFactory::querySupport(BackendType type)
  164. { return type == BackendType::Playback; }
  165. std::string SDL2BackendFactory::probe(BackendType type)
  166. {
  167. std::string outnames;
  168. if(type != BackendType::Playback)
  169. return outnames;
  170. int num_devices{SDL_GetNumAudioDevices(SDL_FALSE)};
  171. /* Includes null char. */
  172. outnames.append(defaultDeviceName, sizeof(defaultDeviceName));
  173. for(int i{0};i < num_devices;++i)
  174. {
  175. std::string name{DEVNAME_PREFIX};
  176. name += SDL_GetAudioDeviceName(i, SDL_FALSE);
  177. if(!name.empty())
  178. outnames.append(name.c_str(), name.length()+1);
  179. }
  180. return outnames;
  181. }
  182. BackendPtr SDL2BackendFactory::createBackend(DeviceBase *device, BackendType type)
  183. {
  184. if(type == BackendType::Playback)
  185. return BackendPtr{new Sdl2Backend{device}};
  186. return nullptr;
  187. }