Loading drum samples

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.

Loading drum samples

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.

Engage Thrusters

  1. First we will import the required modules. To open the sound file, we will use the 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
  2. Next, we will add Attributes to track the loaded samples. The user will invariably load more than one drum sample. Therefore, we need to track the Entry widget where the drum sample was loaded, the location of each of the drum samples, and a number indicating the current drum number. Accordingly, we create two lists called 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)
  3. We then add a 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))
  4. Finally, we code our 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:

    • We define a callback function within our function because we need to track several drum samples.
    • To track the widget, through which a sound sample has been loaded, we set the self.current_drum_no value to be equal to the drum_num value received as an argument from the button command callback.
    • In a 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.
    • Using os.path.basename from the os module, we obtain the filename from the file path and insert it into corresponding Entry widget.
    • If askopenfilename fails, we use tkMessageBox.showerror to display a custom error message.

Objective Complete – Mini Debriefing

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.151.44