Support for multiple beat patterns

Our drum program is now functional. You can load drum samples and define a beat pattern and our drum machine will play it.

Let's now extend our drum machine to create more than one pattern in the same program. This will provide us with the ability to play different patterns simply by changing the pattern number. This gives the user the ability to make different beats for the intro, verse, chorus, bridge, and other parts of a song. The pattern-changing user interface is highlighted in red in the following screenshot:

At the very outset, we have an Entry widget adjacent to the Pattern Number Spinbox widget. We want to display the current pattern number in that Entry widget. We accordingly create a method, display_pattern_name(), which does this task (see code 3.08.py):

 def display_pattern_name(self):
self.current_pattern_name_widget.config(state='normal')
self.current_pattern_name_widget.delete(0, 'end')
self.current_pattern_name_widget.insert(0,
'Pattern {}'.format(self.current_pattern_index))
self.current_pattern_name_widget.config(state='readonly')

We want the pattern name to display in the text widget when the program initially launches. Therefore, we modify our create_top_bar() method to include a call to this newly defined method (see code 3.08.py).

A change of pattern requires several changes. First of all, let's modify the on_pattern_changed() command callback to call a new method, change_pattern(), as follows (see code 3.08.py):

 def on_pattern_changed(self):
self.change_pattern()

Next, let's define the change_pattern() method:

 def change_pattern(self):
self.current_pattern_index = int(self.pattern_index_widget.get())
self.display_pattern_name()
self.create_left_drum_loader()
self.display_all_drum_file_names()
self.create_right_button_matrix()
self.display_all_button_colors()

The preceding code should almost read like plain English and the steps involved in a change of pattern should be self-explanatory.

This completes coding our drum machine to support multiple beat patterns. Go ahead and run code 3.08.py. Load some drum files, define the first beat pattern, and play it. Change the beat pattern using the Spinbox widget at the top left,
load new drums, and define a new pattern. Then, play that pattern. While it is playing, try switching to your first beat pattern. The change should happen seamlessly.

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

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