Skip to content

Commit

Permalink
test/run_filter: add command line parser
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Oct 30, 2024
1 parent d7b4634 commit 2ef36af
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
1 change: 1 addition & 0 deletions test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ executable(
include_directories: inc,
dependencies: [
filter_glue_dep,
cmdline_dep,
],
)

Expand Down
66 changes: 52 additions & 14 deletions test/run_filter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "ConfigGlue.hxx"
#include "ReadFrames.hxx"
#include "cmdline/OptionDef.hxx"
#include "cmdline/OptionParser.hxx"
#include "lib/fmt/RuntimeError.hxx"
#include "fs/Path.hxx"
#include "fs/NarrowPath.hxx"
Expand All @@ -17,6 +19,7 @@
#include "io/FileDescriptor.hxx"
#include "util/StringBuffer.hxx"
#include "util/PrintException.hxx"
#include "LogBackend.hxx"

#include <cassert>
#include <memory>
Expand All @@ -26,6 +29,51 @@
#include <stdlib.h>
#include <stdio.h>

struct CommandLine {
FromNarrowPath config_path;

const char *filter_name = nullptr;

AudioFormat audio_format{44100, SampleFormat::S16, 2};

bool verbose = false;
};

enum Option {
OPTION_VERBOSE,
};

static constexpr OptionDef option_defs[] = {
{"verbose", 'v', false, "Verbose logging"},
};

static CommandLine
ParseCommandLine(int argc, char **argv)
{
CommandLine c;

OptionParser option_parser(option_defs, argc, argv);
while (auto o = option_parser.Next()) {
switch (Option(o.index)) {
case OPTION_VERBOSE:
c.verbose = true;
break;
}
}

auto args = option_parser.GetRemaining();
if (args.size() < 2 || args.size() > 3)
throw std::runtime_error("Usage: run_filter CONFIG NAME [FORMAT] <IN");

c.config_path = args[0];
c.filter_name = args[1];

if (args.size() > 2)
c.audio_format = ParseAudioFormat(args[2], false);

return c;
}

static std::unique_ptr<PreparedFilter>
LoadFilter(const ConfigData &config, const char *name)
{
Expand All @@ -40,24 +88,14 @@ LoadFilter(const ConfigData &config, const char *name)

int main(int argc, char **argv)
try {
if (argc < 3 || argc > 4) {
fprintf(stderr, "Usage: run_filter CONFIG NAME [FORMAT] <IN\n");
return EXIT_FAILURE;
}

const FromNarrowPath config_path = argv[1];

AudioFormat audio_format(44100, SampleFormat::S16, 2);
const auto c = ParseCommandLine(argc, argv);
SetLogThreshold(c.verbose ? LogLevel::DEBUG : LogLevel::INFO);

/* read configuration file (mpd.conf) */

const auto config = AutoLoadConfigFile(config_path);

/* parse the audio format */

if (argc > 3)
audio_format = ParseAudioFormat(argv[3], false);
const auto config = AutoLoadConfigFile(c.config_path);

auto audio_format = c.audio_format;
const size_t in_frame_size = audio_format.GetFrameSize();

/* initialize the filter */
Expand Down

0 comments on commit 2ef36af

Please sign in to comment.