Laravel text to speech converter play and download as MP3

Author - Krishna Solanki

Laravel Text to speech converter is transforming the text into artificial human speech. Convert text into corresponding language speech. You can download that audio speech and play the audio speech.

The following steps for creating text to speech converter.

  • Create a laravel project using composer

     Laravel new text-to-speech-converter

  • Create an account in VoiceRSS and get your API key from VoiceRSS

Registration URL of VoiceRSS is http://www.voicerss.org

 

Voice RSS Registration

After Register in VoiceRSS, log in using your email address and password.

Login URL is http://www.voicerss.org/login.aspx.

 

Voice RSS Login

After Login get your API key. http://www.voicerss.org/personel/default.aspx link for getting your API key

  • Set Your API Key In The Environment File

        Open you .env file add your API key for Voice RSS.

         Ex. VOICE_RSS_API_KEY=your_api_key

  • Filesystem Configuration

          In config/filesystems.php add following code for creating speeches disk on which all audio files will be saved.

'disks' => [
         …
        'speeches' => [
            'driver' => 'local',
            'root' => storage_path('app/public/speeches'),
        ],
         ...
    ],

Create A Library For VoiceRSS

Create Library folder inside App Folder. Download VoiceRSS SDK for from http://www.voicerss.org/downloads/voicerss_tts_php.zip. Extract zip and put the voicerss_tts.php file inside App\Library folder and rename to VoiceRss.php.

Create Text To Speech Convert Form

Make a form which takes text and textual content language ( In which language you have written text ).

Create A Controller Which Convert Text Into Speech

               use App\Library\VoiceRSS;
               …

		try {		
			
			$tts = new VoiceRSS;
			$voice = $tts->speech([
			    'key' => env('VOICE_RSS_API_KEY'),
			    'hl' =>  $request->lan,
			    'src' => $request->text,
			    'r' => '0',
			    'c' => 'mp3',
			    'f' => '44khz_16bit_stereo',
			    'ssml' => 'false',
			    'b64' => 'false'
			]);	
				
			$filename = Str::uuid().'.mp3';
			if( empty($voice["error"]) ) {		
				$rawData = $voice["response"];					
				if (!File::exists(storage_path('app/public/speeches')))
				{
					Storage::makeDirectory(public_path('storage/speeches'));
				}

				Storage::disk('speeches')->put($filename, $rawData);
				$speechFilelink =  asset('storage/speeches/'.$filename);							   		                 
			   	$urls["play-url"] = $speechFilelink;		   	
			   	$urls["download-file"] = $filename;			   
			   	$data = array('status' => 200, 'responseText' => $urls);
	          			return response()->json($data);		
			}

	   		$data = array('status' => 400, 'responseText' => "Please try again!");
           			return response()->json($data);     

		} 
		catch (SitemapParserException $e) {
		    	$data = array('status' => 400, 'responseText' => $e->getMessage());
            			return response()->json($data);
		}

Using voiceRss Library create the VoiceRSS instance( $tts = new VoiceRSS ). Using this instance generate speech. Pass variables key, hl, src,  r, c, f, ssml, and b64 as array format.

key*

Your API key, which is store at ENV file.

hl*

Written textual content language

src*

Your textual content (length limited – 100KB).

r

Speech Rate from -10 (slowest speed) up to 10 (fastest speed). The default value is 0 (normal speed).

c

Speech audio code. The default value is WAV.

f

Speech audio formats. The default value is 8khz_8bit_mono.

ssml

SSML textual content format. Either true or false. The default value is false

b64

Defines output as a Base64 string format. Either true or false. The default value is false.

(*) fields are mandatory, other fields are optional.

  • Support Language (hl) 

       Catalan, Chinese (China), Chinese (Hong Kong), Chinese (Taiwan), Danish, Dutch, English (Australia),           English (Canada), English (Great Britain), English (India), English (United States), Finnish, French                   (Canada), French (France), German, Italian, Japanese, Korean, Norwegian, Polish, Portuguese (Brazil),         Portuguese (Portugal), Russian, Spanish (Mexico), Spanish (Spain), Swedish (Sweden)

  • Support Audio Code (c)

    MP3, WAV, AAC, OGG, CAF

  • Support Audio Formats

 (8 kHz, 8 Bit, Mono), (8 kHz, 8 Bit, Stereo), (8 kHz, 16 Bit, Mono), (8 kHz, 16 Bit, Stereo), (11 kHz, 8 Bit,  Mono), (11 kHz, 8 Bit, Stereo),(11 kHz, 16 Bit, Mono), (11 kHz, 16 Bit, Stereo), (12 kHz, 8 Bit, Mono), (12 kHz, 8 Bit, Stereo), (12 kHz, 16 Bit, Mono), (12 kHz, 16 Bit, Stereo), (16 kHz, 8 Bit, Mono), (16 kHz, 8 Bit, Stereo), (16 kHz, 16 Bit, Mono), (16 kHz, 16 Bit, Stereo), (22 kHz, 8 Bit, Mono), (22 kHz, 8 Bit, Stereo), (22  kHz, 16 Bit, Mono), (22 kHz, 16 Bit, Stereo), (24 kHz, 8 Bit, Mono), (24 kHz, 8 Bit, Stereo), (24 kHz, 16 Bit, Mono), (24 kHz, 16 Bit, Stereo)

(32 kHz, 8 Bit, Mono), (32 kHz, 8 Bit, Stereo), (32 kHz, 16 Bit, Mono), (32 kHz, 16 Bit, Stereo), (44 kHz, 8 Bit, Mono), (44 kHz, 8 Bit, Stereo), (44 kHz, 16 Bit, Mono), (44  kHz, 16 Bit, Stereo), (48 kHz, 8 Bit, Mono), (48 kHz, 8 Bit, Stereo), (48 kHz, 16 Bit, Mono), (48 kHz, 16 Bit, Stereo), (ALaw, 8 kHz, Mono), (ALaw, 8 kHz, Stereo), (ALaw, 11 kHz, Mono), (ALaw, 11 kHz, Stereo), (ALaw, 22 kHz, Mono), (ALaw, 22 kHz, Stereo), (ALaw, 44 kHz, Mono), (ALaw, 44 kHz, Stereo), (uLaw, 8 kHz, Mono), (uLaw, 8 kHz, Stereo), (uLaw, 11 kHz, Mono), (uLaw, 11 kHz, Stereo), (uLaw, 22 kHz, Mono), (uLaw, 22 kHz, Stereo), (uLaw, 44 kHz, Mono), (uLaw, 44 kHz, Stereo)

Here you go

Speech instance returns result as an array format.  From this array gets errors and response. If successfully convert text into speech error field is empty. For storing the audio file inside the storage check speeches folder is exist or not. If speech folder does not exist inside your storage, create that folder and store speech file as MP3 format. Download and play an audio file using that storage link.

VoiceRSS Document

VoiceRss Document link is http://www.voicerss.org/api/documentation.aspx to know more about VoiceRSS. There are many service providers available to convert text into speech, likewise Acapela, AmazonPolly, Google, Picotts, ResponsiveVoice, Voice RSS, etc.

From the above steps, you can create a text to speech interface easily. If you have any questions or feedback feel free to add a comment below this content. Thanks for reading this article. I hope you enjoy this topic.

As a result we have Github repo as usual, Enjoy It.

https://github.com/logisticinfotech/Laravel-text-to-speech

Free SEO Checker |
Test your website for free with OnAirSEO.com

Get Your SEO report!

Don’t miss the next post!

Loading

Related Posts