[英]Simple Pygame Audio at a Frequency
How can I create a 440 Hz sound that plays smoothly forever using audio with Pygame? 如何在Pygame中使用音频创建一个能够流畅播放的440 Hz声音? I assume this should be easy enough, but I don't want to use any stupid files to accomplish the task.
我认为这应该很容易,但我不想使用任何愚蠢的文件来完成任务。 The final intent of this is to play a note while a key is being held down as I have asked in another question.
这样做的最终目的是在我按照另一个问题提出的要求按下按键时播放音符。 Any help would be greatly appreciated as I have wasted great quantities of time trying to find an answer to this.
任何帮助将不胜感激,因为我浪费了大量的时间试图找到答案。
After getting entirely too many "ValueError: Array depth must match number of mixer channels", and other similar errors, I discovered this working example http://www.mail-archive.com/pygame-users@seul.org/msg16140.html . 在获得完全过多的“ValueError:数组深度必须与混频器通道数匹配”以及其他类似错误后,我发现了这个工作示例http://www.mail-archive.com/pygame-users@seul.org/msg16140。 HTML 。 It properly generates a multi-dimensional 16 bit integer array that works with a stereo mixer.
它可以正确生成一个多维16位整数数组,可与立体声混音器配合使用。 Minimum working example below, mostly lifted from the previous link with the necessary pygame bits thrown in. Tested in Python 2.7.2 with pygame.ver '1.9.1release'.
下面的最小工作示例,大部分从上一个链接中提取所需的pygame位。使用pygame.ver'1.9.1release'在Python 2.7.2中测试。
This example will play a 440 Hz tone out of one speaker and a 550 Hz tone out of the other speaker in a stereo setup. 此示例将在立体声设置中播放一个扬声器的440 Hz音调和另一个扬声器的550 Hz音调。 After a bit of playing with the duration I discovered that audible clicks will pop up in the sound loop if you set the "duration" variable to anything other than a whole number.
经过一段时间的播放后,我发现如果将“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()
What kind of 440 Hz sound? 什么样的440赫兹声音? There are many types of waves at "440 Hz": sine, saw, square, etc. You could have a flute playing an A, and that might count too.
“440 Hz”有许多类型的波浪:正弦波,锯齿波,方波等。你可以用长笛演奏A,这也可能算得上。
Assuming you want a sine wave, it looks like you can generate a Sound object with pygame.sndarray.samples
. 假设您想要一个正弦波,看起来您可以使用
pygame.sndarray.samples
生成一个Sound对象。 (I haven't tested this) You can create the samples with: (我没有测试过这个)你可以创建样本:
samples = [math.sin(2.0 * math.pi * frequency * t / sample_rate) for t in xrange(0, duration_in_samples)]
This is hopefully basic sine wave stuff. 这是希望基本的正弦波。
frequency
is the desired frequency, in Hz. frequency
是所需频率,单位为Hz。 sample_rate
is the number of samples / second in the generated audio: 44100 Hz is a typical value. sample_rate
是生成的音频中的采样数/秒:44100 Hz是典型值。 duration_in_samples
is the length of the audio. duration_in_samples
是音频的长度。 (5 * 44100 == 5 seconds, if your audio is at a 44100 Hz sample rate.) (5 * 44100 == 5秒,如果您的音频采样率为44100 Hz。)
It looks like you might have to convert samples
to a numpy.array
before passing to pygame.sndarray.samples
. 它看起来像你可能要转换
samples
为numpy.array
传递给前pygame.sndarray.samples
。 The docs indicate that the audio should match the format returned by pygame.mixer.get_init
, so adjust samples
appropriately, but that's the basic idea. 文档表明音频应该与
pygame.mixer.get_init
返回的格式匹配,因此适当调整samples
,但这是基本的想法。 ( mixer.get_init
will tell you the sample_rate
variable above, and whether you need to account for stereo, and if you need to adjust the amplitude of the wave or shift it.) (
mixer.get_init
会告诉你上面的sample_rate
变量,以及你是否需要考虑立体声,以及你是否需要调整波的幅度或移动它。)
Make samples
a integral number of waves long, and it should loop. 使
samples
长度为整数波,并且应该循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.