使用PyGame播放音频文件时的时间安排不正确 [英] Bad timing when playing audio files with PyGame
问题描述
当我使用PyGame每0.5秒播放一次声音时:
When I play a sound every 0.5 second with PyGame:
import pygame, time
pygame.mixer.init()
s = pygame.mixer.Sound("2.wav")
for i in range(8):
pygame.mixer.Channel(i).play(s)
time.sleep(0.5)
它根本不正确地遵守时间.
it doesn't respect the timing correctly at all.
这就像有0.2秒的暂停,而不是0.7秒,然后又是0.2秒,这是非常不规律的.
It's like there are pause of 0.2 sec than 0.7 sec then 0.2 sec again, it's very irregular.
注意:
I know that
time.sleep()
is not the most accurate in the world, but even with the more accurate solutions from here, the problem is still present
在RaspberryPi上测试
Tested on a RaspberryPi
如果我播放许多不同的文件s[i].play()
,而我的音域很大,问题仍然存在.因此,问题并非出自它试图重播相同文件的事实
The problem is still there if I play many different files s[i].play()
, with i in a big range. So the problem doesn't come from the fact it tries to replay the same file
推荐答案
正如您在自己的答案中所写,计时问题的原因很可能是音频回调与应用程序其余部分分离的事实.
As you wrote in your own answer, the reason for the timing problems very likely is the fact that the audio callback runs decoupled from the rest of the application.
音频后端通常具有某种时钟,可从回调函数内部和外部对其进行访问. 我看到了两种可能的解决方案:
The audio backend typically has some kind of a clock which is accessible from both inside the callback function and outside of it. I see two possible solutions:
-
使用允许您自己实现回调函数的库,计算回调函数中声音的开始时间,将这些时间与音频时钟"的当前时间进行比较,然后将声音写入到在输出缓冲区中的适当位置输出.
use a library that allows you to implement the callback function yourself, calculate the starting times of your sounds inside the callback function, compare those times with the current time of the "audio clock" and write your sound to the output at the appropriate position in the output buffer.
使用一个允许您指定开始播放声音的确切时间(以音频时钟"为单位)的库.该库将为您完成上一点的步骤.
use a library that allows you to specify the exact time (in terms of the "audio clock") when to start playing your sounds. This library would do the steps of the previous point for you.
对于第一个选项,您可以使用声音设备模块.回调函数(您将必须实现)将获得一个名为time
的参数,该参数具有一个属性time.outputBufferDacTime
,这是一个浮点值,用于指定输出缓冲区的第一个样本的时间(以秒为单位)将被播放.
For the first option, you could use the sounddevice module. The callback function (which you'll have to implement) will get an argument named time
, which has an attribute time.outputBufferDacTime
, which is a floating point value specifying the time (in seconds) when the first sample of the output buffer will be played back.
完全公开:我是sounddevice
模块的作者,所以我的建议颇有偏见.
Full disclosure: I'm the author of the sounddevice
module, so my recommendation is quite biased.
最近,我开始研究 rtmixer 模块,用于第二个选项.
请注意,这是处于非常早期的开发状态,因此请谨慎使用.
使用此模块,您无需编写回调函数,可以使用函数rtmixer.Mixer.play_buffer()
在指定时间(以秒为单位)播放音频缓冲区.作为参考,您可以从rtmixer.Mixer.time
获取当前时间.
Quite recently, I've started working on the rtmixer module, which can be used for the second option.
Please note that this is in very early development state, so use it with caution.
With this module, you don't have to write a callback function, you can use the function rtmixer.Mixer.play_buffer()
to play an audio buffer at a specified time (in seconds). For reference, you can get the current time from rtmixer.Mixer.time
.
这篇关于使用PyGame播放音频文件时的时间安排不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!