libopenmpt_example_c_mem.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * libopenmpt_example_c_mem.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_mem 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 <portaudio.h>
  19. #define BUFFERSIZE 480
  20. #define SAMPLERATE 48000
  21. static int16_t left[BUFFERSIZE];
  22. static int16_t right[BUFFERSIZE];
  23. static int16_t * const buffers[2] = { left, right };
  24. static int16_t interleaved_buffer[BUFFERSIZE * 2];
  25. static int is_interleaved = 0;
  26. static void libopenmpt_example_logfunc( const char * message, void * userdata ) {
  27. (void)userdata;
  28. if ( message ) {
  29. fprintf( stderr, "openmpt: %s\n", message );
  30. }
  31. }
  32. static int libopenmpt_example_errfunc( int error, void * userdata ) {
  33. (void)userdata;
  34. (void)error;
  35. return OPENMPT_ERROR_FUNC_RESULT_DEFAULT & ~OPENMPT_ERROR_FUNC_RESULT_LOG;
  36. }
  37. static void libopenmpt_example_print_error( const char * func_name, int mod_err, const char * mod_err_str ) {
  38. if ( !func_name ) {
  39. func_name = "unknown function";
  40. }
  41. if ( mod_err == OPENMPT_ERROR_OUT_OF_MEMORY ) {
  42. mod_err_str = openmpt_error_string( mod_err );
  43. if ( !mod_err_str ) {
  44. fprintf( stderr, "Error: %s\n", "OPENMPT_ERROR_OUT_OF_MEMORY" );
  45. } else {
  46. fprintf( stderr, "Error: %s\n", mod_err_str );
  47. openmpt_free_string( mod_err_str );
  48. mod_err_str = NULL;
  49. }
  50. } else {
  51. if ( !mod_err_str ) {
  52. mod_err_str = openmpt_error_string( mod_err );
  53. if ( !mod_err_str ) {
  54. fprintf( stderr, "Error: %s failed.\n", func_name );
  55. } else {
  56. fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str );
  57. }
  58. openmpt_free_string( mod_err_str );
  59. mod_err_str = NULL;
  60. }
  61. fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str );
  62. }
  63. }
  64. typedef struct blob_t {
  65. size_t size;
  66. void * data;
  67. } blob_t;
  68. static void free_blob( blob_t * blob ) {
  69. if ( blob ) {
  70. if ( blob->data ) {
  71. free( blob->data );
  72. blob->data = 0;
  73. }
  74. blob->size = 0;
  75. free( blob );
  76. }
  77. }
  78. #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
  79. static blob_t * load_file( const wchar_t * filename ) {
  80. #else
  81. static blob_t * load_file( const char * filename ) {
  82. #endif
  83. blob_t * result = 0;
  84. blob_t * blob = 0;
  85. FILE * file = 0;
  86. long tell_result = 0;
  87. blob = malloc( sizeof( blob_t ) );
  88. if ( !blob ) {
  89. goto fail;
  90. }
  91. memset( blob, 0, sizeof( blob_t ) );
  92. #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
  93. file = _wfopen( filename, L"rb" );
  94. #else
  95. file = fopen( filename, "rb" );
  96. #endif
  97. if ( !file ) {
  98. goto fail;
  99. }
  100. if ( fseek( file, 0, SEEK_END ) != 0 ) {
  101. goto fail;
  102. }
  103. tell_result = ftell( file );
  104. if ( tell_result < 0 ) {
  105. goto fail;
  106. }
  107. if ( (unsigned long)(size_t)(unsigned long)tell_result != (unsigned long)tell_result ) {
  108. goto fail;
  109. }
  110. blob->size = (size_t)tell_result;
  111. if ( fseek( file, 0, SEEK_SET ) != 0 ) {
  112. goto fail;
  113. }
  114. blob->data = malloc( blob->size );
  115. if ( !blob->data ) {
  116. goto fail;
  117. }
  118. memset( blob->data, 0, blob->size );
  119. if ( fread( blob->data, 1, blob->size, file ) != blob->size ) {
  120. goto fail;
  121. }
  122. result = blob;
  123. blob = 0;
  124. goto cleanup;
  125. fail:
  126. result = 0;
  127. cleanup:
  128. if ( blob ) {
  129. free_blob( blob );
  130. blob = 0;
  131. }
  132. if ( file ) {
  133. fclose( file );
  134. file = 0;
  135. }
  136. return result;
  137. }
  138. #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
  139. #if defined( __clang__ ) && !defined( _MSC_VER )
  140. int wmain( int argc, wchar_t * argv[] );
  141. #endif
  142. int wmain( int argc, wchar_t * argv[] ) {
  143. #else
  144. int main( int argc, char * argv[] ) {
  145. #endif
  146. int result = 0;
  147. blob_t * blob = 0;
  148. openmpt_module * mod = 0;
  149. int mod_err = OPENMPT_ERROR_OK;
  150. const char * mod_err_str = NULL;
  151. size_t count = 0;
  152. PaError pa_error = paNoError;
  153. int pa_initialized = 0;
  154. PaStream * stream = 0;
  155. if ( argc != 2 ) {
  156. fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_mem SOMEMODULE'." );
  157. goto fail;
  158. }
  159. #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
  160. if ( wcslen( argv[1] ) == 0 ) {
  161. fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_mem SOMEMODULE'." );
  162. goto fail;
  163. }
  164. #else
  165. if ( strlen( argv[1] ) == 0 ) {
  166. fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_mem SOMEMODULE'." );
  167. goto fail;
  168. }
  169. #endif
  170. blob = load_file( argv[1] );
  171. if ( !blob ) {
  172. fprintf( stderr, "Error: %s\n", "load_file() failed." );
  173. goto fail;
  174. }
  175. mod = openmpt_module_create_from_memory2( blob->data, blob->size, &libopenmpt_example_logfunc, NULL, &libopenmpt_example_errfunc, NULL, &mod_err, &mod_err_str, NULL );
  176. if ( !mod ) {
  177. libopenmpt_example_print_error( "openmpt_module_create_from_memory2()", mod_err, mod_err_str );
  178. openmpt_free_string( mod_err_str );
  179. mod_err_str = NULL;
  180. goto fail;
  181. }
  182. pa_error = Pa_Initialize();
  183. if ( pa_error != paNoError ) {
  184. fprintf( stderr, "Error: %s\n", "Pa_Initialize() failed." );
  185. goto fail;
  186. }
  187. pa_initialized = 1;
  188. is_interleaved = 0;
  189. pa_error = Pa_OpenDefaultStream( &stream, 0, 2, paInt16 | paNonInterleaved, SAMPLERATE, paFramesPerBufferUnspecified, NULL, NULL );
  190. if ( pa_error == paSampleFormatNotSupported ) {
  191. is_interleaved = 1;
  192. pa_error = Pa_OpenDefaultStream( &stream, 0, 2, paInt16, SAMPLERATE, paFramesPerBufferUnspecified, NULL, NULL );
  193. }
  194. if ( pa_error != paNoError ) {
  195. fprintf( stderr, "Error: %s\n", "Pa_OpenStream() failed." );
  196. goto fail;
  197. }
  198. if ( !stream ) {
  199. fprintf( stderr, "Error: %s\n", "Pa_OpenStream() failed." );
  200. goto fail;
  201. }
  202. pa_error = Pa_StartStream( stream );
  203. if ( pa_error != paNoError ) {
  204. fprintf( stderr, "Error: %s\n", "Pa_StartStream() failed." );
  205. goto fail;
  206. }
  207. while ( 1 ) {
  208. openmpt_module_error_clear( mod );
  209. count = is_interleaved ? openmpt_module_read_interleaved_stereo( mod, SAMPLERATE, BUFFERSIZE, interleaved_buffer ) : openmpt_module_read_stereo( mod, SAMPLERATE, BUFFERSIZE, left, right );
  210. mod_err = openmpt_module_error_get_last( mod );
  211. mod_err_str = openmpt_module_error_get_last_message( mod );
  212. if ( mod_err != OPENMPT_ERROR_OK ) {
  213. libopenmpt_example_print_error( "openmpt_module_read_stereo()", mod_err, mod_err_str );
  214. openmpt_free_string( mod_err_str );
  215. mod_err_str = NULL;
  216. }
  217. if ( count == 0 ) {
  218. break;
  219. }
  220. pa_error = is_interleaved ? Pa_WriteStream( stream, interleaved_buffer, (unsigned long)count ) : Pa_WriteStream( stream, buffers, (unsigned long)count );
  221. if ( pa_error == paOutputUnderflowed ) {
  222. pa_error = paNoError;
  223. }
  224. if ( pa_error != paNoError ) {
  225. fprintf( stderr, "Error: %s\n", "Pa_WriteStream() failed." );
  226. goto fail;
  227. }
  228. }
  229. result = 0;
  230. goto cleanup;
  231. fail:
  232. result = 1;
  233. cleanup:
  234. if ( stream ) {
  235. if ( Pa_IsStreamActive( stream ) == 1 ) {
  236. Pa_StopStream( stream );
  237. }
  238. Pa_CloseStream( stream );
  239. stream = 0;
  240. }
  241. if ( pa_initialized ) {
  242. Pa_Terminate();
  243. pa_initialized = 0;
  244. (void)pa_initialized;
  245. }
  246. if ( mod ) {
  247. openmpt_module_destroy( mod );
  248. mod = 0;
  249. }
  250. if ( blob ) {
  251. free_blob( blob );
  252. blob = 0;
  253. }
  254. return result;
  255. }