1
0

openmpt123_sdl2.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * openmpt123_sdl2.hpp
  3. * -------------------
  4. * Purpose: libopenmpt command line player
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #ifndef OPENMPT123_SDL2_HPP
  10. #define OPENMPT123_SDL2_HPP
  11. #include "openmpt123_config.hpp"
  12. #include "openmpt123.hpp"
  13. #if defined(MPT_WITH_SDL2)
  14. #if defined(__clang__)
  15. #pragma clang diagnostic push
  16. #pragma clang diagnostic ignored "-Wimplicit-fallthrough"
  17. #pragma clang diagnostic ignored "-Wreserved-id-macro"
  18. #endif // __clang__
  19. #include <SDL.h>
  20. #if defined(__clang__)
  21. #pragma clang diagnostic pop
  22. #endif // __clang__
  23. #ifdef main
  24. #undef main
  25. #endif
  26. #ifdef SDL_main
  27. #undef SDL_main
  28. #endif
  29. #if (SDL_COMPILEDVERSION < SDL_VERSIONNUM(2, 0, 4))
  30. MPT_WARNING("Support for SDL2 < 2.0.4 has been deprecated and will be removed in a future openmpt123 version.")
  31. #endif
  32. namespace openmpt123 {
  33. struct sdl2_exception : public exception {
  34. private:
  35. static std::string text_from_code( int code ) {
  36. std::ostringstream s;
  37. s << code;
  38. return s.str();
  39. }
  40. public:
  41. sdl2_exception( int code, const char * error ) : exception( text_from_code( code ) + " (" + ( error ? std::string(error) : std::string("NULL") ) + ")" ) { }
  42. };
  43. static void check_sdl2_error( int e ) {
  44. if ( e < 0 ) {
  45. throw sdl2_exception( e, SDL_GetError() );
  46. }
  47. }
  48. class sdl2_raii {
  49. public:
  50. sdl2_raii( Uint32 flags ) {
  51. check_sdl2_error( SDL_Init( flags ) );
  52. }
  53. ~sdl2_raii() {
  54. SDL_Quit();
  55. }
  56. };
  57. class sdl2_stream_raii : public write_buffers_interface {
  58. private:
  59. std::ostream & log;
  60. sdl2_raii sdl2;
  61. int dev;
  62. std::size_t channels;
  63. bool use_float;
  64. std::size_t sampleQueueMaxFrames;
  65. std::vector<float> sampleBufFloat;
  66. std::vector<std::int16_t> sampleBufInt;
  67. protected:
  68. std::uint32_t round_up_power2(std::uint32_t x)
  69. {
  70. std::uint32_t result = 1;
  71. while ( result < x ) {
  72. result *= 2;
  73. }
  74. return result;
  75. }
  76. public:
  77. sdl2_stream_raii( commandlineflags & flags, std::ostream & log_ )
  78. : log(log_)
  79. , sdl2( SDL_INIT_NOPARACHUTE | SDL_INIT_TIMER | SDL_INIT_AUDIO )
  80. , dev(-1)
  81. , channels(flags.channels)
  82. , use_float(flags.use_float)
  83. , sampleQueueMaxFrames(0)
  84. {
  85. if ( flags.buffer == default_high ) {
  86. flags.buffer = 160;
  87. } else if ( flags.buffer == default_low ) {
  88. flags.buffer = 80;
  89. }
  90. if ( flags.period == default_high ) {
  91. flags.period = 20;
  92. } else if ( flags.period == default_low ) {
  93. flags.period = 10;
  94. }
  95. flags.apply_default_buffer_sizes();
  96. SDL_AudioSpec audiospec;
  97. std::memset( &audiospec, 0, sizeof( SDL_AudioSpec ) );
  98. audiospec.freq = flags.samplerate;
  99. audiospec.format = ( flags.use_float ? AUDIO_F32SYS : AUDIO_S16SYS );
  100. audiospec.channels = flags.channels;
  101. audiospec.silence = 0;
  102. audiospec.samples = round_up_power2( ( flags.buffer * flags.samplerate ) / ( 1000 * 2 ) );
  103. audiospec.size = audiospec.samples * audiospec.channels * ( flags.use_float ? sizeof( float ) : sizeof( std::int16_t ) );
  104. audiospec.callback = NULL;
  105. audiospec.userdata = NULL;
  106. if ( flags.verbose ) {
  107. log << "SDL2:" << std::endl;
  108. log << " latency: " << ( audiospec.samples * 2.0 / flags.samplerate ) << " (2 * " << audiospec.samples << ")" << std::endl;
  109. log << std::endl;
  110. }
  111. sampleQueueMaxFrames = round_up_power2( ( flags.buffer * flags.samplerate ) / ( 1000 * 2 ) );
  112. SDL_AudioSpec audiospec_obtained;
  113. std::memset( &audiospec_obtained, 0, sizeof( SDL_AudioSpec ) );
  114. std::memcpy( &audiospec_obtained, &audiospec, sizeof( SDL_AudioSpec ) );
  115. dev = SDL_OpenAudioDevice( NULL, 0, &audiospec, &audiospec_obtained, 0 );
  116. if ( dev < 0 ) {
  117. check_sdl2_error( dev );
  118. } else if ( dev == 0 ) {
  119. check_sdl2_error( -1 );
  120. }
  121. SDL_PauseAudioDevice( dev, 0 );
  122. }
  123. ~sdl2_stream_raii() {
  124. SDL_PauseAudioDevice( dev, 1 );
  125. SDL_CloseAudioDevice( dev );
  126. }
  127. private:
  128. std::size_t get_num_writeable_frames() {
  129. std::size_t num_queued_frames = SDL_GetQueuedAudioSize( dev ) / ( use_float ? sizeof( float ) : sizeof( std::int16_t ) ) / channels;
  130. if ( num_queued_frames > sampleQueueMaxFrames ) {
  131. return 0;
  132. }
  133. return sampleQueueMaxFrames - num_queued_frames;
  134. }
  135. template<typename Tsample>
  136. void write_frames( const Tsample * buffer, std::size_t frames ) {
  137. while ( frames > 0 ) {
  138. std::size_t chunk_frames = std::min( frames, get_num_writeable_frames() );
  139. if ( chunk_frames > 0 ) {
  140. check_sdl2_error( SDL_QueueAudio( dev, buffer, chunk_frames * channels * ( use_float ? sizeof( float ) : sizeof( std::int16_t ) ) ) );
  141. frames -= chunk_frames;
  142. buffer += chunk_frames * channels;
  143. } else {
  144. SDL_Delay( 1 );
  145. }
  146. }
  147. }
  148. public:
  149. void write( const std::vector<float*> buffers, std::size_t frames ) override {
  150. sampleBufFloat.clear();
  151. for ( std::size_t frame = 0; frame < frames; ++frame ) {
  152. for ( std::size_t channel = 0; channel < channels; ++channel ) {
  153. sampleBufFloat.push_back( buffers[channel][frame] );
  154. }
  155. }
  156. write_frames( sampleBufFloat.data(), frames );
  157. }
  158. void write( const std::vector<std::int16_t*> buffers, std::size_t frames ) override {
  159. sampleBufInt.clear();
  160. for ( std::size_t frame = 0; frame < frames; ++frame ) {
  161. for ( std::size_t channel = 0; channel < channels; ++channel ) {
  162. sampleBufInt.push_back( buffers[channel][frame] );
  163. }
  164. }
  165. write_frames( sampleBufInt.data(), frames );
  166. }
  167. bool pause() override {
  168. SDL_PauseAudioDevice( dev, 1 );
  169. return true;
  170. }
  171. bool unpause() override {
  172. SDL_PauseAudioDevice( dev, 0 );
  173. return true;
  174. }
  175. bool sleep( int ms ) override {
  176. SDL_Delay( ms );
  177. return true;
  178. }
  179. };
  180. static std::string show_sdl2_devices( std::ostream & /* log */ ) {
  181. std::ostringstream devices;
  182. std::size_t device_index = 0;
  183. devices << " SDL2:" << std::endl;
  184. sdl2_raii sdl2( SDL_INIT_NOPARACHUTE | SDL_INIT_AUDIO );
  185. for ( int driver = 0; driver < SDL_GetNumAudioDrivers(); ++driver ) {
  186. const char * driver_name = SDL_GetAudioDriver( driver );
  187. if ( !driver_name ) {
  188. continue;
  189. }
  190. if ( std::string( driver_name ).empty() ) {
  191. continue;
  192. }
  193. if ( SDL_AudioInit( driver_name ) < 0 ) {
  194. continue;
  195. }
  196. for ( int device = 0; device < SDL_GetNumAudioDevices( 0 ); ++device ) {
  197. const char * device_name = SDL_GetAudioDeviceName( device, 0 );
  198. if ( !device_name ) {
  199. continue;
  200. }
  201. if ( std::string( device_name ).empty() ) {
  202. continue;
  203. }
  204. devices << " " << device_index << ": " << driver_name << " - " << device_name << std::endl;
  205. device_index++;
  206. }
  207. SDL_AudioQuit();
  208. }
  209. return devices.str();
  210. }
  211. } // namespace openmpt123
  212. #endif // MPT_WITH_SDL2
  213. #endif // OPENMPT123_SDL2_HPP