I am using pygame (version = 2.0.1) in python==3.9.1 in windows OS laptop. I use the pygame.mixer module to play my mp3 file. My code:
from time import sleep
from pygame import mixer
audio_length = 5.184
mixer.init(frequency=26500)
# mixer.init(frequency=44100)
mixer.music.load("Test.mp3")
mixer.music.play()
sleep(audio_length)
mixer.music.stop()
mixer.quit()
The above code works without errors.. BUT the audio is played at the same default frequency no matter what value I set in mixer.init().
This seems to be quite strange because the above code works perfectly fine (with frequency change) in python 3.7 and pygame 1.9.6
I realised this only after i thought about upgrading my python version.
Is this a bug which I have to report? In that case how do I proceed so?
Or... is it just that I'm not following the new way in python 3.9?
In case, the file I used for testing is required, you can download from https://drive.google.com/file/d/1vd7aO5ahFBHfoT97_0RSBU4reNAWseB6/view?usp=sharing
This is a known issue that hasn't been resolved as of today.
https://github.com/pygame/pygame/issues/2435
Mixer init expects to also get the size and the channels, so you will have to do it like this:
mixer.init(frequency=26500, size=-16, channels=2)
For more info, see pygame docs: https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.init
Related
Need to mention that im new with Python and i decided to create a bot for multiplayer game, to autobuy items on auctionhouse, using opencv and pyautogui, so far everything was going pretty well, the cursor was heading to the right point on the screen (reload auction), but
pyautogui.click(clicks=1) isnt working in game window.
IDE (PyCharm) is running with admin rights, googled alot about the topic, but nothing works so far. Will be pleased if anyone could help me, this is my first big project i really want to work with, so hopefully you guys can help me :D
additional info: game uses Battle Eye anticheat, engine Java (probably... Game is called Stalcraft, you can find it on steam, looks like its something Minecraft-based, but im not sure about it)
OS: Win 10 x64
Python:3.11
What i tried:
pyautogui library (tried pyautogui.MoveTo(x,y) and the method with pyautogui.locateCenterOnScreen("whatever.png",confidence=0.85 Need to mention that first method works only in IDE, the second one based on img recognition also works with browser. Tried this in other apps, but no results. It's just hovering cursor on the right place, but no clicks at all)
pydirectinput library
Here's what i got so far
import cv2
import random
import pyautogui
from time import sleep
import imutils
import numpy as np
import pydirectinput
pyautogui.FAILSAFE=True
rng=random.uniform(0.87, 1.3)
sleep(5)
pyautogui.size()
print(pyautogui.size())
pyautogui.position()
print(pyautogui.position())
pyautogui.moveTo(x=1344, y=342, duration=rng)
pyautogui.click(1344, 342, clicks=5)```
This might be very silly. But I had no luck with .click. In my sample I used .moveTo as you did, but then .mouseDown() and .mouseUp() to simulate a click. Also sometimes it required a delay between the two. I wonder if that combo would help instead?
pyautogui.mouseDown()
sleep(1)
pyautogui.mouseUp()
quick question.
I'm running pygame under linux just to play some audio files.
I've got some .wav files and I'm having problems playing them back at the right speed.
import pygame.mixer, sys, time
#plays too fast
pygame.mixer.init(44100)
pygame.mixer.music.load(sys.argv[1])
pygame.mixer.music.play()
time.sleep(5)
pygame.mixer.quit()
#plays too slow
pygame.mixer.init(22100)
pygame.mixer.music.load(sys.argv[1])
pygame.mixer.music.play()
time.sleep(5)
pygame.mixer.quit()
I've ggogle code searched some stuff but everybody seems to be fine calling the init function with its default parameters. Can others try running this script and seeing if they get the same behavior or not? Does anybody know how to speed it up? Or adjust the speed for each file?
Thanks.
I had some mp3 audio tracks playing back slowed down. I updated the mixer frequency to be based on the mp3 sample rate using mutagen like so:
import pygame, mutagen.mp3
song_file = "your_music.mp3"
mp3 = mutagen.mp3.MP3(song_file)
pygame.mixer.init(frequency=mp3.info.sample_rate)
pygame.mixer.music.load(song_file)
pygame.mixer.music.play()
And it fixed the problem.
To improve Chris H answer. Here is a example of how to use the wave library.
import wave
import pygame
file_path = '/path/to/sound.wav'
file_wav = wave.open(file_path)
frequency = file_wav.getframerate()
pygame.mixer.init(frequency=frequency)
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
Remember that if you want to change frequency or any other parameter used in pygame.mixer.init you must call pygame.mixer.quit first. Pygame documentation
Open your audio file in a free audio tool like Audacity. It will tell you the sampling rate of your media. It will also allow you to convert to a different sampling rate so all your sounds can be the same.
I figured it out...
There is a wave module http://docs.python.org/library/wave.html and it can read the sample rate for wav files.
If you're using Ogg Vorbis (.ogg) encoding, the same problem of stuttering audio happens. You'll have to read the frequency of what you're trying to play before initializing the mixer object.
Here's how to play .ogg audio with appropriate frequency using pygame.
from pyogg import VorbisFile
from pygame import mixer
# path to your audio
path = "./file.ogg"
# an object representing the audio, see https://github.com/Zuzu-Typ/PyOgg
sound = VorbisFile(path)
# pull the frequency out of the Vorbis abstraction
frequency = sound.frequency
# initialize the mixer
mixer.init(frequency=frequency)
# add the audio to the mixer's music channel
mixer.music.load(path)
# mixer.music.set_volume(1.0)
# mixer.music.fadeout(15)
# play
mixer.music.play()
I have Pi Zero running Raspbian Buster Lite, there's no display attached. It runs a python program at startup (crontab #reboot entry), something like this:
#reboot sudo su username /home/username/launcher.sh >> /home/username/crontab.log 2>&1
I have to initialize pygame display, because I need to process the Sound object end events. Without initializing the display, the events are not triggered.
The code breaks at this line, with error "pygame.error: Unable to open a console terminal"
os.putenv('SDL_VIDEODRIVER', 'dummy')
pygame.display.init() # error at this line
My code used to work in the previous Raspbian (Stretch) version. I have experimented with all kinds of drivers, added/removed pygame.display.set_mode((1, 1)), but no luck.
I am aware of the other posts that tackle with the same question, but I have exhausted all those approaches - changing the driver to fbcon, skipping the set_mode line, etc. The only thing that works is running the program as root:
#reboot /home/username/launcher.sh >> /home/username/crontab.log 2>&1
But it's a bad idea, from the security perspective. I'd rather run it as unpriviliged user.
Is there a new kind of hack to get it past the display.init()?
pygame.init initializes all Pygame modules. You do not need to initialize all Pygame modules, only the ones you need. For playing music it is sufficient to initialize pygame.mixer. Some examples
import pygame
pygame.mixer.init()
pygame.mixer.music.load('music.mp3')
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.delay(10)
pygame.mixer.quit()
import pygame
pygame.mixer.init()
my_sound = pygame.mixer.Sound('music.mp3')
my_sound.play(0)
while pygame.mixer.get_busy():
pygame.time.delay(10)
pygame.quit()
import pygame.mixer
pygame.mixer.init()
my_sound = pygame.mixer.Sound('sound.wav')
my_sound.play(0)
pygame.time.wait(int(my_sound.get_length() * 1000))
pygame.mixer.quit()
However, you cannot use the event module because it is tied to the display module.
Is there any way to control where pygame creates the game screen? It seems to always create it in the same general area but not in a consistent location.
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = str(position[0]) + "," + str(position[1])
as per http://pygame.org/wiki/FrequentlyAskedQuestions
You can also just center the screen with
import pygame, os
os.environ['SDL_VIDEO_CENTERED'] = '1'
Note that these should be done before you initialize pygame in the main loop. I do it right after I import os for example. And since they are not actually part of pygame, you can probably use it elsewhere, though things like gtk and wxpython provide their own mechanisms.
Positioning of windows is not handled by the client application. It's handled by the Window manager (metacity etc.).
The SDL library on which PyGame is based does have a few environment variables which can be used to give hints to the Window manager. These are hints which the WM may ignore but it's the best you can do.
The comments over here have an example.
im currently having trouble with the python 3rd party library pygame.
Other posts on stackoverflow didn't help me so i opened a new post.
I want to load a *.mp3 file into the program and play it. My code so far (copied from other posts):
import pygame,time,sys
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
print ("Mixer settings", pygame.mixer.get_init())
print ("Mixer channels", pygame.mixer.get_num_channels())
pygame.mixer.music.set_volume(1.0)
pygame.mixer.music.load("test.mp3")
pygame.mixer.music.play()
clock = pygame.time.Clock()
while pygame.mixer.music.get_busy():
clock.tick(30)
Here is a link to a screenshot from the Windows Sound panel: http://i.imgur.com/fUvJXof.png
I see that there is some output, but i cant hear anything on my headphones.
I hope some has an idea what the problem is.
Thanks
Check your hardware/audio drivers. It doesn't sound like (pardon the pun!) an issue with python or pygame. Does your audio work outside python? Also, if you've got other programs open, they can sometimes take control of your audio drivers and not allow other programs (in this case python) to output.