播放simultanously两个声音在Python不pygame的 [英] Play two sounds simultanously in PYTHON without pygame

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

问题描述

我在做与嵌入式计算机模块,EXM32入门工具包一个项目,我想模拟8个音符钢琴。操作系统是Linux,我在Python编程。我的问题是Python版本是2.4没有'pygame的'库同时播放两个声音。在我现在用python中使用os.system('aplay ./Do.wav')的发挥,从Linux控制台,声音。

该simplificated的问题是:我可以用另一个库做一样的:

  SND1 = pygame.mixer.Sound('./ Do.wav')
    SND 2 = pygame.mixer.Sound('./ Re.wav')    snd1.play()
    snd2.play()

玩'做'和'重'simultanously?我可以用auidoop和波库。

我试着使用线程,但问题是,该计划等到控制台命令已经完成。可我用另一个库?或方法做'波'或'audioop? (这最后的图书馆,我相信只对操纵声音文件)
完整的code是:

 进口termios的,SYS,操作系统,时间
termios的=的termios
#我写了这个方法模拟的KeyEvent。我还没有得到更好的库来做到这一点
高清getkey():
    FD = sys.stdin.fileno()
    老= termios.tcgetattr(FD)
    新= termios.tcgetattr(FD)
    新的[3] =新[3]和安培; 〜TERMIOS.ICANON&安培; 〜TERMIOS.ECHO
    新[6] [TERMIOS.VMIN] = 1
    新[6] [TERMIOS.VTIME] = 0
    termios.tcsetattr(FD,TERMIOS.TCSANOW,新)
    KEY_ pressed =无
    尝试:
            KEY_ pressed = os.read(FD,1)
    最后:
            termios.tcsetattr(FD,TERMIOS.TCSAFLUSH,老)
    返回KEY_ pressedDEF键preSS(注):    如果注== DO:
            使用os.system('aplay ./notas_musicales/Do.wav')
    ELIF注== RE:
            使用os.system('aplay ./notas_musicales/Re.wav')
    ELIF注== MI:
            使用os.system('aplay ./notas_musicales/Mi.wav')
    ELIF注== FA:
            使用os.system('aplay ./notas_musicales/Fa.wav')
    ELIF注== SOL:
            使用os.system('aplay ./notas_musicales/Sol.wav')
    ELIF注== LA:
            使用os.system('aplay ./notas_musicales/La.wav')
    ELIF注== SI:
            使用os.system('aplay ./notas_musicales/Si.wav')
DO ='A'
RE ='S'
MI ='D'
FA ='F'
SOL ='G'
LA =H
SI ='J'
KEY_ pressed =
I = 1#in每次迭代程序进入另一个'如果',以不中断
#系统最后的声音。
而(KEY_ pressed ='N'!):
    KEY_ pressed = getkey()
    如果我== 1:
        键preSS(KEY_ pressed)
        I = 0
    ELIF我== 0:
        键preSS(KEY_ pressed)
        I = 1
    打印ORD(KEY_ pressed)


解决方案

由于默认的Python实现只有一个线程在同一时间运行在全球间preTER锁(GIL)。所以这不会帮助你多少在这种情况下。

此外,使用os.system 等待命令完成,并产生一个额外的外壳中运行命令您应该使用的 suprocess.Popen 相反,它会返回只要它已启动程序,默认情况下不产卵一个额外的外壳。下面code应该开始双方球员的紧密合作,尽可能:

 导入子DO = subprocess.Popen(['aplay','./notas_musicales/Do.wav'])
重新= subprocess.Popen(['aplay','./notas_musicales/Re.wav'])

I'm doing a project with an embedded computer module, the EXM32 Starter Kit and I want to simulate a piano with 8 musical notes. The OS is linux and I'm programming in Python. My problem is that version of Python is the 2.4 without 'pygame' library to play two sounds simultaneously. At now I am using in python "os.system('aplay ./Do.wav')" to play, from linux console, the sound.

The simplificated question is: Can I use another library to do the same as:

    snd1 =  pygame.mixer.Sound('./Do.wav')
    snd2 =  pygame.mixer.Sound('./Re.wav')

    snd1.play()
    snd2.play()

to play 'Do' and 'Re' simultanously? I can use "auidoop" and "wave" library.

I tried use threading but the problem is that the program wait until the console command has been finished. Another library that can I used? or the method to do with 'wave' or 'audioop'?? (this last library I believe is only for manipulated sound files) The complete code is:

import termios, sys, os, time
TERMIOS = termios
#I wrote this method to simulate keyevent. I haven't got better libraries to do this
def getkey():
    fd = sys.stdin.fileno()
    old = termios.tcgetattr(fd)
    new = termios.tcgetattr(fd)
    new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
    new[6][TERMIOS.VMIN] = 1
    new[6][TERMIOS.VTIME] = 0
    termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
    key_pressed = None
    try:
            key_pressed = os.read(fd, 1)
    finally:
            termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
    return key_pressed

def keyspress(note):

    if note == DO:
            os.system('aplay  ./notas_musicales/Do.wav')
    elif note == RE:
            os.system('aplay ./notas_musicales/Re.wav')
    elif note == MI:
            os.system('aplay ./notas_musicales/Mi.wav')
    elif note == FA:
            os.system('aplay ./notas_musicales/Fa.wav')
    elif note == SOL:
            os.system('aplay ./notas_musicales/Sol.wav')
    elif note == LA:
            os.system('aplay ./notas_musicales/La.wav')
    elif note == SI:
            os.system('aplay ./notas_musicales/Si.wav')


DO = 'a'
RE = 's'
MI = 'd'
FA = 'f'
SOL = 'g'
LA = 'h'
SI = 'j'
key_pressed = ""
i = 1

#in each iteration the program enter into the other 'if' to doesn't interrupt
#the last sound.
while(key_pressed != 'n'):
    key_pressed = getkey()
    if i == 1:
        keyspress(key_pressed)
        i = 0
    elif i == 0:
        keyspress(key_pressed)
        i = 1
    print ord(key_pressed)

解决方案

Due to the global interpreter lock ("GIL") in the default python implementation only one thread will be running at a time. So that won't help you much in this case.

Additionally, os.system waits for the command to finish, and spawns an extra shell to run the command in. You should use suprocess.Popen instead, which will return as soon as it has started the program, and by default does not spawn an extra shell. The following code should start both players as closely together as possible:

import subprocess

do = subprocess.Popen(['aplay', './notas_musicales/Do.wav'])
re = subprocess.Popen(['aplay', './notas_musicales/Re.wav'])

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

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