Text to Voice in Perl
Generate audio from text with a Perl script using the Narakeet REST API. The example below uses HTTP::Tiny, which ships with Perl 5.14 and later — no CPAN module to install. It runs anywhere Perl runs: cron jobs, CGI handlers, system administration scripts, and modern web backends.
For endpoint details, authentication, and cross-language features, see the main Text to Speech API reference.
- Perl Text to Speech Example
- Perl Text to Voice Script
- Perl TTS Module
- Perl Text to Speech API Options
- Batch Audio Generation with Perl
Perl Text to Speech Example
The script below sends text to the Narakeet API and saves the response as an MP3 file. The text_to_speech subroutine takes the HTTP client as its first argument, keeping transport concerns separate from the API logic.
use strict;
use warnings;
use HTTP::Tiny;
sub text_to_speech {
my ($http, $api_key, $voice, $text, $output_path) = @_;
my $url = "https://api.narakeet.com/text-to-speech/mp3?voice=$voice";
my $response = $http->request('POST', $url, {
headers => {
'Accept' => 'application/octet-stream',
'Content-Type' => 'text/plain',
'x-api-key' => $api_key,
},
content => $text,
});
if (!$response->{success}) {
die "API error $response->{status}: $response->{content}\n";
}
open my $fh, '>:raw', $output_path or die "Creating file: $!\n";
print $fh $response->{content};
close $fh;
}
my $api_key = $ENV{NARAKEET_API_KEY} || '';
if ($api_key eq '') {
print "Please set NARAKEET_API_KEY environment variable\n";
exit 1;
}
my $http = HTTP::Tiny->new(timeout => 30);
text_to_speech($http, $api_key, 'matt', 'Hi there from Perl', 'output.mp3');
print "File saved at: output.mp3\n";
Set NARAKEET_API_KEY in your environment before running — create a key from the API Keys page. Then run with perl tts.pl.
For the complete project, see the Perl streaming API example on GitHub.
Perl Text to Voice Script
Perl excels at text processing and system automation, making a Perl text to voice script a natural fit for workflows that already involve Perl. Typical use cases:
- Cron jobs that read log summaries or monitoring alerts and produce spoken audio notifications
- CGI or Dancer2 web handlers that return audio responses for accessibility
- Migration scripts that convert a directory of text files into MP3s in a single pass
- System administration tools that generate voice prompts for IVR or PBX phone systems
- Build pipelines that produce localised audio assets from
.poor.pottranslation files
For content longer than 1 KB or uncompressed WAV output, switch to the Long Content (Polling) API.
Perl TTS Module
With Narakeet, no CPAN text to speech module is required. HTTP::Tiny has been part of Perl core since version 5.14 (2011), so the example runs out of the box on virtually every modern Perl installation. There is no XS compilation step, no libespeak to link against, and no dependency chain to manage. The text_to_speech subroutine in the example above can be dropped into any existing Perl project as-is.
If your system uses an older Perl that predates HTTP::Tiny, LWP::UserAgent from the libwww-perl distribution is a widely available alternative.
Perl Text to Speech API Options
| Parameter | Example | Effect |
|---|---|---|
voice |
matt |
Selects the speaking voice (full list) |
voice-speed |
1.2 |
Speeds up reading; values below 1.0 slow it down |
voice-volume |
soft |
Sets the output volume (soft, medium, loud) |
| Format (path) | /mp3, /m4a |
Changes the audio codec; /wav requires the polling API |
For pitch control, sentence pauses, and multi-voice scripts, use the script header format inside the request body. Full reference at Configuring Audio Tasks.
Batch Audio Generation with Perl
Perl’s text manipulation strengths pair well with batch audio generation. Read a list of phrases from a file, a database query, or STDIN, iterate with a while loop, and call the API for each entry. Because HTTP::Tiny reuses connections automatically when making multiple requests to the same host, a batch script stays efficient without extra connection management code. For very large batches, consider the Narakeet batch processing endpoint, which accepts a multi-scene source file and returns a ZIP archive of individual audio files in a single request.