1515import pygame .mixer
1616import pyqtgraph as pg
1717from mutagen .mp3 import MP3
18+ from mutagen .flac import FLAC
19+ from mutagen .oggvorbis import OggVorbis
20+ from mutagen .wavpack import WavPack
21+ import wave
1822from mutagen .easyid3 import EasyID3
1923import glob
2024import qasync
@@ -139,6 +143,15 @@ def __init__(self):
139143 vbox .setSpacing (0 )
140144 vbox .setContentsMargins (0 , 0 , 0 , 0 )
141145
146+ # Initialisation des attributs de layout
147+ self .control_layout = QHBoxLayout ()
148+ self .albumArtLayout = QHBoxLayout ()
149+ self .infoLayout = QVBoxLayout ()
150+ self .progressLayout = QHBoxLayout ()
151+ self .volumeLayout = QHBoxLayout ()
152+ self .controlAllLayout = QVBoxLayout ()
153+ self .playlistManagerLayout = QHBoxLayout ()
154+
142155 self .waveformWorker = None
143156
144157 if getattr (sys , 'frozen' , False ):
@@ -168,6 +181,9 @@ def __init__(self):
168181
169182 self .newPos = 0.0
170183
184+ self .volumeMinusLabel = None
185+ self .volumePlusLabel = None
186+
171187 # Initialiser le QTimer pour vérifier l'état de la lecture
172188 self .playback_checker = QTimer (self )
173189 self .playback_checker .timeout .connect (self .check_playback_status )
@@ -190,11 +206,15 @@ def __init__(self):
190206
191207 self .is_playing = False
192208
209+ self .volume = 80
210+
193211 self .track_paths = list () # Liste pour stocker les chemins des fichiers
194212 self .current_track_index = 0
195213
196214 icon_path = os .path .join (self .application_path , 'data' , 'Music bot.png' ) # Chemin vers votre icône
197215 self .setWindowIcon (QIcon (icon_path ))
216+
217+ self .audio_file = None
198218
199219 # Initialisation de l'interface utilisateur après avoir défini total_duration
200220 self .initUI ()
@@ -418,9 +438,26 @@ def mouseReleaseEvent(self, event):
418438 self .oldPos = None
419439
420440 def get_audio_length (self , file_path ):
421- audio = MP3 (file_path )
422- audio_length = audio .info .length # durée en secondes
423- return audio_length
441+ ext = os .path .splitext (file_path )[- 1 ].lower ()
442+
443+ if ext == '.mp3' :
444+ audio = MP3 (file_path )
445+ audio_length = audio .info .length
446+ elif ext == '.flac' :
447+ audio = FLAC (file_path )
448+ audio_length = audio .info .length
449+ elif ext == '.ogg' :
450+ audio = OggVorbis (file_path )
451+ audio_length = audio .info .length
452+ elif ext == '.wav' :
453+ with wave .open (file_path , 'r' ) as audio :
454+ frames = audio .getnframes ()
455+ rate = audio .getframerate ()
456+ audio_length = frames / float (rate )
457+ else :
458+ audio_length = 0 # ou gérer l'erreur
459+
460+ return audio_length # durée en secondes
424461
425462 def load_svg_in_label (self , label , svg_path , size = QSize (30 , 30 )):
426463 svg_widget = QSvgWidget (svg_path )
@@ -889,6 +926,29 @@ def toggleMaximizeRestore(self):
889926 else :
890927 self .showMaximized ()
891928
929+ def get_metadata (self , file_path ):
930+ ext = os .path .splitext (file_path )[- 1 ].lower ()
931+ metadata = {}
932+
933+ if ext == '.mp3' :
934+ audio = EasyID3 (file_path )
935+ elif ext == '.flac' :
936+ audio = FLAC (file_path )
937+ elif ext == '.ogg' :
938+ audio = OggVorbis (file_path )
939+ elif ext == '.wav' :
940+ audio = WavPack (file_path ) # ou utiliser une autre bibliothèque appropriée pour les fichiers WAV
941+ else :
942+ return None # Format non supporté ou inconnu
943+
944+ # Assurez-vous de récupérer la chaîne complète si la valeur est une liste
945+ metadata ['artist' ] = ' ' .join (audio .get ('artist' , ['Unknown Artist' ]))
946+ metadata ['title' ] = ' ' .join (audio .get ('title' , ['Unknown Title' ]))
947+ metadata ['album' ] = ' ' .join (audio .get ('album' , ['Unknown Album' ]))
948+ metadata ['tracknumber' ] = ' ' .join (audio .get ('tracknumber' , ['0' ])).split ('/' )[0 ]
949+
950+ return metadata
951+
892952 def load_music (self ):
893953 try :
894954 # Ouvrir le QFileDialog pour sélectionner la musique
@@ -899,6 +959,7 @@ def load_music(self):
899959 folder_path = QFileDialog .getExistingDirectory (None , "Select Folder" , default_music_folder )
900960 if not folder_path :
901961 return
962+ print ("Chemins des pistes chargées :" , folder_path )
902963 if is_discord_running ():
903964 if self .RPC is None :
904965 self .RPC .connect ()
@@ -910,29 +971,29 @@ def load_music(self):
910971 temp_track_paths = []
911972 temp_ordered_playlist = {}
912973 for file in os .listdir (folder_path ):
913- if file .endswith (".mp3" ):
914- filepath = os .path .join (folder_path , file )
974+ if file .endswith (( ".mp3" , ".wav" , ".flac" , ".ogg" ) ):
975+ self . filePath = os .path .join (folder_path , file )
915976 try :
916- audio = EasyID3 (filepath )
917- track_num = audio .get ('tracknumber' , ['0' ])[0 ].split ('/' )[0 ]
918- artist = audio .get ('artist' , ['Unknown Artist' ])[0 ]
919- title = audio .get ('title' , [file ])[0 ]
920- album_name = audio .get ('album' , ['Unknown Album' ])[0 ]
921- temp_track_list .append ((int (track_num ), artist , title , filepath ))
922- temp_track_paths .append (filepath )
923- if album_name not in temp_ordered_playlist :
924- temp_ordered_playlist [album_name ] = []
925- temp_ordered_playlist [album_name ].append ((int (track_num ), artist , title , filepath ))
977+ metadata = self .get_metadata (self .filePath )
978+ if metadata :
979+ temp_track_list .append ((int (metadata ['tracknumber' ]), metadata ['artist' ], metadata ['title' ], self .filePath ))
980+ temp_track_paths .append (self .filePath )
981+ album_name = metadata ['album' ]
982+ if album_name not in temp_ordered_playlist :
983+ temp_ordered_playlist [album_name ] = []
984+ temp_ordered_playlist [album_name ].append ((int (metadata ['tracknumber' ]), metadata ['artist' ], metadata ['title' ], self .filePath ))
926985 except Exception as e :
927- print (f"Erreur avec le fichier { filepath } : { e } " )
986+ print (f"Erreur avec le fichier { self . filePath } : { e } " )
928987 self .track_list .extend (temp_track_list )
929988 self .track_paths .extend (temp_track_paths )
930989 for album in temp_ordered_playlist :
931990 if album not in self .orderedPlaylist :
932991 self .orderedPlaylist [album ] = []
933992 self .orderedPlaylist [album ].extend (temp_ordered_playlist [album ])
934993 self .random_order ()
935- if not self .is_playing and first :
994+ # Jouer la première piste si nécessaire
995+ if self .track_paths and not self .is_playing :
996+ print ("Tentative de jouer : " , self .track_paths [0 ])
936997 self .play_track (0 )
937998
938999 # Mettre à jour le bouton pour qu'il fonctionne maintenant comme Play/Pause
@@ -1100,6 +1161,17 @@ async def play_track_async(self, cover_path, title, artist):
11001161
11011162 def play_track (self , index ):
11021163 if 0 <= index < len (self .track_paths ):
1164+ self .filePath = self .track_paths [index ]
1165+ ext = os .path .splitext (self .filePath )[- 1 ].lower ()
1166+
1167+ if ext in ['.flac' , '.mp3' , '.ogg' ]:
1168+ # Convertir le fichier en WAV pour la lecture
1169+ format_map = {'.flac' : 'flac' , '.mp3' : 'mp3' , '.ogg' : 'ogg' }
1170+ audio_format = format_map .get (ext , 'mp3' ) # Par défaut à 'mp3' si le format n'est pas trouvé
1171+ audio = AudioSegment .from_file (self .filePath , format = audio_format )
1172+ temp_file = 'temp.wav' # Nom de fichier temporaire
1173+ audio .export (temp_file , format = 'wav' )
1174+ self .filePath = temp_file
11031175
11041176 if not pygame .mixer .get_init ():
11051177 pygame .mixer .init ()
@@ -1112,10 +1184,10 @@ def play_track(self, index):
11121184
11131185 self .current_track_index = index
11141186
1115- # Lire les métadonnées du fichier MP3
1116- audio = EasyID3 (self .filePath )
1117- artist = audio .get ('artist' , ['Unknown Artist' ])[ 0 ] # Remplacer par 'Unknown Artist' si non disponible
1118- title = audio .get ('title' , ['Unknown Title' ])[ 0 ] # Remplacer par 'Unknown Title' si non disponible
1187+ # Lire les métadonnées du fichier Audio
1188+ audio = self . get_metadata (self .filePath )
1189+ artist = audio .get ('artist' , ['Unknown Artist' ]) # Remplacer par 'Unknown Artist' si non disponible
1190+ title = audio .get ('title' , ['Unknown Title' ]) # Remplacer par 'Unknown Title' si non disponible
11191191
11201192 # Mettre à jour l'interface graphique avec les métadonnées
11211193 self .artistLabel .setText (f'Artiste : { artist } ' )
0 commit comments