Our main objective is to play sound files, in the order of a beat pattern decided by the user. To do this we need to add sound files to the drum machine.
Our program does not have any preloaded drum files. Instead, we want to let the user select from a wide variety of drum files. Thus, besides the normal drum, you can play a Japanese tsuzumi, an Indian tabla, Latin American bongo drums, or just about any other sound that you want to add to your rhythm. All you need is a small .wav
or .ogg
file containing that sound's sample.
Let us code the ability to add this drum sample to our program.
The drum sample is to be loaded on the left bar, as shown in the preceding screenshot. We have already created buttons with folder icons to the left-hand side of our drum pad. The desired functionality is simple.
When a user clicks on any of the left buttons, they should open a file dialog letting the user choose a .wav
or .ogg
file. When the user selects the file and clicks on Open, the Entry widget next to that button should be populated with the name of the file. Further, the location of the drum sample file should be added to a list for playing it later.
tkFileDialog
module. We will also use the tkMessageBox
module to display certain pop-up messages. We will also need to extract the filename of the given sound sample using the os
module. Let us begin by importing the three modules (given in the following code) into our current namespace (refer to the same code present in 3.04.py
):import tkFileDialog import tkMessageBox import os
self.widget_drum_name
and self.widget_drum_file_name
to store the Entry widget instance and file location respectively.We also declare a variable self.current_drum_no
to track the current drum number.
We choose to initialize these variables and list under our initialization method __init__
(refer to the code in 3.04.py
):
def __init__(self): self.widget_drum_name = [] self.widget_drum_file_name = [0]*MAX_DRUM_NUM self.current_drum_no = 0
We then modify our create_left_pad
method to include a line that appends a list of all drum Entry widgets in our newly-created list self.widget_drum_name
:
self.widget_drum_name.append(self.drum_entry)
command
callback to the buttons in our create_left_pad
method to load drum samples, as shown in the following code snippet:button = Button(left_frame, image=tbicon, command=self.drum_load(i))
drum_load
method as follows (refer to the code in 3.04.py
):def drum_load(self, drum_no): def callback(): self.current_drum_no = drum_no try: file_name = tkFileDialog.askopenfilename(defaultextension=".wav", filetypes=[("Wave Files","*.wav"),("OGG Files","*.ogg")])Files","*.ogg")]) if not file_name: return try: delself.widget_drum_file_name[drum_no] except: pass self.widget_drum_file_name.insert(drum_no, file_name) drum_name = os.path.basename(file_name) self.widget_drum_name[drum_no].delete(0, END) self.widget_drum_name[drum_no].insert(0, drum_name) except: tkMessageBox.showerror('Invalid', "Error loading drum samples") return callback
The description of the code is listed as follows:
self.current_drum_no
value to be equal to the drum_num
value received as an argument from the button command
callback.try
block, we use tkFileDialog.askopenfilename
to get the filename of the drum sample. We then check whether a filename already exists in our filename list. If it does, we delete it.os.path.basename
from the os
module, we obtain the filename from the file path and insert it into corresponding Entry widget.askopenfilename
fails, we use tkMessageBox.showerror
to display a custom error message.In this iteration, we imported modules to handle dialogs and message boxes. We then added attributes to track drum samples. Finally, we added command
callbacks to buttons which when clicked open a dialog for the user to select drum samples.
Our code is now capable of loading drum samples and storing all necessary records that we will require to play beat patterns.
Next, let us turn our attention to playing the beat samples as per a user-defined pattern.
18.218.151.44