Subprocess trouver un fichier .wav et le jouer

Yourou -  
mamiemando Messages postés 33228 Date d'inscription   Statut Modérateur Dernière intervention   -
Bonjour,

Je suis en train de faire un programme pour transformer les noms en notes de musique. Le programme marche bien mais maintenant j'essaye de faire jouer les notes automatiquement par python. J'utilise
subprocess
mais je n'arrive pas a lui faire trouver les fichiers
.wav
.

import subprocess

d = { 'a':'A', 'b':'B', 'c':'C', 'd':'D', 'e':'E', 'f':'F', 'g':'G', 'h':'A',
     'i':'B', 'j':'C', 'k':'D', 'l':'E', 'm':'F', 'n':'G', 'o':'A', 'p':'B',
     'q':'C', 'r':'D', 's':'E', 't':'F', 'u':'G', 'v':'A', 'w':'B', 'x':'C',
     'y':'D', 'z':'E','A':'A', 'B':'B', 'C':'C', 'D':'D', 'E':'E', 'F':'F',
     'G':'G', 'H':'A', 'I':'B', 'J':'C', 'K':'D', 'L':'E', 'M':'F', 'N':'G',
     'O':'A', 'P':'B', 'Q':'C', 'R':'D', 'S':'E', 'T':'F', 'U':'G', 'V':'A',
     'W':'B', 'X':'C', 'Y':'D', 'Z':'E', ' ':' ', '-':' '}
b = { 'A':'la', 'B':'si', 'C':'do', 'D':'re', 'E':'mi', 'F':'fa', 'G':'sol',
     ' ':' '}
c = { 'A':'a h o v', 'B':'b i p w ', 'C':'c j q x', 'D':'d k r y',
     'E':'e l s z', 'F':'f m t', 'G':'g n u'}

def s(nom):
    i = 0
    v = len(nom)
    for i in range(v) :
        print(nom[i], ' ', d[nom[i]], ' ', b[d[nom[i]]])
        play(d[nom[i]])
        i += 1

def n(note):
    j = 0
    w = len(note)
    for j in range(w):
        print(note[j], ' ', c[note[j]])

def f():
    print("A la \nB si \nC do \nD re \nE mi \nF fa \nG sol")
#aider moi ou je vais tous casser
def play(nOtE):
    print('yep')
    if nOtE == 'A':
        subprocess.Popen(["aplay", "C:/Users/g-abc-pedia-33.WPROD/.spyder-py3/e4.wav"])
    elif nOtE == 'B':
        subprocess.Popen(["aplay", "C:/Users/g-abc-pedia-33.WPROD/.spyder-py3/b4.wav"])
    elif nOtE == 'C':
        subprocess.Popen(["aplay", "C:/Users/g-abc-pedia-33.WPROD/.spyder-py3/c4.wav"])
    elif nOtE == 'D':
        subprocess.Popen(["aplay", "C:/Users/g-abc-pedia-33.WPROD/.spyder-py3/d4.wav"])
    elif nOtE == 'E':
        subprocess.Popen(["aplay", "C:/Users/g-abc-pedia-33.WPROD/.spyder-py3/e4.wav"])
    elif nOtE == 'F':
        subprocess.Popen(["aplay", "C:/Users/g-abc-pedia-33.WPROD/.spyder-py3/f4.wav"])
    elif nOtE == 'G':
        subprocess.Popen(["aplay", "C:/Users/g-abc-pedia-33.WPROD/.spyder-py3/g4.wav"])

Mais quand j'essaye le programme ça me renvoie

>>>s('P')
P B si
yep
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
s('P')
File "C:/Users/g-abc-pedia-33.WPROD/Desktop/noteA4/python/marchestp.py", line 20, in s
play(d[nom[i]])
File "C:/Users/g-abc-pedia-33.WPROD/Desktop/noteA4/python/marchestp.py", line 37, in play
subprocess.Popen(["aplay", "C:/Users/g-abc-pedia-33.WPROD/.spyder-py3/b4.wav"])
File "C:\Users\g-abc-pedia-33.WPROD\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\g-abc-pedia-33.WPROD\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 1307, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] Le fichier spécifié est introuvable


Et si vous avez une idée pour ne pas avoir a mettre les "" quand j'appelle ma fonction je suis preneur.

Merci d'avance

1 réponse

  1. mamiemando Messages postés 33228 Date d'inscription   Statut Modérateur Dernière intervention   7 944
     
    Bonjour,

    Je vois que tu appelles
    aplay
    , qui est un programme traditionnellement utiliser pour jouer un son sous linux. Or les chemins de tes sons laisse entendre que tu es sous windows. D'où ma question, as-tu un exécutable
    aplay
    ?

    Voici ce qui se passerait sous linux :
    (mando@aldur) (~) $ ipython3
    Python 3.8.6 (default, Sep 25 2020, 09:36:53)
    Type 'copyright', 'credits' or 'license' for more information
    IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

    In [1]: import subprocess

    In [2]: subprocess.Popen(["aplay", "/usr/share/sounds/alsa/Front_Center.wav"])
    Out[2]: <subprocess.Popen at 0x7fa9fd2f8190>

    Lecture WAVE '/usr/share/sounds/alsa/Front_Center.wav' : Signed 16 bit Little Endian, Fréquence 48000 Hz, Mono


    Bonne chance
    0