diff --git a/examples/sound-effects/python/.env.example b/examples/sound-effects/python/.env.example new file mode 100644 index 0000000..17ef4a6 --- /dev/null +++ b/examples/sound-effects/python/.env.example @@ -0,0 +1 @@ +ELEVENLABS_API_KEY= \ No newline at end of file diff --git a/examples/sound-effects/python/Pipfile.lock b/examples/sound-effects/python/Pipfile.lock new file mode 100644 index 0000000..5b49bcb --- /dev/null +++ b/examples/sound-effects/python/Pipfile.lock @@ -0,0 +1,30 @@ +{ + "_meta": { + "hash": { + "sha256": "d2d90667a448d35bfa46954e84f0ded77c2008da725d160502bfa46d24503ee3" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.11" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "python-dotenv": { + "hashes": [ + "sha256:e324ee90a023d808f1959c46bcbc04446a10ced277783dc6ee09987c37ec10ca", + "sha256:f7b63ef50f1b690dddf550d03497b66d609393b40b564ed0d674909a68ebf16a" + ], + "index": "pypi", + "markers": "python_version >= '3.8'", + "version": "==1.0.1" + } + }, + "develop": {} +} diff --git a/examples/sound-effects/python/main.py b/examples/sound-effects/python/main.py new file mode 100644 index 0000000..cca4c6e --- /dev/null +++ b/examples/sound-effects/python/main.py @@ -0,0 +1,31 @@ +import os +from elevenlabs import SoundGenerationSettingsResponseModel +from elevenlabs.client import ElevenLabs + +from dotenv import load_dotenv + +load_dotenv() + +elevenlabs = ElevenLabs(api_key=os.getenv("ELEVENLABS_API_KEY")) + + +def generate_sound_effect(text: str, output_path: str): + print("Generating sound effects...") + + result = elevenlabs.text_to_sound_effect.convert( + text=text, + generation_settings=SoundGenerationSettingsResponseModel( + duration_seconds=10, # Optional + prompt_influence=0.3, # Optional + ), + ) + + with open(output_path, "wb") as f: + for chunk in result: + f.write(chunk) + + print(f"Audio saved to {output_path}") + + +if __name__ == "__main__": + generate_sound_effect("Dog barking", "output.mp3") diff --git a/examples/sound-effects/python/requirements.txt b/examples/sound-effects/python/requirements.txt new file mode 100644 index 0000000..5e8e39a --- /dev/null +++ b/examples/sound-effects/python/requirements.txt @@ -0,0 +1 @@ +elevenlabs \ No newline at end of file diff --git a/examples/sound-effects/python/test.py b/examples/sound-effects/python/test.py new file mode 100644 index 0000000..b914238 --- /dev/null +++ b/examples/sound-effects/python/test.py @@ -0,0 +1,17 @@ +import os +from main import generate_sound_effect + + +def test_generate_sound_effect(): + FILE_PATH = "output.mp3" + + os.remove(FILE_PATH) if os.path.exists(FILE_PATH) else None + assert not os.path.exists(FILE_PATH) + + generate_sound_effect("Dog barking", FILE_PATH) + + assert os.path.exists(FILE_PATH) + assert os.path.getsize(FILE_PATH) > 0 + + # cleanup + os.remove(FILE_PATH)