libopenmpt_example_c_pipe.c 4.0 KB

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