![audio decibel convert to linear scale audio decibel convert to linear scale](https://c8.alamy.com/comp/KE4TR5/the-decibel-scale-sound-level-KE4TR5.jpg)
In this particular case we don't have to worry about this too much, because on typical architectures (x86, x86_64, ARM) reading and writing a single float is an atomic operation: the reads and writes cannot be intermingled and are generally safe. These issues are beyond the scope of this tutorial and will be discussed in a future tutorial. Writing to a member variable like level in one thread (the GUI thread) and reading the same value from another thread (the audio thread) is technically undefined behaviour in C++ if no thread synchronisation takes place (either via critical sections or using atomics). Techniques to accomplish this are explored in other tutorials (see Tutorial: Build a sine wave synthesiser).Īnother issue is related to thread safety. To avoid this, in a real-world synthesiser we would want a level value that changes smoothly. Such changes will typically introduce audio artefacts such as an audible crackling. One issue with this approach is that the level variable might change its value abruptly during the execution of the audio thread (in this case, in between two calls to getNextAudioBlock). Note that this is almost identical to the getNextAudioBlock() function from Tutorial: Control audio levels except that we just take a function-local copy of the level value then calculate our levelScale value as before. We initialise the level member to zero in the constructor and convert this to decibels using the Decibels::gainToDecibels() function to give the slider its initial position (using the Slider::setValue() function) like so: In the demo project for this tutorial we store a float member level in the MainContentComponent class and update this value when the slider changes using a listener. Since the conversion from decibels to linear gain involves some potentially CPU-intensive arithmetic, it would be wise to avoid performing the conversion too often, especially on the audio thread. In the Tutorial: Control audio levels we accessed the slider's value directly in the getNextAudioBlock() function. Otherwise we convert this remaining text to a double value and return it. : Check if the remaining text is "-INF" and return -100 in this case (this is the default value in decibels for -INF dB that the Decibels class uses, see the Notes for this tutorial for a discussion on this).: Strip off the "dB" suffix from the text (if it is present), and trim the text to remove any leading or trailing whitespace (using the String::trim() function).This enables the user to enter a value into the text box and have it checked and converted to a valid value for our slider. For example, we override the virtual function Slider::getTextFromValue() by using the Decibels::toString() function (in the DecibelSlider class) like so: It also provides a simple means of converting values in decibels to a String object. The Decibels class contains a number of static functions necessary for the conversion between the values in decibels and linear gain. Note INF refers to infinity where -INF dB is treated as silence for the purposes of our application. This is to customise the way that values are converted such that we can display -INF dB when the level drops very low. While a suffix to the text displayed within a Slider object's text box can be added using the Slider::setTextValueSuffix() function, we need one more customisation.
![audio decibel convert to linear scale audio decibel convert to linear scale](https://salfordacoustics.co.uk/wp-content/uploads/2019/01/decibel-scale.jpg)
In this custom slider class the text box interface is customised to display the value in decibels. This is achieved by creating a custom slider class, DecibelSlider that inherits from the Slider class. Notice this time that the text displayed adjacent to the slider not only shows the value in decibels but it also shows the suffix "dB". The demo project main window showing the control slider in decibels. The user interface for the demo project is shown in the following screenshot. Most audio applications express gain to users in decibels as this often feels more natural as the values are varied (or compared). This value in decibels needs to be converted to a linear gain value before being used in the audio processing algorithm. This time the slider value is represented in decibels. In a similar way to the Tutorial: Control audio levels demo project, the demo project for this tutorial presents a window containing a single slider. You should have followed that tutorial before continuing. Note This tutorial leads on from Tutorial: Control audio levels. If you need help with this step, see Tutorial: Projucer Part 1: Getting started with the Projucer. Unzip the project and open the first header file in the Projucer. This is a more common way in which to present audio level values to the user within audio applications.ĭownload the demo project for this tutorial here: PIP | ZIP. This tutorial shows how to process audio to change its output level using the decibel scale.