1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:37:46 +00:00

Utilities/aplay: Print progress in seconds and minutes by default

By default, aplay now prints the played time, the remaining time and the
total duration of the file in seconds and minutes. This is much more
intuitive. The old sample-based format is kept and hidden behind the -s
flag.
This commit is contained in:
kleines Filmröllchen 2022-01-14 12:45:33 +01:00 committed by Andreas Kling
parent 8467327807
commit 4d3a5c21b0

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2022, kleines Filmröllchen <filmroellchen@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -10,6 +11,7 @@
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/EventLoop.h> #include <LibCore/EventLoop.h>
#include <LibMain/Main.h> #include <LibMain/Main.h>
#include <math.h>
#include <stdio.h> #include <stdio.h>
// The Kernel has issues with very large anonymous buffers. // The Kernel has issues with very large anonymous buffers.
@ -20,10 +22,12 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
{ {
const char* path = nullptr; const char* path = nullptr;
bool should_loop = false; bool should_loop = false;
bool show_sample_progress = false;
Core::ArgsParser args_parser; Core::ArgsParser args_parser;
args_parser.add_positional_argument(path, "Path to audio file", "path"); args_parser.add_positional_argument(path, "Path to audio file", "path");
args_parser.add_option(should_loop, "Loop playback", "loop", 'l'); args_parser.add_option(should_loop, "Loop playback", "loop", 'l');
args_parser.add_option(show_sample_progress, "Show playback progress in samples", "sample-progress", 's');
args_parser.parse(arguments); args_parser.parse(arguments);
Core::EventLoop loop; Core::EventLoop loop;
@ -59,7 +63,27 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (samples.value()->sample_count() > 0) { if (samples.value()->sample_count() > 0) {
// We can read and enqueue more samples // We can read and enqueue more samples
out("\033[u"); out("\033[u");
if (show_sample_progress) {
out("{}/{}", loader->loaded_samples(), loader->total_samples()); out("{}/{}", loader->loaded_samples(), loader->total_samples());
} else {
auto playing_seconds = static_cast<int>(floor(static_cast<double>(loader->loaded_samples()) / static_cast<double>(loader->sample_rate())));
auto playing_minutes = playing_seconds / 60;
auto playing_seconds_of_minute = playing_seconds % 60;
auto total_seconds = static_cast<int>(floor(static_cast<double>(loader->total_samples()) / static_cast<double>(loader->sample_rate())));
auto total_minutes = total_seconds / 60;
auto total_seconds_of_minute = total_seconds % 60;
auto remaining_seconds = total_seconds - playing_seconds;
auto remaining_minutes = remaining_seconds / 60;
auto remaining_seconds_of_minute = remaining_seconds % 60;
out("\033[1m{:02d}:{:02d}\033[0m [{}{:02d}:{:02d}] -- {:02d}:{:02d}",
playing_minutes, playing_seconds_of_minute,
remaining_seconds == 0 ? " " : "-",
remaining_minutes, remaining_seconds_of_minute,
total_minutes, total_seconds_of_minute);
}
fflush(stdout); fflush(stdout);
resampler.reset(); resampler.reset();
auto resampled_samples = TRY(Audio::resample_buffer(resampler, *samples.value())); auto resampled_samples = TRY(Audio::resample_buffer(resampler, *samples.value()));