python unable 改變一個音效Pygame



python pygame play wav file (1)

如果我理解正確,你有25個聲音文件,你想要按照變速的順序播放它們。 這25個聲音文件的名稱按照它們的音高排序。

建立

該解決方案假設您將音頻文件放在一個文件夾中,並按順序逐漸增加音高。

代碼將根據速度計數器加載文件夾中按順序列出的文件[可根據需要修改]

代碼寫在Windows 8和Python 2.7上

示例代碼

import pygame.mixer, pygame.time
import os

mixer = pygame.mixer  
mixer.init()  #Initialize Mixer

#Your path to audio files
filepath = "C:\\yourAudioFilePath\\"

#Iterate through counter and  audio files

for x, i in zip(range(0,25),os.listdir(filepath)):
    if i.endswith(".wav"):
        mySoundFile = mixer.Sound(filepath + i)
        print "Speed Variable = " , x , " and file = ", i 
        channel = mySoundFile.play(0)
        while channel.get_busy():  #Check if Channel is busy
            pygame.time.wait(100)  #  wait in ms until song is played
        print "........"

產量

我按順序重新命名了一堆音頻文件以符合您的要求。

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 ================================
>>> 
Speed Variable =  0  playing file =  audio000.wav
........
Speed Variable =  1  playing file =  audio001.wav
........
Speed Variable =  2  playing file =  audio002.wav
........
Speed Variable =  3  playing file =  audio003.wav
........
Speed Variable =  5  playing file =  audio005.wav
........
Speed Variable =  6  playing file =  audio006.wav
........
Speed Variable =  7  playing file =  audio007.wav
........
Speed Variable =  8  playing file =  audio008.wav
........
Speed Variable =  9  playing file =  audio009.wav
........
Speed Variable =  10  playing file =  audio0010.wav
........
Speed Variable =  11  playing file =  audio0011.wav
........
Speed Variable =  12  playing file =  audio0012.wav
........
Speed Variable =  13  playing file =  audio0013.wav
........
Speed Variable =  14  playing file =  audio0014.wav
........
Speed Variable =  15  playing file =  audio0015.wav
........
Speed Variable =  16  playing file =  audio0016.wav
........
Speed Variable =  17  playing file =  audio0017.wav
........
Speed Variable =  18  playing file =  audio0018.wav
........
Speed Variable =  19  playing file =  audio0019.wav
........
Speed Variable =  20  playing file =  audio0020.wav
........
Speed Variable =  21  playing file =  audio0021.wav
........
Speed Variable =  22  playing file =  audio0022.wav
........
Speed Variable =  23  playing file =  audio0023.wav
........
Speed Variable =  24  playing file =  audio0024.wav
........
>>> 

我有大約25個類似的聲音文件,每個都有不同的頻率。 我已經寫了一些代碼來改變基於速度變化的聲音文件正在播放。 從理論上講,聲音的音高應該隨著速度的增加而增加(文件數越高,音高越高)。下面是我所寫的:

CurrentSound = 'CarEngine0.wav'
OldSound = CurrentSound

while True:

    CurrentSound = 'CarEngine' + str(abs(int(speed*2))) + '.wav'
    CarSound = pygame.mixer.Sound(CurrentSound)
    if not OldSound == CurrentSound:
        OldSound = CurrentSound
        CarSound.stop()
    CarSound.play(-1)

如果我打電話給CurrentSoundOldSound打印,他們確實會改變,但是聲音似乎不是。 我做錯了什麼,有沒有更好的辦法呢?