简体   繁体   English

在Pygame中更改sonid频率

[英]Change sonid frequency in Pygame

I want to change sonid frequency if a button is pressed. 如果按下按钮,我想更改sonid频率。 I'm using Pygame, specifically the mixer module. 我正在使用Pygame,特别是混音器模块。 When I set the frecuency for the first time I can't set again. 当我第一次设置频率时,我无法重新设置。 I have read the Pygame's docs and exist the pre_init() method, this set the frequency before that music play. 我已经阅读了Pygame的文档并且存在pre_init()方法,这在音乐播放之前设置了频率。 Right now I have this: 现在我有这个:

def play_full(self, frequency=22050):
    """Play if a button is pressed"""
    pygame.mixer.pre_init(frequency=frequency)
    pygame.mixer.init()
    pygame.mixer.music.load(os.path.join(os.path.dirname(__file__), "../music/hi.mp3"))
    pygame.mixer.music.play()

But the frequency doesn't change with I send the parameter to the function. 但是当我将参数发送到函数时,频率不会改变。 How I can change the frequency according to button pressed?, Are there some example?. 如何根据按下的按钮改变频率?,是否有一些例子? By the way, I'm using PyQt4 for GUI. 顺便说一下,我正在使用PyQt4进行GUI。

In pygame.mixer.init() you need to set the frequency the size and the channels, In your case you will write a code like this: 在pygame.mixer.init()中,您需要设置频率大小和通道,在您的情况下,您将编写如下代码:

import pygame.mixer

def play_full(self, my_frequency):
    pygame.mixer.init(my_frequency, size=-16, channels=2)
    pygame.mixer.music.load('YOUR_FILE_DIRECTORY_GO_HERE')
    pygame.mixer.music.play()

That way you can set frequency for what you want to do. 这样你就可以为你想做的事情设定频率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM