Playing the drum machine
Now that we have a mechanism to load drum samples and a mechanism to define beat patterns in place, let us add the ability to play these beat patterns. In many ways, this is the core of our program.
Let us first understand the functionality that we want to achieve here.
Once the user has loaded one or more drum samples and has defined a beat pattern using the toggle buttons, we need to scan each column of the pattern to see if it finds a green button (a True
value in our data structure).
If the value is True
for a given location in the matrix, our code should play the corresponding drum sample before moving ahead. If two or more drum samples are selected in the same column, all the samples should play almost simultaneously.
Moreover, there should be a fixed time gap between the playing of each successive column, which will define the tempo of the music.
To achieve this functionality, we need to import the pygame
module, to play the sounds, and the time
module, to define...