In this tutorial, we will walk through the process of generating sound effects using the ElevenLabs API in JavaScript. This powerful API allows you to create high-quality sound effects from simple text descriptions, which can be used in various applications such as game development and music production.
Requirements
Before we begin, ensure you have the following:
- An ElevenLabs account with an API key (here’s how to find your API key)
- Node.js installed on your machine
Setting Up
First, you need to install the ElevenLabs SDK. Open your terminal and run the following command:
npm install elevenlabsNext, create a .env file in your project directory and add your API key:
ELEVENLABS_API_KEY=your_elevenlabs_api_key_hereGenerating Sound Effects
Now, let's write a JavaScript function to generate a sound effect from a text description and save it to a file.
const { ElevenLabs } = require('elevenlabs');
const fs = require('fs');
require('dotenv').config();
const elevenlabs = new ElevenLabs({
apiKey: process.env.ELEVENLABS_API_KEY
});
async function generateSoundEffect(text, outputPath) {
console.log('Generating sound effect...');
const result = await elevenlabs.textToSoundEffects.convert({
text: text,
durationSeconds: 10, // Optional
promptInfluence: 0.3 // Optional
});
const writeStream = fs.createWriteStream(outputPath);
result.pipe(writeStream);
writeStream.on('finish', () => {
console.log(`Audio saved to ${outputPath}`);
});
}
generateSoundEffect('Dog barking', 'output.mp3');Configuration
Here are some optional parameters you can use when generating sound effects:
| Parameter | Description |
|---|---|
| durationSeconds | The duration of the sound effect in seconds. If not provided, the API will automatically determine the correct length. The maximum value is 22. |
| promptInfluence | The amount of influence the prompt has on the generated sound effect. If not provided, the API will use the default value of 0.3. |
API Pricing
The API is charged at 100 characters per generation with automatic duration or 25 characters per second with a set duration.
Next Steps
We’re excited to see what you build with the API. Here are some ideas of what you might want to use it for:
- Adding sound effect generation to a video editing application
- Enabling users to create on-demand samples for their music production
- A new type of video game where every sound is generated dynamically
For higher rate limits or volume-based discounts, please contact sales.