diff options
author | Jean-Francois Mauguit <jfmauguit@mac.com> | 2024-09-24 09:03:25 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-24 09:03:25 -0400 |
commit | bab614c421ed7ae329d26bf028c4a3b1d2450f5a (patch) | |
tree | 12f17f78986871dd2cfb0a56e5e93b545c1ae0d0 /Src/external_dependencies/openmpt-trunk/examples | |
parent | 4bde6044fddf053f31795b9eaccdd2a5a527d21f (diff) | |
parent | 20d28e80a5c861a9d5f449ea911ab75b4f37ad0d (diff) | |
download | winamp-bab614c421ed7ae329d26bf028c4a3b1d2450f5a.tar.gz |
Merge pull request #5 from WinampDesktop/community
Merge to main
Diffstat (limited to 'Src/external_dependencies/openmpt-trunk/examples')
8 files changed, 1384 insertions, 0 deletions
diff --git a/Src/external_dependencies/openmpt-trunk/examples/.clang-format b/Src/external_dependencies/openmpt-trunk/examples/.clang-format new file mode 100644 index 00000000..ca83758a --- /dev/null +++ b/Src/external_dependencies/openmpt-trunk/examples/.clang-format @@ -0,0 +1,169 @@ +# clang-format 14 + +Language: Cpp +Standard: c++20 + +AccessModifierOffset: -2 #? +AlignAfterOpenBracket: AlwaysBreak +AlignArrayOfStructures: Left +AlignConsecutiveAssignments: false +AlignConsecutiveBitFields: false +AlignConsecutiveDeclarations: false +AlignConsecutiveMacros: true +AlignEscapedNewlines: DontAlign +AlignOperands: AlignAfterOperator +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortEnumsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: false +AllowShortLambdasOnASingleLine: Inline +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: Yes +AttributeMacros: [] +BinPackArguments: true +BinPackParameters: false +BitFieldColonSpacing: Both +BraceWrapping: + AfterCaseLabel: true + AfterClass: false + AfterControlStatement: MultiLine + AfterEnum: false + AfterFunction: false + AfterNamespace: false + #AfterObjCDeclaration + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: true + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: false + SplitEmptyNamespace: true +#BreakAfterJavaFieldAnnotations +BreakBeforeBinaryOperators: NonAssignment +BreakBeforeBraces: Custom +BreakBeforeConceptDeclarations: true +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeComma +BreakInheritanceList: BeforeComma +BreakStringLiterals: false +ColumnLimit: 0 +CommentPragmas: '' #? +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 2 #? +ContinuationIndentWidth: 2 #? +Cpp11BracedListStyle: true +DeriveLineEnding: true +DerivePointerAlignment: false +EmptyLineAfterAccessModifier: Leave +EmptyLineBeforeAccessModifier: Leave +FixNamespaceComments: true +ForEachMacros: [] +IfMacros: ['MPT_MAYBE_CONSTANT_IF'] +IncludeBlocks: Preserve +IncludeCategories: [] #? +IncludeIsMainRegex: '' #? +IncludeIsMainSourceRegex: '' #? +IndentAccessModifiers: false +IndentCaseBlocks: true +IndentCaseLabels: true +IndentExternBlock: NoIndent +IndentGotoLabels: false +IndentPPDirectives: None +#IndentRequiresClause: true +#BeforeHash +IndentWidth: 2 +IndentWrappedFunctionNames: true +InsertTrailingCommas: None +#JavaImportGroups +#JavaScriptQuotes +#JavaScriptWrapImports +KeepEmptyLinesAtTheStartOfBlocks: true +LambdaBodyIndentation: OuterScope +MacroBlockBegin: '' #? +MacroBlockEnd: '' #? +MaxEmptyLinesToKeep: 3 +NamespaceIndentation: None +NamespaceMacros: [] #? +#ObjCBinPackProtocolList +#ObjCBlockIndentWidth +#ObjCBreakBeforeNestedBlockParam +#ObjCSpaceAfterProperty +#ObjCSpaceBeforeProtocolList +PackConstructorInitializers: Never +#PenaltyBreakAssignment +#PenaltyBreakBeforeFirstCallParameter +#PenaltyBreakComment +#PenaltyBreakFirstLessLess +#PenaltyBreakOpenParenthesis +#PenaltyBreakString +#PenaltyBreakTemplateDeclaration +#PenaltyExcessCharacter +#PenaltyIndentedWhitespace +#PenaltyReturnTypeOnItsOwnLine +PointerAlignment: Middle +PPIndentWidth: -1 +#RawStringFormats +QualifierAlignment: Leave +#QualifierOrder: ['static', 'inline', 'constexpr', 'volatile', 'const', 'restrict', 'type'] +ReferenceAlignment: Pointer +ReflowComments: false +RemoveBracesLLVM: false +SeparateDefinitionBlocks: Leave +ShortNamespaceLines: 1 +SortIncludes: false +#SortJavaStaticImport +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceAroundPointerQualifiers: Default +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDeclarationName: false + AfterFunctionDefinitionName: false + AfterIfMacros: true + AfterOverloadedOperator: false + #AfterRequiresInClause: false + #AfterRequiresInExpression: false + BeforeNonEmptyParentheses: false +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 2 +SpacesInAngles: false +SpacesInCStyleCastParentheses: false +SpacesInConditionalStatement: true +SpacesInContainerLiterals: true +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParentheses: true +SpacesInSquareBrackets: false +StatementAttributeLikeMacros: [] +StatementMacros: [ 'MPT_WARNING', 'MPT_TEST_GROUP_INLINE_IDENTIFIER', 'MPT_TEST_GROUP_INLINE', 'MPT_TEST_GROUP_STATIC' ] #? +TabWidth: 2 +TypenameMacros: [] #? +UseCRLF: false +UseTab: ForContinuationAndIndentation +WhitespaceSensitiveMacros: + - MPT_PP_STRINGIFY diff --git a/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_c.c b/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_c.c new file mode 100644 index 00000000..8d06ef45 --- /dev/null +++ b/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_c.c @@ -0,0 +1,213 @@ +/* + * libopenmpt_example_c.c + * ---------------------- + * Purpose: libopenmpt C API example + * Notes : PortAudio is used for sound output. + * Authors: OpenMPT Devs + * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. + */ + +/* + * Usage: libopenmpt_example_c SOMEMODULE + */ + +#include <memory.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <libopenmpt/libopenmpt.h> +#include <libopenmpt/libopenmpt_stream_callbacks_file.h> + +#include <portaudio.h> + +#define BUFFERSIZE 480 +#define SAMPLERATE 48000 + +static int16_t left[BUFFERSIZE]; +static int16_t right[BUFFERSIZE]; +static int16_t * const buffers[2] = { left, right }; +static int16_t interleaved_buffer[BUFFERSIZE * 2]; +static int is_interleaved = 0; + +static void libopenmpt_example_logfunc( const char * message, void * userdata ) { + (void)userdata; + if ( message ) { + fprintf( stderr, "openmpt: %s\n", message ); + } +} + +static int libopenmpt_example_errfunc( int error, void * userdata ) { + (void)userdata; + (void)error; + return OPENMPT_ERROR_FUNC_RESULT_DEFAULT & ~OPENMPT_ERROR_FUNC_RESULT_LOG; +} + +static void libopenmpt_example_print_error( const char * func_name, int mod_err, const char * mod_err_str ) { + if ( !func_name ) { + func_name = "unknown function"; + } + if ( mod_err == OPENMPT_ERROR_OUT_OF_MEMORY ) { + mod_err_str = openmpt_error_string( mod_err ); + if ( !mod_err_str ) { + fprintf( stderr, "Error: %s\n", "OPENMPT_ERROR_OUT_OF_MEMORY" ); + } else { + fprintf( stderr, "Error: %s\n", mod_err_str ); + openmpt_free_string( mod_err_str ); + mod_err_str = NULL; + } + } else { + if ( !mod_err_str ) { + mod_err_str = openmpt_error_string( mod_err ); + if ( !mod_err_str ) { + fprintf( stderr, "Error: %s failed.\n", func_name ); + } else { + fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str ); + } + openmpt_free_string( mod_err_str ); + mod_err_str = NULL; + } + fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str ); + } +} + +#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) +#if defined( __clang__ ) && !defined( _MSC_VER ) +int wmain( int argc, wchar_t * argv[] ); +#endif +int wmain( int argc, wchar_t * argv[] ) { +#else +int main( int argc, char * argv[] ) { +#endif + + int result = 0; + FILE * file = 0; + openmpt_module * mod = 0; + int mod_err = OPENMPT_ERROR_OK; + const char * mod_err_str = NULL; + size_t count = 0; + PaError pa_error = paNoError; + int pa_initialized = 0; + PaStream * stream = 0; + + if ( argc != 2 ) { + fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c SOMEMODULE'." ); + goto fail; + } + +#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) + if ( wcslen( argv[1] ) == 0 ) { + fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c SOMEMODULE'." ); + goto fail; + } + file = _wfopen( argv[1], L"rb" ); +#else + if ( strlen( argv[1] ) == 0 ) { + fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c SOMEMODULE'." ); + goto fail; + } + file = fopen( argv[1], "rb" ); +#endif + if ( !file ) { + fprintf( stderr, "Error: %s\n", "fopen() failed." ); + goto fail; + } + + mod = openmpt_module_create2( openmpt_stream_get_file_callbacks(), file, &libopenmpt_example_logfunc, NULL, &libopenmpt_example_errfunc, NULL, &mod_err, &mod_err_str, NULL ); + if ( !mod ) { + libopenmpt_example_print_error( "openmpt_module_create2()", mod_err, mod_err_str ); + openmpt_free_string( mod_err_str ); + mod_err_str = NULL; + goto fail; + } + openmpt_module_set_error_func( mod, NULL, NULL ); + + pa_error = Pa_Initialize(); + if ( pa_error != paNoError ) { + fprintf( stderr, "Error: %s\n", "Pa_Initialize() failed." ); + goto fail; + } + pa_initialized = 1; + + pa_error = Pa_OpenDefaultStream( &stream, 0, 2, paInt16 | paNonInterleaved, SAMPLERATE, paFramesPerBufferUnspecified, NULL, NULL ); + if ( pa_error == paSampleFormatNotSupported ) { + is_interleaved = 1; + pa_error = Pa_OpenDefaultStream( &stream, 0, 2, paInt16, SAMPLERATE, paFramesPerBufferUnspecified, NULL, NULL ); + } + if ( pa_error != paNoError ) { + fprintf( stderr, "Error: %s\n", "Pa_OpenStream() failed." ); + goto fail; + } + if ( !stream ) { + fprintf( stderr, "Error: %s\n", "Pa_OpenStream() failed." ); + goto fail; + } + + pa_error = Pa_StartStream( stream ); + if ( pa_error != paNoError ) { + fprintf( stderr, "Error: %s\n", "Pa_StartStream() failed." ); + goto fail; + } + + while ( 1 ) { + + openmpt_module_error_clear( mod ); + count = is_interleaved ? openmpt_module_read_interleaved_stereo( mod, SAMPLERATE, BUFFERSIZE, interleaved_buffer ) : openmpt_module_read_stereo( mod, SAMPLERATE, BUFFERSIZE, left, right ); + mod_err = openmpt_module_error_get_last( mod ); + mod_err_str = openmpt_module_error_get_last_message( mod ); + if ( mod_err != OPENMPT_ERROR_OK ) { + libopenmpt_example_print_error( "openmpt_module_read_stereo()", mod_err, mod_err_str ); + openmpt_free_string( mod_err_str ); + mod_err_str = NULL; + } + if ( count == 0 ) { + break; + } + + pa_error = is_interleaved ? Pa_WriteStream( stream, interleaved_buffer, (unsigned long)count ) : Pa_WriteStream( stream, buffers, (unsigned long)count ); + if ( pa_error == paOutputUnderflowed ) { + pa_error = paNoError; + } + if ( pa_error != paNoError ) { + fprintf( stderr, "Error: %s\n", "Pa_WriteStream() failed." ); + goto fail; + } + } + + result = 0; + + goto cleanup; + +fail: + + result = 1; + +cleanup: + + if ( stream ) { + if ( Pa_IsStreamActive( stream ) == 1 ) { + Pa_StopStream( stream ); + } + Pa_CloseStream( stream ); + stream = 0; + } + + if ( pa_initialized ) { + Pa_Terminate(); + pa_initialized = 0; + (void)pa_initialized; + } + + if ( mod ) { + openmpt_module_destroy( mod ); + mod = 0; + } + + if ( file ) { + fclose( file ); + file = 0; + } + + return result; +} diff --git a/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_c_mem.c b/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_c_mem.c new file mode 100644 index 00000000..6fcd17a9 --- /dev/null +++ b/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_c_mem.c @@ -0,0 +1,303 @@ +/* + * libopenmpt_example_c_mem.c + * -------------------------- + * Purpose: libopenmpt C API example + * Notes : PortAudio is used for sound output. + * Authors: OpenMPT Devs + * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. + */ + +/* + * Usage: libopenmpt_example_c_mem SOMEMODULE + */ + +#include <memory.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <libopenmpt/libopenmpt.h> + +#include <portaudio.h> + +#define BUFFERSIZE 480 +#define SAMPLERATE 48000 + +static int16_t left[BUFFERSIZE]; +static int16_t right[BUFFERSIZE]; +static int16_t * const buffers[2] = { left, right }; +static int16_t interleaved_buffer[BUFFERSIZE * 2]; +static int is_interleaved = 0; + +static void libopenmpt_example_logfunc( const char * message, void * userdata ) { + (void)userdata; + if ( message ) { + fprintf( stderr, "openmpt: %s\n", message ); + } +} + +static int libopenmpt_example_errfunc( int error, void * userdata ) { + (void)userdata; + (void)error; + return OPENMPT_ERROR_FUNC_RESULT_DEFAULT & ~OPENMPT_ERROR_FUNC_RESULT_LOG; +} + +static void libopenmpt_example_print_error( const char * func_name, int mod_err, const char * mod_err_str ) { + if ( !func_name ) { + func_name = "unknown function"; + } + if ( mod_err == OPENMPT_ERROR_OUT_OF_MEMORY ) { + mod_err_str = openmpt_error_string( mod_err ); + if ( !mod_err_str ) { + fprintf( stderr, "Error: %s\n", "OPENMPT_ERROR_OUT_OF_MEMORY" ); + } else { + fprintf( stderr, "Error: %s\n", mod_err_str ); + openmpt_free_string( mod_err_str ); + mod_err_str = NULL; + } + } else { + if ( !mod_err_str ) { + mod_err_str = openmpt_error_string( mod_err ); + if ( !mod_err_str ) { + fprintf( stderr, "Error: %s failed.\n", func_name ); + } else { + fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str ); + } + openmpt_free_string( mod_err_str ); + mod_err_str = NULL; + } + fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str ); + } +} + +typedef struct blob_t { + size_t size; + void * data; +} blob_t; + +static void free_blob( blob_t * blob ) { + if ( blob ) { + if ( blob->data ) { + free( blob->data ); + blob->data = 0; + } + blob->size = 0; + free( blob ); + } +} + +#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) +static blob_t * load_file( const wchar_t * filename ) { +#else +static blob_t * load_file( const char * filename ) { +#endif + blob_t * result = 0; + + blob_t * blob = 0; + FILE * file = 0; + long tell_result = 0; + + blob = malloc( sizeof( blob_t ) ); + if ( !blob ) { + goto fail; + } + memset( blob, 0, sizeof( blob_t ) ); + +#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) + file = _wfopen( filename, L"rb" ); +#else + file = fopen( filename, "rb" ); +#endif + if ( !file ) { + goto fail; + } + + if ( fseek( file, 0, SEEK_END ) != 0 ) { + goto fail; + } + + tell_result = ftell( file ); + if ( tell_result < 0 ) { + goto fail; + } + if ( (unsigned long)(size_t)(unsigned long)tell_result != (unsigned long)tell_result ) { + goto fail; + } + blob->size = (size_t)tell_result; + + if ( fseek( file, 0, SEEK_SET ) != 0 ) { + goto fail; + } + + blob->data = malloc( blob->size ); + if ( !blob->data ) { + goto fail; + } + memset( blob->data, 0, blob->size ); + + if ( fread( blob->data, 1, blob->size, file ) != blob->size ) { + goto fail; + } + + result = blob; + blob = 0; + goto cleanup; + +fail: + + result = 0; + +cleanup: + + if ( blob ) { + free_blob( blob ); + blob = 0; + } + + if ( file ) { + fclose( file ); + file = 0; + } + + return result; +} + +#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) +#if defined( __clang__ ) && !defined( _MSC_VER ) +int wmain( int argc, wchar_t * argv[] ); +#endif +int wmain( int argc, wchar_t * argv[] ) { +#else +int main( int argc, char * argv[] ) { +#endif + + int result = 0; + blob_t * blob = 0; + openmpt_module * mod = 0; + int mod_err = OPENMPT_ERROR_OK; + const char * mod_err_str = NULL; + size_t count = 0; + PaError pa_error = paNoError; + int pa_initialized = 0; + PaStream * stream = 0; + + if ( argc != 2 ) { + fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_mem SOMEMODULE'." ); + goto fail; + } + +#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) + if ( wcslen( argv[1] ) == 0 ) { + fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_mem SOMEMODULE'." ); + goto fail; + } +#else + if ( strlen( argv[1] ) == 0 ) { + fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_mem SOMEMODULE'." ); + goto fail; + } +#endif + blob = load_file( argv[1] ); + if ( !blob ) { + fprintf( stderr, "Error: %s\n", "load_file() failed." ); + goto fail; + } + + mod = openmpt_module_create_from_memory2( blob->data, blob->size, &libopenmpt_example_logfunc, NULL, &libopenmpt_example_errfunc, NULL, &mod_err, &mod_err_str, NULL ); + if ( !mod ) { + libopenmpt_example_print_error( "openmpt_module_create_from_memory2()", mod_err, mod_err_str ); + openmpt_free_string( mod_err_str ); + mod_err_str = NULL; + goto fail; + } + + pa_error = Pa_Initialize(); + if ( pa_error != paNoError ) { + fprintf( stderr, "Error: %s\n", "Pa_Initialize() failed." ); + goto fail; + } + pa_initialized = 1; + + is_interleaved = 0; + pa_error = Pa_OpenDefaultStream( &stream, 0, 2, paInt16 | paNonInterleaved, SAMPLERATE, paFramesPerBufferUnspecified, NULL, NULL ); + if ( pa_error == paSampleFormatNotSupported ) { + is_interleaved = 1; + pa_error = Pa_OpenDefaultStream( &stream, 0, 2, paInt16, SAMPLERATE, paFramesPerBufferUnspecified, NULL, NULL ); + } + if ( pa_error != paNoError ) { + fprintf( stderr, "Error: %s\n", "Pa_OpenStream() failed." ); + goto fail; + } + if ( !stream ) { + fprintf( stderr, "Error: %s\n", "Pa_OpenStream() failed." ); + goto fail; + } + + pa_error = Pa_StartStream( stream ); + if ( pa_error != paNoError ) { + fprintf( stderr, "Error: %s\n", "Pa_StartStream() failed." ); + goto fail; + } + + while ( 1 ) { + + openmpt_module_error_clear( mod ); + count = is_interleaved ? openmpt_module_read_interleaved_stereo( mod, SAMPLERATE, BUFFERSIZE, interleaved_buffer ) : openmpt_module_read_stereo( mod, SAMPLERATE, BUFFERSIZE, left, right ); + mod_err = openmpt_module_error_get_last( mod ); + mod_err_str = openmpt_module_error_get_last_message( mod ); + if ( mod_err != OPENMPT_ERROR_OK ) { + libopenmpt_example_print_error( "openmpt_module_read_stereo()", mod_err, mod_err_str ); + openmpt_free_string( mod_err_str ); + mod_err_str = NULL; + } + if ( count == 0 ) { + break; + } + + pa_error = is_interleaved ? Pa_WriteStream( stream, interleaved_buffer, (unsigned long)count ) : Pa_WriteStream( stream, buffers, (unsigned long)count ); + if ( pa_error == paOutputUnderflowed ) { + pa_error = paNoError; + } + if ( pa_error != paNoError ) { + fprintf( stderr, "Error: %s\n", "Pa_WriteStream() failed." ); + goto fail; + } + } + + result = 0; + + goto cleanup; + +fail: + + result = 1; + +cleanup: + + if ( stream ) { + if ( Pa_IsStreamActive( stream ) == 1 ) { + Pa_StopStream( stream ); + } + Pa_CloseStream( stream ); + stream = 0; + } + + if ( pa_initialized ) { + Pa_Terminate(); + pa_initialized = 0; + (void)pa_initialized; + } + + if ( mod ) { + openmpt_module_destroy( mod ); + mod = 0; + } + + if ( blob ) { + free_blob( blob ); + blob = 0; + } + + return result; +} diff --git a/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_c_pipe.c b/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_c_pipe.c new file mode 100644 index 00000000..c444893e --- /dev/null +++ b/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_c_pipe.c @@ -0,0 +1,155 @@ +/* + * libopenmpt_example_c_pipe.c + * --------------------------- + * Purpose: libopenmpt C API simple pipe example + * Notes : This example writes raw 48000Hz / stereo / 16bit native endian PCM data to stdout. + * Authors: OpenMPT Devs + * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. + */ + +/* + * Usage: cat SOMEMODULE | libopenmpt_example_c_pipe | aplay --file-type raw --format=dat + */ + +#include <memory.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include <errno.h> +#include <unistd.h> + +#include <libopenmpt/libopenmpt.h> +#include <libopenmpt/libopenmpt_stream_callbacks_fd.h> + +#define BUFFERSIZE 480 +#define SAMPLERATE 48000 + +static void libopenmpt_example_logfunc( const char * message, void * userdata ) { + (void)userdata; + if ( message ) { + fprintf( stderr, "openmpt: %s\n", message ); + } +} + +static int libopenmpt_example_errfunc( int error, void * userdata ) { + (void)userdata; + (void)error; + return OPENMPT_ERROR_FUNC_RESULT_DEFAULT & ~OPENMPT_ERROR_FUNC_RESULT_LOG; +} + +static void libopenmpt_example_print_error( const char * func_name, int mod_err, const char * mod_err_str ) { + if ( !func_name ) { + func_name = "unknown function"; + } + if ( mod_err == OPENMPT_ERROR_OUT_OF_MEMORY ) { + mod_err_str = openmpt_error_string( mod_err ); + if ( !mod_err_str ) { + fprintf( stderr, "Error: %s\n", "OPENMPT_ERROR_OUT_OF_MEMORY" ); + } else { + fprintf( stderr, "Error: %s\n", mod_err_str ); + openmpt_free_string( mod_err_str ); + mod_err_str = NULL; + } + } else { + if ( !mod_err_str ) { + mod_err_str = openmpt_error_string( mod_err ); + if ( !mod_err_str ) { + fprintf( stderr, "Error: %s failed.\n", func_name ); + } else { + fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str ); + } + openmpt_free_string( mod_err_str ); + mod_err_str = NULL; + } + fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str ); + } +} + +static ssize_t xwrite( int fd, const void * buffer, size_t size ) { + size_t written = 0; + ssize_t retval = 0; + while ( written < size ) { + retval = write( fd, (const char *)buffer + written, size - written ); + if ( retval < 0 ) { + if ( errno != EINTR ) { + break; + } + retval = 0; + } + written += retval; + } + return written; +} + +static int16_t buffer[BUFFERSIZE * 2]; + +#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) +#if defined( __clang__ ) && !defined( _MSC_VER ) +int wmain( int argc, wchar_t * argv[] ); +#endif +int wmain( int argc, wchar_t * argv[] ) { +#else +int main( int argc, char * argv[] ) { +#endif + + int result = 0; + openmpt_module * mod = 0; + int mod_err = OPENMPT_ERROR_OK; + const char * mod_err_str = NULL; + size_t count = 0; + size_t written = 0; + (void)argv; + + if ( argc != 1 ) { + fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_pipe' and connect stdin and stdout." ); + goto fail; + } + + mod = openmpt_module_create2( openmpt_stream_get_fd_callbacks(), (void *)(uintptr_t)STDIN_FILENO, &libopenmpt_example_logfunc, NULL, &libopenmpt_example_errfunc, NULL, &mod_err, &mod_err_str, NULL ); + if ( !mod ) { + libopenmpt_example_print_error( "openmpt_module_create2()", mod_err, mod_err_str ); + openmpt_free_string( mod_err_str ); + mod_err_str = NULL; + goto fail; + } + + while ( 1 ) { + + openmpt_module_error_clear( mod ); + count = openmpt_module_read_interleaved_stereo( mod, SAMPLERATE, BUFFERSIZE, buffer ); + mod_err = openmpt_module_error_get_last( mod ); + mod_err_str = openmpt_module_error_get_last_message( mod ); + if ( mod_err != OPENMPT_ERROR_OK ) { + libopenmpt_example_print_error( "openmpt_module_read_interleaved_stereo()", mod_err, mod_err_str ); + openmpt_free_string( mod_err_str ); + mod_err_str = NULL; + } + if ( count == 0 ) { + break; + } + + written = xwrite( STDOUT_FILENO, buffer, count * 2 * sizeof( int16_t ) ); + if ( written == 0 ) { + fprintf( stderr, "Error: %s\n", "write() failed." ); + goto fail; + } + } + + result = 0; + + goto cleanup; + +fail: + + result = 1; + +cleanup: + + if ( mod ) { + openmpt_module_destroy( mod ); + mod = 0; + } + + return result; +} diff --git a/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_c_probe.c b/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_c_probe.c new file mode 100644 index 00000000..058a7e1e --- /dev/null +++ b/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_c_probe.c @@ -0,0 +1,183 @@ +/* + * libopenmpt_example_c_probe.c + * ---------------------------- + * Purpose: libopenmpt C API probing example + * Notes : (currently none) + * Authors: OpenMPT Devs + * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. + */ + +/* + * Usage: libopenmpt_example_c_probe SOMEMODULE ... + * Returns 0 on successful probing for all files. + * Returns 1 on failed probing for 1 or more files. + * Returns 2 on error. + */ + +#define LIBOPENMPT_EXAMPLE_PROBE_RESULT_BINARY 1 +#define LIBOPENMPT_EXAMPLE_PROBE_RESULT_FLOAT 2 + +#define LIBOPENMPT_EXAMPLE_PROBE_RESULT LIBOPENMPT_EXAMPLE_PROBE_RESULT_BINARY + +#include <memory.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <libopenmpt/libopenmpt.h> +#include <libopenmpt/libopenmpt_stream_callbacks_file.h> + +static void libopenmpt_example_logfunc( const char * message, void * userdata ) { + (void)userdata; + + if ( message ) { + fprintf( stderr, "%s\n", message ); + } +} + +#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) +static int probe_file( const wchar_t * filename ) { +#else +static int probe_file( const char * filename ) { +#endif + + int result = 0; + int mod_err = OPENMPT_ERROR_OK; + FILE * file = NULL; + +#if ( LIBOPENMPT_EXAMPLE_PROBE_RESULT == LIBOPENMPT_EXAMPLE_PROBE_RESULT_BINARY ) + int result_binary = 0; + int probe_file_header_result = OPENMPT_PROBE_FILE_HEADER_RESULT_FAILURE; + const char * probe_file_header_result_str = NULL; +#endif +#if ( LIBOPENMPT_EXAMPLE_PROBE_RESULT == LIBOPENMPT_EXAMPLE_PROBE_RESULT_FLOAT ) + double probability = 0.0; +#endif + +#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) + if ( wcslen( filename ) == 0 ) { + fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_probe SOMEMODULE'." ); + goto fail; + } +#else + if ( strlen( filename ) == 0 ) { + fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_probe SOMEMODULE'." ); + goto fail; + } +#endif + +#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) + file = _wfopen( filename, L"rb" ); +#else + file = fopen( filename, "rb" ); +#endif + if ( !file ) { + fprintf( stderr, "Error: %s\n", "fopen() failed." ); + goto fail; + } + +#if ( LIBOPENMPT_EXAMPLE_PROBE_RESULT == LIBOPENMPT_EXAMPLE_PROBE_RESULT_BINARY ) + probe_file_header_result = openmpt_probe_file_header_from_stream( OPENMPT_PROBE_FILE_HEADER_FLAGS_DEFAULT, openmpt_stream_get_file_callbacks(), file, &libopenmpt_example_logfunc, NULL, &openmpt_error_func_default, NULL, &mod_err, NULL ); + probe_file_header_result_str = NULL; + switch ( probe_file_header_result ) { + case OPENMPT_PROBE_FILE_HEADER_RESULT_SUCCESS: + probe_file_header_result_str = "Success "; + result_binary = 1; + break; + case OPENMPT_PROBE_FILE_HEADER_RESULT_FAILURE: + probe_file_header_result_str = "Failure "; + result_binary = 0; + break; + case OPENMPT_PROBE_FILE_HEADER_RESULT_WANTMOREDATA: + probe_file_header_result_str = "WantMoreData"; + result_binary = 0; + break; + case OPENMPT_PROBE_FILE_HEADER_RESULT_ERROR: + result_binary = 0; + (void)result_binary; + fprintf( stderr, "Error: %s\n", "openmpt_probe_file_header() failed." ); + goto fail; + break; + default: + result_binary = 0; + (void)result_binary; + fprintf( stderr, "Error: %s\n", "openmpt_probe_file_header() failed." ); + goto fail; + break; + } +#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) + fprintf( stdout, "%s - %ls\n", probe_file_header_result_str, filename ); +#else + fprintf( stdout, "%s - %s\n", probe_file_header_result_str, filename ); +#endif + if ( result_binary ) { + result = 0; + } else { + result = 1; + } +#elif ( LIBOPENMPT_EXAMPLE_PROBE_RESULT == LIBOPENMPT_EXAMPLE_PROBE_RESULT_FLOAT ) + probability = openmpt_could_open_probability2( openmpt_stream_get_file_callbacks(), file, 0.25, &libopenmpt_example_logfunc, NULL, &openmpt_error_func_default, NULL, &mod_err, NULL ); +#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) + fprintf( stdout, "%s: %f - %ls\n", "Result", probability, filename ); +#else + fprintf( stdout, "%s: %f - %s\n", "Result", probability, filename ); +#endif + if ( probability >= 0.5 ) { + result = 0; + } else { + result = 1; + } +#else +#error "LIBOPENMPT_EXAMPLE_PROBE_RESULT is wrong" +#endif + + goto cleanup; + +fail: + + result = 2; + +cleanup: + + if ( file ) { + fclose( file ); + file = 0; + } + + return result; +} + +#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) +#if defined( __clang__ ) && !defined( _MSC_VER ) +int wmain( int argc, wchar_t * argv[] ); +#endif +int wmain( int argc, wchar_t * argv[] ) { +#else +int main( int argc, char * argv[] ) { +#endif + + int global_result = 0; + + if ( argc <= 1 ) { + fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_probe SOMEMODULE ...'." ); + goto fail; + } + + for ( int i = 1; i < argc; ++i ) { + int result = probe_file( argv[i] ); + if ( result > global_result ) { + global_result = result; + } + } + + goto cleanup; + +fail: + + global_result = 2; + +cleanup: + + return global_result; +} diff --git a/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_c_stdout.c b/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_c_stdout.c new file mode 100644 index 00000000..664032fd --- /dev/null +++ b/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_c_stdout.c @@ -0,0 +1,179 @@ +/* + * libopenmpt_example_c_stdout.c + * ----------------------------- + * Purpose: libopenmpt C API simple example + * Notes : This example writes raw 48000Hz / stereo / 16bit native endian PCM data to stdout. + * Authors: OpenMPT Devs + * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. + */ + +/* + * Usage: libopenmpt_example_c_stdout SOMEMODULE | aplay --file-type raw --format=dat + */ + +#include <memory.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <errno.h> +#include <unistd.h> + +#include <libopenmpt/libopenmpt.h> +#include <libopenmpt/libopenmpt_stream_callbacks_file.h> + +#define BUFFERSIZE 480 +#define SAMPLERATE 48000 + +static void libopenmpt_example_logfunc( const char * message, void * userdata ) { + (void)userdata; + if ( message ) { + fprintf( stderr, "openmpt: %s\n", message ); + } +} + +static int libopenmpt_example_errfunc( int error, void * userdata ) { + (void)userdata; + (void)error; + return OPENMPT_ERROR_FUNC_RESULT_DEFAULT & ~OPENMPT_ERROR_FUNC_RESULT_LOG; +} + +static void libopenmpt_example_print_error( const char * func_name, int mod_err, const char * mod_err_str ) { + if ( !func_name ) { + func_name = "unknown function"; + } + if ( mod_err == OPENMPT_ERROR_OUT_OF_MEMORY ) { + mod_err_str = openmpt_error_string( mod_err ); + if ( !mod_err_str ) { + fprintf( stderr, "Error: %s\n", "OPENMPT_ERROR_OUT_OF_MEMORY" ); + } else { + fprintf( stderr, "Error: %s\n", mod_err_str ); + openmpt_free_string( mod_err_str ); + mod_err_str = NULL; + } + } else { + if ( !mod_err_str ) { + mod_err_str = openmpt_error_string( mod_err ); + if ( !mod_err_str ) { + fprintf( stderr, "Error: %s failed.\n", func_name ); + } else { + fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str ); + } + openmpt_free_string( mod_err_str ); + mod_err_str = NULL; + } + fprintf( stderr, "Error: %s failed: %s\n", func_name, mod_err_str ); + } +} + +static ssize_t xwrite( int fd, const void * buffer, size_t size ) { + size_t written = 0; + ssize_t retval = 0; + while ( written < size ) { + retval = write( fd, (const char *)buffer + written, size - written ); + if ( retval < 0 ) { + if ( errno != EINTR ) { + break; + } + retval = 0; + } + written += retval; + } + return written; +} + +static int16_t buffer[BUFFERSIZE * 2]; + +#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) +#if defined( __clang__ ) && !defined( _MSC_VER ) +int wmain( int argc, wchar_t * argv[] ); +#endif +int wmain( int argc, wchar_t * argv[] ) { +#else +int main( int argc, char * argv[] ) { +#endif + + int result = 0; + FILE * file = 0; + openmpt_module * mod = 0; + int mod_err = OPENMPT_ERROR_OK; + const char * mod_err_str = NULL; + size_t count = 0; + size_t written = 0; + + if ( argc != 2 ) { + fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_stdout SOMEMODULE'." ); + goto fail; + } + +#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) + if ( wcslen( argv[1] ) == 0 ) { + fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_stdout SOMEMODULE'." ); + goto fail; + } + file = _wfopen( argv[1], L"rb" ); +#else + if ( strlen( argv[1] ) == 0 ) { + fprintf( stderr, "Error: %s\n", "Wrong invocation. Use 'libopenmpt_example_c_stdout SOMEMODULE'." ); + goto fail; + } + file = fopen( argv[1], "rb" ); +#endif + if ( !file ) { + fprintf( stderr, "Error: %s\n", "fopen() failed." ); + goto fail; + } + + mod = openmpt_module_create2( openmpt_stream_get_file_callbacks(), file, &libopenmpt_example_logfunc, NULL, &libopenmpt_example_errfunc, NULL, &mod_err, &mod_err_str, NULL ); + if ( !mod ) { + libopenmpt_example_print_error( "openmpt_module_create2()", mod_err, mod_err_str ); + openmpt_free_string( mod_err_str ); + mod_err_str = NULL; + goto fail; + } + + while ( 1 ) { + + openmpt_module_error_clear( mod ); + count = openmpt_module_read_interleaved_stereo( mod, SAMPLERATE, BUFFERSIZE, buffer ); + mod_err = openmpt_module_error_get_last( mod ); + mod_err_str = openmpt_module_error_get_last_message( mod ); + if ( mod_err != OPENMPT_ERROR_OK ) { + libopenmpt_example_print_error( "openmpt_module_read_interleaved_stereo()", mod_err, mod_err_str ); + openmpt_free_string( mod_err_str ); + mod_err_str = NULL; + } + if ( count == 0 ) { + break; + } + + written = xwrite( STDOUT_FILENO, buffer, count * 2 * sizeof( int16_t ) ); + if ( written == 0 ) { + fprintf( stderr, "Error: %s\n", "write() failed." ); + goto fail; + } + } + + result = 0; + + goto cleanup; + +fail: + + result = 1; + +cleanup: + + if ( mod ) { + openmpt_module_destroy( mod ); + mod = 0; + } + + if ( file ) { + fclose( file ); + file = 0; + } + + return result; +} diff --git a/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_c_unsafe.c b/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_c_unsafe.c new file mode 100644 index 00000000..c2e0651c --- /dev/null +++ b/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_c_unsafe.c @@ -0,0 +1,68 @@ +/* + * libopenmpt_example_c_unsafe.c + * ----------------------------- + * Purpose: libopenmpt C API simplified example + * Notes : PortAudio is used for sound output. + * Authors: OpenMPT Devs + * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. + */ + +/* + * Usage: libopenmpt_example_c_unsafe SOMEMODULE + * CAUTION: This simple example does no error cheking at all. + */ + +#include <memory.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <libopenmpt/libopenmpt.h> +#include <libopenmpt/libopenmpt_stream_callbacks_file.h> + +#include <portaudio.h> + +#define BUFFERSIZE 480 +#define SAMPLERATE 48000 + +static int16_t left[BUFFERSIZE]; +static int16_t right[BUFFERSIZE]; +static int16_t * const buffers[2] = { left, right }; + +#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) +#if defined( __clang__ ) && !defined( _MSC_VER ) +int wmain( int argc, wchar_t * argv[] ); +#endif +int wmain( int argc, wchar_t * argv[] ) { +#else +int main( int argc, char * argv[] ) { +#endif + FILE * file = 0; + openmpt_module * mod = 0; + size_t count = 0; + PaStream * stream = 0; + (void)argc; +#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) + file = _wfopen( argv[1], L"rb" ); +#else + file = fopen( argv[1], "rb" ); +#endif + mod = openmpt_module_create2( openmpt_stream_get_file_callbacks(), file, NULL, NULL, NULL, NULL, NULL, NULL, NULL ); + fclose( file ); + Pa_Initialize(); + Pa_OpenDefaultStream( &stream, 0, 2, paInt16 | paNonInterleaved, SAMPLERATE, paFramesPerBufferUnspecified, NULL, NULL ); + Pa_StartStream( stream ); + while ( 1 ) { + count = openmpt_module_read_stereo( mod, SAMPLERATE, BUFFERSIZE, left, right ); + if ( count == 0 ) { + break; + } + Pa_WriteStream( stream, buffers, (unsigned long)count ); + } + Pa_StopStream( stream ); + Pa_CloseStream( stream ); + Pa_Terminate(); + openmpt_module_destroy( mod ); + return 0; +} diff --git a/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_cxx.cpp b/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_cxx.cpp new file mode 100644 index 00000000..81a140c3 --- /dev/null +++ b/Src/external_dependencies/openmpt-trunk/examples/libopenmpt_example_cxx.cpp @@ -0,0 +1,114 @@ +/* + * libopenmpt_example_cxx.cpp + * -------------------------- + * Purpose: libopenmpt C++ API example + * Notes : PortAudio C++ is used for sound output. + * Authors: OpenMPT Devs + * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. + */ + +/* + * Usage: libopenmpt_example_cxx SOMEMODULE + */ + +#include <exception> +#include <fstream> +#include <iostream> +#include <new> +#include <stdexcept> +#include <vector> + +#include <libopenmpt/libopenmpt.hpp> + +#if defined( __clang__ ) +#if ( ( __clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__ ) >= 40000 ) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-dynamic-exception-spec" +#endif +#endif +#include <portaudiocpp/PortAudioCpp.hxx> +#if defined( __clang__ ) +#if ( ( __clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__ ) >= 40000 ) +#pragma clang diagnostic pop +#endif +#endif + +#if ( defined( _WIN32 ) || defined( WIN32 ) ) && ( defined( _UNICODE ) || defined( UNICODE ) ) +#if defined( __GNUC__ ) || ( defined( __clang__ ) && !defined( _MSC_VER ) ) +// 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/>). +extern "C" int wmain( int /*argc*/, wchar_t * /*argv*/[] ); +extern "C" int wmain( int argc, wchar_t * argv[] ) { +#else +int wmain( int argc, wchar_t * argv[] ) { +#endif +#else +int main( int argc, char * argv[] ) { +#endif + try { + if ( argc != 2 ) { + throw std::runtime_error( "Usage: libopenmpt_example_cxx SOMEMODULE" ); + } + constexpr std::size_t buffersize = 480; + constexpr std::int32_t samplerate = 48000; + std::ifstream file( argv[1], std::ios::binary ); + openmpt::module mod( file ); + portaudio::AutoSystem portaudio_initializer; + portaudio::System & portaudio = portaudio::System::instance(); + std::vector<float> left( buffersize ); + std::vector<float> right( buffersize ); + std::vector<float> interleaved_buffer( buffersize * 2 ); + bool is_interleaved = false; +#if defined( _MSC_VER ) && defined( _PREFAST_ ) + // work-around bug in VS2019 MSVC 16.5.5 static analyzer + is_interleaved = false; + portaudio::DirectionSpecificStreamParameters outputstream_parameters( portaudio.defaultOutputDevice(), 2, portaudio::FLOAT32, false, portaudio.defaultOutputDevice().defaultHighOutputLatency(), 0 ); + portaudio::StreamParameters stream_parameters( portaudio::DirectionSpecificStreamParameters::null(), outputstream_parameters, samplerate, paFramesPerBufferUnspecified, paNoFlag ); + portaudio::BlockingStream stream( stream_parameters ); +#else + portaudio::BlockingStream stream = [&]() + { + try { + is_interleaved = false; + portaudio::DirectionSpecificStreamParameters outputstream_parameters( portaudio.defaultOutputDevice(), 2, portaudio::FLOAT32, false, portaudio.defaultOutputDevice().defaultHighOutputLatency(), 0 ); + portaudio::StreamParameters stream_parameters( portaudio::DirectionSpecificStreamParameters::null(), outputstream_parameters, samplerate, paFramesPerBufferUnspecified, paNoFlag ); + return portaudio::BlockingStream( stream_parameters ); + } catch ( const portaudio::PaException & e ) { + if ( e.paError() != paSampleFormatNotSupported ) { + throw; + } + is_interleaved = true; + portaudio::DirectionSpecificStreamParameters outputstream_parameters( portaudio.defaultOutputDevice(), 2, portaudio::FLOAT32, true, portaudio.defaultOutputDevice().defaultHighOutputLatency(), 0 ); + portaudio::StreamParameters stream_parameters( portaudio::DirectionSpecificStreamParameters::null(), outputstream_parameters, samplerate, paFramesPerBufferUnspecified, paNoFlag ); + return portaudio::BlockingStream( stream_parameters ); + } + }(); +#endif + stream.start(); + while ( true ) { + std::size_t count = is_interleaved ? mod.read_interleaved_stereo( samplerate, buffersize, interleaved_buffer.data() ) : mod.read( samplerate, buffersize, left.data(), right.data() ); + if ( count == 0 ) { + break; + } + try { + if ( is_interleaved ) { + stream.write( interleaved_buffer.data(), static_cast<unsigned long>( count ) ); + } else { + const float * const buffers[2] = { left.data(), right.data() }; + stream.write( buffers, static_cast<unsigned long>( count ) ); + } + } catch ( const portaudio::PaException & pa_exception ) { + if ( pa_exception.paError() != paOutputUnderflowed ) { + throw; + } + } + } + stream.stop(); + } catch ( const std::bad_alloc & ) { + std::cerr << "Error: " << std::string( "out of memory" ) << std::endl; + return 1; + } catch ( const std::exception & e ) { + std::cerr << "Error: " << std::string( e.what() ? e.what() : "unknown error" ) << std::endl; + return 1; + } + return 0; +} |