- Tkinter GUI Application Development Blueprints(Second Edition)
- Bhaskar Chaudhary
- 189字
- 2021-06-24 18:35:14
Initializing pygame
The pygame module is a set of highly portable modules that runs on most operating systems. We will use the mixer module from pygame to play the sound files.
Assuming that you have installed the package, let's begin by importing pygame (see code 3.06.py):
import pygame
According to the official API documentation of the mixer module at http://www.pygame.org/docs/ref/mixer.html, we need to initialize pygame before we can play back the audio files.
We initialize pygame in a new method called init_pygame (see code 3.06.py):
def init_pygame(self):
pygame.mixer.pre_init(44100, -16, 1, 512)
pygame.init()
The mixer.pre_init method is a special requirement for our drum machine because the lack of it causes a lot of sound lagging. We will not get into the details of audio programming here, but suffice to say that the arguments to the pre_init method are as follows:
pre_init(frequency=22050, size=-16, channels=2, buffersize=512)
After pygame is initialized like this, the documentation suggests the following code to play the sound. Let's add this to our code as well (see code 3.06.py):
def play_sound(self, sound_filename):
if sound_filename is not None:
pygame.mixer.Sound(sound_filename).play()
- Advanced Machine Learning with Python
- JavaScript 從入門到項目實踐(超值版)
- Practical Internet of Things Security
- 算法精粹:經典計算機科學問題的Python實現
- 新編Premiere Pro CC從入門到精通
- Elastic Stack應用寶典
- 基于Swift語言的iOS App 商業實戰教程
- JavaScript 程序設計案例教程
- Mastering Android Game Development
- Mastering Backbone.js
- Mastering C++ Multithreading
- Java零基礎實戰
- Hands-On Full Stack Development with Spring Boot 2.0 and React
- Image Processing with ImageJ
- Android應用開發實戰