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