From 9327a1a6f629a6cf257d22479df515ee6895ad0f Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sun, 17 Nov 2024 04:46:43 -0800 Subject: [PATCH] Use fmt in relevant examples --- CMakeLists.txt | 10 ++-- examples/aldebug.cpp | 119 ++++++++++++++++++++-------------------- examples/aldirect.cpp | 30 +++++----- examples/alffplay.cpp | 142 ++++++++++++++++++++++-------------------------- examples/allafplay.cpp | 27 +++++---- examples/alstreamcb.cpp | 21 ++++--- 6 files changed, 168 insertions(+), 181 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 33d2888d..9a50af4c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1716,12 +1716,12 @@ if(ALSOFT_EXAMPLES) set_target_properties(alrecord PROPERTIES ${DEFAULT_TARGET_PROPS}) add_executable(aldebug examples/aldebug.cpp) - target_link_libraries(aldebug PRIVATE ${LINKER_FLAGS} alsoft.excommon ${UNICODE_FLAG}) + target_link_libraries(aldebug PRIVATE ${LINKER_FLAGS} alsoft.excommon ${UNICODE_FLAG} fmt::fmt) set_target_properties(aldebug PROPERTIES ${DEFAULT_TARGET_PROPS}) add_executable(allafplay examples/allafplay.cpp) target_link_libraries(allafplay PRIVATE ${LINKER_FLAGS} alsoft.common alsoft.excommon - ${UNICODE_FLAG}) + ${UNICODE_FLAG} fmt::fmt) set_target_properties(allafplay PROPERTIES ${DEFAULT_TARGET_PROPS}) if(ALSOFT_INSTALL_EXAMPLES) @@ -1763,12 +1763,12 @@ if(ALSOFT_EXAMPLES) add_executable(alstreamcb examples/alstreamcb.cpp) target_link_libraries(alstreamcb PRIVATE ${LINKER_FLAGS} SndFile::SndFile alsoft.excommon - ${UNICODE_FLAG}) + ${UNICODE_FLAG} fmt::fmt) set_target_properties(alstreamcb PROPERTIES ${DEFAULT_TARGET_PROPS}) add_executable(aldirect examples/aldirect.cpp) target_link_libraries(aldirect PRIVATE ${LINKER_FLAGS} SndFile::SndFile alsoft.excommon - ${UNICODE_FLAG}) + ${UNICODE_FLAG} fmt::fmt) set_target_properties(aldirect PROPERTIES ${DEFAULT_TARGET_PROPS}) add_executable(alconvolve examples/alconvolve.c) @@ -1824,7 +1824,7 @@ if(ALSOFT_EXAMPLES) add_executable(alffplay examples/alffplay.cpp) target_include_directories(alffplay PRIVATE ${FFMPEG_INCLUDE_DIRS}) target_link_libraries(alffplay - PRIVATE ${LINKER_FLAGS} SDL2::SDL2 ${FFMPEG_LIBRARIES} alsoft.excommon) + PRIVATE ${LINKER_FLAGS} SDL2::SDL2 ${FFMPEG_LIBRARIES} alsoft.excommon fmt::fmt) set_target_properties(alffplay PROPERTIES ${DEFAULT_TARGET_PROPS}) if(ALSOFT_INSTALL_EXAMPLES) diff --git a/examples/aldebug.cpp b/examples/aldebug.cpp index b01afdc8..a3f52e54 100644 --- a/examples/aldebug.cpp +++ b/examples/aldebug.cpp @@ -28,7 +28,6 @@ #include #include #include -#include #include #include #include @@ -39,12 +38,14 @@ #include "AL/alext.h" #include "alspan.h" -#include "alstring.h" +#include "fmt/core.h" #include "win_main_utf8.h" namespace { +using namespace std::string_view_literals; + struct DeviceCloser { void operator()(ALCdevice *device) const noexcept { alcCloseDevice(device); } }; @@ -56,46 +57,46 @@ struct ContextDestroyer { using ContextPtr = std::unique_ptr; -constexpr auto GetDebugSourceName(ALenum source) noexcept -> const char* +constexpr auto GetDebugSourceName(ALenum source) noexcept -> std::string_view { switch(source) { - case AL_DEBUG_SOURCE_API_EXT: return "API"; - case AL_DEBUG_SOURCE_AUDIO_SYSTEM_EXT: return "Audio System"; - case AL_DEBUG_SOURCE_THIRD_PARTY_EXT: return "Third Party"; - case AL_DEBUG_SOURCE_APPLICATION_EXT: return "Application"; - case AL_DEBUG_SOURCE_OTHER_EXT: return "Other"; + case AL_DEBUG_SOURCE_API_EXT: return "API"sv; + case AL_DEBUG_SOURCE_AUDIO_SYSTEM_EXT: return "Audio System"sv; + case AL_DEBUG_SOURCE_THIRD_PARTY_EXT: return "Third Party"sv; + case AL_DEBUG_SOURCE_APPLICATION_EXT: return "Application"sv; + case AL_DEBUG_SOURCE_OTHER_EXT: return "Other"sv; } - return ""; + return ""sv; } -constexpr auto GetDebugTypeName(ALenum type) noexcept -> const char* +constexpr auto GetDebugTypeName(ALenum type) noexcept -> std::string_view { switch(type) { - case AL_DEBUG_TYPE_ERROR_EXT: return "Error"; - case AL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_EXT: return "Deprecated Behavior"; - case AL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_EXT: return "Undefined Behavior"; - case AL_DEBUG_TYPE_PORTABILITY_EXT: return "Portability"; - case AL_DEBUG_TYPE_PERFORMANCE_EXT: return "Performance"; - case AL_DEBUG_TYPE_MARKER_EXT: return "Marker"; - case AL_DEBUG_TYPE_PUSH_GROUP_EXT: return "Push Group"; - case AL_DEBUG_TYPE_POP_GROUP_EXT: return "Pop Group"; - case AL_DEBUG_TYPE_OTHER_EXT: return "Other"; + case AL_DEBUG_TYPE_ERROR_EXT: return "Error"sv; + case AL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_EXT: return "Deprecated Behavior"sv; + case AL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_EXT: return "Undefined Behavior"sv; + case AL_DEBUG_TYPE_PORTABILITY_EXT: return "Portability"sv; + case AL_DEBUG_TYPE_PERFORMANCE_EXT: return "Performance"sv; + case AL_DEBUG_TYPE_MARKER_EXT: return "Marker"sv; + case AL_DEBUG_TYPE_PUSH_GROUP_EXT: return "Push Group"sv; + case AL_DEBUG_TYPE_POP_GROUP_EXT: return "Pop Group"sv; + case AL_DEBUG_TYPE_OTHER_EXT: return "Other"sv; } - return ""; + return ""sv; } -constexpr auto GetDebugSeverityName(ALenum severity) noexcept -> const char* +constexpr auto GetDebugSeverityName(ALenum severity) noexcept -> std::string_view { switch(severity) { - case AL_DEBUG_SEVERITY_HIGH_EXT: return "High"; - case AL_DEBUG_SEVERITY_MEDIUM_EXT: return "Medium"; - case AL_DEBUG_SEVERITY_LOW_EXT: return "Low"; - case AL_DEBUG_SEVERITY_NOTIFICATION_EXT: return "Notification"; + case AL_DEBUG_SEVERITY_HIGH_EXT: return "High"sv; + case AL_DEBUG_SEVERITY_MEDIUM_EXT: return "Medium"sv; + case AL_DEBUG_SEVERITY_LOW_EXT: return "Low"sv; + case AL_DEBUG_SEVERITY_NOTIFICATION_EXT: return "Notification"sv; } - return ""; + return ""sv; } auto alDebugMessageCallbackEXT = LPALDEBUGMESSAGECALLBACKEXT{}; @@ -115,7 +116,7 @@ int main(al::span args) /* Print out usage if -h was specified */ if(args.size() > 1 && (args[1] == "-h" || args[1] == "--help")) { - std::cerr<< "Usage: "<] [-nodebug]\n"; + fmt::println(stderr, "Usage: {} [-device ] [-nodebug]", args[0]); return 1; } @@ -127,20 +128,20 @@ int main(al::span args) { device = DevicePtr{alcOpenDevice(std::string{args[1]}.c_str())}; if(!device) - std::cerr<< "Failed to open \""< args) auto context = ContextPtr{alcCreateContext(device.get(), attribs.data())}; if(!context || alcMakeContextCurrent(context.get()) == ALC_FALSE) { - std::cerr<< "Could not create and set a context!\n"; + fmt::println(stderr, "Could not create and set a context!"); return 1; } @@ -180,14 +181,14 @@ int main(al::span args) alDebugMessageControlEXT(AL_DONT_CARE_EXT, AL_DONT_CARE_EXT, AL_DEBUG_SEVERITY_LOW_EXT, 0, nullptr, AL_TRUE); - printf("Context flags: 0x%08x\n", alGetInteger(AL_CONTEXT_FLAGS_EXT)); + fmt::println("Context flags: 0x{:08x}", alGetInteger(AL_CONTEXT_FLAGS_EXT)); /* A debug context has debug output enabled by default. But in case this * isn't a debug context, explicitly enable it (probably won't get much, if * anything, in that case). */ - printf("Default debug state is: %s\n", - alIsEnabled(AL_DEBUG_OUTPUT_EXT) ? "enabled" : "disabled"); + fmt::println("Default debug state is: {}", + alIsEnabled(AL_DEBUG_OUTPUT_EXT) ? "enabled"sv : "disabled"sv); alEnable(AL_DEBUG_OUTPUT_EXT); /* The max debug message length property will allow us to define message @@ -195,15 +196,15 @@ int main(al::span args) * terminator. */ const auto maxloglength = alGetInteger(AL_MAX_DEBUG_MESSAGE_LENGTH_EXT); - printf("Max debug message length: %d\n", maxloglength); + fmt::println("Max debug message length: {}", maxloglength); - fputs("\n", stdout); + fmt::println(""); /* Doppler Velocity is deprecated since AL 1.1, so this should generate a * deprecation debug message. We'll first handle debug messages through the * message log, meaning we'll query for and read it afterward. */ - printf("Calling alDopplerVelocity(0.5f)...\n"); + fmt::println("Calling alDopplerVelocity(0.5f)..."); alDopplerVelocity(0.5f); for(auto numlogs = alGetInteger(AL_DEBUG_LOGGED_MESSAGES_EXT);numlogs > 0;--numlogs) @@ -220,7 +221,7 @@ int main(al::span args) &msglength, message.data()); if(read != 1) { - fprintf(stderr, "Read %d debug messages, expected to read 1\n", read); + fmt::println(stderr, "Read {} debug messages, expected to read 1", read); break; } @@ -231,15 +232,15 @@ int main(al::span args) */ const auto msgstr = std::string_view{message.data(), static_cast(msglength ? msglength-1 : 0)}; - printf("Got message from log:\n" - " Source: %s\n" - " Type: %s\n" - " ID: %u\n" - " Severity: %s\n" - " Message: \"%.*s\"\n", GetDebugSourceName(source), GetDebugTypeName(type), id, - GetDebugSeverityName(severity), al::sizei(msgstr), msgstr.data()); + fmt::println("Got message from log:\n" + " Source: {}\n" + " Type: {}\n" + " ID: {}\n" + " Severity: {}\n" + " Message: \"{}\"", GetDebugSourceName(source), GetDebugTypeName(type), id, + GetDebugSeverityName(severity), msgstr); } - fputs("\n", stdout); + fmt::println(""); /* Now set up a callback function. This lets us print the debug messages as * they happen without having to explicitly query and get them. @@ -252,41 +253,41 @@ int main(al::span args) * null terminator. */ const auto msgstr = std::string_view{message, static_cast(length)}; - printf("Got message from callback:\n" - " Source: %s\n" - " Type: %s\n" - " ID: %u\n" - " Severity: %s\n" - " Message: \"%.*s\"\n", GetDebugSourceName(source), GetDebugTypeName(type), id, - GetDebugSeverityName(severity), al::sizei(msgstr), msgstr.data()); + fmt::println("Got message from callback:\n" + " Source: {}\n" + " Type: {}\n" + " ID: {}\n" + " Severity: {}\n" + " Message: \"{}\"", GetDebugSourceName(source), GetDebugTypeName(type), id, + GetDebugSeverityName(severity), msgstr); }; alDebugMessageCallbackEXT(debug_callback, nullptr); if(const auto numlogs = alGetInteger(AL_DEBUG_LOGGED_MESSAGES_EXT)) - fprintf(stderr, "%d left over logged message%s!\n", numlogs, (numlogs==1)?"":"s"); + fmt::println(stderr, "{} left over logged message{}!", numlogs, (numlogs==1)?"":"s"); /* This should also generate a deprecation debug message, which will now go * through the callback. */ - printf("Calling alGetInteger(AL_DOPPLER_VELOCITY)...\n"); + fmt::println("Calling alGetInteger(AL_DOPPLER_VELOCITY)..."); auto dv [[maybe_unused]] = alGetInteger(AL_DOPPLER_VELOCITY); - fputs("\n", stdout); + fmt::println(""); /* These functions are notoriously unreliable for their behavior, they will * likely generate portability debug messages. */ - printf("Calling alcSuspendContext and alcProcessContext...\n"); + fmt::println("Calling alcSuspendContext and alcProcessContext..."); alcSuspendContext(context.get()); alcProcessContext(context.get()); fputs("\n", stdout); - printf("Pushing a debug group, making some invalid calls, and popping the debug group...\n"); + fmt::println("Pushing a debug group, making some invalid calls, and popping the debug group..."); alPushDebugGroupEXT(AL_DEBUG_SOURCE_APPLICATION_EXT, 0, -1, "Error test group"); alSpeedOfSound(0.0f); /* Can't set the label of the null buffer. */ alObjectLabelEXT(AL_BUFFER, 0, -1, "The null buffer"); alPopDebugGroupEXT(); - fputs("\n", stdout); + fmt::println(""); /* All done, insert a custom message and unset the callback. The context * and device will clean themselves up. diff --git a/examples/aldirect.cpp b/examples/aldirect.cpp index d7964add..9fffc0c6 100644 --- a/examples/aldirect.cpp +++ b/examples/aldirect.cpp @@ -30,7 +30,6 @@ #include #include #include -#include #include #include #include @@ -45,6 +44,7 @@ #include "alspan.h" #include "common/alhelpers.h" +#include "fmt/core.h" #include "win_main_utf8.h" @@ -102,12 +102,13 @@ ALuint LoadSound(ALCcontext *context, const std::string_view filename) SndFilePtr sndfile{sf_open(std::string{filename}.c_str(), SFM_READ, &sfinfo)}; if(!sndfile) { - std::cerr<< "Could not open audio in "< sf_count_t{std::numeric_limits::max()}/byteblockalign) { - std::cerr<< "Too many sample frames in "<(num_frames / splblockalign * byteblockalign); - std::cout<< "Loading: "< args) /* Print out usage if no arguments were specified */ if(args.size() < 2) { - std::cerr<< "Usage: "<] \n"; + fmt::println(stderr, "Usage: {} [-device ] ", args[0]); return 1; } @@ -330,20 +330,20 @@ int main(al::span args) { device = p_alcOpenDevice(std::string{args[1]}.c_str()); if(!device) - std::cerr<< "Failed to open \""< args) if(!context) { p_alcCloseDevice(device); - std::cerr<< "Could not create a context!\n"; + fmt::println(stderr, "Could not create a context!"); return 1; } @@ -446,7 +446,7 @@ int main(al::span args) /* Get the source offset. */ ALfloat offset{}; alGetSourcefDirect(context, source, AL_SEC_OFFSET, &offset); - printf("\rOffset: %f ", offset); + fmt::print(" \rOffset: {:.02f}", offset); fflush(stdout); } while(alGetErrorDirect(context) == AL_NO_ERROR && state == AL_PLAYING); printf("\n"); diff --git a/examples/alffplay.cpp b/examples/alffplay.cpp index 4f89ff58..d433a874 100644 --- a/examples/alffplay.cpp +++ b/examples/alffplay.cpp @@ -19,8 +19,6 @@ #include #include #include -#include -#include #include #include #include @@ -74,10 +72,13 @@ _Pragma("GCC diagnostic pop") #include "alnumeric.h" #include "alspan.h" #include "common/alhelpers.h" +#include "fmt/core.h" +#include "fmt/format.h" namespace { +using voidp = void*; using fixed32 = std::chrono::duration>; using nanoseconds = std::chrono::nanoseconds; using microseconds = std::chrono::microseconds; @@ -221,11 +222,11 @@ public: if(!packet) { if(!ret) return AVErrorEOF; - std::cerr<< "Failed to send flush packet: "<nb_samples <= 0); @@ -861,21 +862,19 @@ void AL_APIENTRY AudioState::eventCallback(ALenum eventType, ALuint object, ALui return; } - std::cout<< "\n---- AL Event on AudioState "<(length)}<<"\n----"<< - std::endl; + fmt::println("\n" + "Object ID: {}\n" + "Parameter: {}\n" + "Message: {}\n----", + object, param, std::string_view{message, static_cast(length)}); if(eventType == AL_EVENT_TYPE_DISCONNECTED_SOFT) { @@ -1140,7 +1139,7 @@ int AudioState::handler() mDecodedFrame.reset(av_frame_alloc()); if(!mDecodedFrame) { - std::cerr<< "Failed to allocate audio frame" < errstr{}; - std::cerr<< "Failed to allocate SwrContext: " - < errstr{}; - std::cerr<< "Failed to allocate SwrContext: " - < errstr{}; - std::cerr<< "Failed to initialize audio converter: " - <(mBuffers[0])); if(alGetError() != AL_NO_ERROR) { - fprintf(stderr, "Failed to set buffer callback\n"); + fmt::println(stderr, "Failed to set buffer callback"); alSourcei(mSource, AL_BUFFER, 0); } else @@ -1385,8 +1384,7 @@ int AudioState::handler() break; } if(ALenum err{alGetError()}) - std::cerr<< "Got AL error: 0x"<width, frame->height); if(!mImage) - std::cerr<< "Failed to create YV12 texture!" <width; mHeight = frame->height; } @@ -1539,7 +1537,7 @@ void VideoState::updateVideo(SDL_Window *screen, SDL_Renderer *renderer, bool re frame->data[2], frame->linesize[2] ); else if(SDL_LockTexture(mImage, nullptr, &pixels, &pitch) != 0) - std::cerr<< "Failed to lock texture" <interrupt_callback = intcb; if(avformat_open_input(&fmtctx, mFilename.c_str(), nullptr, nullptr) != 0) { - std::cerr<< "Failed to open "<codec_id)}; if(!codec || avcodec_open2(avctx.get(), codec, nullptr) < 0) { - std::cerr<< "Unsupported codec: "<codec_id) - << " (0x"<codec_id<codec_id), + int{avctx->codec_id}); return false; } @@ -1812,7 +1810,7 @@ int MovieState::parse_handler() if(video_index < 0 && audio_index < 0) { - std::cerr<< mFilename<<": could not open codecs" < std::string { using hours = std::chrono::hours; using minutes = std::chrono::minutes; - seconds t{rhs.mTime}; if(t.count() < 0) - { - os << '-'; - t *= -1; - } + return "0s"; // Only handle up to hour formatting if(t >= hours{1}) - os << duration_cast(t).count() << 'h' << std::setfill('0') << std::setw(2) - << (duration_cast(t).count() % 60) << 'm'; - else - os << duration_cast(t).count() << 'm' << std::setfill('0'); - os << std::setw(2) << (duration_cast(t).count() % 60) << 's' << std::setw(0) - << std::setfill(' '); - return os; + return fmt::format("{}h{:02}m{:02}s", duration_cast(t).count(), + duration_cast(t).count()%60, t.count()%60); + return fmt::format("{}m{:02}s", duration_cast(t).count(), t.count()%60); } - int main(al::span args) { SDL_SetMainReady(); @@ -1912,7 +1898,7 @@ int main(al::span args) if(args.size() < 2) { - std::cerr<< "Usage: "<] [-direct] " <] [-direct] ", args[0]); return 1; } /* Register all formats and codecs */ @@ -1924,7 +1910,7 @@ int main(al::span args) if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) { - std::cerr<< "Could not initialize SDL - <<"< args) SDL_Window *screen{SDL_CreateWindow(AppName.c_str(), 0, 0, 640, 480, SDL_WINDOW_RESIZABLE)}; if(!screen) { - std::cerr<< "SDL: could not set video mode - exiting" < args) } if(!ok) { - std::cerr<< "IYUV pixelformat textures not supported on renderer "< args) } if(!renderer) { - std::cerr<< "SDL: could not create renderer - exiting" < args) ALCdevice *device{alcGetContextsDevice(alcGetCurrentContext())}; if(alcIsExtensionPresent(device,"ALC_SOFT_device_clock")) { - std::cout<< "Found ALC_SOFT_device_clock" <( alcGetProcAddress(device, "alcGetInteger64vSOFT")); } @@ -1988,13 +1975,13 @@ int main(al::span args) if(alIsExtensionPresent("AL_SOFT_source_latency")) { - std::cout<< "Found AL_SOFT_source_latency" <( alGetProcAddress("alGetSourcei64vSOFT")); } if(alIsExtensionPresent("AL_SOFT_events")) { - std::cout<< "Found AL_SOFT_events" <( alGetProcAddress("alEventControlSOFT")); alEventCallbackSOFT = reinterpret_cast( @@ -2002,7 +1989,7 @@ int main(al::span args) } if(alIsExtensionPresent("AL_SOFT_callback_buffer")) { - std::cout<< "Found AL_SOFT_callback_buffer" <( alGetProcAddress("alBufferCallbackSOFT")); } @@ -2014,44 +2001,44 @@ int main(al::span args) { if(alIsExtensionPresent("AL_SOFT_direct_channels_remix")) { - std::cout<< "Found AL_SOFT_direct_channels_remix" < args) } if(!movState) { - std::cerr<< "Could not start a video" <setTitle(screen); @@ -2087,7 +2074,8 @@ int main(al::span args) if(cur_time != last_time) { auto end_time = std::chrono::duration_cast(movState->getDuration()); - std::cout<< " \r "< args) movState->mVideo.updateVideo(screen, renderer, force_redraw); } - std::cerr<< "SDL_WaitEvent error - "< #include #include -#include #include #include #include @@ -98,6 +97,7 @@ #include "alspan.h" #include "alstring.h" #include "common/alhelpers.h" +#include "fmt/core.h" #include "win_main_utf8.h" @@ -576,10 +576,10 @@ auto LoadLAF(const fs::path &fname) -> std::unique_ptr | (uint32_t{uint8_t(input[2])}<<16u) | (uint32_t{uint8_t(input[3])}<<24u); }(); - std::cout<< "Filename: "<mQuality)<<'\n'; - std::cout<< " mode: "<mMode)<<'\n'; - std::cout<< " track count: "<mNumTracks<<'\n'; + fmt::println("Filename: {}", fname.string()); + fmt::println(" quality: {}", GetQualityName(laf->mQuality)); + fmt::println(" mode: {}", GetModeName(laf->mMode)); + fmt::println(" track count: {}", laf->mNumTracks); if(laf->mNumTracks == 0) throw std::runtime_error{"No tracks"}; @@ -623,7 +623,7 @@ auto LoadLAF(const fs::path &fname) -> std::unique_ptr auto y_axis = read_float(chan.subspan<4,4>()); auto lfe_flag = int{chan[8]}; - std::cout<< "Track "< std::unique_ptr channel.mIsLfe = lfe_flag != 0; } } - std::cout<< "Channels: "<mChannels.size()<<'\n'; + fmt::println("Channels: {}", laf->mChannels.size()); /* For "objects" mode, ensure there's enough tracks with position data to * handle the audio channels. @@ -664,9 +664,9 @@ auto LoadLAF(const fs::path &fname) -> std::unique_ptr | (uint64_t{uint8_t(input[4])}<<32u) | (uint64_t{uint8_t(input[5])}<<40u) | (uint64_t{uint8_t(input[6])}<<48u) | (uint64_t{uint8_t(input[7])}<<56u); }(); - std::cout<< "Sample rate: "<mSampleRate<<'\n'; - std::cout<< "Length: "<mSampleCount<<" samples (" - <<(static_cast(laf->mSampleCount)/static_cast(laf->mSampleRate))<<" sec)\n"; + fmt::println("Sample rate: {}", laf->mSampleRate); + fmt::println("Length: {} samples ({:.2f} sec)", laf->mSampleCount, + static_cast(laf->mSampleCount)/static_cast(laf->mSampleRate)); /* Position vectors get split across the PCM chunks if the sample rate * isn't a multiple of 48. Each PCM chunk is exactly one second (the sample @@ -914,7 +914,7 @@ try { } } catch(std::exception& e) { - std::cerr<< "Error playing "< args) -> int @@ -922,8 +922,7 @@ auto main(al::span args) -> int /* Print out usage if no arguments were specified */ if(args.size() < 2) { - fprintf(stderr, "Usage: %.*s [-device ] \n", al::sizei(args[0]), - args[0].data()); + fmt::println(stderr, "Usage: {} [-device ] \n", args[0]); return 1; } args = args.subspan(1); @@ -954,7 +953,7 @@ auto main(al::span args) -> int { #define LOAD_PROC(x) do { \ x = reinterpret_cast(alGetProcAddress(#x)); \ - if(!x) fprintf(stderr, "Failed to find function '%s'\n", #x); \ + if(!x) fmt::println(stderr, "Failed to find function '{}'\n", #x##sv);\ } while(0) LOAD_PROC(alGenFilters); LOAD_PROC(alDeleteFilters); diff --git a/examples/alstreamcb.cpp b/examples/alstreamcb.cpp index ef8324e5..64c440b6 100644 --- a/examples/alstreamcb.cpp +++ b/examples/alstreamcb.cpp @@ -47,8 +47,8 @@ #include "AL/alext.h" #include "alspan.h" -#include "alstring.h" #include "common/alhelpers.h" +#include "fmt/core.h" #include "win_main_utf8.h" @@ -129,7 +129,7 @@ struct StreamPlayer { mSndfile = sf_open(filename.c_str(), SFM_READ, &mSfInfo); if(!mSndfile) { - fprintf(stderr, "Could not open audio in %s: %s\n", filename.c_str(), + fmt::println(stderr, "Could not open audio in {}: {}", filename, sf_strerror(mSndfile)); return false; } @@ -342,7 +342,7 @@ struct StreamPlayer { alSourcei(mSource, AL_BUFFER, static_cast(mBuffer)); if(ALenum err{alGetError()}) { - fprintf(stderr, "Failed to set callback: %s (0x%04x)\n", alGetString(err), err); + fmt::println(stderr, "Failed to set callback: {} (0x{:04x})", alGetString(err), err); return false; } return true; @@ -370,11 +370,11 @@ struct StreamPlayer { ? (mDecoderOffset-readable) / mBytesPerBlock * mSamplesPerBlock : (size_t{static_cast(pos)} + mStartOffset)) / static_cast(mSfInfo.samplerate); - printf("\r %zum%02zus (%3zu%% full)", curtime/60, curtime%60, + fmt::print("\r {}m{:02}s ({:3}% full)", curtime/60, curtime%60, readable * 100 / mBufferData.size()); } else - fputs("Starting...", stdout); + fmt::println("Starting..."); fflush(stdout); while(!sf_error(mSndfile)) @@ -466,8 +466,7 @@ int main(al::span args) /* Print out usage if no arguments were specified */ if(args.size() < 2) { - fprintf(stderr, "Usage: %.*s [-device ] \n", al::sizei(args[0]), - args[0].data()); + fmt::println(stderr, "Usage: {} [-device ] ", args[0]); return 1; } args = args.subspan(1); @@ -485,7 +484,7 @@ int main(al::span args) if(!alIsExtensionPresent("AL_SOFT_callback_buffer")) { - fprintf(stderr, "AL_SOFT_callback_buffer extension not available\n"); + fmt::println(stderr, "AL_SOFT_callback_buffer extension not available"); return 1; } @@ -510,8 +509,8 @@ int main(al::span args) else if(sep = namepart.rfind('\\'); sep < namepart.size()) namepart = namepart.substr(sep+1); - printf("Playing: %.*s (%s, %dhz)\n", al::sizei(namepart), namepart.data(), - FormatName(player->mFormat), player->mSfInfo.samplerate); + fmt::println("Playing: {} ({}, {}hz)", namepart, FormatName(player->mFormat), + player->mSfInfo.samplerate); fflush(stdout); if(!player->prepare()) @@ -528,7 +527,7 @@ int main(al::span args) player->close(); } /* All done. */ - printf("Done.\n"); + fmt::println("Done."); return 0; } -- 2.11.4.GIT