Pygame - 永久调整主音量

时间:2014-07-21 17:36:04

标签: python audio numpy pygame volume

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

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()

1 个答案:

答案 0 :(得分:0)

您正在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()