| 1 |
/* |
| 2 |
* libopenmpt_example_cxx.cpp |
| 3 |
* -------------------------- |
| 4 |
* Purpose: libopenmpt C++ API example |
| 5 |
* Notes : PortAudio C++ 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_cxx SOMEMODULE |
| 12 |
*/ |
| 13 |
|
| 14 |
#if defined( __MINGW32__ ) && !defined( __MINGW64__ ) |
| 15 |
#include <sys/types.h> |
| 16 |
#endif |
| 17 |
|
| 18 |
#include <exception> |
| 19 |
#include <fstream> |
| 20 |
#include <iostream> |
| 21 |
#include <new> |
| 22 |
#include <stdexcept> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
#include <libopenmpt/libopenmpt.hpp> |
| 26 |
|
| 27 |
#if defined( __clang__ ) |
| 28 |
#if ( ( __clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__ ) >= 40000 ) |
| 29 |
#pragma clang diagnostic push |
| 30 |
#pragma clang diagnostic ignored "-Wdeprecated-dynamic-exception-spec" |
| 31 |
#endif |
| 32 |
#elif defined( __GNUC__ ) && !defined( __clang__ ) && !defined( _MSC_VER ) |
| 33 |
#pragma GCC diagnostic push |
| 34 |
#pragma GCC diagnostic ignored "-Wsuggest-override" |
| 35 |
#endif |
| 36 |
#include <portaudiocpp/PortAudioCpp.hxx> |
| 37 |
#if defined( __clang__ ) |
| 38 |
#if ( ( __clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__ ) >= 40000 ) |
| 39 |
#pragma clang diagnostic pop |
| 40 |
#endif |
| 41 |
#elif defined( __GNUC__ ) && !defined( __clang__ ) && !defined( _MSC_VER ) |
| 42 |
#pragma GCC diagnostic pop |
| 43 |
#endif |
| 44 |
|
| 45 |
#if defined( __DJGPP__ ) |
| 46 |
#include <crt0.h> |
| 47 |
#endif /* __DJGPP__ */ |
| 48 |
|
| 49 |
#if defined( __DJGPP__ ) |
| 50 |
/* clang-format off */ |
| 51 |
extern "C" int _crt0_startup_flags = 0 |
| 52 |
| _CRT0_FLAG_NONMOVE_SBRK /* force interrupt compatible allocation */ |
| 53 |
| _CRT0_DISABLE_SBRK_ADDRESS_WRAP /* force NT compatible allocation */ |
| 54 |
| _CRT0_FLAG_LOCK_MEMORY /* lock all code and data at program startup */ |
| 55 |
| 0; |
| 56 |
/* clang-format on */ |
| 57 |
#endif /* __DJGPP__ */ |
| 58 |
#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) |
| 59 |
#if defined( __GNUC__ ) || ( defined( __clang__ ) && !defined( _MSC_VER ) ) |
| 60 |
// mingw-w64 g++ does only default to special C linkage for "main", but not for "wmain" (see <https://sourceforge.net/p/mingw-w64/wiki2/Unicode%20apps/>). |
| 61 |
extern "C" int wmain( int /*argc*/, wchar_t * /*argv*/[] ); |
| 62 |
extern "C" int wmain( int argc, wchar_t * argv[] ) { |
| 63 |
#else |
| 64 |
int wmain( int argc, wchar_t * argv[] ) { |
| 65 |
#endif |
| 66 |
#else |
| 67 |
int main( int argc, char * argv[] ) { |
| 68 |
#endif |
| 69 |
#if defined( __DJGPP__ ) |
| 70 |
/* clang-format off */ |
| 71 |
_crt0_startup_flags &= ~_CRT0_FLAG_LOCK_MEMORY; /* disable automatic locking for all further memory allocations */ |
| 72 |
/* clang-format on */ |
| 73 |
#endif /* __DJGPP__ */ |
| 74 |
try { |
| 75 |
if ( argc != 2 ) { |
| 76 |
throw std::runtime_error( "Usage: libopenmpt_example_cxx SOMEMODULE" ); |
| 77 |
} |
| 78 |
constexpr std::size_t buffersize = 480; |
| 79 |
constexpr std::int32_t samplerate = 48000; |
| 80 |
std::ifstream file( argv[1], std::ios::binary ); |
| 81 |
openmpt::module mod( file ); |
| 82 |
portaudio::AutoSystem portaudio_initializer; |
| 83 |
portaudio::System & portaudio = portaudio::System::instance(); |
| 84 |
std::vector<float> left( buffersize ); |
| 85 |
std::vector<float> right( buffersize ); |
| 86 |
std::vector<float> interleaved_buffer( buffersize * 2 ); |
| 87 |
bool is_interleaved = false; |
| 88 |
#if defined( _MSC_VER ) && defined( _PREFAST_ ) |
| 89 |
// work-around bug in VS2019 MSVC 16.5.5 static analyzer |
| 90 |
is_interleaved = false; |
| 91 |
portaudio::DirectionSpecificStreamParameters outputstream_parameters( portaudio.defaultOutputDevice(), 2, portaudio::FLOAT32, false, portaudio.defaultOutputDevice().defaultHighOutputLatency(), 0 ); |
| 92 |
portaudio::StreamParameters stream_parameters( portaudio::DirectionSpecificStreamParameters::null(), outputstream_parameters, samplerate, paFramesPerBufferUnspecified, paNoFlag ); |
| 93 |
portaudio::BlockingStream stream( stream_parameters ); |
| 94 |
#else |
| 95 |
portaudio::BlockingStream stream = [&]() |
| 96 |
{ |
| 97 |
try { |
| 98 |
is_interleaved = false; |
| 99 |
portaudio::DirectionSpecificStreamParameters outputstream_parameters( portaudio.defaultOutputDevice(), 2, portaudio::FLOAT32, false, portaudio.defaultOutputDevice().defaultHighOutputLatency(), 0 ); |
| 100 |
portaudio::StreamParameters stream_parameters( portaudio::DirectionSpecificStreamParameters::null(), outputstream_parameters, samplerate, paFramesPerBufferUnspecified, paNoFlag ); |
| 101 |
return portaudio::BlockingStream( stream_parameters ); |
| 102 |
} catch ( const portaudio::PaException & e ) { |
| 103 |
if ( e.paError() != paSampleFormatNotSupported ) { |
| 104 |
throw; |
| 105 |
} |
| 106 |
is_interleaved = true; |
| 107 |
portaudio::DirectionSpecificStreamParameters outputstream_parameters( portaudio.defaultOutputDevice(), 2, portaudio::FLOAT32, true, portaudio.defaultOutputDevice().defaultHighOutputLatency(), 0 ); |
| 108 |
portaudio::StreamParameters stream_parameters( portaudio::DirectionSpecificStreamParameters::null(), outputstream_parameters, samplerate, paFramesPerBufferUnspecified, paNoFlag ); |
| 109 |
return portaudio::BlockingStream( stream_parameters ); |
| 110 |
} |
| 111 |
}(); |
| 112 |
#endif |
| 113 |
stream.start(); |
| 114 |
while ( true ) { |
| 115 |
std::size_t count = is_interleaved ? mod.read_interleaved_stereo( samplerate, buffersize, interleaved_buffer.data() ) : mod.read( samplerate, buffersize, left.data(), right.data() ); |
| 116 |
if ( count == 0 ) { |
| 117 |
break; |
| 118 |
} |
| 119 |
try { |
| 120 |
if ( is_interleaved ) { |
| 121 |
stream.write( interleaved_buffer.data(), static_cast<unsigned long>( count ) ); |
| 122 |
} else { |
| 123 |
const float * const buffers[2] = { left.data(), right.data() }; |
| 124 |
stream.write( buffers, static_cast<unsigned long>( count ) ); |
| 125 |
} |
| 126 |
} catch ( const portaudio::PaException & pa_exception ) { |
| 127 |
if ( pa_exception.paError() != paOutputUnderflowed ) { |
| 128 |
throw; |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
stream.stop(); |
| 133 |
} catch ( const std::bad_alloc & ) { |
| 134 |
std::cerr << "Error: " << std::string( "out of memory" ) << std::endl; |
| 135 |
return 1; |
| 136 |
} catch ( const std::exception & e ) { |
| 137 |
std::cerr << "Error: " << std::string( e.what() ? e.what() : "unknown error" ) << std::endl; |
| 138 |
return 1; |
| 139 |
} |
| 140 |
return 0; |
| 141 |
} |