Skip to content

Commit

Permalink
added sfx example
Browse files Browse the repository at this point in the history
  • Loading branch information
lharries committed Jun 16, 2024
1 parent ea93012 commit 611b150
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/sound-effects/python/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ELEVENLABS_API_KEY=
30 changes: 30 additions & 0 deletions examples/sound-effects/python/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions examples/sound-effects/python/main.py
Original file line number Diff line number Diff line change
@@ -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")
1 change: 1 addition & 0 deletions examples/sound-effects/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
elevenlabs
17 changes: 17 additions & 0 deletions examples/sound-effects/python/test.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 611b150

Please sign in to comment.