python is playing the sound in slowmotion.bitrate of the fle.
Below is the code:
import pygame,time
pygame.mixer.init()
pygame.mixer.music.load('backgroundmusic.mp3')
pygame.mixer.music.play(-1)
time.sleep(20)
pygame.mixer.music.stop()
When you export a sound file to mp3, there is a certain bit rate. This is basically how fast the sound plays. If the bit rate is wrong, the sound plays too fast or too slow. Pygame.mixer is set to play things at the bit rate that will already be default on the program that you save the file on, but if you downloaded the file online this could be difficult. It is best to just set the bitrate to the one of the file using the "size" argument of mixer.init(). However there may be another problem: While pygame.music may support mp3 files, I beleive pygame.mixer is intended for wav files. All this being said, your script doesn't have any issues, but it would be best to save it as a .wav file.
Related
I want to playback an .mp3 file at 50% it's original speed, naturally pitched down (i.e. keeping the pitch is not my requirement).
I am using pygame.mixer and found several solutions (such as this one) that suggest changing the frequency on initialization should do the job as shown below, however that does not work for me.
from pygame import mixer
mixer.init(frequency=22050) # my track.mp3 has a 44100 sample rate
mixer.music.load("track.mp3")
mixer.music.play()
Am I doing something wrong? No matter to what value i change frequency it does not at all change the playback speed or pitch of the music played.
I found this solution here, with which I am able to speed down my track and play it back using mixer.Sound(speed_change(sound, 0.5).raw_data()).play(), however this method does not allow me to use the mixer.music functionality such as seek(time) or setting the start time when using .play().
I am generally also open to solutions that are not using pygame mixer, however I do need to be able to stop and/or pause the playback and have a seek functionality.
One way to do this would be to use this Audio Slowdown Software.]1
This would have the advantage of being easily reusable without having to type extra code or extra lines.
I am trying to make a simple program that plays some narration, and has a sound effect on a trigger. Being new to python, I am just building up functions in layers. Getting music to work was easy, as it should be, but I cannot get sound to work.
I have read of a few people with similar issues, but either they are also unsolved, or the symptoms are slightly different, and the solutions don't work for me.
I have tried this on a pi3 (which is the end target), and windows 7, both running latest python 3, and pygame.
import pygame
import time
import os
pygame.mixer.init()
#pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init()
pygame.mixer.music.load("English.mp3")
pygame.mixer.music.play()
print (os.getcwd())
shot = pygame.mixer.Sound("gun-gunshot-02.wav")
shot.play()
while True:
Time.sleep(1) # for testing and irritation prevention if sound ever plays
shot.play()
I should also note that the sound effect does work if I play it using music, but of course it replaces the narration.
Error is as follows same on both machines, same with both mp3 and wav:
C:\Users\me\Documents\Interrupter
Traceback (most recent call last):
File "C:/Users/me/Documents/Interrupter/simpletest.py", line 11, in
shot = pygame.mixer.Sound("gun-gunshot-02.wav")
pygame.error: Unable to open file 'gun-gunshot-02.wav'
Thanks in advance
So I have solved the issue, which is of file compatibility.
I don't know what the right kind of wav file is (other than uncompressed). I had tried using a wav but it did not work.
However I finally tried .ogg since that was the only thing listed as working in the online manual for pygame. As soon as I used .ogg everything worked as planned.
For anyone else with this issue I used Audacity to export my .mp3 and .wav files to .ogg using defaults.
Pygame is supporting only 2 type of files.
OGG and WAV
Sound can be loaded from an OGG audio file
or
from an uncompressed WAV.
for more information please visit to
https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Sound
Okay here's my code:
import pygame
pygame.init()
pygame.mixer.init()
track1 = pygame.mixer.Sound("boink.ogg")
track1.play()
So I am using a mac and I used homebrew to download the pygame 64 bit version. Everything works well, but when I try to make sounds using the mixer, all that I hear is a clicking sound. Has anyone experienced this in the past that may be able to help?
Also I have tried this with many different ogg files, so it is not something wrong with the sound file.
Can you try this?
import pygame
import time
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
song = pygame.mixer.Sound('boink.ogg')
song.play()
time.sleep(song.get_length())
Not to sound crude, but have you tried playing a file without using any kind of object? , just play simple sound file and see if it works? I usually start that way.
Here is what I tried as basics (and it works). Do mind, this is on windows as I do not own a MAC, but it will provide insight into if at all file is playing.
Also, try playing different format files (wav , mp3 etc).
import pygame
pygame.mixer.init()
pygame.mixer.music.load('boink.ogg')
pygame.mixer.music.play(0)
I had the same click when playing an mp3 with the pygame.mixer.Sound and .play code.
This worked for me:
pygame.mixer.pre_init()
#then instantiate and start your controller
pygame.mixer.init()
#then in your button click or wherever
pygame.mixer.music.load('mysound.mp3')
pygame.mixer.music.play()
A few things you can try:
Make sure that the path to your sound file is correct.
Turns out Pygame's pygame.mixer.Sound() only accepts Mono WAV sound files.
for my second point, you can go here to convert your ogg file to mono wav files.
My PyGame mixer in 2.7 won't work with the sound option. I can make it work with mixer.music but not with mixer.sound, with mixer.sound it makes a small ticking noise and then stops. Code:
import pygame
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
song = pygame.mixer.Sound("song.mp3")
pygame.mixer.Sound.play(song)
No error, it just won't play and gives a small ticking noise.
On windows 7-x64 btw.
Usually, Pygame will not play mp3 files. You could test to see if .wav and .ogg files will play first, to make sure your code is correct (based on what you pasted, it seems to be right). I suggest converting your mp3 sounds to ogg for Pygame.
you just created an object called song.
instead of "pygame.mixer.Sound.play(song)" try this:
song.play()
This can easily be solved because your song file should be loaded as music, not as a normal sound. Therefore, the following code makes it work perfectly:
import pygame
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
pygame.mixer.music.load("song.mp3")
pygame.mixer.music.play()
Pygame does play mp3 files. I had the same problem but I found out the solution:
if you saved your mp3 file as 'filename.mp3', and you wrote down the .mp3 file extension yourself, then the filename in pygame's pygame.mixer.music.load() function must be written as 'filename.mp3.mp3', because python expects you to add the .mp3. Sometimes the .mp3 is already included in the filename if you manually saved it as that.
Therefore, try this:
pygame.mixer.music.load('filename.mp3.mp3')
For playing my sounds I have this working code:
import pygame
pygame.init()
pygame.mixer.pre_init(44100, 16, 2)
screen = pygame.display.set_mode([100,100])
print(pygame.mixer.get_num_channels(),"cannels")
sounda = pygame.mixer.Sound("track.mp3")
print("Length",sounda.get_length())
print("Volume",sounda.get_volume())
channela = sounda.play()
while channela.get_busy():
pygame.time.delay(100)
pygame.quit()
You can see I am mostly using .mp3 files.
But my problem is that it can play some sounds but others result the error which says that the sound can not be loaded. Are there any differences between mp3 files and do you know how to solve this problem?
Thanks in advance
So I have found out there can be differences in one sound format so that pygame has problems with it. With the program ocenaudio I can manipulate and save my sounds in multiple types of the .wav format. Pygame can deal with one of them.
But loading music has never been a problem...
If Pygame is not loading your audio, there are 3 possibilities.
Possibility 1:
Pygame cannot find your file. When saying the name of a file, make sure either the sound file is in the same directory as your program file, or you can give the entire file path instead (like '/home/person/sound.mp3', instead of just 'sound.mp3'.)
Possibility 2:
Your file is the wrong format. Though it is called .mp3, it may be a different type. Try using an audio conversion program.
Possibility 3:
Pygame just plain cannot read the file. Pygame works best with .ogg files, but sometimes has issues with other types. Just use an audio conversion program to convert it to .ogg, and it might work.
If none of these work, giving an exact error message would be appreciated.