libopenmpt_example_c_stdout.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * libopenmpt_example_c_stdout.c
  3. * -----------------------------
  4. * Purpose: libopenmpt C API simple example
  5. * Notes : This example writes raw 48000Hz / stereo / 16bit native endian PCM data to stdout.
  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_stdout SOMEMODULE | aplay --file-type raw --format=dat
  11. */
  12. #include <memory.h>
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <errno.h>
  18. #include <unistd.h>
  19. #include <libopenmpt/libopenmpt.h>
  20. #include <libopenmpt/libopenmpt_stream_callbacks_file.h>
  21. #define BUFFERSIZE 480
  22. #define SAMPLERATE 48000
  23. static void libopenmpt_example_logfunc( const char * message, void * userdata ) {
  24. (void)userdata;
  25. if ( message ) {
  26. fprintf( stderr, "openmpt: %s\n", message );
  27. }
  28. }
  29. static int libopenmpt_example_errfunc( int error, void * userdata ) {
  30. (void)userdata;
  31. (void)error;
  32. return OPENMPT_ERROR_FUNC_RESULT_DEFAULT & ~OPENMPT_ERROR_FUNC_RESULT_LOG;
  33. }
  34. static void libopenmpt_example_print_error( const char * func_name, int mod_err, const char * mod_err_str ) {
  35. if ( !func_name ) {
  36. func_name = "unknown function";
  37. }
  38. if ( mod_err == OPENMPT_ERROR_OUT_OF_MEMORY ) {
  39. mod_err_str = openmpt_error_string( mod_err );
  40. if ( !mod_err_str ) {
  41. fprintf( stderr, "Error: %s\n", "OPENMPT_ERROR_OUT_OF_MEMORY" );
  42. } else {
  43. fprintf( stderr, "Error: %s\n", mod_err_str );
  44. openmpt_free_string( mod_err_str );
  45. mod_err_str = NULL;
  46. }
  47. } else {
  48. if ( !mod_err_str ) {
  49. mod_err_str = openmpt_error_string( mod_err );
  50. if ( !mod_err_str ) {
  51. fprintf( stderr, "Error: %s failed.\n", func_name );
  52. } else {
  53. fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str );
  54. }
  55. openmpt_free_string( mod_err_str );
  56. mod_err_str = NULL;
  57. }
  58. fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str );
  59. }
  60. }
  61. static ssize_t xwrite( int fd, const void * buffer, size_t size ) {
  62. size_t written = 0;
  63. ssize_t retval = 0;
  64. while ( written < size ) {
  65. retval = write( fd, (const char *)buffer + written, size - written );
  66. if ( retval < 0 ) {
  67. if ( errno != EINTR ) {
  68. break;
  69. }
  70. retval = 0;
  71. }
  72. written += retval;
  73. }
  74. return written;
  75. }
  76. static int16_t buffer[BUFFERSIZE * 2];
  77. #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
  78. #if defined( __clang__ ) && !defined( _MSC_VER )
  79. int wmain( int argc, wchar_t * argv[] );
  80. #endif
  81. int wmain( int argc, wchar_t * argv[] ) {
  82. #else
  83. int main( int argc, char * argv[] ) {
  84. #endif
  85. int result = 0;
  86. FILE * file = 0;
  87. openmpt_module * mod = 0;
  88. int mod_err = OPENMPT_ERROR_OK;
  89. const char * mod_err_str = NULL;
  90. size_t count = 0;
  91. size_t written = 0;
  92. if ( argc != 2 ) {
  93. fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_stdout SOMEMODULE'." );
  94. goto fail;
  95. }
  96. #if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) )
  97. if ( wcslen( argv[1] ) == 0 ) {
  98. fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_stdout SOMEMODULE'." );
  99. goto fail;
  100. }
  101. file = _wfopen( argv[1], L"rb" );
  102. #else
  103. if ( strlen( argv[1] ) == 0 ) {
  104. fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_stdout SOMEMODULE'." );
  105. goto fail;
  106. }
  107. file = fopen( argv[1], "rb" );
  108. #endif
  109. if ( !file ) {
  110. fprintf( stderr, "Error: %s\n", "fopen() failed." );
  111. goto fail;
  112. }
  113. mod = openmpt_module_create2( openmpt_stream_get_file_callbacks(), file, &libopenmpt_example_logfunc, NULL, &libopenmpt_example_errfunc, NULL, &mod_err, &mod_err_str, NULL );
  114. if ( !mod ) {
  115. libopenmpt_example_print_error( "openmpt_module_create2()", mod_err, mod_err_str );
  116. openmpt_free_string( mod_err_str );
  117. mod_err_str = NULL;
  118. goto fail;
  119. }
  120. while ( 1 ) {
  121. openmpt_module_error_clear( mod );
  122. count = openmpt_module_read_interleaved_stereo( mod, SAMPLERATE, BUFFERSIZE, buffer );
  123. mod_err = openmpt_module_error_get_last( mod );
  124. mod_err_str = openmpt_module_error_get_last_message( mod );
  125. if ( mod_err != OPENMPT_ERROR_OK ) {
  126. libopenmpt_example_print_error( "openmpt_module_read_interleaved_stereo()", mod_err, mod_err_str );
  127. openmpt_free_string( mod_err_str );
  128. mod_err_str = NULL;
  129. }
  130. if ( count == 0 ) {
  131. break;
  132. }
  133. written = xwrite( STDOUT_FILENO, buffer, count * 2 * sizeof( int16_t ) );
  134. if ( written == 0 ) {
  135. fprintf( stderr, "Error: %s\n", "write() failed." );
  136. goto fail;
  137. }
  138. }
  139. result = 0;
  140. goto cleanup;
  141. fail:
  142. result = 1;
  143. cleanup:
  144. if ( mod ) {
  145. openmpt_module_destroy( mod );
  146. mod = 0;
  147. }
  148. if ( file ) {
  149. fclose( file );
  150. file = 0;
  151. }
  152. return result;
  153. }