| 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 |
/* |
| 11 |
* Usage: libopenmpt_example_c_stdout SOMEMODULE | aplay --file-type raw --format=dat |
| 12 |
*/ |
| 13 |
|
| 14 |
#if defined( __MINGW32__ ) && !defined( __MINGW64__ ) |
| 15 |
#include <sys/types.h> |
| 16 |
#endif |
| 17 |
|
| 18 |
#include <memory.h> |
| 19 |
#include <stdint.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#include <errno.h> |
| 25 |
#include <unistd.h> |
| 26 |
|
| 27 |
#include <libopenmpt/libopenmpt.h> |
| 28 |
#include <libopenmpt/libopenmpt_stream_callbacks_file.h> |
| 29 |
|
| 30 |
#if defined( __DJGPP__ ) |
| 31 |
#include <crt0.h> |
| 32 |
#endif /* __DJGPP__ */ |
| 33 |
|
| 34 |
#define BUFFERSIZE 480 |
| 35 |
#define SAMPLERATE 48000 |
| 36 |
|
| 37 |
static void libopenmpt_example_logfunc( const char * message, void * userdata ) { |
| 38 |
(void)userdata; |
| 39 |
if ( message ) { |
| 40 |
fprintf( stderr, "openmpt: %s\n", message ); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
static int libopenmpt_example_errfunc( int error, void * userdata ) { |
| 45 |
(void)userdata; |
| 46 |
(void)error; |
| 47 |
return OPENMPT_ERROR_FUNC_RESULT_DEFAULT & ~OPENMPT_ERROR_FUNC_RESULT_LOG; |
| 48 |
} |
| 49 |
|
| 50 |
static void libopenmpt_example_print_error( const char * func_name, int mod_err, const char * mod_err_str ) { |
| 51 |
if ( !func_name ) { |
| 52 |
func_name = "unknown function"; |
| 53 |
} |
| 54 |
if ( mod_err == OPENMPT_ERROR_OUT_OF_MEMORY ) { |
| 55 |
mod_err_str = openmpt_error_string( mod_err ); |
| 56 |
if ( !mod_err_str ) { |
| 57 |
fprintf( stderr, "Error: %s\n", "OPENMPT_ERROR_OUT_OF_MEMORY" ); |
| 58 |
} else { |
| 59 |
fprintf( stderr, "Error: %s\n", mod_err_str ); |
| 60 |
openmpt_free_string( mod_err_str ); |
| 61 |
mod_err_str = NULL; |
| 62 |
} |
| 63 |
} else { |
| 64 |
if ( !mod_err_str ) { |
| 65 |
mod_err_str = openmpt_error_string( mod_err ); |
| 66 |
if ( !mod_err_str ) { |
| 67 |
fprintf( stderr, "Error: %s failed.\n", func_name ); |
| 68 |
} else { |
| 69 |
fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str ); |
| 70 |
} |
| 71 |
openmpt_free_string( mod_err_str ); |
| 72 |
mod_err_str = NULL; |
| 73 |
} |
| 74 |
fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
static ssize_t xwrite( int fd, const void * buffer, size_t size ) { |
| 79 |
size_t written = 0; |
| 80 |
ssize_t retval = 0; |
| 81 |
while ( written < size ) { |
| 82 |
retval = write( fd, (const char *)buffer + written, size - written ); |
| 83 |
if ( retval < 0 ) { |
| 84 |
if ( errno != EINTR ) { |
| 85 |
break; |
| 86 |
} |
| 87 |
retval = 0; |
| 88 |
} |
| 89 |
written += retval; |
| 90 |
} |
| 91 |
return written; |
| 92 |
} |
| 93 |
|
| 94 |
static int16_t buffer[BUFFERSIZE * 2]; |
| 95 |
|
| 96 |
#if defined( __DJGPP__ ) |
| 97 |
/* clang-format off */ |
| 98 |
int _crt0_startup_flags = 0 |
| 99 |
| _CRT0_FLAG_NONMOVE_SBRK /* force interrupt compatible allocation */ |
| 100 |
| _CRT0_DISABLE_SBRK_ADDRESS_WRAP /* force NT compatible allocation */ |
| 101 |
| _CRT0_FLAG_LOCK_MEMORY /* lock all code and data at program startup */ |
| 102 |
| 0; |
| 103 |
/* clang-format on */ |
| 104 |
#endif /* __DJGPP__ */ |
| 105 |
#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) |
| 106 |
#if defined( __clang__ ) && !defined( _MSC_VER ) |
| 107 |
int wmain( int argc, wchar_t * argv[] ); |
| 108 |
#endif |
| 109 |
int wmain( int argc, wchar_t * argv[] ) { |
| 110 |
#else |
| 111 |
int main( int argc, char * argv[] ) { |
| 112 |
#endif |
| 113 |
#if defined( __DJGPP__ ) |
| 114 |
/* clang-format off */ |
| 115 |
_crt0_startup_flags &= ~_CRT0_FLAG_LOCK_MEMORY; /* disable automatic locking for all further memory allocations */ |
| 116 |
/* clang-format on */ |
| 117 |
#endif /* __DJGPP__ */ |
| 118 |
|
| 119 |
int result = 0; |
| 120 |
FILE * file = 0; |
| 121 |
openmpt_module * mod = 0; |
| 122 |
int mod_err = OPENMPT_ERROR_OK; |
| 123 |
const char * mod_err_str = NULL; |
| 124 |
size_t count = 0; |
| 125 |
size_t written = 0; |
| 126 |
|
| 127 |
if ( argc != 2 ) { |
| 128 |
fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_stdout SOMEMODULE'." ); |
| 129 |
goto fail; |
| 130 |
} |
| 131 |
|
| 132 |
#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) |
| 133 |
if ( wcslen( argv[1] ) == 0 ) { |
| 134 |
fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_stdout SOMEMODULE'." ); |
| 135 |
goto fail; |
| 136 |
} |
| 137 |
file = _wfopen( argv[1], L"rb" ); |
| 138 |
#else |
| 139 |
if ( strlen( argv[1] ) == 0 ) { |
| 140 |
fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_stdout SOMEMODULE'." ); |
| 141 |
goto fail; |
| 142 |
} |
| 143 |
file = fopen( argv[1], "rb" ); |
| 144 |
#endif |
| 145 |
if ( !file ) { |
| 146 |
fprintf( stderr, "Error: %s\n", "fopen() failed." ); |
| 147 |
goto fail; |
| 148 |
} |
| 149 |
|
| 150 |
mod = openmpt_module_create2( openmpt_stream_get_file_callbacks2(), file, &libopenmpt_example_logfunc, NULL, &libopenmpt_example_errfunc, NULL, &mod_err, &mod_err_str, NULL ); |
| 151 |
if ( !mod ) { |
| 152 |
libopenmpt_example_print_error( "openmpt_module_create2()", mod_err, mod_err_str ); |
| 153 |
openmpt_free_string( mod_err_str ); |
| 154 |
mod_err_str = NULL; |
| 155 |
goto fail; |
| 156 |
} |
| 157 |
|
| 158 |
while ( 1 ) { |
| 159 |
|
| 160 |
openmpt_module_error_clear( mod ); |
| 161 |
count = openmpt_module_read_interleaved_stereo( mod, SAMPLERATE, BUFFERSIZE, buffer ); |
| 162 |
mod_err = openmpt_module_error_get_last( mod ); |
| 163 |
mod_err_str = openmpt_module_error_get_last_message( mod ); |
| 164 |
if ( mod_err != OPENMPT_ERROR_OK ) { |
| 165 |
libopenmpt_example_print_error( "openmpt_module_read_interleaved_stereo()", mod_err, mod_err_str ); |
| 166 |
openmpt_free_string( mod_err_str ); |
| 167 |
mod_err_str = NULL; |
| 168 |
} |
| 169 |
if ( count == 0 ) { |
| 170 |
break; |
| 171 |
} |
| 172 |
|
| 173 |
written = xwrite( STDOUT_FILENO, buffer, count * 2 * sizeof( int16_t ) ); |
| 174 |
if ( written == 0 ) { |
| 175 |
fprintf( stderr, "Error: %s\n", "write() failed." ); |
| 176 |
goto fail; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
result = 0; |
| 181 |
|
| 182 |
goto cleanup; |
| 183 |
|
| 184 |
fail: |
| 185 |
|
| 186 |
result = 1; |
| 187 |
|
| 188 |
cleanup: |
| 189 |
|
| 190 |
if ( mod ) { |
| 191 |
openmpt_module_destroy( mod ); |
| 192 |
mod = 0; |
| 193 |
} |
| 194 |
|
| 195 |
if ( file ) { |
| 196 |
fclose( file ); |
| 197 |
file = 0; |
| 198 |
} |
| 199 |
|
| 200 |
return result; |
| 201 |
} |