Yo,
C’est bizarre, ça plante avec la fonction random
Je ne comprends pas car ça lit le fichier si indiqué en dur.
Mais ça plante en random avec un joli pygame.error: Unable to open file '/home/pi/Downloads/0452.wav'
Pareil avec tous les fichiers, en wav ou mp3
La suite un autre jour du coup.
Vais pas pouvoir coder ce soir
@Nabla, n’étant pas chez moi pour Noël, dépourvu d’une framboise, je ne peut pas tester de mon coté…
Tu as essayé de remplacer pygame.mixer.init() par pygame.init() ?
Peut être qu’en réinitialisant le module complet, ça marchera.
Bon, je n’y connais rien en python, mais je suis complètement barré.
Donc, j’ai pondu un script qui tourne chez moi
J’ai listé les fichiers et je leur attribue un numéro puis j’utilise randrange pour tirer un N° au hasard.
Script sans doute largement améliorable compte tenu de mon niveau …
import RPi.GPIO as GPIO
import time
import pygame
from pygame.locals import *
import os
import random
from random import randrange
GPIO.setmode(GPIO.BCM) #Attention au choix du port ; voir le site https://fr.pinout.xyz/
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
son01 = "/home/pi/MES_SONS/0218.wav"
son02 = "/home/pi/MES_SONS/0477.wav"
son03 = "/home/pi/MES_SONS/1111.wav"
son04 = "/home/pi/MES_SONS/son.wav"
while True : #boucle jusqu'à interruption
try:
print ("\n attente boucle")
#on attend que le bouton soit pressé
GPIO.wait_for_edge(24, GPIO.FALLING)
# on a appuyé sur le bouton...
print ("\n On tire un son au hasard")
#Initialisation du son
pygame.mixer.init()
Fichier_a_lire = randrange(1, 4)
if Fichier_a_lire == 1:
son = son01
if Fichier_a_lire == 2:
son = son02
if Fichier_a_lire == 3:
son = son03
if Fichier_a_lire == 4:
son = son04
print ("\n On joue ce son")
son = pygame.mixer.Sound(son)
canal = son.play()
time.sleep(5)
print ("\n OK ; on est prêt pour une nouvelle aventure")
if (GPIO.input(24) == 0): #si bouton encore enfoncé
print ("Fermeture du programme")
break # alors on casse la boucle
except KeyboardInterrupt:
print ('sortie du programme!')
raise
GPIO.cleanup() # reinitialisation GPIO lors d'une sortie normale
Je t’envoie déjà ça par courriel.
++
Édit : Test parmi d’autres ; mais test avec notes. J’ai appuyé 20 fois sur le bouton lors de la même séquence. Le son01 est sorti 8 fois soit 40% Le son02 est sorti 4 fois soit 20% Le son03 est sorti 8 fois soit 40% Le son04 est sorti 0 fois soit 0% La séquence réelle est : 1;1;1;3;2;1;3;3;1;1;3;2;1;3;2;2;3;3;1;3
En parcourant le forum j’ai vu que le sujet avait été pas mal abordé. et vu toute l’aide que j’ai eu je met ici le code qui marche.
Pour la sortie d’un son en aléatoire d’un fichier .wav dans un dossier
Merci pour l’aide de tous (du forum et en dehors):
#!/usr/bin/python3
– coding: utf-8 -
import os
import RPi.GPIO as GPIO
import time
import pygame
from pygame.locals import *
import os
import random
from random import randrange
Path where sounds are stored
path = ‹ /home/pi/MES_SONS/ ›
Initialize empty list
files = []
r=root, d=directories, f = files
for r, d, f in os.walk(path):
for file in f:
# detect all files with wav extension
if ‹ .wav › in file or ‹ .WAV › in file:
files.append(os.path.join(r, file))
while True : #boucle jusqu’à interruption
try:
print ("\n attente boucle") #on attend que le bouton soit pressé
GPIO.wait_for_edge(24, GPIO.RISING)
# if previous sound is finished
if(not pygame.mixer.get_busy()):
# Initialize pygame mixer
pygame.mixer.init()
# Get a random number in the range of the file list size
random_number = randrange(len(files))
# Get corresponding filename
filename = files[random_number]
print("The selected file name is " + filename)
son = pygame.mixer.Sound(filename)
print(‹ Play the sound ›)
canal = son.play()
except KeyboardInterrupt:
print (‹ Leave program ! ›)
raise
GPIO.cleanup() # reinitialisation GPIO lors d’une sortie normale
Pour info, la fonction randrange est de type inclus à exclus.
Soit randrange(0,10) va de 0 à 9.
Ceci explique cela.
Et merci pour ton retour avec un code qui fonctionne.
++