🛠️🐜 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.

221 lines
6.5 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. #include <SDL2/SDL.h>
  31. namespace {
  32. #ifdef _WIN32
  33. #define DEVNAME_PREFIX "OpenAL Soft on "
  34. #else
  35. #define DEVNAME_PREFIX ""
  36. #endif
  37. constexpr char defaultDeviceName[] = DEVNAME_PREFIX "Default Device";
  38. struct Sdl2Backend final : public BackendBase {
  39. Sdl2Backend(DeviceBase *device) noexcept : BackendBase{device} { }
  40. ~Sdl2Backend() override;
  41. void audioCallback(Uint8 *stream, int len) noexcept;
  42. static void audioCallbackC(void *ptr, Uint8 *stream, int len) noexcept
  43. { static_cast<Sdl2Backend*>(ptr)->audioCallback(stream, len); }
  44. void open(const char *name) override;
  45. bool reset() override;
  46. void start() override;
  47. void stop() override;
  48. SDL_AudioDeviceID mDeviceID{0u};
  49. uint mFrameSize{0};
  50. uint mFrequency{0u};
  51. DevFmtChannels mFmtChans{};
  52. DevFmtType mFmtType{};
  53. uint mUpdateSize{0u};
  54. DEF_NEWDEL(Sdl2Backend)
  55. };
  56. Sdl2Backend::~Sdl2Backend()
  57. {
  58. if(mDeviceID)
  59. SDL_CloseAudioDevice(mDeviceID);
  60. mDeviceID = 0;
  61. }
  62. void Sdl2Backend::audioCallback(Uint8 *stream, int len) noexcept
  63. {
  64. const auto ulen = static_cast<unsigned int>(len);
  65. assert((ulen % mFrameSize) == 0);
  66. mDevice->renderSamples(stream, ulen / mFrameSize, mDevice->channelsFromFmt());
  67. }
  68. void Sdl2Backend::open(const char *name)
  69. {
  70. SDL_AudioSpec want{}, have{};
  71. want.freq = static_cast<int>(mDevice->Frequency);
  72. switch(mDevice->FmtType)
  73. {
  74. case DevFmtUByte: want.format = AUDIO_U8; break;
  75. case DevFmtByte: want.format = AUDIO_S8; break;
  76. case DevFmtUShort: want.format = AUDIO_U16SYS; break;
  77. case DevFmtShort: want.format = AUDIO_S16SYS; break;
  78. case DevFmtUInt: /* fall-through */
  79. case DevFmtInt: want.format = AUDIO_S32SYS; break;
  80. case DevFmtFloat: want.format = AUDIO_F32; break;
  81. }
  82. want.channels = (mDevice->FmtChans == DevFmtMono) ? 1 : 2;
  83. want.samples = static_cast<Uint16>(minu(mDevice->UpdateSize, 8192));
  84. want.callback = &Sdl2Backend::audioCallbackC;
  85. want.userdata = this;
  86. /* Passing nullptr to SDL_OpenAudioDevice opens a default, which isn't
  87. * necessarily the first in the list.
  88. */
  89. SDL_AudioDeviceID devid;
  90. if(!name || strcmp(name, defaultDeviceName) == 0)
  91. devid = SDL_OpenAudioDevice(nullptr, SDL_FALSE, &want, &have, SDL_AUDIO_ALLOW_ANY_CHANGE);
  92. else
  93. {
  94. const size_t prefix_len = strlen(DEVNAME_PREFIX);
  95. if(strncmp(name, DEVNAME_PREFIX, prefix_len) == 0)
  96. devid = SDL_OpenAudioDevice(name+prefix_len, SDL_FALSE, &want, &have,
  97. SDL_AUDIO_ALLOW_ANY_CHANGE);
  98. else
  99. devid = SDL_OpenAudioDevice(name, SDL_FALSE, &want, &have, SDL_AUDIO_ALLOW_ANY_CHANGE);
  100. }
  101. if(!devid)
  102. throw al::backend_exception{al::backend_error::NoDevice, "%s", SDL_GetError()};
  103. DevFmtChannels devchans{};
  104. if(have.channels >= 2)
  105. devchans = DevFmtStereo;
  106. else if(have.channels == 1)
  107. devchans = DevFmtMono;
  108. else
  109. {
  110. SDL_CloseAudioDevice(devid);
  111. throw al::backend_exception{al::backend_error::DeviceError,
  112. "Unhandled SDL channel count: %d", int{have.channels}};
  113. }
  114. DevFmtType devtype{};
  115. switch(have.format)
  116. {
  117. case AUDIO_U8: devtype = DevFmtUByte; break;
  118. case AUDIO_S8: devtype = DevFmtByte; break;
  119. case AUDIO_U16SYS: devtype = DevFmtUShort; break;
  120. case AUDIO_S16SYS: devtype = DevFmtShort; break;
  121. case AUDIO_S32SYS: devtype = DevFmtInt; break;
  122. case AUDIO_F32SYS: devtype = DevFmtFloat; break;
  123. default:
  124. SDL_CloseAudioDevice(devid);
  125. throw al::backend_exception{al::backend_error::DeviceError, "Unhandled SDL format: 0x%04x",
  126. have.format};
  127. }
  128. if(mDeviceID)
  129. SDL_CloseAudioDevice(mDeviceID);
  130. mDeviceID = devid;
  131. mFrameSize = BytesFromDevFmt(devtype) * have.channels;
  132. mFrequency = static_cast<uint>(have.freq);
  133. mFmtChans = devchans;
  134. mFmtType = devtype;
  135. mUpdateSize = have.samples;
  136. mDevice->DeviceName = name ? name : defaultDeviceName;
  137. }
  138. bool Sdl2Backend::reset()
  139. {
  140. mDevice->Frequency = mFrequency;
  141. mDevice->FmtChans = mFmtChans;
  142. mDevice->FmtType = mFmtType;
  143. mDevice->UpdateSize = mUpdateSize;
  144. mDevice->BufferSize = mUpdateSize * 2; /* SDL always (tries to) use two periods. */
  145. setDefaultWFXChannelOrder();
  146. return true;
  147. }
  148. void Sdl2Backend::start()
  149. { SDL_PauseAudioDevice(mDeviceID, 0); }
  150. void Sdl2Backend::stop()
  151. { SDL_PauseAudioDevice(mDeviceID, 1); }
  152. } // namespace
  153. BackendFactory &SDL2BackendFactory::getFactory()
  154. {
  155. static SDL2BackendFactory factory{};
  156. return factory;
  157. }
  158. bool SDL2BackendFactory::init()
  159. { return (SDL_InitSubSystem(SDL_INIT_AUDIO) == 0); }
  160. bool SDL2BackendFactory::querySupport(BackendType type)
  161. { return type == BackendType::Playback; }
  162. std::string SDL2BackendFactory::probe(BackendType type)
  163. {
  164. std::string outnames;
  165. if(type != BackendType::Playback)
  166. return outnames;
  167. int num_devices{SDL_GetNumAudioDevices(SDL_FALSE)};
  168. /* Includes null char. */
  169. outnames.append(defaultDeviceName, sizeof(defaultDeviceName));
  170. for(int i{0};i < num_devices;++i)
  171. {
  172. std::string name{DEVNAME_PREFIX};
  173. name += SDL_GetAudioDeviceName(i, SDL_FALSE);
  174. if(!name.empty())
  175. outnames.append(name.c_str(), name.length()+1);
  176. }
  177. return outnames;
  178. }
  179. BackendPtr SDL2BackendFactory::createBackend(DeviceBase *device, BackendType type)
  180. {
  181. if(type == BackendType::Playback)
  182. return BackendPtr{new Sdl2Backend{device}};
  183. return nullptr;
  184. }