sdl2.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 <cassert>
  23. #include <cstdlib>
  24. #include <cstring>
  25. #include <string>
  26. #include "AL/al.h"
  27. #include "alcmain.h"
  28. #include "alexcpt.h"
  29. #include "almalloc.h"
  30. #include "alu.h"
  31. #include "logging.h"
  32. #include <SDL2/SDL.h>
  33. namespace {
  34. #ifdef _WIN32
  35. #define DEVNAME_PREFIX "OpenAL Soft on "
  36. #else
  37. #define DEVNAME_PREFIX ""
  38. #endif
  39. constexpr ALCchar defaultDeviceName[] = DEVNAME_PREFIX "Default Device";
  40. struct Sdl2Backend final : public BackendBase {
  41. Sdl2Backend(ALCdevice *device) noexcept : BackendBase{device} { }
  42. ~Sdl2Backend() override;
  43. void audioCallback(Uint8 *stream, int len) noexcept;
  44. static void audioCallbackC(void *ptr, Uint8 *stream, int len) noexcept
  45. { static_cast<Sdl2Backend*>(ptr)->audioCallback(stream, len); }
  46. void open(const ALCchar *name) override;
  47. bool reset() override;
  48. bool start() override;
  49. void stop() override;
  50. void lock() override;
  51. void unlock() override;
  52. SDL_AudioDeviceID mDeviceID{0u};
  53. ALuint mFrameSize{0};
  54. ALuint mFrequency{0u};
  55. DevFmtChannels mFmtChans{};
  56. DevFmtType mFmtType{};
  57. ALuint mUpdateSize{0u};
  58. DEF_NEWDEL(Sdl2Backend)
  59. };
  60. Sdl2Backend::~Sdl2Backend()
  61. {
  62. if(mDeviceID)
  63. SDL_CloseAudioDevice(mDeviceID);
  64. mDeviceID = 0;
  65. }
  66. void Sdl2Backend::audioCallback(Uint8 *stream, int len) noexcept
  67. {
  68. const auto ulen = static_cast<unsigned int>(len);
  69. assert((ulen % mFrameSize) == 0);
  70. aluMixData(mDevice, stream, ulen / mFrameSize);
  71. }
  72. void Sdl2Backend::open(const ALCchar *name)
  73. {
  74. SDL_AudioSpec want{}, have{};
  75. want.freq = static_cast<int>(mDevice->Frequency);
  76. switch(mDevice->FmtType)
  77. {
  78. case DevFmtUByte: want.format = AUDIO_U8; break;
  79. case DevFmtByte: want.format = AUDIO_S8; break;
  80. case DevFmtUShort: want.format = AUDIO_U16SYS; break;
  81. case DevFmtShort: want.format = AUDIO_S16SYS; break;
  82. case DevFmtUInt: /* fall-through */
  83. case DevFmtInt: want.format = AUDIO_S32SYS; break;
  84. case DevFmtFloat: want.format = AUDIO_F32; break;
  85. }
  86. want.channels = (mDevice->FmtChans == DevFmtMono) ? 1 : 2;
  87. want.samples = static_cast<Uint16>(mDevice->UpdateSize);
  88. want.callback = &Sdl2Backend::audioCallbackC;
  89. want.userdata = this;
  90. /* Passing nullptr to SDL_OpenAudioDevice opens a default, which isn't
  91. * necessarily the first in the list.
  92. */
  93. if(!name || strcmp(name, defaultDeviceName) == 0)
  94. mDeviceID = SDL_OpenAudioDevice(nullptr, SDL_FALSE, &want, &have,
  95. SDL_AUDIO_ALLOW_ANY_CHANGE);
  96. else
  97. {
  98. const size_t prefix_len = strlen(DEVNAME_PREFIX);
  99. if(strncmp(name, DEVNAME_PREFIX, prefix_len) == 0)
  100. mDeviceID = SDL_OpenAudioDevice(name+prefix_len, SDL_FALSE, &want, &have,
  101. SDL_AUDIO_ALLOW_ANY_CHANGE);
  102. else
  103. mDeviceID = SDL_OpenAudioDevice(name, SDL_FALSE, &want, &have,
  104. SDL_AUDIO_ALLOW_ANY_CHANGE);
  105. }
  106. if(mDeviceID == 0)
  107. throw al::backend_exception{ALC_INVALID_VALUE, "%s", SDL_GetError()};
  108. mDevice->Frequency = static_cast<ALuint>(have.freq);
  109. if(have.channels == 1)
  110. mDevice->FmtChans = DevFmtMono;
  111. else if(have.channels == 2)
  112. mDevice->FmtChans = DevFmtStereo;
  113. else
  114. throw al::backend_exception{ALC_INVALID_VALUE, "Unhandled SDL channel count: %d",
  115. int{have.channels}};
  116. switch(have.format)
  117. {
  118. case AUDIO_U8: mDevice->FmtType = DevFmtUByte; break;
  119. case AUDIO_S8: mDevice->FmtType = DevFmtByte; break;
  120. case AUDIO_U16SYS: mDevice->FmtType = DevFmtUShort; break;
  121. case AUDIO_S16SYS: mDevice->FmtType = DevFmtShort; break;
  122. case AUDIO_S32SYS: mDevice->FmtType = DevFmtInt; break;
  123. case AUDIO_F32SYS: mDevice->FmtType = DevFmtFloat; break;
  124. default:
  125. throw al::backend_exception{ALC_INVALID_VALUE, "Unhandled SDL format: 0x%04x",
  126. have.format};
  127. }
  128. mDevice->UpdateSize = have.samples;
  129. mDevice->BufferSize = have.samples * 2; /* SDL always (tries to) use two periods. */
  130. mFrameSize = mDevice->frameSizeFromFmt();
  131. mFrequency = mDevice->Frequency;
  132. mFmtChans = mDevice->FmtChans;
  133. mFmtType = mDevice->FmtType;
  134. mUpdateSize = mDevice->UpdateSize;
  135. mDevice->DeviceName = name ? name : defaultDeviceName;
  136. }
  137. bool 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 true;
  146. }
  147. bool Sdl2Backend::start()
  148. {
  149. SDL_PauseAudioDevice(mDeviceID, 0);
  150. return 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. }