From aa2b509cd3c2ffa113b7119aec5bc2216cfdf2f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Heusipp?= Date: Tue, 24 Sep 2024 13:10:29 +0000 Subject: [PATCH] [Ref] openmpt123: Move knowledge about audio and file backends into wrapper class. git-svn-id: https://source.openmpt.org/svn/openmpt/trunk/OpenMPT@21738 56274372-70c3-4bfc-bfc3-4c3a0b034d27 --- openmpt123/openmpt123.cpp | 168 +++++++++++++++++++++----------------- 1 file changed, 91 insertions(+), 77 deletions(-) diff --git a/openmpt123/openmpt123.cpp b/openmpt123/openmpt123.cpp index 91c145a150e..58a7c05ec7d 100644 --- a/openmpt123/openmpt123.cpp +++ b/openmpt123/openmpt123.cpp @@ -153,11 +153,24 @@ struct show_long_version_number_exception : public std::exception { constexpr auto libopenmpt_encoding = mpt::common_encoding::utf8; -class file_audio_stream_raii : public file_audio_stream_base { +class file_audio_stream : public file_audio_stream_base { private: std::unique_ptr impl; public: - file_audio_stream_raii( const commandlineflags & flags, const mpt::native_path & filename, concat_stream & log ) + static void show_versions( concat_stream & log ) { +#ifdef MPT_WITH_FLAC + log << MPT_USTRING(" FLAC ") << mpt::transcode( mpt::source_encoding, FLAC__VERSION_STRING ) << MPT_USTRING(", ") << mpt::transcode( mpt::source_encoding, FLAC__VENDOR_STRING ) << MPT_USTRING(", API ") << FLAC_API_VERSION_CURRENT << MPT_USTRING(".") << FLAC_API_VERSION_REVISION << MPT_USTRING(".") << FLAC_API_VERSION_AGE << MPT_USTRING(" ") << lf; +#endif +#ifdef MPT_WITH_SNDFILE + char sndfile_info[128]; + std::memset( sndfile_info, 0, sizeof( sndfile_info ) ); + sf_command( 0, SFC_GET_LIB_VERSION, sndfile_info, sizeof( sndfile_info ) ); + sndfile_info[127] = '\0'; + log << MPT_USTRING(" libsndfile ") << mpt::transcode( sndfile_encoding, sndfile_info ) << MPT_USTRING(" ") << lf; +#endif + } +public: + file_audio_stream( const commandlineflags & flags, const mpt::native_path & filename, concat_stream & log ) : impl(nullptr) { if ( !flags.force_overwrite ) { @@ -173,11 +186,11 @@ class file_audio_stream_raii : public file_audio_stream_base { #if MPT_OS_WINDOWS && !MPT_OS_WINDOWS_WINRT } else if ( flags.output_extension == MPT_NATIVE_PATH("wav") ) { impl = std::make_unique( filename, flags, log ); -#endif +#endif #ifdef MPT_WITH_FLAC } else if ( flags.output_extension == MPT_NATIVE_PATH("flac") ) { impl = std::make_unique( filename, flags, log ); -#endif +#endif #ifdef MPT_WITH_SNDFILE } else { impl = std::make_unique( filename, flags, log ); @@ -187,7 +200,7 @@ class file_audio_stream_raii : public file_audio_stream_base { throw exception( MPT_USTRING("file format handler '") + mpt::transcode( flags.output_extension ) + MPT_USTRING("' not found") ); } } - virtual ~file_audio_stream_raii() { + virtual ~file_audio_stream() { return; } void write_metadata( std::map metadata ) override { @@ -204,11 +217,74 @@ class file_audio_stream_raii : public file_audio_stream_base { } }; -class realtime_audio_stream_raii : public write_buffers_interface { +class realtime_audio_stream : public write_buffers_interface { private: std::unique_ptr impl; public: - realtime_audio_stream_raii( commandlineflags & flags, concat_stream & log ) + static void show_versions( concat_stream & log ) { +#ifdef MPT_WITH_SDL2 + log << MPT_USTRING(" libSDL2 "); + SDL_version sdlver; + std::memset( &sdlver, 0, sizeof( SDL_version ) ); + SDL_GetVersion( &sdlver ); + log << static_cast( sdlver.major ) << MPT_USTRING(".") << static_cast( sdlver.minor ) << MPT_USTRING(".") << static_cast( sdlver.patch ); + const char * revision = SDL_GetRevision(); + if ( revision ) { + log << MPT_USTRING(" (") << mpt::transcode( sdl2_encoding, revision ) << MPT_USTRING(")"); + } + log << MPT_USTRING(", "); + std::memset( &sdlver, 0, sizeof( SDL_version ) ); + SDL_VERSION( &sdlver ); + log << MPT_USTRING("API: ") << static_cast( sdlver.major ) << MPT_USTRING(".") << static_cast( sdlver.minor ) << MPT_USTRING(".") << static_cast( sdlver.patch ); + log << MPT_USTRING(" ") << lf; +#endif +#ifdef MPT_WITH_PULSEAUDIO + log << MPT_USTRING(" ") << MPT_USTRING("libpulse, libpulse-simple") << MPT_USTRING(" (headers ") << mpt::transcode( pulseaudio_encoding, pa_get_headers_version() ) << MPT_USTRING(", API ") << PA_API_VERSION << MPT_USTRING(", PROTOCOL ") << PA_PROTOCOL_VERSION << MPT_USTRING(", library ") << mpt::transcode( pulseaudio_encoding, ( pa_get_library_version() ? pa_get_library_version() : "unknown" ) ) << MPT_USTRING(") ") << lf; +#endif +#ifdef MPT_WITH_PORTAUDIO + log << MPT_USTRING(" ") << mpt::transcode( portaudio_encoding, Pa_GetVersionText() ) << MPT_USTRING(" (") << Pa_GetVersion() << MPT_USTRING(") ") << lf; +#endif + } + static void show_drivers( concat_stream & drivers ) { + drivers << MPT_USTRING(" Available drivers:") << lf; + drivers << MPT_USTRING(" default") << lf; +#if defined( MPT_WITH_PULSEAUDIO ) + drivers << MPT_USTRING(" pulseaudio") << lf; +#endif +#if defined( MPT_WITH_SDL2 ) + drivers << MPT_USTRING(" sdl2") << lf; +#endif +#if defined( MPT_WITH_PORTAUDIO ) + drivers << MPT_USTRING(" portaudio") << lf; +#endif +#if MPT_OS_WINDOWS + drivers << MPT_USTRING(" waveout") << lf; +#endif +#if defined( MPT_WITH_ALLEGRO42 ) + drivers << MPT_USTRING(" allegro42") << lf; +#endif + } + static void show_devices( concat_stream & devices, concat_stream & log ) { + devices << MPT_USTRING(" Available devices:") << lf; + devices << MPT_USTRING(" default: default") << lf; +#if defined( MPT_WITH_PULSEAUDIO ) + devices << show_pulseaudio_devices( log ); +#endif +#if defined( MPT_WITH_SDL2 ) + devices << show_sdl2_devices( log ); +#endif +#if defined( MPT_WITH_PORTAUDIO ) + devices << show_portaudio_devices( log ); +#endif +#if MPT_OS_WINDOWS && !MPT_OS_WINDOWS_WINRT + devices << show_waveout_devices( log ); +#endif +#if defined( MPT_WITH_ALLEGRO42 ) + devices << show_allegro42_devices( log ); +#endif + } +public: + realtime_audio_stream( commandlineflags & flags, concat_stream & log ) : impl(nullptr) { if constexpr ( false ) { @@ -239,7 +315,7 @@ class realtime_audio_stream_raii : public write_buffers_interface { throw exception( MPT_USTRING("audio driver '") + flags.driver + MPT_USTRING("' not found") ); } } - virtual ~realtime_audio_stream_raii() { + virtual ~realtime_audio_stream() { return; } void write_metadata( std::map metadata ) override { @@ -438,38 +514,8 @@ static void show_banner( concat_stream & log, verbosity banner ) { log << lf; log << MPT_USTRING(" libopenmpt compiler: ") << mpt::transcode( libopenmpt_encoding, openmpt::string::get( "build_compiler" ) ) << lf; log << MPT_USTRING(" libopenmpt features: ") << mpt::transcode( libopenmpt_encoding, openmpt::string::get( "library_features" ) ) << lf; -#ifdef MPT_WITH_SDL2 - log << MPT_USTRING(" libSDL2 "); - SDL_version sdlver; - std::memset( &sdlver, 0, sizeof( SDL_version ) ); - SDL_GetVersion( &sdlver ); - log << static_cast( sdlver.major ) << MPT_USTRING(".") << static_cast( sdlver.minor ) << MPT_USTRING(".") << static_cast( sdlver.patch ); - const char * revision = SDL_GetRevision(); - if ( revision ) { - log << MPT_USTRING(" (") << mpt::transcode( sdl2_encoding, revision ) << MPT_USTRING(")"); - } - log << MPT_USTRING(", "); - std::memset( &sdlver, 0, sizeof( SDL_version ) ); - SDL_VERSION( &sdlver ); - log << MPT_USTRING("API: ") << static_cast( sdlver.major ) << MPT_USTRING(".") << static_cast( sdlver.minor ) << MPT_USTRING(".") << static_cast( sdlver.patch ); - log << MPT_USTRING(" ") << lf; -#endif -#ifdef MPT_WITH_PULSEAUDIO - log << MPT_USTRING(" ") << MPT_USTRING("libpulse, libpulse-simple") << MPT_USTRING(" (headers ") << mpt::transcode( pulseaudio_encoding, pa_get_headers_version() ) << MPT_USTRING(", API ") << PA_API_VERSION << MPT_USTRING(", PROTOCOL ") << PA_PROTOCOL_VERSION << MPT_USTRING(", library ") << mpt::transcode( pulseaudio_encoding, ( pa_get_library_version() ? pa_get_library_version() : "unknown" ) ) << MPT_USTRING(") ") << lf; -#endif -#ifdef MPT_WITH_PORTAUDIO - log << MPT_USTRING(" ") << mpt::transcode( portaudio_encoding, Pa_GetVersionText() ) << MPT_USTRING(" (") << Pa_GetVersion() << MPT_USTRING(") ") << lf; -#endif -#ifdef MPT_WITH_FLAC - log << MPT_USTRING(" FLAC ") << mpt::transcode( mpt::source_encoding, FLAC__VERSION_STRING ) << MPT_USTRING(", ") << mpt::transcode( mpt::source_encoding, FLAC__VENDOR_STRING ) << MPT_USTRING(", API ") << FLAC_API_VERSION_CURRENT << MPT_USTRING(".") << FLAC_API_VERSION_REVISION << MPT_USTRING(".") << FLAC_API_VERSION_AGE << MPT_USTRING(" ") << lf; -#endif -#ifdef MPT_WITH_SNDFILE - char sndfile_info[128]; - std::memset( sndfile_info, 0, sizeof( sndfile_info ) ); - sf_command( 0, SFC_GET_LIB_VERSION, sndfile_info, sizeof( sndfile_info ) ); - sndfile_info[127] = '\0'; - log << MPT_USTRING(" libsndfile ") << mpt::transcode( sndfile_encoding, sndfile_info ) << MPT_USTRING(" ") << lf; -#endif + realtime_audio_stream::show_versions( log ); + file_audio_stream::show_versions( log ); log << lf; } @@ -1955,23 +2001,7 @@ static void parse_openmpt123( commandlineflags & flags, const std::vector drivers; - drivers << MPT_USTRING(" Available drivers:") << lf; - drivers << MPT_USTRING(" default") << lf; -#if defined( MPT_WITH_PULSEAUDIO ) - drivers << MPT_USTRING(" pulseaudio") << lf; -#endif -#if defined( MPT_WITH_SDL2 ) - drivers << MPT_USTRING(" sdl2") << lf; -#endif -#if defined( MPT_WITH_PORTAUDIO ) - drivers << MPT_USTRING(" portaudio") << lf; -#endif -#if MPT_OS_WINDOWS - drivers << MPT_USTRING(" waveout") << lf; -#endif -#if defined( MPT_WITH_ALLEGRO42 ) - drivers << MPT_USTRING(" allegro42") << lf; -#endif + realtime_audio_stream::show_drivers( drivers ); throw show_help_exception( drivers.str() ); } else if ( nextarg == MPT_USTRING("default") ) { flags.driver = MPT_USTRING(""); @@ -1984,23 +2014,7 @@ static void parse_openmpt123( commandlineflags & flags, const std::vector devices; - devices << MPT_USTRING(" Available devices:") << lf; - devices << MPT_USTRING(" default: default") << lf; -#if defined( MPT_WITH_PULSEAUDIO ) - devices << show_pulseaudio_devices(log); -#endif -#if defined( MPT_WITH_SDL2 ) - devices << show_sdl2_devices( log ); -#endif -#if defined( MPT_WITH_PORTAUDIO ) - devices << show_portaudio_devices( log ); -#endif -#if MPT_OS_WINDOWS && !MPT_OS_WINDOWS_WINRT - devices << show_waveout_devices( log ); -#endif -#if defined( MPT_WITH_ALLEGRO42 ) - devices << show_allegro42_devices( log ); -#endif + realtime_audio_stream::show_devices( devices, log ); throw show_help_exception( devices.str() ); } else if ( nextarg == MPT_USTRING("default") ) { flags.device = MPT_USTRING(""); @@ -2379,17 +2393,17 @@ static mpt::uint8 main( std::vector args ) { render_files( flags, log, stdout_audio_stream, prng ); } else if ( !flags.output_filename.empty() ) { flags.apply_default_buffer_sizes(); - file_audio_stream_raii file_audio_stream( flags, flags.output_filename, log ); + file_audio_stream file_audio_stream( flags, flags.output_filename, log ); render_files( flags, log, file_audio_stream, prng ); } else { - realtime_audio_stream_raii audio_stream( flags, log ); + realtime_audio_stream audio_stream( flags, log ); render_files( flags, log, audio_stream, prng ); } } break; case Mode::Render: { for ( const auto & filename : flags.filenames ) { flags.apply_default_buffer_sizes(); - file_audio_stream_raii file_audio_stream( flags, filename + MPT_NATIVE_PATH(".") + flags.output_extension, log ); + file_audio_stream file_audio_stream( flags, filename + MPT_NATIVE_PATH(".") + flags.output_extension, log ); render_file( flags, filename, log, file_audio_stream ); flags.playlist_index++; }