Cabbage Logo
Back to Cabbage Site

How to limit the volume level in Csound?

In order to protect my ears, I wish to implement a sound level limiter.
Using ASIO4all, there is no way to limit to level of the sound output… and it is really too much loud.
So, I’d like to add a vumeter ranging from volume 0 (no sound) to a maximum ( lower than the 0dbfs of Csound) which would be the “maximum acceptable volume for my ears”.

I tried using aOut gain aOut, kFactor and aOut = aOut*kFactor I don’t find these solution satisfactory as these do not limit the level of the output.

Thank you for your help.

It’s not something I’ve ever really had to do but have you looked at the various limiters that ship with Csound? There are also some compressors that might be of help.

Thank you Rory.
The example given in the limiters… almost killed my ears. It is so loud and noisy (the “wrap” is so loud and distorded !!)
Maybe I should better formulate my question : is there a way to know how many db the sound output is and then to tell csound not to go over a % of that lvl ? (like it is implemented in reaper for instance) ?
Sorry if I do not use the right vocabulary…

Ok, you could perhaps send all signals to a single instrument and do all your mixing and outputting there. You could test to see if the signal gets above a particular level using the rms opcode. If it is, you can mute the output? This is a crude example, but might give you some ideas.

form caption("Untitled") size(400, 300), colour(58, 110, 182), pluginID("def1")
keyboard bounds(18, 32, 348, 80) mouseoeverkeycolour(255, 255, 0, 128) 
-n -d -+rtmidi=NULL -M0 -m0d --midi-key-cps=4 --midi-velocity-amp=5
; Initialize the global variables. 
sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1

;instrument will be triggered by keyboard widget
instr 1
    kAmp line 1, 5, 10
    kEnv madsr .1, .2, .6, .4
    aOut vco2 p5*kAmp, p4
    chnset aOut*kEnv, "LeftSignal"
    chnset aOut*kEnv, "RightSignal"

instr LIMIT
    aL chnget "LeftSignal"
    aR chnget "RightSignal"
    kRmsL rms aL
    kRmsR rms aR
    if kRmsL < 1 && kRmsR < 1 then
        outs aL, aR

;causes Csound to run for about 7000 years...
f0 z
i"LIMIT" 0 z

Thank you a lot !
This is also interesting to combine with the vumeter widget.
Somehow, I found out that it is possible to reduce the volume thanks to windows mixer even with asio4all.
Now, I can survive my experiments :slight_smile:

That’s good to know.