Waveshaping: Creating A Guitar Distortion Effect
For the final post on this blog, I will finally dive into
some more signal processing work for my guitar. To recap and add some context
here, we have previously explored how audio programming works, with samples
being taken from input sound or being generated for synthesizing sound. We
explored playing with some different outputs we can create and how random
values can make a white noise generator, and values output in a wave pattern
can create a particular frequency. Finally, we cracked into the Juce DSP module
to make our lives a little easier and let some of the details be handled behind
the scenes with a few exposed methods we can use to process our audio signal.
Now I want to use this knowledge and apply it to an incoming guitar signal and use
some wave shaping functions to create cool distortion effects and GUI to
manipulate them. For this I will pick up where we left off and use the DSP module’s
wave shaper class for this task.
Once
again you will need to start with the “basic plugin” template when you make a
new project. The code I have written for this final blog example is pretty straight
forward. Like before there is a volume slider- this time a rotary knob – that will
be used to control the volume. I used a DSP gain object to control the output
level like in the previous example, so if you have questions on this process
please refer back to that post. Next, I created a series of DSP wave shaper
objects in the PluginProcessor.h file and passed in a different function for each
of them. This will become relevant later as each of these functions will shape
the wave differently. Next I had to set up the prepareToPlay() function in the
PluginProcessor.cpp file. Here I instantiated and set up a dsp::ProcessSpec
object like before and then called the prepare method on each of my DSP module
Objects (Gain and WaveShaper objects).
Next, I created
several sliders that basically function as on/off switches like you might have
on a physical amplifier and a series of wave shaper objects each with a
different function passed in for how they will shape the input. The sliders can
only take a value of zero or one with zero being deactivated and one being
activated. When each switch is turned on a different wave shaper object is activated,
and its process method will be called on the audio block to be output. This
means all the wave shaping functions can be stacked, but the main point of the
program was to just hear each function individually to experiment with the
sounds. Having more options is not a problem here though, as it just gives us
more sounds to play with.
After setting
up the prepareToPlay() method and sliders I set up my processBlock() method by instantiating
an AudioBlock object and then setting up a few if statements that check if each
slider is activated, and for each, call the process method of the corresponding
wave shaper object. At the end of this method I call the process method of the
gain object to multiply the final output by the value of the volume slider,
cleanly cutting volume to the desired level or not at all if turned all the way
up. The DSP module really comes in handy here as we do not have to loop through
the samples of the audio output buffer and manipulate each value like we did
with the white noise generator, the sine wave synthesizer, and my first volume
knob example. The DSP objects have their own process method that handles this
for us so we just have to call them in the order we want the objects to affect the
signal.
Each of the wave shaping functions cause a distortion of the signal and this is similar to the clipping that naturally happens in analog vacuum tube driven amplifiers. There are other factors that affect the final sound of a real guitar amplifier such as the speaker cabinet and any frequency filtering that is done to the signal before it is output, but it is still cool to be able to build a program that mimics this process at all.
I hope this introduction to audio
programming was interesting and helpful to you if you are just starting out with
this technology. Personally, I felt that it could be difficult finding some
explanations for even some of the most basic concepts of audio programming so I
will be happy if this blog at least gave you an idea of what audio programming
looks like in a very big picture sense. Let me know if there are any final questions
and/or what you thought of the programs if you successfully built and ran them.
Comments
Post a Comment