我正在尝试使用python在Raspberry Pi上以不同的音量同时播放音频样本:
from time import sleep
import threading
import pygame
def play(sample,volume):
sample.set_volume(volume)
sample.play()
pygame.init()
pygame.mixer.init()
sound = pygame.mixer.Sound('/home/pi/Desktop/sample.wav')
thr=threading.Thread(target=play,args=([sound,1]),kwargs={})
thr.start()
sleep(1)
thr=threading.Thread(target=play,args=([sound,0.5]),kwargs={})
thr.start()
sleep(1)
thr=threading.Thread(target=play,args=([sound,0.25]),kwargs={})
thr.start()
sleep(1)
thr=threading.Thread(target=play,args=([sound,0.1]),kwargs={})
thr.start()
sleep(5)
但在这个例子中,我有一个滞后于最后播放的样本。 另外,我想有8个频道,8个样本。你知道吗
这个代码怎么了? 有没有更好的方法或图书馆?你知道吗
编辑 我试过使用频道,但在播放sound2时仍然有延迟
from time import sleep
import pygame
pygame.mixer.init()
channel1 = pygame.mixer.Channel(0)
channel2 = pygame.mixer.Channel(1)
sound1 = pygame.mixer.Sound('piano.wav') #duration 4s
sound2 = pygame.mixer.Sound('kick.wav') #duration <1s
channel1.play(sound1)
channel2.play(sound2)
sleep(0.25)
channel2.stop()
channel2.play(sound2)
sleep(0.25)
channel2.stop()
channel2.play(sound2)
sleep(0.25)
channel2.stop()
channel2.play(sound2)
sleep(5)
目前没有回答
相关问题 更多 >
编程相关推荐