- 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()
- SPSS數(shù)據(jù)挖掘與案例分析應(yīng)用實(shí)踐
- WildFly:New Features
- GraphQL學(xué)習(xí)指南
- INSTANT FreeMarker Starter
- 單片機(jī)C語(yǔ)言程序設(shè)計(jì)實(shí)訓(xùn)100例:基于STC8051+Proteus仿真與實(shí)戰(zhàn)
- C語(yǔ)言從入門(mén)到精通(第4版)
- 用Flutter極速構(gòu)建原生應(yīng)用
- Python深度學(xué)習(xí):基于TensorFlow
- AIRIOT物聯(lián)網(wǎng)平臺(tái)開(kāi)發(fā)框架應(yīng)用與實(shí)戰(zhàn)
- Node學(xué)習(xí)指南(第2版)
- Mastering Apache Storm
- Python自然語(yǔ)言理解:自然語(yǔ)言理解系統(tǒng)開(kāi)發(fā)與應(yīng)用實(shí)戰(zhàn)
- 工業(yè)機(jī)器人離線編程
- 計(jì)算機(jī)系統(tǒng)解密:從理解計(jì)算機(jī)到編寫(xiě)高效代碼
- 微服務(wù)設(shè)計(jì)