Guitar Effects — An Object Oriented Design
By: Nitish Sharma
Tags: Decorator Pattern, Guitar, Guitar Effects, Object Oriented, Design Patterns

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

PRS SE Tremonti — Vintage Cherry

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.

Guitar pedal setup

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

Dec­o­ra­tor is a struc­tur­al design pat­tern that lets you attach new behav­iours to objects by plac­ing these objects inside spe­cial wrap­per objects that con­tain the behaviours.

Decorator pattern UML diagram

Com­po­nent declares the com­mon inter­face for both wrap­pers and wrapped objects

declares the com­mon inter­face for both wrap­pers and wrapped objects Con­crete Com­po­nent is a class of objects being wrapped. It defines the basic behav­iour, which can be altered by decorators”

is a class of objects being wrapped. It defines the basic behav­iour, which can be altered by decorators” Base Dec­o­ra­tor class has a field for ref­er­enc­ing a wrapped object.”

class has a field for ref­er­enc­ing a wrapped object.” Con­crete Dec­o­ra­tors define extra behav­iours that can be added to com­po­nents dynam­i­cal­ly

Classes and Objects

In simple words, with the Decorator pat­tern 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

Component interface ElectricGuitar — Concrete component

Concrete component GuitarPedal — Base decorator

Base decorator Concrete decorator classes for flights —ReverbPedal, DelayPedal, DistortionPedal, AcousticPedal, SaxophonePedal, VolumePedal

Class diagram for the system

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 ) ) )"

References