- 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()
- Web程序設(shè)計(jì)及應(yīng)用
- MATLAB圖像處理超級(jí)學(xué)習(xí)手冊(cè)
- Building Mobile Applications Using Kendo UI Mobile and ASP.NET Web API
- Windows Server 2012 Unified Remote Access Planning and Deployment
- 高級(jí)語(yǔ)言程序設(shè)計(jì)(C語(yǔ)言版):基于計(jì)算思維能力培養(yǎng)
- 西門子S7-200 SMART PLC編程從入門到實(shí)踐
- HTML 5與CSS 3權(quán)威指南(第3版·上冊(cè))
- HTML5+CSS3 Web前端開發(fā)技術(shù)(第2版)
- Java程序設(shè)計(jì)案例教程
- 基于SpringBoot實(shí)現(xiàn):Java分布式中間件開發(fā)入門與實(shí)戰(zhàn)
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)(第二版)
- Web編程基礎(chǔ):HTML5、CSS3、JavaScript(第2版)
- Android應(yīng)用程序設(shè)計(jì)
- 一覽眾山小:ASP.NET Web開發(fā)修行實(shí)錄
- JSP應(yīng)用與開發(fā)技術(shù)(第3版)