• <bdo id='pUFce'></bdo><ul id='pUFce'></ul>

    <tfoot id='pUFce'></tfoot>
      1. <small id='pUFce'></small><noframes id='pUFce'>

      2. <legend id='pUFce'><style id='pUFce'><dir id='pUFce'><q id='pUFce'></q></dir></style></legend>
        <i id='pUFce'><tr id='pUFce'><dt id='pUFce'><q id='pUFce'><span id='pUFce'><b id='pUFce'><form id='pUFce'><ins id='pUFce'></ins><ul id='pUFce'></ul><sub id='pUFce'></sub></form><legend id='pUFce'></legend><bdo id='pUFce'><pre id='pUFce'><center id='pUFce'></center></pre></bdo></b><th id='pUFce'></th></span></q></dt></tr></i><div id='pUFce'><tfoot id='pUFce'></tfoot><dl id='pUFce'><fieldset id='pUFce'></fieldset></dl></div>

        在没有pygame的PYTHON中同时播放两个声音

        时间:2023-11-07
          <tbody id='smq1D'></tbody>

          <small id='smq1D'></small><noframes id='smq1D'>

            <legend id='smq1D'><style id='smq1D'><dir id='smq1D'><q id='smq1D'></q></dir></style></legend>

                • <bdo id='smq1D'></bdo><ul id='smq1D'></ul>
                  <i id='smq1D'><tr id='smq1D'><dt id='smq1D'><q id='smq1D'><span id='smq1D'><b id='smq1D'><form id='smq1D'><ins id='smq1D'></ins><ul id='smq1D'></ul><sub id='smq1D'></sub></form><legend id='smq1D'></legend><bdo id='smq1D'><pre id='smq1D'><center id='smq1D'></center></pre></bdo></b><th id='smq1D'></th></span></q></dt></tr></i><div id='smq1D'><tfoot id='smq1D'></tfoot><dl id='smq1D'><fieldset id='smq1D'></fieldset></dl></div>
                • <tfoot id='smq1D'></tfoot>
                • 本文介绍了在没有pygame的PYTHON中同时播放两个声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  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'])
                  

                  这篇关于在没有pygame的PYTHON中同时播放两个声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Python 中运行后,如何在一段时间内禁用 Raspberry Pi GPIO 事件? 下一篇:为什么 HTTPS 请求会产生 SSL CERTIFICATE_VERIFY_FAILED 错误?

                  相关文章

                  <tfoot id='RMLok'></tfoot>
                    <bdo id='RMLok'></bdo><ul id='RMLok'></ul>

                  1. <i id='RMLok'><tr id='RMLok'><dt id='RMLok'><q id='RMLok'><span id='RMLok'><b id='RMLok'><form id='RMLok'><ins id='RMLok'></ins><ul id='RMLok'></ul><sub id='RMLok'></sub></form><legend id='RMLok'></legend><bdo id='RMLok'><pre id='RMLok'><center id='RMLok'></center></pre></bdo></b><th id='RMLok'></th></span></q></dt></tr></i><div id='RMLok'><tfoot id='RMLok'></tfoot><dl id='RMLok'><fieldset id='RMLok'></fieldset></dl></div>
                    <legend id='RMLok'><style id='RMLok'><dir id='RMLok'><q id='RMLok'></q></dir></style></legend>

                  2. <small id='RMLok'></small><noframes id='RMLok'>