sdl2.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "alcmain.h"
  27. #include "almalloc.h"
  28. #include "alu.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(ALCdevice *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>(mDevice->UpdateSize);
  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. if(!name || strcmp(name, defaultDeviceName) == 0)
  90. mDeviceID = SDL_OpenAudioDevice(nullptr, SDL_FALSE, &want, &have,
  91. 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. mDeviceID = SDL_OpenAudioDevice(name+prefix_len, SDL_FALSE, &want, &have,
  97. SDL_AUDIO_ALLOW_ANY_CHANGE);
  98. else
  99. mDeviceID = SDL_OpenAudioDevice(name, SDL_FALSE, &want, &have,
  100. SDL_AUDIO_ALLOW_ANY_CHANGE);
  101. }
  102. if(mDeviceID == 0)
  103. throw al::backend_exception{al::backend_error::NoDevice, "%s", SDL_GetError()};
  104. mDevice->Frequency = static_cast<uint>(have.freq);
  105. if(have.channels == 1)
  106. mDevice->FmtChans = DevFmtMono;
  107. else if(have.channels == 2)
  108. mDevice->FmtChans = DevFmtStereo;
  109. else
  110. throw al::backend_exception{al::backend_error::DeviceError,
  111. "Unhandled SDL channel count: %d", int{have.channels}};
  112. switch(have.format)
  113. {
  114. case AUDIO_U8: mDevice->FmtType = DevFmtUByte; break;
  115. case AUDIO_S8: mDevice->FmtType = DevFmtByte; break;
  116. case AUDIO_U16SYS: mDevice->FmtType = DevFmtUShort; break;
  117. case AUDIO_S16SYS: mDevice->FmtType = DevFmtShort; break;
  118. case AUDIO_S32SYS: mDevice->FmtType = DevFmtInt; break;
  119. case AUDIO_F32SYS: mDevice->FmtType = DevFmtFloat; break;
  120. default:
  121. throw al::backend_exception{al::backend_error::DeviceError, "Unhandled SDL format: 0x%04x",
  122. have.format};
  123. }
  124. mDevice->UpdateSize = have.samples;
  125. mDevice->BufferSize = have.samples * 2; /* SDL always (tries to) use two periods. */
  126. mFrameSize = mDevice->frameSizeFromFmt();
  127. mFrequency = mDevice->Frequency;
  128. mFmtChans = mDevice->FmtChans;
  129. mFmtType = mDevice->FmtType;
  130. mUpdateSize = mDevice->UpdateSize;
  131. mDevice->DeviceName = name ? name : defaultDeviceName;
  132. }
  133. bool Sdl2Backend::reset()
  134. {
  135. mDevice->Frequency = mFrequency;
  136. mDevice->FmtChans = mFmtChans;
  137. mDevice->FmtType = mFmtType;
  138. mDevice->UpdateSize = mUpdateSize;
  139. mDevice->BufferSize = mUpdateSize * 2;
  140. setDefaultWFXChannelOrder();
  141. return true;
  142. }
  143. void Sdl2Backend::start()
  144. { SDL_PauseAudioDevice(mDeviceID, 0); }
  145. void Sdl2Backend::stop()
  146. { SDL_PauseAudioDevice(mDeviceID, 1); }
  147. } // namespace
  148. BackendFactory &SDL2BackendFactory::getFactory()
  149. {
  150. static SDL2BackendFactory factory{};
  151. return factory;
  152. }
  153. bool SDL2BackendFactory::init()
  154. { return (SDL_InitSubSystem(SDL_INIT_AUDIO) == 0); }
  155. bool SDL2BackendFactory::querySupport(BackendType type)
  156. { return type == BackendType::Playback; }
  157. std::string SDL2BackendFactory::probe(BackendType type)
  158. {
  159. std::string outnames;
  160. if(type != BackendType::Playback)
  161. return outnames;
  162. int num_devices{SDL_GetNumAudioDevices(SDL_FALSE)};
  163. /* Includes null char. */
  164. outnames.append(defaultDeviceName, sizeof(defaultDeviceName));
  165. for(int i{0};i < num_devices;++i)
  166. {
  167. std::string name{DEVNAME_PREFIX};
  168. name += SDL_GetAudioDeviceName(i, SDL_FALSE);
  169. if(!name.empty())
  170. outnames.append(name.c_str(), name.length()+1);
  171. }
  172. return outnames;
  173. }
  174. BackendPtr SDL2BackendFactory::createBackend(ALCdevice *device, BackendType type)
  175. {
  176. if(type == BackendType::Playback)
  177. return BackendPtr{new Sdl2Backend{device}};
  178. return nullptr;
  179. }