Repository navigation
MIXER command improvements & general mixer enhancements + fixes - #2868
Conversation
cb9cc57 to
7fd0230
Compare
d70b2cf to
3248752
Compare
3248752 to
7f7d3fa
Compare
17bf955 to
6be9ed7
Compare
22db435 to
5683b44
Compare
|
I was curious about the
IMO for your use case I think it's syntactically a bit nicer to have However, Overall, I don't hate it. I think you could do exactly the same thing with a tagged union and a switch statement. But if you like the syntax of |
|
Thanks for the feedback @weirddan455. Just a few further comments / thoughts why I did things this particular way. I'm not "arguing" as you're already fine with this, just offering further explanations, maybe for the benefit of others too. And my comment still stands; if everybody else than me really doesn't like this, then we're back to tagged unions / virtual functions / out vars 😄
I like the struct Executor {
void operator()(const SelectChannel cmd);
void operator()(const SetVolume cmd);
void operator()(const SetStereoMode cmd);
void operator()(const SetCrossfeedStrength cmd);
void operator()(const SetReverbLevel cmd);
void operator()(const SetChorusLevel cmd);
private:
bool global_command = false;
// If 'master_channel' is true, then the MASTER channel is selected,
// otherwise 'channel' points to the selected non-master channel.
bool master_channel = false;
std::shared_ptr<MixerChannel> channel = {};
};
Yeah, performance is absolutely a non-issue here, that wasn't even a consideration. We're only dealing with a handful (less than say 20) mixer commands on each MIXER command invocation. There are only a few (or zero) MIXER command invocations typically in the autoexec section of a config, or users execute MIXER commands manually several tens of seconds apart. No tight loops, no tens or hunders of thousands of commands being continuously evaluated, so maintainability trumps everything in this case. However, for my OSD work, I will also need a similar command pattern, but that will need to be efficient as thousands of draw commands will be evaluated per-frame at the FPS rate. But even in that case, I'd measure the performance impact of this, but you're saying
A nice property of the
There's one more thing as well which I mentioned, namely that I like to use std::variant<Error, std::queue<Command>> ParseCommands(...)
static std::variant<Error, Command> parse_volume_command(const std::string& s,
const std::string& channel_name)
static std::variant<Error, Command> parse_reverb_command(const std::string& s,
const std::string& channel_name)What I'm "simulating" here is Scala's Either type which is super handy for propagating either a result of type R, or an error type E as a single self-contained unit from functions. So you define it as Without this, we're back to the old clunky way of using an "out type" for the actual error, something like this: static std::option<Command> parse_volume_command(const std::string& s,
const std::string& channel_name, Error& out_error)Sure, that works, but I find the const auto result = parse_crossfeed_command(arg, curr_channel_name);
if (auto cmd = std::get_if<Command>(&result); cmd) {
commands.emplace(*cmd);
++curr_channel_command_count;
} else {
return std::get<Error>(result);
}The clunky option with the out param would look like this. You don't need to do the Error err = {};
if (auto result = parse_crossfeed_command(arg, curr_channel_name, err); result) {
commands.emplace(*result);
++curr_channel_command_count;
} else {
return err;
} |
5683b44 to
b1a3c17
Compare
Yeah, measuring is the only way to be sure when it comes to performance. I could be wrong here but I think you would want to avoid doing thousands of heap allocations per frame if you can. Also, virtual functions in C++ are a bit worse than a normal function pointer because you have to take a double indirection through the vtable.
I saw those for error handling. I don't love it but there's also not really a better option. I'd prefer So |
An AudioFrame is 4-byte long so passing it by value is the most efficient.
Mixer channel settings are now persisted in a map by their name whenever a channel gets destroyed or added. This is necessary to make channel settings "survive" removing an audio device and adding it back later, or special scenarios such as changing the `sbtype` setting which always deletes and immediately recreates the SB channel plus optionally the CMS and OPL channels.
This warning is raised when the handling of two different exception types is identical. This a fairly low value warning that doesn't accomplish much, but it creates unnecessary warnings when we just "do nothing" for multiple different exception types. Details: https://pvs-studio.com/en/docs/warnings/v2010/
- Clamping was removed as it encouraged loose parsing and error reporting which was a source for bugs and unexpected behaviour. - Same goes for lenient parsing (e.g. allowing extra non-numeric characters after the numbers). Number parsing is now strict, no extra characters are allowed at the end. - Specific functions that were not generally useful have been removed (e.g. 'parse_prefixed_percentage'). - Number parsing function names have been normalised for consistency.
- Rebuild the MIXER command for testability and add extensive unit tests. - Make command parsing strict; previously, the mixer silently accepted all sort of invalid input and it wasn't clear which commands were applied and which were ignored. - Mixer command invocations are now "transactional"; either all commands are applied (if no errors), or none at all (if there is at least one error). - Display helpful error messages to guide the user to correct their mistakes. - Rework the 'crossfeed' setting; now it uses preset names as the 'chorus' setting. Setting global crossfeed via 'x<NUMBER>` has been deprecated as it was unsupportable in a sane and consistent way. - Improve the help text of the MIXER command.
2fdf93a to
bc635e7
Compare
Done. |
|
I played a little with the command, just one minor remark from my side. |
|
Thanks @kcgen and @FeralChild64 for the review. Merge time 😎 |
|
@johnnovak -> can you check:
Getting an 'invalid command' on my side: |
Confimed @kcgen, it's a bug. Started debugging it, maybe I'll have time to finish it tomorrow and raise a fix PR. Update: It's rather subtle, please wait until I come up with a fix. |


Description
This is a significant rework of the command parsing, error reporting, and state handling of the
MIXERcommand. The following bug fixes and improvements have been made:Mixer channel settings now "survive" deleting then later re-creating the channel instead of simply getting the default settings when the channel is recreated. For example, if you customise the settings of the
SBandOPLchannels, then changesbtypeto something else, theSBandOPLchannels will retain their settings. Similar situation for theFSYNTHchannel when settingmidideviceto something else then back tofluidsynth.The
MIXERcommand no longer accepts slightly incorrect input; command parsing is now strict and we're displaying error messages for invalid commands. These messages also try to guide the user to correct their mistakes.This might cause backward compatibility issues if people had slightly (or very!) incorrect
MIXERcommand invocations in their configs, so we're also logging a warning when the MIXER command fails. That should give people a chance to correct these mistakes. Examples of invalid input that is no longer accepted:MIXER ASDF R1– Previously, theASDFparam was ignored because there is no channel with that name, so the scope stayed at "global scope" (all channels), then a global reverb setting of 1 was applied to all channels, which is super confusing.MIXER SB 10 GUS– This set theSBvolume to 10; now it fails because every channel name must be followed by one or more commands to remove ambiguity, and there's nothing afterGUS.MIXER SB 10 BLAH 4– This set theSBchannel's volume to 10 and ignored the non-existingBLAHchannel; now an error is raised thatBLAHis an invalid command.MIXER SB ASDF– This just printed the mixer status but did not raise any errors; now it does.MIXER SB R150– This silently clamped the reverb level to 100 and set it for theSBchannel; now we raise an error.MIXER GUS 50– If the GUS was not enabled, this just displayed the current mixer settings; now it prints and error saying that theGUSchannel is not active.MIXERcommand handling is now "transactional"—if there is a single error in a list of multiple commands, no command will be executed but an error for the first problem will be raised. This is just common sense and leads to less confusing behaviour overall.The
crossfeedconfig setting has been revised; now it works the same way as thechorussetting (it haslight,normalandstrongpresets, andondefaults tonormal). The previous behaviour where you could set a global crossfeed value (e.g.,crossfeed = 30) was unsupportable even with a lot of special casing. It was simply impossible to make that behaviour consistent with thereverbandchorussettings—I literally rewrote it in 4-5 different ways before I settled on this solution. It just can't be done.Other bundled changes
The number parsing string util functions have been revised to make them more generic, and the parsing is now strict. Previously, non-digit characters after digits were silently ignored; this now results in a parse error. Strict parsing is desirable as lenient parsing is a fertile breeding ground for subtle bugs and surprising behaviour (if affected the mixer commands, for example).
The flow-on effect is that the argument parsing of the
MOREcommand is now stricter:MORE /t4x fnameworked before (it set the tab size to 4 chars), but now that generates an "illegal switch" error (/t4is the correct invocation). Other DOS commands implemented by us might be affected too.Technical notes
I started using the
std::visitandstd::variantstd lib features which are ideally suited for command line parsers and any kind of scenario when dealing with polymorphic command execution. I'm very cautious when adopting new C++ features, but these are so ideally suited to the task that I found them irresistible 😎 They're quite sane and low overhead, both mentally and syntax-wise.std::variantis quite nice even in isolation; it can be used to return an "Either" or "Result" type common in functional languages that encapsulates either a "success" or an "error" result. IMO, this is the preferred alternative to the hacky old C/C++ way of providing "out" pointer arguments to return "secondary" results, such as errors.A few nice introductory articles about using
std::visitandstd::varianttogether to implement command parsers & executors:If everybody hates them, we can discuss finding an alternative way, but it would be a lot more complex and there would be a lot of boilerplate involved, so I'm strongly in favour of using
std::visitandstd::variantjudiciously going forward (at least in non-performance intensive code).Related issues
Fixes #2468
List of various issues fixed: #2468 (comment)
Manual testing
Proof of manual testing. These are end-to-end tests that exercise the whole stack; it would be difficult to create an automated test fixture/harness that wires up )just enough components_ to make this possible and would yield the same level of confidence.
These are highest-value tests, the absolutely highest value there is. Therefore, I'd like to keep them around by all means, preferably in an automated form. They are invaluable for regression testing when anyone touches the mixer the next time (some situations are complex and subtle; most people wouldn't even know what regressions to look out for... that includes me 6 months from now 😎).
Btw, one of the reasons why this PR was dragging on for so long was that redoing all this testing manually takes like 20 minutes (in a relaxed manner) and I was getting sick of it after re-doing it for the 10th time...
My low-cost solution is to write a batch file containing these commands that pipes the results to a file, then have an
expected.txtthat contains the expected outcome with the ANSI colour codes stripped. Then write a bash script that takes the actual output, strips the ANSI stuff out, then diffs it againstexpected.txt. Would do the job, but better ideas are welcome (hint: suggesting to write an actual end-to-end test harness that would take days or weeks is not a better idea 😎)Error messages
Setting persistence
MIDI stuff
Checklist
Please tick the items as you have addressed them. Don't remove items; leave the ones that are not applicable unchecked.
I have: