幻想森林

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 9090|回复: 0

[讨论] GoogleTextSpeech

[复制链接]

86

主题

250

帖子

390

积分

版主

Rank: 7Rank: 7Rank: 7

积分
390
发表于 2009-12-16 12:19:06 | 显示全部楼层 |阅读模式
http://dl.dropbox.com/u/2877500/googlespeech.swf
  1. // forked from TheCoolMuseum's mp3 可変速再生
  2. package
  3. {
  4.     import flash.display.*;
  5.     import flash.events.*;
  6.     import flash.geom.*;
  7.     import flash.media.Sound;
  8.     import flash.media.SoundChannel;
  9.     import flash.net.URLRequest;
  10.     import flash.text.TextField;
  11.     import flash.text.TextFieldAutoSize;
  12.     import flash.text.TextFieldType;
  13.     import flash.text.TextFormat;
  14.     import flash.utils.ByteArray;
  15.     import caurina.transitions.Tweener;   
  16.     import flash.utils.Timer;
  17.     import com.bit101.components.HUISlider;
  18.     import com.bit101.components.PushButton;
  19.     /**
  20.      * [url]http://jp.techcrunch.com/archives/20091214the-unofficial-google-text-to-speech-api/[/url]
  21.      */
  22.    
  23.     [SWF(width = "465", height = "465", backgroundColor = 0xFFFFFF, frameRate = "60")]
  24.    
  25.     public class Text2Speech
  26.     extends Sprite
  27.     {
  28.         private const REQ_URL_BASE:String = "http://translate.google.com/translate_tts?tl=en&q=";
  29.         
  30.         private var outputSnd:Sound = new Sound();
  31.         private var sampleTable:ByteArray;
  32.         private var bufferLength:int = 2048;
  33.         private var speed:Number = 1.3;
  34.         private var vol:Number = 0.5;
  35.         private var rowDelay:Number = 100;
  36.         private var rowIndex:int = 0;
  37.         
  38.         
  39.         private var tfd:TextField = new TextField();
  40.         
  41.         public function Text2Speech()
  42.         {
  43.             var spdSlider:HUISlider = new HUISlider(this, 50, 420, "speed", function ():void
  44.             {
  45.                 speed = spdSlider.value;
  46.             });
  47.             spdSlider.minimum = 0.2;
  48.             spdSlider.maximum = 3.0;
  49.             spdSlider.value = speed;
  50.             spdSlider.width = 400;
  51.             tfd.autoSize = TextFieldAutoSize.LEFT;
  52.             tfd.wordWrap = true;
  53.             tfd.width = 400;
  54.             addChild(tfd);
  55.             tfd.x = 20;
  56.             tfd.defaultTextFormat = new TextFormat("OCRB", 24);
  57.             outputSnd.addEventListener(SampleDataEvent.SAMPLE_DATA, player);
  58.             init();
  59.             
  60.             speachNext();
  61.             
  62.         }
  63.         private function init():void
  64.         {
  65.             rowIndex = 0;
  66.             tfd.text = "";
  67.             tfd.y = 350;
  68.             
  69.         }
  70.         private function speachNext():void
  71.         {
  72.             if (rowIndex >= lyrics.length)
  73.             {
  74.                 init();
  75.             }
  76.             var text:String = lyrics[rowIndex]
  77.             rowIndex++;
  78.             var timer:Timer = new Timer(rowDelay, 1);
  79.             timer.addEventListener(TimerEvent.TIMER_COMPLETE, function ():void
  80.             {
  81.                 timer.removeEventListener(TimerEvent.TIMER_COMPLETE, arguments.callee);
  82.                 speach(text + "\n");               
  83.             });
  84.             timer.start();
  85.         }
  86.         
  87.         private function appendChar(char:String):void
  88.         {
  89.             var lastHeight:Number = tfd.textHeight;
  90.             tfd.appendText(char);
  91.             tfd.y -= tfd.textHeight - lastHeight;
  92.         }
  93.         
  94.         private function speach(text:String):void
  95.         {
  96.             var sourceSnd:Sound = new Sound();
  97.             sourceSnd.addEventListener(Event.COMPLETE, function (event:Event):void
  98.             {        
  99.                 var txtLen:int = text.length;
  100.                 var charTime:Number = sourceSnd.length / speed / txtLen;
  101.                 var timers:Array = [];
  102.                 for (var i:int = 0; i < txtLen; i++)
  103.                 {
  104.                     timers.push(new Timer(charTime * i, 1));
  105.                 }
  106.                
  107.                 timers.forEach(function(timer:Timer, index:int, arr:Array):void {
  108.                     var char:String = text.substr(index, 1);                    
  109.                     timer.addEventListener(TimerEvent.TIMER_COMPLETE, function ():void
  110.                     {
  111.                         timer.removeEventListener(TimerEvent.TIMER_COMPLETE, arguments.callee);
  112.                         appendChar(char);
  113.                     });
  114.                     timer.start();
  115.                     
  116.                 });               
  117.                
  118.                 sampleTable = new ByteArray();
  119.                 sourceSnd.extract(sampleTable, sourceSnd.length / 1000 * 44100, 0);
  120.                 sampleTable.position = 0;
  121.                 outputSnd.play();
  122.                
  123.             });
  124.             sourceSnd.load(new URLRequest(REQ_URL_BASE + encodeURI(text)));
  125.         }
  126.         private function player(event:SampleDataEvent):void {
  127.             var pos:Number = sampleTable.position / 8;
  128.             var length:Number = sampleTable.length / 8;
  129.             for (var i:int = 0; i < bufferLength; i++ ) {
  130.                 pos += speed;
  131.                 if (pos > length-1)
  132.                 {
  133.                     speachNext();
  134.                     break;
  135.                 }
  136.                 sampleTable.position = Math.round(pos) * 8;
  137.                 event.data.writeFloat(sampleTable.readFloat() * vol);
  138.                 event.data.writeFloat(sampleTable.readFloat() * vol);
  139.             }
  140.         }
  141.         
  142.         private var lyrics:Array = [
  143.             //"Froms FROM-29827281-12:",
  144.             //"Test Assessment Report",
  145.             "This was a triumph.",
  146.             "I'm making a note here:",
  147.             "HUGE SUCCESS.",
  148.             "It's hard to overstate my satisfaction.",
  149.             "Aperture Science",
  150.             "We do what we must",
  151.             "because we can.",
  152.             "For the good of all of us.",
  153.             "Except the ones who are dead.",
  154.             "But theres no sense",
  155.             "crying over every mistake.",
  156.             "You just keep on trying",
  157.             "till you run out of cake.",
  158.             "And the Science gets done.",
  159.             "And you make a neat gun.",
  160.             "For the people who are still alive.",
  161.             //"Froms FROM-55551-5:",
  162.             //"Personnel File Addendum:",
  163.             //"Dear <<Subject Name Here>>,",
  164.             "I'm not even angry.",
  165.             "I'm being so sincere right now.",
  166.             "Even though you broke my heart.",
  167.             "And killed me.",
  168.             "And tore me to pieces.",
  169.             "And threw every piece into a fire.",
  170.             "As they burned it hurt",
  171.             "because I was so happy for you!",
  172.             "Now these points of data",
  173.             "make a beautiful line.",
  174.             "And we're out of beta.",
  175.             "We're releasing on time.",
  176.             "So I'm GLaD.",
  177.             "I got burned.",
  178.             "Think of all the things we learned",
  179.             "for the people who are still alive.",
  180.             //"Froms FROM-55551-6:",
  181.             //"Personnel File Addendum Addendum:",
  182.             
  183.             //"One last thing:",
  184.             "Go ahead and leave me.",
  185.             "I think I prefer to stay inside.",
  186.             "Maybe you'll find someone else to help you.",
  187.             "Maybe Black Mesa...",
  188.             "THAT WAS A JOKE.",
  189.             "FAT CHANCE.",
  190.             "Anyway, this cake is great.",
  191.             "It's so delicious and moist.",
  192.             "Look at me still talking",
  193.             "when there's Science to do.",
  194.             "When I look out there,",
  195.             "it makes me GLaD I'm not you.",
  196.             "I've experiments to run.",
  197.             "There is research to be done.",
  198.             "On the people who are still alive.",
  199.             "PS: And believe me I am still alive.",
  200.             "PPS: I'm doing Science and I'm still alive.",
  201.             "PPPS: I feel FANTASTIC and I'm still alive.",
  202.             "FINAL THOUGHT:",
  203.             "While you're dying I'll be still alive",
  204.             "FINAL THOUGHT PS:",
  205.             "And when you're dead I will be still alive",
  206.             "STILL ALIVE"
  207.             
  208.         ];        
  209.    
  210.     }
  211.    
  212. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|幻想森林

GMT+8, 2024-3-29 10:24 , Processed in 0.040927 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表