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. 

The drum sample is to be loaded on the left bar, as shown in the following screenshot:

Let's code the ability to add drum samples to our program.

We have already created buttons with folder icons on the left-hand side of our drum pad. Now we need to make it functional. 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 our data structure at the appropriate place.

First, we will import the required modules.

We will use the filedialog module to ask the user to select drum files. We have already used the file dialog module in Chapter 2, Making a Text Editor. The functionality here is very similar. We will also need to extract the filename of the given sound sample using the os module. Let's begin by importing the two modules (see code 3.05.py):

import os
from tkinter import filedialog

The buttons we created for uploading drum files are attached to the on_open_file_button_clicked method through a command callback. We created a dummy method earlier by that name. We now modify that method to add the required functionality (see code 3.05.py):

def on_open_file_button_clicked(self, drum_index):
def event_handler():
file_path = filedialog.askopenfilename
(defaultextension=".wav", filetypes=[("Wave Files",
"*.wav"), ("OGG Files", "*.ogg")])
if not file_path:
return
self.set_drum_file_path(drum_index, file_path)
self.display_all_drum_file_names()
return event_handler

The preceding method again returns a function, as we need to track which of the drum files was actually selected from all the rows of drum files. 

The preceding code does three things:

  • Asks the user for the file path using Tkinter's filedialog
  • Modifies the underlying data structure to save the provided file path
  • Calls another method to display the filename in the adjacent Entry widget

The next two methods are then responsible for displaying all drum names in the frontend (see code 3.05.py):

def display_all_drum_file_names(self):
for i, drum_name in enumerate(self.get_list_of_drum_files()):
self.display_drum_name(i, drum_name)

def display_drum_name(self, text_widget_num, file_path):
if file_path is None: return
drum_name = os.path.basename(file_path)
self.drum_load_entry_widget [text_widget_num].delete(0, END)
self.drum_load_entry_widget[text_widget_num].insert(0, drum_name)

The preceding method uses os.path.basename from the os module to obtain the filename from the file path.

This completes the section. Our code is now capable of loading drum samples and storing records of all file paths in the data structure. Go ahead and try loading some drum samples (see code 3.05.py) and the program should display the name of the drum file in the adjacent Entry widget.

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

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