RMS detection issue - Will Pirkle - Designing Audio Effect Plug-Ins in C++


#21

Yes, the algorithm to calculate RMS is incorrect. The square root should be taken of the sum of the squares of each data point.


#22

I think you should differentiate between the coding style discussion that does not consider what exactly the code does and the actual correctness/sensefulness of the math :wink:

I think you are right there is absolutely no senseful point in computing std::sqrt (absLevel * absLevel) for a single float, as @daniel pointed out some posts above, this equation would make some sense if the square root is computed over a normalized sum of multiple samples, but obviously this function doesn’t do anything like that. However I don’t know that book, maybe the author extends this function to this behavior in a following section?

@jules Your code example looks clean and readable as the whole JUCE codebase :heart_eyes: However what is your reason to not choose a switch case statement in this case and write it like that?

float CEnvelopeDetector::detect (float level)
{
    auto absLevel = std::abs (level);

    switch (mode)
    {
        case Mode::peak:    return absLevel;
        case Mode::midSide: return absLevel * absLevel;
        case Mode::RMS:     return std::sqrt (absLevel * absLevel);
        default:            return level;
    }
}

#23

There’s a lot worse examples of code in those projects than that, don’t get me wrong (there are string conversion “helpers” that require the caller to delete char buffers manually :neutral_face:).

But I was going through it last night and I believe it’s a typo in the code that @pajczur is looking at. I know there were a lot of updates since the initial publication, both in the code available online and what will eventually be in the 2nd edition.

@pajczur you may want to double check the files you downloaded. I was digging through it last night and the code I saw was not what you posted. Specifically check the source from the Chapter 3 projects against what you have.


#24

If there were more items then a switch would probably have been better.

TBH in reality I probably wouldn’t use any kind of if/switch. A better approach would use templates to select different algorithms into a class that applies it, and maybe a virtual method wrapper if there’s a need to dynamically choose between them.


#25

I read Will Pirkle in the title and was waiting for Matthieu to join the party and bash it :wink:


#26

Will Pirkle’s book’s are great, they teach in a very friendly way how to start on audio programming. Ok, the code examples might not be that great, but they do the job.
He states very clearly that the goal of his books is not performance, but basic audio programming education, and IMHO, does it very well.


#27

I found this as well.


#28

haha, done!


#29

You can do both, you can give proper DSP theory and proper code practices. It’s not rocket science.