简单的pygame的声音在频率 [英] Simple Pygame Audio at a Frequency
问题描述
如何创建,发挥永远顺利使用音频与pygame的一个440赫兹的声音?我想这应该是很容易的,但我不希望使用任何愚蠢的文件来完成任务。这样做的最终目的是当一个键被按下,因为我已经问了另一个问题来弹奏音符。任何帮助将大大AP preciated因为我已经浪费了时间大量试图寻找一个答案。
How can I create a 440 Hz sound that plays smoothly forever using audio with Pygame? 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.
推荐答案
什么样的440赫兹的声音?还有在440赫兹,许多类型的波:正弦波,锯,广场等,你可以有一个长笛演奏的A,以及可能算过
What kind of 440 Hz sound? 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.
假设你想要一个正弦波,它看起来像你可以生成 pygame.sndarray.samples
Sound对象。 (我没有测试这个)你可以创建样本:
Assuming you want a sine wave, it looks like you can generate a Sound object with pygame.sndarray.samples
. (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)]
这是希望基本正弦波的东西。 频率
是所需的频率,单位为Hz。 SAMPLE_RATE
为样本/秒的生成的音频数:44100赫兹是一个典型的价值。 duration_in_samples
是音频的长度。 (5 * 44100 = = 5秒,如果你的声音是在44100赫兹的采样率。)
This is hopefully basic sine wave stuff. frequency
is the desired frequency, in Hz. sample_rate
is the number of samples / second in the generated audio: 44100 Hz is a typical value. duration_in_samples
is the length of the audio. (5 * 44100 == 5 seconds, if your audio is at a 44100 Hz sample rate.)
看起来你可能传递到<$ C $之前样品
转换为 numpy.array
C> pygame.sndarray.samples 。该文档显示,音频应匹配 pygame.mixer.get_init
返回的格式,因此调整样品
适当的,但这是最基本的理念。 ( mixer.get_init
会告诉你 SAMPLE_RATE
变量之上,以及是否需要考虑音响,如果你需要调整波的幅度或移位了。)
It looks like you might have to convert samples
to a numpy.array
before passing to 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. (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.)
请样品
长波的整数,它应该循环。
Make samples
a integral number of waves long, and it should loop.
这篇关于简单的pygame的声音在频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!