HulaLoop
Simple cross-platform audio loopback and recording.
CLIArgs.h
1 #ifndef HL_CLI_ARGS_H
2 #define HL_CLI_ARGS_H
3 
4 #include <cstdio>
5 #include <QCommandLineParser>
6 
7 #include <hlaudio/hlaudio.h>
8 #include <hlcontrol/hlcontrol.h>
9 
10 #include "CLICommon.h"
11 
12 using namespace hula;
13 
19 #define HL_OUT_FILE_SO "f"
20 #define HL_OUT_FILE_LO "out-file"
21 #define HL_DELAY_TIME_SO "d"
22 #define HL_DELAY_TIME_LO "delay"
23 #define HL_RECORD_TIME_SO "t"
24 #define HL_RECORD_TIME_LO "time"
25 #define HL_TRIGGER_RECORD_SO "r"
26 #define HL_TRIGGER_RECORD_LO "record"
27 #define HL_SAMPLE_RATE_SO "s"
28 #define HL_SAMPLE_RATE_LO "sample-rate"
29 #define HL_ENCODING_SO "e"
30 #define HL_ENCODING_LO "encoding"
31 #define HL_INPUT_DEVICE_SO "i"
32 #define HL_INPUT_DEVICE_LO "input-device"
33 #define HL_OUTPUT_DEVICE_SO "o"
34 #define HL_OUTPUT_DEVICE_LO "output-device"
35 #define HL_LIST_DEVICES_SO "l"
36 #define HL_LIST_DEVICES_LO "list"
37 #define HL_LANG_SO "g"
38 #define HL_LANG_LO "lang"
39 
45 void invalidArg(QString name, QString arg, QString message = "")
46 {
47  fprintf(stderr, "%s\n", qPrintable(CLI::tr("Invalid argument '%1' provided to option '%2'.").arg(arg, name)));
48  if (!message.isEmpty())
49  {
50  fprintf(stderr, "%s\n", qPrintable(message));
51  }
52 }
53 
61 bool parseArgsQt(QCoreApplication &app, HulaImmediateArgs &extraArgs)
62 {
63  QCommandLineParser parser;
64  parser.setApplicationDescription(CLI::tr("Simple cross-platform audio loopback and recording."));
65 
66  parser.addHelpOption();
67  parser.addVersionOption();
68 
69  parser.addOptions(
70  {
71  {{HL_OUT_FILE_SO, HL_OUT_FILE_LO}, CLI::tr("Path to audio output file."), CLI::tr("output filepath")},
72  {{HL_DELAY_TIME_SO, HL_DELAY_TIME_LO}, CLI::tr("Duration, in seconds, of the countdown timer before record."), CLI::tr("delay")},
73  {{HL_RECORD_TIME_SO, HL_RECORD_TIME_LO}, CLI::tr("Duration, in seconds, of the record."), CLI::tr("record duration")},
74  {{HL_TRIGGER_RECORD_SO, HL_TRIGGER_RECORD_LO}, CLI::tr("Start the countdown/record immediately.")},
75  {{HL_SAMPLE_RATE_SO, HL_SAMPLE_RATE_LO}, CLI::tr("Desired sample rate of the output file."), CLI::tr("sample rate")},
76  {{HL_ENCODING_SO, HL_ENCODING_LO}, CLI::tr("Encoding format for the output file. Valid options are WAV and MP3. This will default to WAV."), CLI::tr("encoding")},
77  {{HL_INPUT_DEVICE_SO, HL_INPUT_DEVICE_LO}, CLI::tr("System name of the input device. This will default if not provided."), CLI::tr("input device name")},
78  {{HL_OUTPUT_DEVICE_SO, HL_OUTPUT_DEVICE_LO}, CLI::tr("System name of the output device. This will default if not provided."), CLI::tr("output device name")},
79  {{HL_LIST_DEVICES_SO, HL_LIST_DEVICES_LO}, CLI::tr("List available input and output devices.")},
80  {{HL_LANG_SO, HL_LANG_LO}, CLI::tr("Set the language of the application."), CLI::tr("target language")}
81  });
82 
83  // This will exit if any of the args are incorrect
84  parser.process(app);
85 
86  // Setup the settings module
88 
89  if (parser.isSet(HL_LANG_LO))
90  {
91  bool success = settings->loadLanguage(&app, parser.value(HL_LANG_LO).toStdString());
92  if (success)
93  {
94  printf("%s\n", qPrintable(CLI::tr("Translation file successfully loaded.")));
95  }
96  else
97  {
98  fprintf(stderr, "%s\n", qPrintable(CLI::tr("Could not find translation file for %1.").arg(parser.value(HL_LANG_LO))));
99  }
100  }
101 
102  if (parser.isSet(HL_OUT_FILE_LO))
103  {
104  extraArgs.outputFilePath = parser.value(HL_OUT_FILE_LO).toStdString();
105  }
106 
107  if (parser.isSet(HL_DELAY_TIME_LO))
108  {
109  extraArgs.delay = parser.value(HL_DELAY_TIME_LO).toStdString();
110  }
111 
112  if (parser.isSet(HL_RECORD_TIME_LO))
113  {
114  extraArgs.duration = parser.value(HL_RECORD_TIME_LO).toStdString();
115  }
116 
117  if (parser.isSet(HL_TRIGGER_RECORD_LO))
118  {
119  extraArgs.startRecord = true;
120  }
121 
122  if (parser.isSet(HL_SAMPLE_RATE_LO))
123  {
124  bool ok = false;
125  int rate = parser.value(HL_SAMPLE_RATE_LO).toInt(&ok);
126  if (!ok)
127  {
128  invalidArg(HL_SAMPLE_RATE_LO, parser.value(HL_SAMPLE_RATE_LO));
129  return false;
130  }
131  settings->setSampleRate(rate);
132  }
133 
134  if (parser.isSet(HL_ENCODING_LO))
135  {
136  std::string encoding = parser.value(HL_ENCODING_LO).toStdString();
137  if (encoding == "WAV")
138  {
139  encoding = WAV;
140  }
141  else
142  {
143  invalidArg(HL_ENCODING_LO, parser.value(HL_ENCODING_LO), CLI::tr("Only WAV format is supported at this time."));
144  return false;
145  }
146  }
147 
148  if (parser.isSet(HL_INPUT_DEVICE_LO))
149  {
150  extraArgs.inputDevice = parser.value(HL_INPUT_DEVICE_LO).toStdString();
151  }
152 
153  if (parser.isSet(HL_OUTPUT_DEVICE_LO))
154  {
155  extraArgs.outputDevice = parser.value(HL_OUTPUT_DEVICE_LO).toStdString();
156  }
157 
158  if (parser.isSet(HL_LIST_DEVICES_LO))
159  {
160  Transport t;
161 
162  printf("\n-------- %s --------\n", qPrintable(CLI::tr("Device List")));
163  printDeviceList(&t);
164 
165  extraArgs.exit = true;
166  }
167 
168  return true;
169 }
170 
171 #endif // END HL_CLI_ARGS_H
<hlcontrol/hlcontrol.h>
Extra class for managing the state of the application and all audio related processes.
Definition: Transport.h:33
bool loadLanguage(QCoreApplication *app, const std::string &id)
Remove and delete the current translator, replacing it with a new translator of the specified languag...
Definition: HulaSettings.cpp:88
std::string inputDevice
Store the input device name parsed from the CLI flags.
Definition: CLICommon.h:101
void setSampleRate(int)
Set the number of channels for the current global device configuration.
Definition: HulaAudioSettings.cpp:121
<hlaudio/hlaudio.h>
std::string outputFilePath
Store the output file path parsed from the CLI flags.
Definition: CLICommon.h:93
static HulaSettings * getInstance()
Retreive the singular instance of HulaSettings or construct a new one if none exists.
Definition: HulaSettings.cpp:31
std::string outputDevice
Store the output device name parsed from the CLI flags.
Definition: CLICommon.h:109
void printDeviceList(Transport *t)
Utility CLI function to print the device list to the console.
Definition: CLICommon.h:117
bool exit
Signal that the caller should exit.
Definition: CLICommon.h:66
Wrapper around translation functions for Qt.
Definition: Controller.h:10
Singleton class containing all settings for the application.
Definition: HulaSettings.h:23
bool startRecord
Signal that the caller should trigger the record immediately.
Definition: CLICommon.h:61
Args parsed from CLI flags.
Definition: CLICommon.h:56
std::string delay
Store the delay as a string to be converted by InteractiveCLI.
Definition: CLICommon.h:74
std::string duration
Store the record duration as a string to be converted by InteractiveCLI.
Definition: CLICommon.h:85