<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Marco Mastrodonato &#187; jar</title>
	<atom:link href="http://mastrodonato.info/index.php/tag/jar/feed/" rel="self" type="application/rss+xml" />
	<link>http://mastrodonato.info</link>
	<description>Non c&#039;e&#039; prezzo per la miticita&#039;</description>
	<lastBuildDate>Wed, 08 Sep 2010 14:27:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>FreeTTS, una libreria java in jRuby on Rails</title>
		<link>http://mastrodonato.info/index.php/2009/08/freetts-una-libreria-java-in-jruby-on-rails/</link>
		<comments>http://mastrodonato.info/index.php/2009/08/freetts-una-libreria-java-in-jruby-on-rails/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 15:08:19 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[JRuby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[FreeTTS]]></category>
		<category><![CDATA[jar]]></category>
		<category><![CDATA[jRuby]]></category>
		<category><![CDATA[Text to speech]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=75</guid>
		<description><![CDATA[In questo articolo vedremo come utilizzare le classi java contenute in un file JAR Per questo scopo andremo ad utilizzare una simpatica libreria opensource sviluppata dalla Carnegie Mellon University: FreeTTS. L&#8217;acronimo TTS significa Text To speech tradotto: &#8220;da testo a voce&#8221;, permette infatti di trasformare un testo in formato audio. Noi la utilizzeremo in un [...]]]></description>
			<content:encoded><![CDATA[<p>In questo articolo vedremo come utilizzare le classi java contenute in un file JAR<br />
Per questo scopo andremo ad utilizzare una simpatica libreria opensource sviluppata dalla Carnegie Mellon University: <a href="http://freetts.sourceforge.net/">FreeTTS</a>.<br />
L&#8217;acronimo TTS significa Text To speech tradotto: &#8220;da testo a voce&#8221;, permette infatti di trasformare un testo in formato audio. Noi la utilizzeremo in un progetto jRuby on Rails per farci leggere il testo che inseriremo nel db.</p>
<p>Nel <a href="http://mastrodonato.info/index.php/2009/07/configurare-un-ambiente-per-jruby-on-rails/">precedente articolo</a>, abbiamo visto come configurare l&#8217;ambiente, partiamo quindi creando la nuova applicazione:</p>
<p><code><br />
C:>rails ProvaFreeTTS<br />
</code></p>
<p>Nella cartella lib (dalla root) creiamo una sotto cartella freetts, scarichiamo il file <a href="http://sourceforge.net/projects/freetts/files/">freetts-1.2.2-bin.zip</a>, scompattiamo il contenuto in una cartella temporanea, copiamo solamente il contenuto della cartella lib (files jar e jsapi) nella cartella appena creata: tua_applicazionelibfreetts.</p>
<p>Ora creiamo l&#8217;interfaccia per il jar:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#libfreetts.rb</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'freetts/freetts.jar'</span>
&nbsp;
import com.<span style="color:#9900CC;">sun</span>.<span style="color:#9900CC;">speech</span>.<span style="color:#9900CC;">freetts</span>.<span style="color:#9900CC;">Voice</span>
import com.<span style="color:#9900CC;">sun</span>.<span style="color:#9900CC;">speech</span>.<span style="color:#9900CC;">freetts</span>.<span style="color:#9900CC;">VoiceManager</span>
import com.<span style="color:#9900CC;">sun</span>.<span style="color:#9900CC;">speech</span>.<span style="color:#9900CC;">freetts</span>.<span style="color:#9900CC;">util</span>.<span style="color:#9900CC;">Utilities</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> FreeTTS
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#0066ff; font-weight:bold;">@voice</span> = VoiceManager.<span style="color:#9900CC;">getInstance</span>.<span style="color:#9900CC;">getVoice</span><span style="color:#006600; font-weight:bold;">&#40;</span>Utilities.<span style="color:#9900CC;">getProperty</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;voice16kName&quot;</span>,<span style="color:#996600;">&quot;kevin16&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@voice</span>.<span style="color:#9900CC;">allocate</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> speak<span style="color:#006600; font-weight:bold;">&#40;</span>txt=<span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#9966CC; font-weight:bold;">unless</span> txt
    <span style="color:#0066ff; font-weight:bold;">@voice</span>.<span style="color:#9900CC;">speak</span> txt
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Creiamo una semplicissima risorsa &#8220;<em>sentence</em>&#8221; con un solo campo &#8220;<em>body</em>&#8221;</p>
<p><code><br />
C:ProvaFreeTTS>jruby script/generate scaffold sentence body:text<br />
</code></p>
<p>Ora creiamo due nuove operazioni, come ho spiegato approfonditamente in un <a href="http://mastrodonato.info/index.php/2009/06/aggiungere-un-operazione-ad-una-risorsa-in-un-sistema-restful/">precedente articolo</a>.</p>
<p>Iniziamo dal controller aggiungendo in coda:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;">#appcontrollerssentences_controller.rb</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> speak
    <span style="color:#0066ff; font-weight:bold;">@sentence</span> = Sentence.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:sentence</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'freetts'</span>
    tts = FreeTTS.<span style="color:#9900CC;">new</span>
    tts.<span style="color:#9900CC;">speak</span> <span style="color:#0066ff; font-weight:bold;">@sentence</span>.<span style="color:#9900CC;">body</span>
    render <span style="color:#006600; font-weight:bold;">&#40;</span>@sentence.<span style="color:#9900CC;">new_record</span>? ? <span style="color:#ff3333; font-weight:bold;">:new</span> : <span style="color:#ff3333; font-weight:bold;">:edit</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> read
    <span style="color:#0066ff; font-weight:bold;">@sentence</span> = Sentence.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'freetts'</span>
    tts = FreeTTS.<span style="color:#9900CC;">new</span>
    tts.<span style="color:#9900CC;">speak</span> <span style="color:#0066ff; font-weight:bold;">@sentence</span>.<span style="color:#9900CC;">body</span>
    redirect_to <span style="color:#ff3333; font-weight:bold;">:back</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Ora andiamo a modificare le viste.<br />
Creiamo un nuovo file, più precisamente un partial dove inseriremo il form dati per la nostra risorsa, in questo modo utilizzeremo lo stesso codice per tutte le operazioni:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">#appviewssentence_sentence.html.erb
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span> form_for<span style="color:#006600; font-weight:bold;">&#40;</span>@sentence<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">error_messages</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_area</span> <span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">submit</span> <span style="color:#996600;">'Update'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
&lt;h2&gt;Preview&lt;/h2&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> form_for <span style="color:#0066ff; font-weight:bold;">@sentence</span>, <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> speak_sentences_path, <span style="color:#ff3333; font-weight:bold;">:method</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:put</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">error_messages</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_area</span> <span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">submit</span> <span style="color:#996600;">'Speak'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Aggiungiamo un form clone di quello creato dallo scaffold ma con l&#8217;action diversa, per richiamare l&#8217;operazione speak.</p>
<p>Ora modifichiamo le viste create in automatico:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">#appviewssentencenew.html.erb
&nbsp;
&lt;h1&gt;New sentence&lt;/h1&gt;
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span>= render <span style="color:#0066ff; font-weight:bold;">@sentence</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Back'</span>, sentences_path <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
#appviewssentenceedit.html.erb
&nbsp;
&lt;h1&gt;Editing sentence&lt;/h1&gt;
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span>= render <span style="color:#0066ff; font-weight:bold;">@sentence</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Show'</span>, <span style="color:#0066ff; font-weight:bold;">@sentence</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> |
<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Back'</span>, sentences_path <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Infine modifichiamo la lista per richiamare la seconda delle operazioni implementate, read. Sostanzialmente cliccando sulla riga corrispondente leggerà il testo precedentemente memorizzato nel db.</p>
<p>Dobbiamo solamente aggiungere una riga ottenendo questa view:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">#appviewssentenceindex.html.erb
&lt;h1&gt;Listing sentences&lt;/h1&gt;
&nbsp;
&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;Body&lt;/th&gt;
  &lt;/tr&gt;
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#0066ff; font-weight:bold;">@sentences</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>sentence<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;tr&gt;
    &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>=h sentence.<span style="color:#9900CC;">body</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
    &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Read'</span>, read_sentence_path<span style="color:#006600; font-weight:bold;">&#40;</span>sentence<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
    &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Show'</span>, sentence <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
    &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Edit'</span>, edit_sentence_path<span style="color:#006600; font-weight:bold;">&#40;</span>sentence<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
    &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Destroy'</span>, sentence, <span style="color:#ff3333; font-weight:bold;">:confirm</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Are you sure?'</span>, <span style="color:#ff3333; font-weight:bold;">:method</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:delete</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
  &lt;/tr&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&lt;/table&gt;
&nbsp;
&lt;br /&gt;
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'New sentence'</span>, new_sentence_path <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Infine andiamo a modificare il routing:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#configroutes.rb</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#sostituiamo map.resources :sentences con</span>
map.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:sentences</span>, <span style="color:#ff3333; font-weight:bold;">:member</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:read</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:get</span> <span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#ff3333; font-weight:bold;">:collection</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:speak</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:put</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#aggiungiamo</span>
map.<span style="color:#9900CC;">root</span> <span style="color:#ff3333; font-weight:bold;">:controller</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;sentences&quot;</span></pre></div></div>

<p>Configuriamo il file database.yml, creiamo il db e le tabelle con rake e facciamo partire glassfish come spiegato <a href="http://mastrodonato.info/index.php/2009/07/configurare-un-ambiente-per-jruby-on-rails/">qua</a> (cerca database.yml)</p>
<p>Come abbiamo visto, utilizzare librerie java con jruby on rails è semplicissimo, siamo pronti per far pronunciare al nostro server tutte le frasi più sporcaccione!</p>
<p>Il <strong>progetto completo</strong> potete scaricarlo <a href="http://www.gigasize.com/get.php?d=p4wsyz5z29c">qua</a>.</p>
<p>Per ulteriori approfondimenti c&#8217;è anche questo <a href="http://ruby.html.it/articoli/leggi/3110/jruby-java-e-ruby-insieme/1/">articolo</a>, mentre <a href="http://java.html.it/articoli/leggi/3098/text-to-speech-con-java-e-freetts/1/">questo </a>tratta la libreria FreeTTS in un progetto Java.</p>
<p>Buona continuazione.</p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2009/08/freetts-una-libreria-java-in-jruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
