我该如何使用Pygame中的音频创建一个持续流畅演奏440Hz的声音?我认为这应该很容易,但我不想使用任何愚蠢的文件来完成任务。最终打算是在按住某个键时播放一段音符,就像我之前问过的一样。非常感谢您的帮助,因为我已经浪费了大量时间寻找答案。
在遇到太多的“ValueError: Array depth must match number of mixer channels”和其他类似错误之后,我发现了这个可行的例子http://www.mail-archive.com/pygame-users@seul.org/msg16140.html。它能正确生成一个多维的16位整数数组,可以与立体声混音器一起使用。下面是一个最小工作示例,大部分代码从前面的链接中提取并加上必要的pygame代码。在Python 2.7.2中测试过,使用的pygame版本为'1.9.1release'。
这个示例将在立体声设置中从一个扬声器播放440 Hz的音调,从另一个扬声器播放550 Hz的音调。经过一些持续时间的试验,我发现如果将“duration”变量设置为非整数,则声音循环中会出现明显的点击声。
import pygame
from pygame.locals import *
import math
import numpy
size = (1366, 720)
bits = 16
#the number of channels specified here is NOT
#the channels talked about here http://www.pygame.org/docs/ref/mixer.html#pygame.mixer.get_num_channels
pygame.mixer.pre_init(44100, -bits, 2)
pygame.init()
_display_surf = pygame.display.set_mode(size, pygame.HWSURFACE | pygame.DOUBLEBUF)
duration = 1.0 # in seconds
#freqency for the left speaker
frequency_l = 440
#frequency for the right speaker
frequency_r = 550
#this sounds totally different coming out of a laptop versus coming out of headphones
sample_rate = 44100
n_samples = int(round(duration*sample_rate))
#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
#grab the x-coordinate of the sine wave at a given time, while constraining the sample to what our mixer is set to with "bits"
buf[s][0] = int(round(max_sample*math.sin(2*math.pi*frequency_l*t))) # left
buf[s][1] = int(round(max_sample*0.5*math.sin(2*math.pi*frequency_r*t))) # right
sound = pygame.sndarray.make_sound(buf)
#play once, then loop forever
sound.play(loops = -1)
#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
break
pygame.quit()
pygame.sndarray.samples
来生成一个声音对象。(我没有测试过)您可以使用以下代码创建样本:samples = [math.sin(2.0 * math.pi * frequency * t / sample_rate) for t in xrange(0, duration_in_samples)]
frequency
是所需频率,单位为赫兹。 sample_rate
是在生成音频时每秒采样数: 44100 Hz 是一个典型值。 duration_in_samples
是音频长度。(如果您的音频采样率为 44100 Hz,则 5 * 44100 == 5 秒。)pygame.sndarray.samples
之前将 samples
转换为 numpy.array
。文档指出,音频应匹配由 pygame.mixer.get_init
返回的格式,因此请适当调整 samples
,但这就是基本思路。mixer.get_init
将告诉您上面的 sample_rate
变量,以及是否需要考虑立体声,以及是否需要调整波的振幅或移位。samples
成为波长的整数倍,并且它应该循环。