pygame的有播放声音的巨大延迟 [英] PyGame has huge delay with playing sounds

查看:1741
本文介绍了pygame的有播放声音的巨大延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我玩弄建设一个Python脚本,玩节奏如鼓机。我已经使用pygame的处理音频。不过,我体验到调用play和听力的实际音频之间的显著/不可接受的延迟。

我粘贴以下code到交互间preTER,然后一次又一次地执行的最后一行:

 进口pygame的
pygame.mixer.init(频率= 22050,大小= -16,渠道= 2,缓冲= 4096)
S = pygame.mixer.Sound('snare.wav')
s.play(循环= 0,MAXTIME = 0,fade_ms = 0)

pressing之间的时间进入和听到的声音是我最好的猜测400毫秒左右,的确引人注目的和不可接受的。仿佛我在VLC点击播放按钮,等待音频播放的延迟大致相同。

我也同样的结果尝试这个在Windows和Ubuntu都。我的电脑是有点老了,英特尔酷睿i3,2.53GHz的,但我觉得这不应该是一个问题。

我该怎么办这件事?

在一个循环:

这code演示了同样的滞后。

 为我的range(10):
  我打印
  s.play(循环= 0,MAXTIME = 0,fade_ms = 0)
  睡眠状态(2)


解决方案

一个可能的解决方案是降低缓冲区的大小(例如512):

 进口pygame的
pygame.mixer.init(频率= 22050,大小= -16,渠道= 2,缓冲= 512)
S = pygame.mixer.Sound('snare.wav')
s.play(循环= 0,MAXTIME = 0,fade_ms = 0)

I am playing around with building a python script that play rhythms like a drum machine. I have used PyGame to handle the audio. However I experience significant/unacceptable delays between calling play and hearing the actual audio.

I pasted the following code into the interactive interpreter, and then execute the last line again and again:

import pygame
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
s = pygame.mixer.Sound('snare.wav')
s.play(loops=0, maxtime=0, fade_ms=0)

The time between pressing enter and hearing the audio is by my best guess around 400ms, and indeed noticeable and unacceptable. The delay is approximately the same as if I click the play button in VLC and wait for the audio to play.

I have tried this on both Windows and Ubuntu with the same result. My computer is a bit old, an Intel Core i3, 2.53GHz, but I think this should not be a problem.

What can I do about this?

In a loop:

This code demonstrates the same lag.

for i in range(10):
  print i
  s.play(loops=0, maxtime=0, fade_ms=0)
  sleep(2)

解决方案

A possible solution is to decrease the buffer size (example 512):

import pygame
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=512)
s = pygame.mixer.Sound('snare.wav')
s.play(loops=0, maxtime=0, fade_ms=0)

这篇关于pygame的有播放声音的巨大延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