python - 改变音效 Pygame

标签 python python-3.x audio pygame

我有大约 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 变量进行打印,它们会发生变化,但声音似乎没有变化。我做错了什么,是否有更好的方法?

最佳答案

如果我没理解错的话,你有 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
........
>>> 

关于python - 改变音效 Pygame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35780022/

相关文章:

c#-4.0 - 从视频c#中提取音频作为MP3文件

ios - AudioServicesPlaySystemSound不会播放声音

python - 如何显示pandas read_csv()函数读取的csv文件名?

python - 如何用Python实现EXCEL的查找功能

python-3.x - Keras 期望同时出现二维数组和 (1,) 形状

Python 从请求响应中跳过标题行

android - 从 AudioRecord 计算耗时

python - 如何在 django ModelForm 外键中设置默认选定值

python - 如何在python中实现时间事件调度器?

python - 在docker中激活conda环境