简体   繁体   中英

Change sonid frequency in Pygame

I want to change sonid frequency if a button is pressed. I'm using Pygame, specifically the mixer module. 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. 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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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