官术网_书友最值得收藏!

Playing the drum machine

Now that we have a mechanism to load drum samples and a mechanism to define beat patterns in place, let's add the ability to play these beat patterns. In many ways, this is the core of our program.

Let's 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 the temporal gap between them.

主站蜘蛛池模板: 怀化市| 南雄市| 湟中县| 桦川县| 临湘市| 桑日县| 阳高县| 洮南市| 漾濞| 买车| 保亭| 轮台县| 盘锦市| 永年县| 尚义县| 牙克石市| 禹城市| 周宁县| 滁州市| 祁连县| 新泰市| 康平县| 盐边县| 永济市| 抚州市| 墨江| 涿鹿县| 青阳县| 日照市| 逊克县| 松江区| 清新县| 福泉市| 文昌市| 彭州市| 伽师县| 丰台区| 额济纳旗| 安福县| 平舆县| 临沧市|