| 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 |
/* |
| 11 |
* Usage: libopenmpt_example_c SOMEMODULE |
| 12 |
*/ |
| 13 |
|
| 14 |
#if defined( unix ) || defined( __unix__ ) || defined( __unix ) |
| 15 |
#include <unistd.h> |
| 16 |
#if defined( _POSIX_VERSION ) |
| 17 |
#if ( _POSIX_VERSION > 0 ) |
| 18 |
#ifndef POSIX |
| 19 |
#define POSIX |
| 20 |
#endif |
| 21 |
#endif |
| 22 |
#endif |
| 23 |
#endif |
| 24 |
|
| 25 |
#if defined( __MINGW32__ ) && !defined( __MINGW64__ ) |
| 26 |
#include <sys/types.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
#include <memory.h> |
| 30 |
#include <stdint.h> |
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <string.h> |
| 34 |
|
| 35 |
#include <libopenmpt/libopenmpt.h> |
| 36 |
#if OPENMPT_API_VERSION_AT_LEAST( 0, 7, 0 ) |
| 37 |
#if defined( LIBOPENMPT_STREAM_CALLBACKS_FILE_MINGW ) && defined( __MINGW32__ ) |
| 38 |
#include <libopenmpt/libopenmpt_stream_callbacks_file_mingw.h> |
| 39 |
#elif defined( LIBOPENMPT_STREAM_CALLBACKS_FILE_MSVCRT ) && ( defined( _MSC_VER ) || ( defined( __clang__ ) && defined( _WIN32 ) ) ) |
| 40 |
#include <libopenmpt/libopenmpt_stream_callbacks_file_msvcrt.h> |
| 41 |
#elif defined( LIBOPENMPT_STREAM_CALLBACKS_FILE_POSIX ) && defined( POSIX ) && defined( _POSIX_C_SOURCE ) |
| 42 |
#if ( _POSIX_C_SOURCE > 200112L ) |
| 43 |
#include <libopenmpt/libopenmpt_stream_callbacks_file_posix.h> |
| 44 |
#else |
| 45 |
#include <libopenmpt/libopenmpt_stream_callbacks_file.h> |
| 46 |
#endif |
| 47 |
#else |
| 48 |
#include <libopenmpt/libopenmpt_stream_callbacks_file.h> |
| 49 |
#endif |
| 50 |
#else |
| 51 |
#include <libopenmpt/libopenmpt_stream_callbacks_file.h> |
| 52 |
#endif |
| 53 |
|
| 54 |
#if defined( __clang__ ) |
| 55 |
#pragma clang diagnostic push |
| 56 |
#pragma clang diagnostic ignored "-Wstrict-prototypes" |
| 57 |
#elif defined( __GNUC__ ) && !defined( __clang__ ) && !defined( _MSC_VER ) |
| 58 |
#pragma GCC diagnostic push |
| 59 |
#pragma GCC diagnostic ignored "-Wstrict-prototypes" |
| 60 |
#endif |
| 61 |
#include <portaudio.h> |
| 62 |
#if defined( __clang__ ) |
| 63 |
#pragma clang diagnostic pop |
| 64 |
#elif defined( __GNUC__ ) && !defined( __clang__ ) && !defined( _MSC_VER ) |
| 65 |
#pragma GCC diagnostic pop |
| 66 |
#endif |
| 67 |
|
| 68 |
#if defined( __DJGPP__ ) |
| 69 |
#include <crt0.h> |
| 70 |
#endif /* __DJGPP__ */ |
| 71 |
|
| 72 |
#define BUFFERSIZE 480 |
| 73 |
#define SAMPLERATE 48000 |
| 74 |
|
| 75 |
static float left[BUFFERSIZE]; |
| 76 |
static float right[BUFFERSIZE]; |
| 77 |
static float * const buffers[2] = { left, right }; |
| 78 |
static float interleaved_buffer[BUFFERSIZE * 2]; |
| 79 |
static int is_interleaved = 0; |
| 80 |
|
| 81 |
static void libopenmpt_example_logfunc( const char * message, void * userdata ) { |
| 82 |
(void)userdata; |
| 83 |
if ( message ) { |
| 84 |
fprintf( stderr, "openmpt: %s\n", message ); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
static int libopenmpt_example_errfunc( int error, void * userdata ) { |
| 89 |
(void)userdata; |
| 90 |
(void)error; |
| 91 |
return OPENMPT_ERROR_FUNC_RESULT_DEFAULT & ~OPENMPT_ERROR_FUNC_RESULT_LOG; |
| 92 |
} |
| 93 |
|
| 94 |
static void libopenmpt_example_print_error( const char * func_name, int mod_err, const char * mod_err_str ) { |
| 95 |
if ( !func_name ) { |
| 96 |
func_name = "unknown function"; |
| 97 |
} |
| 98 |
if ( mod_err == OPENMPT_ERROR_OUT_OF_MEMORY ) { |
| 99 |
mod_err_str = openmpt_error_string( mod_err ); |
| 100 |
if ( !mod_err_str ) { |
| 101 |
fprintf( stderr, "Error: %s\n", "OPENMPT_ERROR_OUT_OF_MEMORY" ); |
| 102 |
} else { |
| 103 |
fprintf( stderr, "Error: %s\n", mod_err_str ); |
| 104 |
openmpt_free_string( mod_err_str ); |
| 105 |
mod_err_str = NULL; |
| 106 |
} |
| 107 |
} else { |
| 108 |
if ( !mod_err_str ) { |
| 109 |
mod_err_str = openmpt_error_string( mod_err ); |
| 110 |
if ( !mod_err_str ) { |
| 111 |
fprintf( stderr, "Error: %s failed.\n", func_name ); |
| 112 |
} else { |
| 113 |
fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str ); |
| 114 |
} |
| 115 |
openmpt_free_string( mod_err_str ); |
| 116 |
mod_err_str = NULL; |
| 117 |
} |
| 118 |
fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str ); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
#if defined( __DJGPP__ ) |
| 123 |
/* clang-format off */ |
| 124 |
int _crt0_startup_flags = 0 |
| 125 |
| _CRT0_FLAG_NONMOVE_SBRK /* force interrupt compatible allocation */ |
| 126 |
| _CRT0_DISABLE_SBRK_ADDRESS_WRAP /* force NT compatible allocation */ |
| 127 |
| _CRT0_FLAG_LOCK_MEMORY /* lock all code and data at program startup */ |
| 128 |
| 0; |
| 129 |
/* clang-format on */ |
| 130 |
#endif /* __DJGPP__ */ |
| 131 |
#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) |
| 132 |
#if defined( __clang__ ) && !defined( _MSC_VER ) |
| 133 |
int wmain( int argc, wchar_t * argv[] ); |
| 134 |
#endif |
| 135 |
int wmain( int argc, wchar_t * argv[] ) { |
| 136 |
#else |
| 137 |
int main( int argc, char * argv[] ) { |
| 138 |
#endif |
| 139 |
#if defined( __DJGPP__ ) |
| 140 |
/* clang-format off */ |
| 141 |
_crt0_startup_flags &= ~_CRT0_FLAG_LOCK_MEMORY; /* disable automatic locking for all further memory allocations */ |
| 142 |
/* clang-format on */ |
| 143 |
#endif /* __DJGPP__ */ |
| 144 |
|
| 145 |
int result = 0; |
| 146 |
FILE * file = 0; |
| 147 |
openmpt_module * mod = 0; |
| 148 |
int mod_err = OPENMPT_ERROR_OK; |
| 149 |
const char * mod_err_str = NULL; |
| 150 |
size_t count = 0; |
| 151 |
PaError pa_error = paNoError; |
| 152 |
int pa_initialized = 0; |
| 153 |
PaStream * stream = 0; |
| 154 |
|
| 155 |
if ( argc != 2 ) { |
| 156 |
fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c SOMEMODULE'." ); |
| 157 |
goto fail; |
| 158 |
} |
| 159 |
|
| 160 |
#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) |
| 161 |
if ( wcslen( argv[1] ) == 0 ) { |
| 162 |
fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c SOMEMODULE'." ); |
| 163 |
goto fail; |
| 164 |
} |
| 165 |
file = _wfopen( argv[1], L"rb" ); |
| 166 |
#else |
| 167 |
if ( strlen( argv[1] ) == 0 ) { |
| 168 |
fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c SOMEMODULE'." ); |
| 169 |
goto fail; |
| 170 |
} |
| 171 |
file = fopen( argv[1], "rb" ); |
| 172 |
#endif |
| 173 |
if ( !file ) { |
| 174 |
fprintf( stderr, "Error: %s\n", "fopen() failed." ); |
| 175 |
goto fail; |
| 176 |
} |
| 177 |
|
| 178 |
#if OPENMPT_API_VERSION_AT_LEAST( 0, 7, 0 ) |
| 179 |
#if defined( LIBOPENMPT_STREAM_CALLBACKS_FILE_MINGW ) && defined( __MINGW32__ ) |
| 180 |
mod = openmpt_module_create2( openmpt_stream_get_file_mingw_callbacks(), file, &libopenmpt_example_logfunc, NULL, &libopenmpt_example_errfunc, NULL, &mod_err, &mod_err_str, NULL ); |
| 181 |
#elif defined( LIBOPENMPT_STREAM_CALLBACKS_FILE_MSVCRT ) && ( defined( _MSC_VER ) || ( defined( __clang__ ) && defined( _WIN32 ) ) ) |
| 182 |
mod = openmpt_module_create2( openmpt_stream_get_file_msvcrt_callbacks(), file, &libopenmpt_example_logfunc, NULL, &libopenmpt_example_errfunc, NULL, &mod_err, &mod_err_str, NULL ); |
| 183 |
#elif defined( LIBOPENMPT_STREAM_CALLBACKS_FILE_POSIX ) && defined( POSIX ) && defined( _POSIX_C_SOURCE ) |
| 184 |
#if ( _POSIX_C_SOURCE > 200112L ) |
| 185 |
mod = openmpt_module_create2( openmpt_stream_get_file_posix_callbacks(), file, &libopenmpt_example_logfunc, NULL, &libopenmpt_example_errfunc, NULL, &mod_err, &mod_err_str, NULL ); |
| 186 |
#else |
| 187 |
mod = openmpt_module_create2( openmpt_stream_get_file_callbacks2(), file, &libopenmpt_example_logfunc, NULL, &libopenmpt_example_errfunc, NULL, &mod_err, &mod_err_str, NULL ); |
| 188 |
#endif |
| 189 |
#else |
| 190 |
mod = openmpt_module_create2( openmpt_stream_get_file_callbacks2(), file, &libopenmpt_example_logfunc, NULL, &libopenmpt_example_errfunc, NULL, &mod_err, &mod_err_str, NULL ); |
| 191 |
#endif |
| 192 |
#else |
| 193 |
mod = openmpt_module_create2( openmpt_stream_get_file_callbacks(), file, &libopenmpt_example_logfunc, NULL, &libopenmpt_example_errfunc, NULL, &mod_err, &mod_err_str, NULL ); |
| 194 |
#endif |
| 195 |
|
| 196 |
if ( !mod ) { |
| 197 |
libopenmpt_example_print_error( "openmpt_module_create2()", mod_err, mod_err_str ); |
| 198 |
openmpt_free_string( mod_err_str ); |
| 199 |
mod_err_str = NULL; |
| 200 |
goto fail; |
| 201 |
} |
| 202 |
openmpt_module_set_error_func( mod, NULL, NULL ); |
| 203 |
|
| 204 |
pa_error = Pa_Initialize(); |
| 205 |
if ( pa_error != paNoError ) { |
| 206 |
fprintf( stderr, "Error: %s\n", "Pa_Initialize() failed." ); |
| 207 |
goto fail; |
| 208 |
} |
| 209 |
pa_initialized = 1; |
| 210 |
|
| 211 |
pa_error = Pa_OpenDefaultStream( &stream, 0, 2, paFloat32 | paNonInterleaved, SAMPLERATE, paFramesPerBufferUnspecified, NULL, NULL ); |
| 212 |
if ( pa_error == paSampleFormatNotSupported ) { |
| 213 |
is_interleaved = 1; |
| 214 |
pa_error = Pa_OpenDefaultStream( &stream, 0, 2, paFloat32, SAMPLERATE, paFramesPerBufferUnspecified, NULL, NULL ); |
| 215 |
} |
| 216 |
if ( pa_error != paNoError ) { |
| 217 |
fprintf( stderr, "Error: %s\n", "Pa_OpenStream() failed." ); |
| 218 |
goto fail; |
| 219 |
} |
| 220 |
if ( !stream ) { |
| 221 |
fprintf( stderr, "Error: %s\n", "Pa_OpenStream() failed." ); |
| 222 |
goto fail; |
| 223 |
} |
| 224 |
|
| 225 |
pa_error = Pa_StartStream( stream ); |
| 226 |
if ( pa_error != paNoError ) { |
| 227 |
fprintf( stderr, "Error: %s\n", "Pa_StartStream() failed." ); |
| 228 |
goto fail; |
| 229 |
} |
| 230 |
|
| 231 |
while ( 1 ) { |
| 232 |
|
| 233 |
openmpt_module_error_clear( mod ); |
| 234 |
count = is_interleaved ? openmpt_module_read_interleaved_float_stereo( mod, SAMPLERATE, BUFFERSIZE, interleaved_buffer ) : openmpt_module_read_float_stereo( mod, SAMPLERATE, BUFFERSIZE, left, right ); |
| 235 |
mod_err = openmpt_module_error_get_last( mod ); |
| 236 |
mod_err_str = openmpt_module_error_get_last_message( mod ); |
| 237 |
if ( mod_err != OPENMPT_ERROR_OK ) { |
| 238 |
libopenmpt_example_print_error( "openmpt_module_read_stereo()", mod_err, mod_err_str ); |
| 239 |
openmpt_free_string( mod_err_str ); |
| 240 |
mod_err_str = NULL; |
| 241 |
} |
| 242 |
if ( count == 0 ) { |
| 243 |
break; |
| 244 |
} |
| 245 |
|
| 246 |
pa_error = is_interleaved ? Pa_WriteStream( stream, interleaved_buffer, (unsigned long)count ) : Pa_WriteStream( stream, buffers, (unsigned long)count ); |
| 247 |
if ( pa_error == paOutputUnderflowed ) { |
| 248 |
pa_error = paNoError; |
| 249 |
} |
| 250 |
if ( pa_error != paNoError ) { |
| 251 |
fprintf( stderr, "Error: %s\n", "Pa_WriteStream() failed." ); |
| 252 |
goto fail; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
result = 0; |
| 257 |
|
| 258 |
goto cleanup; |
| 259 |
|
| 260 |
fail: |
| 261 |
|
| 262 |
result = 1; |
| 263 |
|
| 264 |
cleanup: |
| 265 |
|
| 266 |
if ( stream ) { |
| 267 |
if ( Pa_IsStreamActive( stream ) == 1 ) { |
| 268 |
Pa_StopStream( stream ); |
| 269 |
} |
| 270 |
Pa_CloseStream( stream ); |
| 271 |
stream = 0; |
| 272 |
} |
| 273 |
|
| 274 |
if ( pa_initialized ) { |
| 275 |
Pa_Terminate(); |
| 276 |
pa_initialized = 0; |
| 277 |
(void)pa_initialized; |
| 278 |
} |
| 279 |
|
| 280 |
if ( mod ) { |
| 281 |
openmpt_module_destroy( mod ); |
| 282 |
mod = 0; |
| 283 |
} |
| 284 |
|
| 285 |
if ( file ) { |
| 286 |
fclose( file ); |
| 287 |
file = 0; |
| 288 |
} |
| 289 |
|
| 290 |
return result; |
| 291 |
} |