File size: 2,502 Bytes
0061c9d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import json
import random
class VoiceProfileManager:
def __init__(self, filename="voice_profiles.json"):
self.filename = filename
self.profiles = []
def load_profiles(self):
try:
with open(self.filename, 'r') as file:
profiles_data = json.load(file)
self.profiles = [VoiceProfile.from_dict(profile) for profile in profiles_data]
except FileNotFoundError:
print(f"File '{self.filename}' not found. Starting with an empty profile list.")
self.profiles = []
def save_profiles(self):
profiles_data = [profile.to_dict() for profile in self.profiles]
with open(self.filename, 'w') as file:
json.dump(profiles_data, file, indent=4)
print(f"Profiles saved to '{self.filename}'.")
def add_profile(self, profile):
self.profiles.append(profile)
def generate_random_profile(self):
name = f"Profile-{len(self.profiles) + 1}"
languages = ["en-US", "en-GB", "fr-FR", "es-ES"] # Example languages
language = random.choice(languages)
pitch = round(random.uniform(0.8, 1.2), 2)
speaking_rate = round(random.uniform(0.7, 1.3), 2)
new_profile = VoiceProfile(name, language, pitch, speaking_rate)
self.add_profile(new_profile)
return new_profile
def list_profiles(self):
if not self.profiles:
print("No profiles found.")
else:
for idx, profile in enumerate(self.profiles, start=1):
print(f"Profile {idx}: {profile.name} - Language: {profile.language}, Pitch: {profile.pitch}, Speaking Rate: {profile.speaking_rate}")
# Example usage:
if __name__ == "__main__":
manager = VoiceProfileManager()
manager.load_profiles()
while True:
print("\nVoice Profile Manager Menu:")
print("1. Generate Random Profile")
print("2. List Profiles")
print("3. Save Profiles")
print("4. Exit")
choice = input("Enter your choice: ")
if choice == "1":
new_profile = manager.generate_random_profile()
print(f"Generated new profile: {new_profile.name}")
elif choice == "2":
manager.list_profiles()
elif choice == "3":
manager.save_profiles()
elif choice == "4":
print("Exiting program.")
break
else:
print("Invalid choice. Please enter a number from the menu.") |