Thomas Hauser
Thomas Hauser

Reputation: 53

Audio changing speed during gameplay

So I am making a game in pygame and my problem is that a certain sound effect will play at different speeds. As far as I can tell there are only two speeds that it will play, sometimes it will play normally, and another time it might play noticeably faster.

Here are some sections of my code, I would include more but I can't imagine anywhere else that the problem might be. Any idea what can cause this?

pygame.mixer.pre_init(44000, -16, 2, 512)
pygame.mixer.init()
pygame.init()

.

rainbowPip = pygame.mixer.Sound('snakey_files/sounds/score up.ogg')
allPips = [redPip,gldPip,dmdPip,crashPip,btnPip,objPip,rainbowPip]
playSound = pygame.mixer.Sound.play
stopSound = pygame.mixer.Sound.stop

.

    if stats1['chime'] == 'on':
        playSound(rainbowPip, -1, 0, 200)
    elif stats1['chime'] == 'off':
        stopSound(rainbowPip)

Thanks.

Upvotes: 1

Views: 1928

Answers (1)

Anil_M
Anil_M

Reputation: 11443

Speed of audio can be varied by varying sampling rate. For a typical wav file it can be 44100 which can be doubled or halved to speed-up or slow-down.

I do not have your complete code, so I created a demo code to explain the concept. Here the input audio file piano.wav is first played at normal speed and then made to run double the original speed. Notice option to slow down speed by using conversion factor of less than one.

Hopefully you will be able to use the code in your program.

Demo Code

import pygame, wave, time

def checkifComplete(channel):
    while channel.get_busy():     #Check if Channel is busy
        pygame.time.wait(800)  #  wait in ms
    channel.stop()

#Set speedUp / Slowdown Factor
#ChangeRATE = 0.5  # set <1 to slow down audio
ChangeRATE = 2    # set >1 to speed up audio


#Define input / output audio files
music_file = "piano.wav"
changed_file = "changed.wav"
swidth = 2

#set up the mixer
freq = 44100     # audio CD quality
bitsize = -16    # unsigned 16 bit
channels = 2     # 1 is mono, 2 is stereo
buffer = 2048    # number of samples

pygame.mixer.init(freq, bitsize, channels, buffer)
pygame.mixer.init()

#Create sound object for Audio
myAudio1 = pygame.mixer.Sound(music_file)

#Create a channel for audio
myChannel1 = pygame.mixer.Channel(1)

#Play Audio
print "Playing audio : ", music_file
myChannel1.play(myAudio1)
checkifComplete(myChannel1) #Check if Audio1 complete

#Open current audio and get parameters
currentSound  = wave.open(music_file, 'rb') #Open input file
sampleRate = currentSound.getframerate()  #Get frame rate
channels = currentSound.getnchannels()  #Get total channels
signal = currentSound.readframes(-1) #Load all frames

#Create new audio with changed parameters
newSound = wave.open(changed_file, 'wb')
newSound.setnchannels(channels)
newSound.setsampwidth(swidth)
print "sampleRate=", sampleRate
newSound.setframerate(sampleRate * ChangeRATE)
sampleRate2 = newSound.getframerate()  #Get new frame rate
print "sampleRate2=", sampleRate2
newSound.writeframes(signal)
newSound.close()

#Create sound object for Changed Audio
myAudio2 = pygame.mixer.Sound(changed_file)

#Create a channel for Changed audio
myChannel2 = pygame.mixer.Channel(2)

#Add changed audio to Channel and Play Changed Audio
print "Playing audio2 : ", changed_file
myChannel2.play(myAudio2)
checkifComplete(myChannel2)

Program Output

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Playing audio :  piano.wav
sampleRate= 44100
sampleRate2= 88200
Playing audio2 :  changed.wav
>>> 

Upvotes: 1

Related Questions