Guitar Effects — An Object Oriented Design
What you see above is pretty close to heaven for a guitarist. I have been playing guitar myself for over a decade now. I started out with a cheap acoustic guitar, played every song I could on it for around 3 years
But no matter what song is played, the sound of an acoustic guitar has its limits. You can’t make it sound louder beyond a point of heavy strumming, you can’t make the notes sustain long enough and you definitely can’t do this.
So after some protest and struggle, I convinced my parents to buy my first electric guitar
This cherry coloured beauty opened the doors for me to experiment with sound.
How?
With guitar pedals.
Guitar Pedals
A pedal or effect pedal is an electronic device that alters the sound of a musical instrument or other audio source through audio signal processing.
Some of the common pedals include distortion, often used with electric guitar in blues and rock music; volume pedals, which affect loudness; time effects, such as reverb and delay, which create echoing sounds and emulate the sound of different spaces and many more.
Instead of directly plugging my guitar into the amplifier (sound output), I bought a few of the above mentioned pedals and plugged my guitar into them and then into the amplifier.
Each pedal comes with a foot-switch that allows the player to toggle it on or bypass it, by pressing it. So while the guitarist is playing the guitar, they can enable or disable the effect with their foot.
Let’s get crackin’
So let’s design this in the object oriented way.
I can plug my guitar directly into the amp and get a clean unmodified sound or I can have the option of toggling different sounds(effects) using guitar pedals in my setup
The pedals are basically providing different behaviours which can be attached to the guitar
Based on this behaviour of this system, we can incorporate the Decorator Design Pattern
Decorator Design Pattern
Decorator is a structural design pattern that lets you attach new behaviours to objects by placing these objects inside special wrapper objects that contain the behaviours.
- Component declares the common interface for both wrappers and wrapped objects
- Concrete Component is a class of objects being wrapped. It defines the basic behaviour, which can be altered by decorators”
- Base Decorator class has a field for referencing a wrapped object.”
- Concrete Decorators define extra behaviours that can be added to components dynamically
Classes and Objects
In simple words, with the Decorator pattern in place, the guitar can be wrapped with a combination of guitar pedals to get different sounds/tones.
So classes for our design will be:
- Guitar — Component interface
- ElectricGuitar — Concrete component
- GuitarPedal — Base decorator
- Concrete decorator classes for flights —ReverbPedal, DelayPedal, DistortionPedal, AcousticPedal, SaxophonePedal, VolumePedal
The Code
An interface for the Guitar class and ElectricGuitar class based on it
GuitarPedal class serving as the base decorator
Various guitar pedals as concrete decorators, which can be used in combinations to produce different tones(behaviours)
This is how this system will function
guitar = ElectricGuitar.new
final_guitar_tone(guitar)# => "Final Tone: Clean"
# Apply a guitar effect using one of the guitar pedals (decorator)guitar = ReverbPedal.new(ElectricGuitar.new)
final_guitar_tone(guitar)# => "Final Tone: Reverb -> ( Clean )"
# Apply combination of guitar effects to get interesting tones
guitar = SaxophonePedal.new(
DistortionPedal.new(
ReverbPedal.new(
ElectricGuitar.new
)
)
)
final_guitar_tone(guitar)# => "Final Tone: Saxophone -> ( Distortion -> ( Reverb -> ( Clean ) ) )"