More updates to learn_sounds. I think I have a good system for multiple sound directories now.
This commit is contained in:
		
							
								
								
									
										108
									
								
								menu.py
									
									
									
									
									
								
							
							
						
						
									
										108
									
								
								menu.py
									
									
									
									
									
								
							| @@ -256,9 +256,10 @@ def learn_sounds(sounds): | ||||
|     """Interactive menu for learning game sounds. | ||||
|      | ||||
|     Allows users to: | ||||
|     - Navigate through available sounds | ||||
|     - Play selected sounds | ||||
|     - Return to menu with escape key | ||||
|     - Navigate through available sounds with up/down arrows | ||||
|     - Navigate between sound categories (folders) using Page Up/Page Down or Left/Right arrows | ||||
|     - Play selected sounds with Enter | ||||
|     - Return to menu with Escape | ||||
|      | ||||
|     Excluded sounds: | ||||
|     - Files in folders named 'ambience' (at any level) | ||||
| @@ -274,15 +275,13 @@ def learn_sounds(sounds): | ||||
|     # Get speech instance | ||||
|     speech = Speech.get_instance() | ||||
|      | ||||
|     currentIndex = 0 | ||||
|      | ||||
|     # Get list of available sound keys (excluding special sounds) | ||||
|     soundKeys = [] | ||||
|      | ||||
|     # Define exclusion criteria | ||||
|     excludedPrefixes = ["game-intro", "music_menu", "_"] | ||||
|     excludedDirs = ["ambience", "."] | ||||
|      | ||||
|     # Organize sounds by directory | ||||
|     soundsByDir = {} | ||||
|      | ||||
|     # Process each sound key in the dictionary | ||||
|     for soundKey in sounds.keys(): | ||||
|         # Skip if key has any excluded prefix | ||||
| @@ -296,56 +295,109 @@ def learn_sounds(sounds): | ||||
|         if any(part.lower() == dirName.lower() or part.startswith('.') for part in parts for dirName in excludedDirs): | ||||
|             continue | ||||
|          | ||||
|         # This sound passed all exclusion criteria | ||||
|         soundKeys.append(soundKey) | ||||
|         # Determine the directory | ||||
|         if '/' in soundKey: | ||||
|             directory = soundKey.split('/')[0] | ||||
|         else: | ||||
|             directory = 'root'  # Root directory sounds | ||||
|              | ||||
|     # Sort sound keys alphabetically | ||||
|     soundKeys.sort() | ||||
|         # Add to sounds by directory | ||||
|         if directory not in soundsByDir: | ||||
|             soundsByDir[directory] = [] | ||||
|         soundsByDir[directory].append(soundKey) | ||||
|      | ||||
|     # Total number of sound files | ||||
|     totalSounds = len(soundKeys) | ||||
|     # Sort each directory's sounds | ||||
|     for directory in soundsByDir: | ||||
|         soundsByDir[directory].sort() | ||||
|      | ||||
|     # If no sounds found, inform the user and return | ||||
|     if totalSounds == 0: | ||||
|     if not soundsByDir: | ||||
|         speech.speak("No sounds available to learn.") | ||||
|         return "menu" | ||||
|      | ||||
|     # Track last spoken index to avoid repetition | ||||
|     # Get list of directories in sorted order | ||||
|     directories = sorted(soundsByDir.keys()) | ||||
|      | ||||
|     # Start with first directory | ||||
|     currentDirIndex = 0 | ||||
|     currentDir = directories[currentDirIndex] | ||||
|     currentSoundKeys = soundsByDir[currentDir] | ||||
|     currentSoundIndex = 0 | ||||
|      | ||||
|     # Display appropriate message based on number of directories | ||||
|     if len(directories) > 1: | ||||
|         messagebox(f"Starting with {currentDir if currentDir != 'root' else 'root directory'} sounds. Use left and right arrows or page up and page down to navigate categories.") | ||||
|      | ||||
|     # Track last spoken to avoid repetition | ||||
|     lastSpoken = -1 | ||||
|     lastDir = None | ||||
|      | ||||
|     # Flag to track when to exit the loop | ||||
|     returnToMenu = False | ||||
|      | ||||
|     while not returnToMenu: | ||||
|         if currentIndex != lastSpoken: | ||||
|             # Speak the sound name followed by its position in the list | ||||
|             speech.speak(f"{soundKeys[currentIndex]}, {currentIndex + 1} of {totalSounds}") | ||||
|             lastSpoken = currentIndex | ||||
|         # Announce directory change | ||||
|         if lastDir != currentDir: | ||||
|             if currentDir == 'root': | ||||
|                 speech.speak(f"Root directory sounds. {len(currentSoundKeys)} sounds available.") | ||||
|             else: | ||||
|                 speech.speak(f"{currentDir} sounds. {len(currentSoundKeys)} sounds available.") | ||||
|             lastDir = currentDir | ||||
|             lastSpoken = -1  # Reset to announce current sound | ||||
|          | ||||
|         # Announce current sound | ||||
|         if currentSoundIndex != lastSpoken: | ||||
|             totalSounds = len(currentSoundKeys) | ||||
|             soundName = currentSoundKeys[currentSoundIndex] | ||||
|             # Remove directory prefix if present | ||||
|             if '/' in soundName: | ||||
|                 displayName = '/'.join(soundName.split('/')[1:]) | ||||
|             else: | ||||
|                 displayName = soundName | ||||
|              | ||||
|             speech.speak(f"{displayName}, {currentSoundIndex + 1} of {totalSounds}") | ||||
|             lastSpoken = currentSoundIndex | ||||
|              | ||||
|         event = pygame.event.wait() | ||||
|         if event.type == pygame.KEYDOWN: | ||||
|             if event.key == pygame.K_ESCAPE: | ||||
|                 returnToMenu = True | ||||
|                  | ||||
|             if event.key in [pygame.K_DOWN, pygame.K_s] and currentIndex < len(soundKeys) - 1: | ||||
|             # Sound navigation | ||||
|             elif event.key in [pygame.K_DOWN, pygame.K_s] and currentSoundIndex < len(currentSoundKeys) - 1: | ||||
|                 pygame.mixer.stop() | ||||
|                 currentIndex += 1 | ||||
|                 currentSoundIndex += 1  # Fixed: Was decreasing instead of increasing | ||||
|                  | ||||
|             if event.key in [pygame.K_UP, pygame.K_w] and currentIndex > 0: | ||||
|             elif event.key in [pygame.K_UP, pygame.K_w] and currentSoundIndex > 0: | ||||
|                 pygame.mixer.stop() | ||||
|                 currentIndex -= 1 | ||||
|                 currentSoundIndex -= 1 | ||||
|                  | ||||
|             if event.key == pygame.K_RETURN: | ||||
|             # Directory navigation | ||||
|             elif event.key in [pygame.K_PAGEDOWN, pygame.K_RIGHT] and currentDirIndex < len(directories) - 1:  # Fixed: Right moves forward | ||||
|                 pygame.mixer.stop() | ||||
|                 currentDirIndex += 1 | ||||
|                 currentDir = directories[currentDirIndex] | ||||
|                 currentSoundKeys = soundsByDir[currentDir] | ||||
|                 currentSoundIndex = 0 | ||||
|                  | ||||
|             elif event.key in [pygame.K_PAGEUP, pygame.K_LEFT] and currentDirIndex > 0:  # Fixed: Left moves backward | ||||
|                 pygame.mixer.stop() | ||||
|                 currentDirIndex -= 1  # Fixed: Was incrementing instead of decrementing | ||||
|                 currentDir = directories[currentDirIndex] | ||||
|                 currentSoundKeys = soundsByDir[currentDir] | ||||
|                 currentSoundIndex = 0 | ||||
|                  | ||||
|             # Play sound | ||||
|             elif event.key == pygame.K_RETURN: | ||||
|                 try: | ||||
|                     soundName = soundKeys[currentIndex] | ||||
|                     soundName = currentSoundKeys[currentSoundIndex] | ||||
|                     pygame.mixer.stop() | ||||
|                     sounds[soundName].play() | ||||
|                 except: | ||||
|                     lastSpoken = -1 | ||||
|                     speech.speak("Could not play sound.") | ||||
|                      | ||||
|         event = pygame.event.clear() | ||||
|         pygame.event.clear() | ||||
|         pygame.event.pump()  # Process pygame's internal events | ||||
|         time.sleep(0.001) | ||||
|          | ||||
|     return "menu" | ||||
|   | ||||
		Reference in New Issue
	
	Block a user