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