Pygame - 永久调整主音量

Posted

技术标签:

【中文标题】Pygame - 永久调整主音量【英文标题】:Pygame - permantly adjusting master volume 【发布时间】:2014-07-21 17:36:03 【问题描述】:

我有一个在按键上播放正弦波音符的程序。这个想法是模拟仪器。我添加了一些代码来调整音量,但是每次播放新音符时,音量的这个值似乎都会刷新。如何调整,以便在按键调整音量时,音量保持在此值,直到再次更改?

import pygame
from pygame.locals import *

import math
import numpy

pygame.init()
bits = 16

pygame.mixer.pre_init(44100, -bits, 2)

def generate_sound(freq):
    volume = 0.5

    for i in range(pygame.mixer.get_num_channels()):
                c = pygame.mixer.Channel(i)
            c.set_volume(volume)

        for s in range(n_samples):
            t = float(s)/sample_rate    # time in seconds
            buf[s][0] = int(round(max_sample*math.sin(2*math.pi*freq*t)))    # left
            buf[s][1] = int(round(max_sample*math.sin(2*math.pi*freq*t)))    # right

        return pygame.sndarray.make_sound(buf)

size = (1200, 720)
screen = pygame.display.set_mode(size, pygame.HWSURFACE | pygame.DOUBLEBUF)


duration = 2.0          # in seconds

sample_rate = 44100

n_samples = int(round(duration*sample_rate))

#lower
sound_130_81 = generate_sound(130.81)   #C

_running = True
while _running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            _running = False


        if event.type == KEYDOWN:
            #Volume control
            if event.key == K_1:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(0.1)

            elif event.key == K_2:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(0.2)

            elif event.key == K_3:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(0.3)

            elif event.key == K_4:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(0.4)

            elif event.key == K_5:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(0.5)

            elif event.key == K_6:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(0.6)

            elif event.key == K_7:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(0.7)

            elif event.key == K_8:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(0.8)

            elif event.key == K_9:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(0.9)

            elif event.key == K_0:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(1.0)

            elif event.key == K_BACKSPACE:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(0.0)

                volume = c.get_volume()

            if event.key == K_ESCAPE:
                _running = False

            #lower notes DOWN

            elif event.key == K_z:
                sound_130_81.play(-1)


        if event.type == KEYUP:

            #lower notes UP

            if event.key == K_z:
                sound_130_81.fadeout(350) #fade sound by .35 seconds

pygame.quit()

【问题讨论】:

删除所有无关代码并放弃一个精简的示例确实会有所帮助,足以证明问题。有关详细信息,请参阅MCVE。 我不确定有多少是相关的,因为相关部分都在整个代码中,而不仅仅是一个部分 请点击 MCVE 的链接,它比我在简短的评论中解释得更好。但是你可以把它分解成相关的部分,即使它们都在整个代码中,并且只需要足够的脚手架来连接它们。例如,显然您不需要对generate_sound 进行几十次调用、几十个关键处理程序等来演示问题。 【参考方案1】:

您在 generate_sound 方法开始时设置音量。您需要为其使用现有值,而不是每次都将其设置为固定值。将其作为可在调用者中调整的参数传入(首选)或将其设置为全局值(如果您的语言支持)。

我不特别了解python,但它可能会像下面这样工作:

import pygame
from pygame.locals import *

import math
import numpy

pygame.init()
bits = 16
volume = 0.5

pygame.mixer.pre_init(44100, -bits, 2)

def generate_sound(freq, volume):

    for i in range(pygame.mixer.get_num_channels()):
                c = pygame.mixer.Channel(i)
            c.set_volume(volume)

        #setup our numpy array to handle 16 bit ints, which is what we set our mixer to expect with "bits" up above
        buf = numpy.zeros((n_samples, 2), dtype = numpy.int16)
        max_sample = 2**(bits - 1) - 1

        for s in range(n_samples):
            t = float(s)/sample_rate    # time in seconds
            buf[s][0] = int(round(max_sample*math.sin(2*math.pi*freq*t)))    # left
            buf[s][1] = int(round(max_sample*math.sin(2*math.pi*freq*t)))    # right

        return pygame.sndarray.make_sound(buf)

size = (1200, 720)
screen = pygame.display.set_mode(size, pygame.HWSURFACE | pygame.DOUBLEBUF)


duration = 2.0          # in seconds

sample_rate = 44100

n_samples = int(round(duration*sample_rate))


#lower
sound_130_81 = generate_sound(130.81, volume)   #C
sound_146_83 = generate_sound(146.83, volume)   #D
sound_164_81 = generate_sound(164.81, volume)   #E
sound_174_61 = generate_sound(174.61, volume)   #F
sound_196_00 = generate_sound(196.00, volume)   #G
sound_220_00 = generate_sound(220.00, volume)   #A
sound_246_94 = generate_sound(246.94, volume)   #B
#middle
sound_261_63 = generate_sound(261.63, volume)   #C
sound_293_66 = generate_sound(293.66, volume)   #D
sound_329_63 = generate_sound(329.63, volume)   #E
sound_349_23 = generate_sound(349.23, volume)   #F
sound_392_00 = generate_sound(392.00, volume)   #G
sound_440_00 = generate_sound(440.00, volume)   #A
sound_493_88 = generate_sound(493.88, volume)   #B
#higher
sound_523_25 = generate_sound(523.25, volume)   #C
sound_587_33 = generate_sound(587.33, volume)   #D
sound_659_25 = generate_sound(659.25, volume)   #E
sound_698_46 = generate_sound(698.46, volume)   #F
sound_783_99 = generate_sound(783.99, volume)   #G
sound_880_00 = generate_sound(880.00, volume)   #A
sound_987_77 = generate_sound(987.77, volume)   #B
sound_1046_50 = generate_sound(1046.50, volume) #C

