element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Enchanted Objects
  • Challenges & Projects
  • Design Challenges
  • Enchanted Objects
  • More
  • Cancel
Enchanted Objects
Blog MagicHat - 16 - Let's MagicHat speak (2)
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: amgalbu
  • Date Created: 29 May 2015 4:26 PM Date Created
  • Views 388 views
  • Likes 5 likes
  • Comments 1 comment
  • tts
  • magic_doctor_hat
  • mp3
  • arduino
  • enchanted-objects
Related
Recommended

MagicHat - 16 - Let's MagicHat speak (2)

amgalbu
amgalbu
29 May 2015

Not that I can play an mp3 file, let's find a way to build a simple TTS speech

As we live in a connected world, why not leveraging the power out there on the web to overtake hardware limitations?

 

I found an interesting site here

http://tts-api.com/

 

that provides a text-to-speech web service

 

To convert a text programmatically from NodeJS, the URL to invoke is the following one

 

var mp3url='http://tts-api.com/tts.mp3?q='+encodeURIComponent(text);

 

Given this url, we can use NodeJS's http.get to download the mp3 file. Here is the code

 

function getMp3(mp3url, filename, cb)
{
console.log("Getting url " + mp3url);
http.get(mp3url, function(response) {
   if (response.statusCode > 300 && response.statusCode < 400 && response.headers.location) {
     console.log("Redirect detected");
     // The location for some (most) redirects will only contain the path,  not the hostname;
     // detect this and add the host to the path.
     if (url.parse(response.headers.location).hostname) {
       // Hostname included; make request to response.headers.location
       console.log("Redirecting to " +  response.headers.location);
       getMp3(response.headers.location,filename, cb);
     } else {
       // Hostname not included; get host from requested URL (url.parse()) and prepend to location.
     }
   // Otherwise no redirect; capture the response as normal           
   } else {
     response.setEncoding('binary');
     response.on('data', function (chunk) {
       console.log("Data " + chunk.length);
       _data =_data.concat(chunk);
     });
     
     //the whole response has been recieved, so we just print it out here
     response.on('end', function () {
        console.log("End " + _data.length);
       
        var f = fs.createWriteStream(filename);
        f.write(new Buffer(_data, 'binary'));
        f.end();
       
        cb();
     });
   }
});
};

 

Here are two kind of magic:

  1. Redirection: when the above-mentioned URL is requested, the request is redirected to a URL like this one

http://media.tts-api.com/1e4e888ac66f8dd41e00c5a7ac36a32a9950d271.mp3

NodeJS's http.get does not provided redirection-following capabilities, we have to implement the logic using the following lines of code

if (response.statusCode > 300 && response.statusCode < 400 && response.headers.location) {
console.log("Redirect detected");
// The location for some (most) redirects will only contain the path,  not the hostname;
// detect this and add the host to the path.
if (url.parse(response.headers.location).hostname) {
            // Hostname included; make request to response.headers.location
            console.log("Redirecting to " +  response.headers.location);
            getMp3(response.headers.location, cb);
} else {
            // Hostname not included; get host from requested URL (url.parse()) and prepend to location.
        }

 

  1. by default, NodeJS filesystem assumes data received after a GET is a text. In this case, we are receiving binary data, so we have to let NodeJS know we want it to treat data as an octect stream. This is accomplished by means of the following instruction

 

response.setEncoding('binary');

 

Finally, we have to instruct NodeJS to parse for a specific string from on serial port

 

yunPort = new serialPort.SerialPort('/dev/ttyATH0', { baudrate: 115200 });

yunPort.on('data', function(data) {

  console.log('data ' + data);
  if (_ws)  
  {
     _data = _data + data;
    if (_data.indexOf("\r\n") > 0)
    {
      // check if this is a PLAY command
      if (_data.substring(0, 5) == "PLAY")
      {
        var text = _data.substring(6);
        var mp3url='http://tts-api.com/tts.mp3?q='+encodeURIComponent(text);
        var filename = '/mnt/sda1/' + text.replace(' ', '_') + '.mp3';
        getMp3(text, filename, function() {
             var exec = require('child_process').exec;
             exec('/usr/bin/madplay ' + filename);
         });
      }  
      else 
        _ws.send(_data);

      _data = "";
    }
 }
});

 

Now, on the Atmel side I can simply write

 

Serial.println("hello world");

 

to make MagicHat speak! That's Internet power!

  • Sign in to reply

Top Comments

  • Workshopshed
    Workshopshed over 10 years ago +1
    I Love it
  • Workshopshed
    Workshopshed over 10 years ago

    I Love it

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube