libopenmpt_example_c.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * libopenmpt_example_c.c
  3. * ----------------------
  4. * Purpose: libopenmpt C API example
  5. * Notes : PortAudio is used for sound output.
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. /*
  10. * Usage: libopenmpt_example_c SOMEMODULE
  11. */
  12. #include <memory.h>
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <libopenmpt/libopenmpt.h>
  18. #include <libopenmpt/libopenmpt_stream_callbacks_file.h>
  19. #include <portaudio.h>
  20. #define BUFFERSIZE 480
  21. #define SAMPLERATE 48000
  22. static int16_t left[BUFFERSIZE];
  23. static int16_t right[BUFFERSIZE];
  24. static int16_t * const buffers[2] = { left, right };
  25. static int16_t interleaved_buffer[BUFFERSIZE * 2];
  26. static int is_interleaved = 0;
  27. static void libopenmpt_example_logfunc( const char * message, void * userdata ) {
  28. (void)userdata;
  29. if ( message ) {
  30. fprintf( stderr, "openmpt: %s\n", message );
  31. }
  32. }
  33. static int libopenmpt_example_errfunc( int error, void * userdata ) {
  34. (void)userdata;
  35. (void)error;
  36. return OPENMPT_ERROR_FUNC_RESULT_DEFAULT & ~OPENMPT_ERROR_FUNC_RESULT_LOG;
  37. }
  38. static void libopenmpt_example_print_error( const char * func_name, int mod_err, const char * mod_err_str ) {
  39. if ( !func_name ) {
  40. func_name = "unknown function";
  41. }
  42. if ( mod_err == OPENMPT_ERROR_OUT_OF_MEMORY ) {
  43. mod_err_str = openmpt_error_string( mod_err );
  44. if ( !mod_err_str ) {
  45. fprintf( stderr, "Error: %s\n", "OPENMPT_ERROR_OUT_OF_MEMORY" );
  46. } else {
  47. fprintf( stderr, "Error: %s\n", mod_err_str );
  48. openmpt_free_string( mod_err_str );
  49. mod_err_str = NULL;
  50. }
  51. } else {
  52. if ( !mod_err_str ) {
  53. mod_err_str = openmpt_error_string( mod_err );
  54. if ( !mod_err_str ) {
  55. fprintf( stderr, "Error: %s failed.\n", func_name );
  56. } else {
  57. fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str );
  58. }
  59. openmpt_free_string( mod_err_str );
  60. mod_err_str = NULL;
  61. }
  62. fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str );
  63. }
  64. }
  65. #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
  66. #if defined( __clang__ ) && !defined( _MSC_VER )
  67. int wmain( int argc, wchar_t * argv[] );
  68. #endif
  69. int wmain( int argc, wchar_t * argv[] ) {
  70. #else
  71. int main( int argc, char * argv[] ) {
  72. #endif
  73. int result = 0;
  74. FILE * file = 0;
  75. openmpt_module * mod = 0;
  76. int mod_err = OPENMPT_ERROR_OK;
  77. const char * mod_err_str = NULL;
  78. size_t count = 0;
  79. PaError pa_error = paNoError;
  80. int pa_initialized = 0;
  81. PaStream * stream = 0;
  82. if ( argc != 2 ) {
  83. fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c SOMEMODULE'." );
  84. goto fail;
  85. }
  86. #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
  87. if ( wcslen( argv[1] ) == 0 ) {
  88. fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c SOMEMODULE'." );
  89. goto fail;
  90. }
  91. file = _wfopen( argv[1], L"rb" );
  92. #else
  93. if ( strlen( argv[1] ) == 0 ) {
  94. fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c SOMEMODULE'." );
  95. goto fail;
  96. }
  97. file = fopen( argv[1], "rb" );
  98. #endif
  99. if ( !file ) {
  100. fprintf( stderr, "Error: %s\n", "fopen() failed." );
  101. goto fail;
  102. }
  103. mod = openmpt_module_create2( openmpt_stream_get_file_callbacks(), file, &libopenmpt_example_logfunc, NULL, &libopenmpt_example_errfunc, NULL, &mod_err, &mod_err_str, NULL );
  104. if ( !mod ) {
  105. libopenmpt_example_print_error( "openmpt_module_create2()", mod_err, mod_err_str );
  106. openmpt_free_string( mod_err_str );
  107. mod_err_str = NULL;
  108. goto fail;
  109. }
  110. openmpt_module_set_error_func( mod, NULL, NULL );
  111. pa_error = Pa_Initialize();
  112. if ( pa_error != paNoError ) {
  113. fprintf( stderr, "Error: %s\n", "Pa_Initialize() failed." );
  114. goto fail;
  115. }
  116. pa_initialized = 1;
  117. pa_error = Pa_OpenDefaultStream( &stream, 0, 2, paInt16 | paNonInterleaved, SAMPLERATE, paFramesPerBufferUnspecified, NULL, NULL );
  118. if ( pa_error == paSampleFormatNotSupported ) {
  119. is_interleaved = 1;
  120. pa_error = Pa_OpenDefaultStream( &stream, 0, 2, paInt16, SAMPLERATE, paFramesPerBufferUnspecified, NULL, NULL );
  121. }
  122. if ( pa_error != paNoError ) {
  123. fprintf( stderr, "Error: %s\n", "Pa_OpenStream() failed." );
  124. goto fail;
  125. }
  126. if ( !stream ) {
  127. fprintf( stderr, "Error: %s\n", "Pa_OpenStream() failed." );
  128. goto fail;
  129. }
  130. pa_error = Pa_StartStream( stream );
  131. if ( pa_error != paNoError ) {
  132. fprintf( stderr, "Error: %s\n", "Pa_StartStream() failed." );
  133. goto fail;
  134. }
  135. while ( 1 ) {
  136. openmpt_module_error_clear( mod );
  137. count = is_interleaved ? openmpt_module_read_interleaved_stereo( mod, SAMPLERATE, BUFFERSIZE, interleaved_buffer ) : openmpt_module_read_stereo( mod, SAMPLERATE, BUFFERSIZE, left, right );
  138. mod_err = openmpt_module_error_get_last( mod );
  139. mod_err_str = openmpt_module_error_get_last_message( mod );
  140. if ( mod_err != OPENMPT_ERROR_OK ) {
  141. libopenmpt_example_print_error( "openmpt_module_read_stereo()", mod_err, mod_err_str );
  142. openmpt_free_string( mod_err_str );
  143. mod_err_str = NULL;
  144. }
  145. if ( count == 0 ) {
  146. break;
  147. }
  148. pa_error = is_interleaved ? Pa_WriteStream( stream, interleaved_buffer, (unsigned long)count ) : Pa_WriteStream( stream, buffers, (unsigned long)count );
  149. if ( pa_error == paOutputUnderflowed ) {
  150. pa_error = paNoError;
  151. }
  152. if ( pa_error != paNoError ) {
  153. fprintf( stderr, "Error: %s\n", "Pa_WriteStream() failed." );
  154. goto fail;
  155. }
  156. }
  157. result = 0;
  158. goto cleanup;
  159. fail:
  160. result = 1;
  161. cleanup:
  162. if ( stream ) {
  163. if ( Pa_IsStreamActive( stream ) == 1 ) {
  164. Pa_StopStream( stream );
  165. }
  166. Pa_CloseStream( stream );
  167. stream = 0;
  168. }
  169. if ( pa_initialized ) {
  170. Pa_Terminate();
  171. pa_initialized = 0;
  172. (void)pa_initialized;
  173. }
  174. if ( mod ) {
  175. openmpt_module_destroy( mod );
  176. mod = 0;
  177. }
  178. if ( file ) {
  179. fclose( file );
  180. file = 0;
  181. }
  182. return result;
  183. }