pygame mixer music change playback speed - python

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.

Related

Movie and Sound component won’t play simultaneously in PsychoPy

I am trying to develop a basic experiment in the PsychoPy Builder where a silent movie plays in the background while I go through a .csv list to play a sequential series of about 100 very short sounds (about 300 milliseconds each). When I play the .csv list by itself, the sounds play perfectly like I would expect, but when I add the video component, only the first sound in the list will play and it won’t play the entire list of sounds. The video does continue to play, but somehow seems to interfere with the sound component or reading through the .csv file.
I tried deactivating the audio of the silent movie because I thought this could interfere with the sound component, but this does not resolve the issue.
It seems the program is waiting for the video to end before it can repeat the loop and go to the next sound in the list, but instead the program just closes as soon as the video ends. Furthermore, I want the single video to be playing while the sound component loops through the list, but I don't know how to apply the loop only to one component and not the video component that is supposed to play simultaneously with the sounds.
I feel like this should be very easy to achieve, yet I could not find someone with a similar issue when googling a solution. Your help is much appreciated.

How can I make a sound trail off at the end in Python?

I'm using winsound in Python to play a short Om chanting sound at the end of my program (in order to emphasize how cool the plot generated by the program is). The Om sound I'm playing is stored in a .wav file. I'd like to make the sound trail off near the end of the clip, so that the volume smoothly reduces to zero and the clip does not end abruptly. How can I make that happen?
Here's the code I'm using to play a sound:
import winsound
winsound.PlaySound('om1.wav', winsound.SND_FILENAME)
Your best bet may be to edit the wav file. Add a fade out with audacity (open source), then just use your existing code to play it
winsound does't seem to provide fade-out. You may edit the .wav file using e.g. Audacity.

Pygame Audio Stops When Window Switches?

I wrote a music player in Python using Pygame and Pydub(I used Pygame for actually playing the music, while Pydub is probably unrelated to the issue).
The music works fine even when windows are switched unless I switch to another Pygame window. I thought this effect would go away if I compiled it(cx_freeze), but that didn't work.
So I was wondering if there is any way to let the music keep playing when the window is switched to another Pygame window.
I used pygame.mixer.music instead of Sound objects if that might somehow be related.
Thanks in advance!
Not exactly a fix, but it seems that if I disable the mixer(pygame.mixer.quit()) it doesn't cancel out the audio. Unfortunately, I haven't found a way to use audio in two pygame windows at once.
Feel free to post another answer if anyone figures out a full fix.

Python - Pygame - Get if specific sound is playing

Pygame's mixer module has pygame.mixer.get_busy which returns a simple boolean.
My problem is that I have sound playing constantly, like explosions and gunshots, and need to know when a specific sound is playing to prevent game dialogue from overlapping.
I considered making a list of currently playing dialogue, creating a timer that counts down as each sound is triggered,but that would require me to add an sound Effects (my module that handles sounds) update in the main game loop.
This seems messy, and like a giant slowdown.
Is there a cleaner way to do this?
You can use the channel object.
Play a specific type of audio in a channel and check to see if the sound is playing.
import pygame
def main(filepath):
pygame.mixer.init()
# If you want more channels, change 8 to a desired number. 8 is the default number of channel
pygame.mixer.set_num_channels(8)
# This is the sound channel
voice = pygame.mixer.Channel(5)
sound = pygame.mixer.Sound(filepath)
voice.play(sound)
if voice.get_busy():
#Do Something

Pygame - Play sounds simultaneously

I am making a Game using Pygame and what I'm trying to do is to have a main sound for every level and some default sounds being heard when collecting points (e.g.)
So, I load the main level music using:
pygame.mixer.music.load(music_file)
pygame.mixer.music.play(-1)
Now, what I want to do is to play a specific sound whenever a player collects a point. I cannot stop the music using:
pygame.mixer.music.stop()
pygame.mixer.music.load(point_music)
pygame.mixer.music.play()
because the level's music will stop playing.
So, I've tried doing something like this:
points_sound = pygame.mixer.Sound("point.mp3")
points_sound.play()
I know that sound playing in pygame runs on its own thread but I am sure that the program/game does not terminate prior to finish playing the sound.
Long story short: The player can collect points but I am unable to make pygame play the collecting points sound.
As sr2222 said in comments:
Docs say mp3 support is limited for music and that only OGG and WAV
are supported for Sound. Have you tried one of the formats that are
officially supported?
Try out OGG or WAV formats for your sounds instead, that should work.
points_sound.play() should return a channel object. This object is necessary for playing the sound.
points_channel = points_sound.play()
This has me helped in my own case.

Categories