| 1 |
/* |
| 2 |
* libopenmpt_example_c_unsafe.c |
| 3 |
* ----------------------------- |
| 4 |
* Purpose: libopenmpt C API simplified 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_unsafe SOMEMODULE |
| 12 |
* CAUTION: This simple example does no error cheking at all. |
| 13 |
*/ |
| 14 |
|
| 15 |
#if defined( __MINGW32__ ) && !defined( __MINGW64__ ) |
| 16 |
#include <sys/types.h> |
| 17 |
#endif |
| 18 |
|
| 19 |
#include <memory.h> |
| 20 |
#include <stdint.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
|
| 25 |
#include <libopenmpt/libopenmpt.h> |
| 26 |
#include <libopenmpt/libopenmpt_stream_callbacks_file.h> |
| 27 |
|
| 28 |
#if defined( __clang__ ) |
| 29 |
#pragma clang diagnostic push |
| 30 |
#pragma clang diagnostic ignored "-Wstrict-prototypes" |
| 31 |
#elif defined( __GNUC__ ) && !defined( __clang__ ) && !defined( _MSC_VER ) |
| 32 |
#pragma GCC diagnostic push |
| 33 |
#pragma GCC diagnostic ignored "-Wstrict-prototypes" |
| 34 |
#endif |
| 35 |
#include <portaudio.h> |
| 36 |
#if defined( __clang__ ) |
| 37 |
#pragma clang diagnostic pop |
| 38 |
#elif defined( __GNUC__ ) && !defined( __clang__ ) && !defined( _MSC_VER ) |
| 39 |
#pragma GCC diagnostic pop |
| 40 |
#endif |
| 41 |
|
| 42 |
#if defined( __DJGPP__ ) |
| 43 |
#include <crt0.h> |
| 44 |
#endif /* __DJGPP__ */ |
| 45 |
|
| 46 |
#define BUFFERSIZE 480 |
| 47 |
#define SAMPLERATE 48000 |
| 48 |
|
| 49 |
static float left[BUFFERSIZE]; |
| 50 |
static float right[BUFFERSIZE]; |
| 51 |
static float * const buffers[2] = { left, right }; |
| 52 |
|
| 53 |
#if defined( __DJGPP__ ) |
| 54 |
/* clang-format off */ |
| 55 |
int _crt0_startup_flags = 0 |
| 56 |
| _CRT0_FLAG_NONMOVE_SBRK /* force interrupt compatible allocation */ |
| 57 |
| _CRT0_DISABLE_SBRK_ADDRESS_WRAP /* force NT compatible allocation */ |
| 58 |
| _CRT0_FLAG_LOCK_MEMORY /* lock all code and data at program startup */ |
| 59 |
| 0; |
| 60 |
/* clang-format on */ |
| 61 |
#endif /* __DJGPP__ */ |
| 62 |
#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) |
| 63 |
#if defined( __clang__ ) && !defined( _MSC_VER ) |
| 64 |
int wmain( int argc, wchar_t * argv[] ); |
| 65 |
#endif |
| 66 |
int wmain( int argc, wchar_t * argv[] ) { |
| 67 |
#else |
| 68 |
int main( int argc, char * argv[] ) { |
| 69 |
#endif |
| 70 |
#if defined( __DJGPP__ ) |
| 71 |
/* clang-format off */ |
| 72 |
_crt0_startup_flags &= ~_CRT0_FLAG_LOCK_MEMORY; /* disable automatic locking for all further memory allocations */ |
| 73 |
/* clang-format on */ |
| 74 |
#endif /* __DJGPP__ */ |
| 75 |
FILE * file = 0; |
| 76 |
openmpt_module * mod = 0; |
| 77 |
size_t count = 0; |
| 78 |
PaStream * stream = 0; |
| 79 |
(void)argc; |
| 80 |
#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) |
| 81 |
file = _wfopen( argv[1], L"rb" ); |
| 82 |
#else |
| 83 |
file = fopen( argv[1], "rb" ); |
| 84 |
#endif |
| 85 |
mod = openmpt_module_create2( openmpt_stream_get_file_callbacks2(), file, NULL, NULL, NULL, NULL, NULL, NULL, NULL ); |
| 86 |
#if defined( __clang__ ) && defined( __clang_analyzer__ ) |
| 87 |
[[clang::suppress]] |
| 88 |
#endif |
| 89 |
fclose( file ); |
| 90 |
Pa_Initialize(); |
| 91 |
Pa_OpenDefaultStream( &stream, 0, 2, paFloat32 | paNonInterleaved, SAMPLERATE, paFramesPerBufferUnspecified, NULL, NULL ); |
| 92 |
Pa_StartStream( stream ); |
| 93 |
while ( 1 ) { |
| 94 |
count = openmpt_module_read_float_stereo( mod, SAMPLERATE, BUFFERSIZE, left, right ); |
| 95 |
if ( count == 0 ) { |
| 96 |
break; |
| 97 |
} |
| 98 |
Pa_WriteStream( stream, buffers, (unsigned long)count ); |
| 99 |
} |
| 100 |
Pa_StopStream( stream ); |
| 101 |
Pa_CloseStream( stream ); |
| 102 |
Pa_Terminate(); |
| 103 |
openmpt_module_destroy( mod ); |
| 104 |
return 0; |
| 105 |
} |