首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在pygame中同时播放多个声音文件?

如何在pygame中同时播放多个声音文件?
EN

Stack Overflow用户
提问于 2021-05-14 02:04:45
回答 1查看 66关注 0票数 2

我正在尝试播放6个声音文件(aiff),每个5MB,同时,或他们可以最接近相同的毫秒。但是我的代码导致了混乱:有些声音确实是同时播放的,但有些却不是。你可以听到3个声音在不同的时间播放。所以..。就像这6个音被分成3组,每组2个音,在不同的时间就形成了这3个音。这应该是一条线索。

这些文件存储在内存中(因为我使用的是加载到内存中的实时lubuntu镜像),所以速度非常快,我假设这不是一个延迟问题。即使当我加载存储在挂盘中的声音时,结果听起来也是一样的。所以这里绝对不是问题。

代码语言:javascript
复制
import pygame as pg
pg.init()
pg.mixer.set_num_channels(50)
pg.mixer.Channel(1).play(pg.mixer.Sound('/home/lubuntu/Piano.pp.B2.aiff'))
pg.mixer.Channel(2).play(pg.mixer.Sound('/home/lubuntu/Piano.pp.A3.aiff'))
pg.mixer.Channel(3).play(pg.mixer.Sound('/home/lubuntu/Piano.pp.A4.aiff'))
pg.mixer.Channel(4).play(pg.mixer.Sound('/home/lubuntu/Piano.pp.A5.aiff'))
pg.mixer.Channel(5).play(pg.mixer.Sound('/home/lubuntu/Piano.pp.A6.aiff'))
pg.mixer.Channel(6).play(pg.mixer.Sound('/home/lubuntu/Piano.pp.A1.aiff'))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-14 03:01:53

从以下位置加载您的6个声音文件进行测试:

http://theremin.music.uiowa.edu/MISpiano.html

我怀疑调用每个play()然后再调用它自己的load()会产生交错的效果,因为在play1已经开始播放之前,play2甚至不能考虑开始加载它的声音。

代码语言:javascript
复制
import pygame as pg
import time

pg.init()
pg.mixer.set_num_channels(50)

foo = [
    pg.mixer.Sound("Piano.pp.A1.aiff"),
    ##pg.mixer.Sound("Piano.pp.A2.aiff"),
    pg.mixer.Sound("Piano.pp.A3.aiff"),
    pg.mixer.Sound("Piano.pp.A4.aiff"),
    pg.mixer.Sound("Piano.pp.A5.aiff"),
    pg.mixer.Sound("Piano.pp.A5.aiff"),
    pg.mixer.Sound("Piano.pp.B2.aiff"),
]

for i,x in enumerate(foo):
    pg.mixer.Channel(i+1).play(x)

time.sleep(10) ## so you can hear something rather than having the app just quit

我认为这或多或少地产生了我在audacity中打开这6个文件并一起播放时听到的内容。只是提醒一下,他们似乎大部分时候都是沉默的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67524067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档