openmpt123_portaudio.hpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * openmpt123_portaudio.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_PORTAUDIO_HPP
  10. #define OPENMPT123_PORTAUDIO_HPP
  11. #include "openmpt123_config.hpp"
  12. #include "openmpt123.hpp"
  13. #if defined(MPT_WITH_PORTAUDIO)
  14. #include <portaudio.h>
  15. namespace openmpt123 {
  16. struct portaudio_exception : public exception {
  17. portaudio_exception( PaError code ) : exception( Pa_GetErrorText( code ) ) { }
  18. };
  19. typedef void (*PaUtilLogCallback ) (const char *log);
  20. #ifdef _MSC_VER
  21. extern "C" void PaUtil_SetDebugPrintFunction(PaUtilLogCallback cb);
  22. #endif
  23. class portaudio_raii {
  24. private:
  25. std::ostream & log;
  26. bool log_set;
  27. bool portaudio_initialized;
  28. static std::ostream * portaudio_log_stream;
  29. private:
  30. static void portaudio_log_function( const char * log ) {
  31. if ( portaudio_log_stream ) {
  32. *portaudio_log_stream << "PortAudio: " << log;
  33. }
  34. }
  35. protected:
  36. void check_portaudio_error( PaError e ) {
  37. if ( e > 0 ) {
  38. return;
  39. }
  40. if ( e == paNoError ) {
  41. return;
  42. }
  43. if ( e == paOutputUnderflowed ) {
  44. log << "PortAudio warning: " << Pa_GetErrorText( e ) << std::endl;
  45. return;
  46. }
  47. throw portaudio_exception( e );
  48. }
  49. public:
  50. portaudio_raii( bool verbose, std::ostream & log ) : log(log), log_set(false), portaudio_initialized(false) {
  51. if ( verbose ) {
  52. portaudio_log_stream = &log;
  53. } else {
  54. portaudio_log_stream = 0;
  55. }
  56. #ifdef _MSC_VER
  57. PaUtil_SetDebugPrintFunction( portaudio_log_function );
  58. log_set = true;
  59. #endif
  60. check_portaudio_error( Pa_Initialize() );
  61. portaudio_initialized = true;
  62. if ( verbose ) {
  63. *portaudio_log_stream << std::endl;
  64. }
  65. }
  66. ~portaudio_raii() {
  67. if ( portaudio_initialized ) {
  68. check_portaudio_error( Pa_Terminate() );
  69. portaudio_initialized = false;
  70. }
  71. if ( log_set ) {
  72. #ifdef _MSC_VER
  73. PaUtil_SetDebugPrintFunction( NULL );
  74. log_set = false;
  75. #endif
  76. }
  77. portaudio_log_stream = 0;
  78. }
  79. };
  80. std::ostream * portaudio_raii::portaudio_log_stream = 0;
  81. class portaudio_stream_blocking_raii : public portaudio_raii, public write_buffers_interface {
  82. private:
  83. PaStream * stream;
  84. bool interleaved;
  85. std::size_t channels;
  86. std::vector<float> sampleBufFloat;
  87. std::vector<std::int16_t> sampleBufInt;
  88. public:
  89. portaudio_stream_blocking_raii( commandlineflags & flags, std::ostream & log )
  90. : portaudio_raii(flags.verbose, log)
  91. , stream(NULL)
  92. , interleaved(false)
  93. , channels(flags.channels)
  94. {
  95. PaStreamParameters streamparameters;
  96. std::memset( &streamparameters, 0, sizeof(PaStreamParameters) );
  97. std::istringstream device_string( flags.device );
  98. int device = -1;
  99. device_string >> device;
  100. streamparameters.device = ( device == -1 ) ? Pa_GetDefaultOutputDevice() : device;
  101. streamparameters.channelCount = flags.channels;
  102. streamparameters.sampleFormat = ( flags.use_float ? paFloat32 : paInt16 ) | paNonInterleaved;
  103. if ( flags.buffer == default_high ) {
  104. streamparameters.suggestedLatency = Pa_GetDeviceInfo( streamparameters.device )->defaultHighOutputLatency;
  105. flags.buffer = static_cast<std::int32_t>( Pa_GetDeviceInfo( streamparameters.device )->defaultHighOutputLatency * 1000.0 );
  106. } else if ( flags.buffer == default_low ) {
  107. streamparameters.suggestedLatency = Pa_GetDeviceInfo( streamparameters.device )->defaultLowOutputLatency;
  108. flags.buffer = static_cast<std::int32_t>( Pa_GetDeviceInfo( streamparameters.device )->defaultLowOutputLatency * 1000.0 );
  109. } else {
  110. streamparameters.suggestedLatency = flags.buffer * 0.001;
  111. }
  112. unsigned long framesperbuffer = 0;
  113. if ( flags.mode != Mode::UI ) {
  114. framesperbuffer = paFramesPerBufferUnspecified;
  115. flags.period = 50;
  116. flags.period = std::min( flags.period, flags.buffer / 3 );
  117. } else if ( flags.period == default_high ) {
  118. framesperbuffer = paFramesPerBufferUnspecified;
  119. flags.period = 50;
  120. flags.period = std::min( flags.period, flags.buffer / 3 );
  121. } else if ( flags.period == default_low ) {
  122. framesperbuffer = paFramesPerBufferUnspecified;
  123. flags.period = 10;
  124. flags.period = std::min( flags.period, flags.buffer / 3 );
  125. } else {
  126. framesperbuffer = flags.period * flags.samplerate / 1000;
  127. }
  128. if ( flags.period <= 0 ) {
  129. flags.period = 1;
  130. }
  131. flags.apply_default_buffer_sizes();
  132. if ( flags.verbose ) {
  133. log << "PortAudio:" << std::endl;
  134. log << " device: "
  135. << streamparameters.device
  136. << " [ " << Pa_GetHostApiInfo( Pa_GetDeviceInfo( streamparameters.device )->hostApi )->name << " / " << Pa_GetDeviceInfo( streamparameters.device )->name << " ] "
  137. << std::endl;
  138. log << " low latency: " << Pa_GetDeviceInfo( streamparameters.device )->defaultLowOutputLatency << std::endl;
  139. log << " high latency: " << Pa_GetDeviceInfo( streamparameters.device )->defaultHighOutputLatency << std::endl;
  140. log << " suggested latency: " << streamparameters.suggestedLatency << std::endl;
  141. log << " frames per buffer: " << framesperbuffer << std::endl;
  142. log << " ui redraw: " << flags.period << std::endl;
  143. }
  144. PaError e = PaError();
  145. e = Pa_OpenStream( &stream, NULL, &streamparameters, flags.samplerate, framesperbuffer, ( flags.dither > 0 ) ? paNoFlag : paDitherOff, NULL, NULL );
  146. if ( e != paNoError ) {
  147. // Non-interleaved failed, try interleaved next.
  148. // This might help broken portaudio on MacOS X.
  149. streamparameters.sampleFormat &= ~paNonInterleaved;
  150. e = Pa_OpenStream( &stream, NULL, &streamparameters, flags.samplerate, framesperbuffer, ( flags.dither > 0 ) ? paNoFlag : paDitherOff, NULL, NULL );
  151. if ( e == paNoError ) {
  152. interleaved = true;
  153. }
  154. check_portaudio_error( e );
  155. }
  156. check_portaudio_error( Pa_StartStream( stream ) );
  157. if ( flags.verbose ) {
  158. log << " channels: " << streamparameters.channelCount << std::endl;
  159. log << " sampleformat: " << ( ( ( streamparameters.sampleFormat & ~paNonInterleaved ) == paFloat32 ) ? "paFloat32" : "paInt16" ) << std::endl;
  160. log << " latency: " << Pa_GetStreamInfo( stream )->outputLatency << std::endl;
  161. log << " samplerate: " << Pa_GetStreamInfo( stream )->sampleRate << std::endl;
  162. log << std::endl;
  163. }
  164. }
  165. ~portaudio_stream_blocking_raii() {
  166. if ( stream ) {
  167. PaError stopped = Pa_IsStreamStopped( stream );
  168. check_portaudio_error( stopped );
  169. if ( !stopped ) {
  170. check_portaudio_error( Pa_StopStream( stream ) );
  171. }
  172. check_portaudio_error( Pa_CloseStream( stream ) );
  173. stream = NULL;
  174. }
  175. }
  176. private:
  177. template<typename Tsample>
  178. void write_frames( const Tsample * buffer, std::size_t frames ) {
  179. while ( frames > 0 ) {
  180. unsigned long chunk_frames = static_cast<unsigned long>( std::min( static_cast<std::uint64_t>( frames ), static_cast<std::uint64_t>( std::numeric_limits<unsigned long>::max() ) ) );
  181. check_portaudio_error( Pa_WriteStream( stream, buffer, chunk_frames ) );
  182. buffer += chunk_frames * channels;
  183. frames -= chunk_frames;
  184. }
  185. }
  186. template<typename Tsample>
  187. void write_frames( std::vector<Tsample*> buffers, std::size_t frames ) {
  188. while ( frames > 0 ) {
  189. unsigned long chunk_frames = static_cast<unsigned long>( std::min( static_cast<std::uint64_t>( frames ), static_cast<std::uint64_t>( std::numeric_limits<unsigned long>::max() ) ) );
  190. check_portaudio_error( Pa_WriteStream( stream, buffers.data(), chunk_frames ) );
  191. for ( std::size_t channel = 0; channel < channels; ++channel ) {
  192. buffers[channel] += chunk_frames;
  193. }
  194. frames -= chunk_frames;
  195. }
  196. }
  197. public:
  198. void write( const std::vector<float*> buffers, std::size_t frames ) override {
  199. if ( interleaved ) {
  200. sampleBufFloat.clear();
  201. for ( std::size_t frame = 0; frame < frames; ++frame ) {
  202. for ( std::size_t channel = 0; channel < channels; ++channel ) {
  203. sampleBufFloat.push_back( buffers[channel][frame] );
  204. }
  205. }
  206. write_frames( sampleBufFloat.data(), frames );
  207. } else {
  208. write_frames( buffers, frames );
  209. }
  210. }
  211. void write( const std::vector<std::int16_t*> buffers, std::size_t frames ) override {
  212. if ( interleaved ) {
  213. sampleBufInt.clear();
  214. for ( std::size_t frame = 0; frame < frames; ++frame ) {
  215. for ( std::size_t channel = 0; channel < channels; ++channel ) {
  216. sampleBufInt.push_back( buffers[channel][frame] );
  217. }
  218. }
  219. write_frames( sampleBufInt.data(), frames );
  220. } else {
  221. write_frames( buffers, frames );
  222. }
  223. }
  224. bool unpause() override {
  225. check_portaudio_error( Pa_StartStream( stream ) );
  226. return true;
  227. }
  228. bool pause() override {
  229. check_portaudio_error( Pa_StopStream( stream ) );
  230. return true;
  231. }
  232. bool sleep( int ms ) override {
  233. Pa_Sleep( ms );
  234. return true;
  235. }
  236. };
  237. #define portaudio_stream_raii portaudio_stream_blocking_raii
  238. static std::string show_portaudio_devices( std::ostream & log ) {
  239. std::ostringstream devices;
  240. devices << " portaudio:" << std::endl;
  241. portaudio_raii portaudio( false, log );
  242. for ( PaDeviceIndex i = 0; i < Pa_GetDeviceCount(); ++i ) {
  243. if ( Pa_GetDeviceInfo( i ) && Pa_GetDeviceInfo( i )->maxOutputChannels > 0 ) {
  244. devices << " " << i << ": ";
  245. if ( Pa_GetHostApiInfo( Pa_GetDeviceInfo( i )->hostApi ) && Pa_GetHostApiInfo( Pa_GetDeviceInfo( i )->hostApi )->name ) {
  246. devices << Pa_GetHostApiInfo( Pa_GetDeviceInfo( i )->hostApi )->name;
  247. } else {
  248. devices << "Host API " << Pa_GetDeviceInfo( i )->hostApi;
  249. }
  250. if ( Pa_GetHostApiInfo( Pa_GetDeviceInfo( i )->hostApi ) ) {
  251. if ( i == Pa_GetHostApiInfo( Pa_GetDeviceInfo( i )->hostApi )->defaultOutputDevice ) {
  252. devices << " (default)";
  253. }
  254. }
  255. devices << " - ";
  256. if ( Pa_GetDeviceInfo( i )->name ) {
  257. devices << Pa_GetDeviceInfo( i )->name;
  258. } else {
  259. devices << "Device " << i;
  260. }
  261. devices << " (";
  262. devices << "high latency: " << Pa_GetDeviceInfo( i )->defaultHighOutputLatency;
  263. devices << ", ";
  264. devices << "low latency: " << Pa_GetDeviceInfo( i )->defaultLowOutputLatency;
  265. devices << ")";
  266. devices << std::endl;
  267. }
  268. }
  269. return devices.str();
  270. }
  271. } // namespace openmpt123
  272. #endif // MPT_WITH_PORTAUDIO
  273. #endif // OPENMPT123_PORTAUDIO_HPP