#This will keep the sound playing forever, the quit event handling allows the pygame window to close without crashing
_running = True
while _running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            _running = False


        if event.type == KEYDOWN:
            #Volume control
            if event.key == K_1:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(0.1)

            elif event.key == K_2:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(0.2)

            elif event.key == K_3:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(0.3)

            elif event.key == K_4:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(0.4)

            elif event.key == K_5:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(0.5)

            elif event.key == K_6:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(0.6)

            elif event.key == K_7:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(0.7)

            elif event.key == K_8:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(0.8)

            elif event.key == K_9:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(0.9)

            elif event.key == K_0:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(1.0)

            elif event.key == K_BACKSPACE:
                for i in range(pygame.mixer.get_num_channels()):
                    c = pygame.mixer.Channel(i)
                c.set_volume(0.0)

                volume = c.get_volume()

            if event.key == K_ESCAPE:
                _running = False

            #lower notes DOWN

            elif event.key == K_z:
                sound_130_81.play(-1)

            elif event.key == K_x:
                sound_146_83.play(-1)

            elif event.key == K_c:
                sound_164_81.play(-1)

            elif event.key == K_v:
                sound_174_61.play(-1)

            elif event.key == K_b:
                sound_196_00.play(-1)

            elif event.key == K_n:
                sound_220_00.play(-1)

            elif event.key == K_m:
                sound_246_94.play(-1)

            elif event.key == K_COMMA:
                sound_261_63.play(-1) 


            #middle notes DOWN

            elif event.key == K_a:
                sound_261_63.play(-1)

            elif event.key == K_s:
                sound_293_66.play(-1)

            elif event.key == K_d:
                sound_329_63.play(-1)

            elif event.key == K_f:
                sound_349_23.play(-1)

            elif event.key == K_g:
                sound_392_00.play(-1)

            elif event.key == K_h:
                sound_440_00.play(-1)

            elif event.key == K_j:
                sound_493_88.play(-1)

            elif event.key == K_k:
                sound_523_25.play(-1)


            #higher notes DOWN

            elif event.key == K_q:
                sound_523_25.play(-1)

            elif event.key == K_w:
                sound_587_33.play(-1)

            elif event.key == K_e:
                sound_659_25.play(-1)

            elif event.key == K_r:
                sound_698_46.play(-1)

            elif event.key == K_t:
                sound_783_99.play(-1)

            elif event.key == K_y:
                sound_880_00.play(-1)

            elif event.key == K_u:
                sound_987_77.play(-1)

            elif event.key == K_i:
                sound_1046_50.play(-1)

        if event.type == KEYUP:

            #lower notes UP

            if event.key == K_z:
                sound_130_81.fadeout(350) #fade sound by .35 seconds

            elif event.key == K_x:
                sound_146_83.fadeout(1000)

            elif event.key == K_c:
                sound_164_81.fadeout(350)

            elif event.key == K_v:
                sound_174_61.fadeout(350)

            elif event.key == K_b:
                sound_196_00.fadeout(350)

            elif event.key == K_n:
                sound_220_00.fadeout(350)

            elif event.key == K_m:
                sound_246_94.fadeout(350)

            elif event.key == K_COMMA:
                sound_261_63.fadeout(350)

            #middle notes UP

            elif event.key == K_a:
                sound_261_63.fadeout(350)

            elif event.key == K_s:
                sound_293_66.fadeout(350)

            elif event.key == K_d:
                sound_329_63.fadeout(350)

            elif event.key == K_f:
                sound_349_23.fadeout(350)

            elif event.key == K_g:
                sound_392_00.fadeout(350)

            elif event.key == K_h:
                sound_440_00.fadeout(350)

            elif event.key == K_j:
                sound_493_88.fadeout(350)

            elif event.key == K_k:
                sound_523_25.fadeout(350)

            #higher notes UP

            elif event.key == K_q:
                sound_523_25.fadeout(350)

            elif event.key == K_w:
                sound_587_33.fadeout(350)

            elif event.key == K_e:
                sound_659_25.fadeout(350)

            elif event.key == K_r:
                sound_698_46.fadeout(350)

            elif event.key == K_t:
                sound_783_99.fadeout(350)

            elif event.key == K_y:
                sound_880_00.fadeout(350)

            elif event.key == K_u:
                sound_987_77.fadeout(350)

            elif event.key == K_i:
                sound_1046_50.fadeout(350)

pygame.quit()

【讨论】:

我已经尝试过了,但我看不出有什么方法可以让这个工作,我尝试的任何事情都会以未定义的音量结束。您能否提供我的代码中的示例? 我可以猜测一下,但是我不懂python,也没有python开发环境。 @user3856722:不要说“我尝试的任何事情都以未定义的音量结束”,而是向我们展示您尝试了什么,以及到底发生了什么以及在哪里(完整的追溯,如果发生的事情是一个例外) . 我无法复习我尝试过的所有内容,因为我做了很多变体,我基本上使用了上面的代码,在不同的地方定义了音量变量,但是尝试,例如,设置音量到按键创建的当前音量的值,与不这样做没有不同的结果...... @user3856722:此答案中显示的代码不会“最终导致音量未定义”(我假设您的意思是NameError?),所以如果这就是您的意思看到,很明显你实际上并没有运行这段代码。我们无法帮助您调试我们看不到的代码。

以上是关于Pygame - 永久调整主音量的主要内容,如果未能解决你的问题,请参考以下文章

录制的视频主题音量能减吗

通过移动设备音量调高按钮调整 chromecast 电视音量

ffmpeg 统一音量/调整音量

无法在我的 iphone 应用程序中调整音量

iOS 应用中调整手机系统音量

在应用内调整音量