|
| 1 | +import os |
| 2 | +import time |
| 3 | +import asyncio |
| 4 | +import base64 |
| 5 | +import tempfile |
| 6 | +from pathlib import Path |
| 7 | +from hume import AsyncHumeClient |
| 8 | +from hume.tts import ( |
| 9 | + PostedContextWithGenerationId, |
| 10 | + PostedUtterance, |
| 11 | + PostedUtteranceVoiceWithName, |
| 12 | + ReturnGeneration, |
| 13 | +) |
| 14 | + |
| 15 | +import aiofiles |
| 16 | + |
| 17 | +from dotenv import load_dotenv |
| 18 | + |
| 19 | +load_dotenv() |
| 20 | + |
| 21 | +# Initialize the Hume client using your API key and the test environment URL. |
| 22 | +api_key = os.getenv("HUME_API_KEY") |
| 23 | +if not api_key: |
| 24 | + raise EnvironmentError("HUME_API_KEY not found in environment variables.") |
| 25 | + |
| 26 | +hume = AsyncHumeClient(api_key=api_key) |
| 27 | + |
| 28 | +# Create an output directory in the temporary folder. |
| 29 | +timestamp = int(time.time() * 1000) # similar to Date.now() in JavaScript |
| 30 | +output_dir = Path(tempfile.gettempdir()) / f"hume-audio-{timestamp}" |
| 31 | + |
| 32 | + |
| 33 | +async def write_result_to_file(base64_encoded_audio: str, filename: str) -> None: |
| 34 | + """ |
| 35 | + Writes the base64-decoded audio from a generation to a .wav file. |
| 36 | + """ |
| 37 | + file_path = output_dir / f"{filename}.wav" |
| 38 | + # Decode the base64-encoded audio data (similar to Buffer.from(..., "base64")) |
| 39 | + audio_data = base64.b64decode(base64_encoded_audio) |
| 40 | + async with aiofiles.open(file_path, "wb") as f: |
| 41 | + await f.write(audio_data) |
| 42 | + print("Wrote", file_path) |
| 43 | + |
| 44 | + |
| 45 | +async def main() -> None: |
| 46 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 47 | + |
| 48 | + print("Results will be written to", output_dir) |
| 49 | + |
| 50 | + # Synthesizing speech with a new voice |
| 51 | + speech1 = await hume.tts.synthesize_json( |
| 52 | + utterances=[ |
| 53 | + PostedUtterance( |
| 54 | + description="A refined, British aristocrat", |
| 55 | + text="Take an arrow from the quiver.", |
| 56 | + ) |
| 57 | + ] |
| 58 | + ) |
| 59 | + await write_result_to_file(speech1.generations[0].audio, "speech1_0") |
| 60 | + |
| 61 | + name = f"aristocrat-{int(time.time())}" |
| 62 | + # Naming the voice and saving it to your voice library |
| 63 | + # for later use |
| 64 | + generation_id = speech1.generations[0].generation_id |
| 65 | + await hume.tts.voices.create( |
| 66 | + name=name, generation_id=generation_id |
| 67 | + ) |
| 68 | + |
| 69 | + # Continuing previously-generated speech |
| 70 | + speech2 = await hume.tts.synthesize_json( |
| 71 | + utterances=[ |
| 72 | + PostedUtterance( |
| 73 | + # Using a voice from your voice library |
| 74 | + voice=PostedUtteranceVoiceWithName(name=name), |
| 75 | + text="Now take a bow.", |
| 76 | + ) |
| 77 | + ], |
| 78 | + # Providing previous context to maintain consistency. |
| 79 | + # This should cause "bow" to rhyme with "toe" and not "cow". |
| 80 | + context=PostedContextWithGenerationId(generation_id=generation_id), |
| 81 | + num_generations=2, |
| 82 | + ) |
| 83 | + |
| 84 | + await write_result_to_file(speech2.generations[0].audio, "speech2_0") |
| 85 | + await write_result_to_file(speech2.generations[1].audio, "speech2_1") |
| 86 | + |
| 87 | + # Acting instructions: modulating the speech from a previously-generated voice |
| 88 | + speech3 = await hume.tts.synthesize_json( |
| 89 | + utterances=[ |
| 90 | + PostedUtterance( |
| 91 | + voice=PostedUtteranceVoiceWithName(name=name), |
| 92 | + description="Murmured softly, with a heavy dose of sarcasm and contempt", |
| 93 | + text="Does he even know how to use that thing?", |
| 94 | + ) |
| 95 | + ], |
| 96 | + context=PostedContextWithGenerationId( |
| 97 | + generation_id=speech2.generations[0].generation_id |
| 98 | + ), |
| 99 | + num_generations=1, |
| 100 | + ) |
| 101 | + await write_result_to_file(speech3.generations[0].audio, "speech3_0") |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + asyncio.run(main()) |
| 106 | + print("Done") |
0 commit comments