Monday 4 February 2013


Text to Speech using Java

Text to speech conversions have fascinated developers for many years. Most modern languages support the technique. In this article Akhilesh demonstrates Java powered text to speech conversion.
This tutorial teaches how to convert the text input into Speech using Java. This can be implemented in two ways. They are,

1) JSAPI 1.0(Java Speech Application Programming Interface)
2) Free TTS(Free Text To Speech)


JSAPI 1.0:-
JSAPI 1.0 is developed by Sun MicroSystems. Jsapi contains two core technologies, viz. Speech Synthesis and Speech Recognition.
The Speech synthesis is a speech engine that converts the text into speech.
The Speech Recognition is used for Speech to text conversion. More Details about the JSAPI have been given below.


FreeTTS:-
FreeTTS is an open source package. It is entirely written by making use of Java programming language. This package can also be used to convert the text into speech.

Necessity for the conversion of the Text into Speech:
Speech synthesis of text in a word processor is an aid to proof-reading. It is easier to detect grammatical and stylistic problems. In the TTS, if we save the file in audio format, the size of the file will be larger than that of text file. It may be more useful in mobile phones in which we can hear the message that we have received instead of reading the SMS.

Methods to convert Text into Speech
Structure analysis: Structure analysis processes the input text to determine where paragraphs, sentences and other structures start and end.
Text pre- processing: This expands any abbreviations or other forms of shorthand.

e.g. St. Joseph becomes Saint Joseph.

Text-to-phoneme conversion: It converts each word to phonemes which refers to a basic unit of sound in a language.

Prosody analysis: It processes the sentence structure, words and phonemes to determine appropriate prosody for the sentence.

Waveform production: Finally, the phonemes and prosody information are used to produce the audio waveform for each sentence.

Requirements for Text to Speech:

The following jar files are very important

cmu_us_kal.jar,
cmulex.jar,
en_us.jar;
freetts.jar;
cmulex.jar;
jsapi.jar;

The above files are available in FreeTTS-1.2.1-bin. FreeTTS is a open source package. Download freetts-1.2.1-bin.zip from 
http://sourceforge.net/projects/freetts/

1. Unzip the freeTTS binary package and check inside the \lib directory, that all the above jar files are available except jsapi.jar.
2. The jsapi.exe file will be available.
3. Run Jsapi.exe, and you will get jsapi.jar.
4. Copy all the Jars (jsapi.jar, freetts.jar, cmu_time_awb.jar, cmu_us_kal.jar, etc.) to that working folder or C:\Program files\java\ jdk1.6.0_03\jre\lib\ext

Important Classes in javax.speech package:
Import javax.speech.*; import javax.speech.synthesis.*;

Speech package contains two important packages namely synthesis and recognizer.


Engine:
The Engine interface is available inside the speech package. The Engine interface is the parent interface for all speech engines including Recognizer and Synthesizer. “Speech engine” is the generic term for a system designed to deal with either speech input or speech output.
import javax.speech.Engine;

The basic processes for using a speech engine in an application are as follows.

1. Identify the application’s functional requirements for an engine (e.g, language or dictation capability).
2. Locate and create an engine that meets those functional requirements.
3. Allocate the resources for the engine.
4. Set up the engine.
5. Begin operation to allocate the engine and resume it.
6. Use the engine. Deallocate the resources of the engine.

Central:
The Central class is the initial access point to all speech input and output capabilities. Central provides the ability to locate, select and create speech recognizers and speech synthesizers

SynthesizerModeDesc
SynthesizerModeDesc extends the EngineModeDesc with properties that are specific to speech synthesizers. A SynthesizerModeDesc inherits engine name, mode name, locale and running properties from EngineModeDesc. SynthesizerModeDesc adds two properties:List of voices provided by the synthesizer Voice to be loaded when the synthesizer is started.
Synthesizer
The Synthesizer interface provides primary access to speech synthesis capabilities



Voice: 
A description of one output voice of a speech synthesizer. Voice objects can be used in selection of synthesis engines (through the SynthesizerModeDesc). The current speaking voice of a Synthesizer can be changed during operation with the setVoice method of the SynthesizerProperties object.


Create a simple program using jsapi speech synthesis.
Import javax.speech.*; import javax.speech.synthesis.*;

Step 1:
 We first import the following Packages.
Step 2 : Create a Synthesizer:-

Synthesizer syn = Central.createSynthesizer(null);

This method creates a default Systhesizer. This Synthesizer gets the default locale.
SynthesizerModeDesc desc=new SynthesizerModeDesc();desc.setLocale(new Locale("de", ""));desc.addVoice(new Voice(null, GENDER_FEMALE, AGE_DONT_CARE, null));Synthesizer synthesizer = Central.createSynthesizer(desc);

The above coding is to select a particular Locale and Particular Voice.

Step 3:
The following code is to allocate and resume the systhesizer.
synthesizer.allocate(); synthesizer.resume();

Step 4:

Voice[] voices = desc.getVoices();
Get the available voices:

The get voices method return all the available voices from the selected Synthesizer.
synthesizer.getSynthesizerProperties(). setVoice(voice);

Step 5:
The setVoice() Method sets a particular Voice.

Step 6:Speak The text
synthesizer.speakPlainText(speaktext, null); synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);

The speakPlainTex(speaktext,null) method speaks the given text until queue is empty.

Step 7:
Deallocate the Synthesizer:
synthesizer.deallocate();
deallocate() method is deallocate the synthesizer.


Demo Programs:
Two demo programs are given below. The first one uses the jsapi.jar and the second one uses freetts.jar.
import javax.speech.*;
import java.util.*;
import javax.speech.synthesis.*;

public class demojsapi
{
String speaktext;
public void dospeak(String speak,String
voicename)
{
speaktext=speak;
String voiceName =voicename;
try
{
SynthesizerModeDesc desc = new
SynthesizerModeDesc(null,"general",
Locale.US,null,null);
Synthesizer synthesizer =
Central.createSynthesizer(desc);
synthesizer.allocate();
synthesizer.resume();
desc = (SynthesizerModeDesc)
synthesizer.getEngineModeDesc();
Voice[] voices = desc.getVoices();
Voice voice = null;
for (int i = 0; i < voices.length; i++)
{
if (voices[i].getName().equals(voiceName))
{
voice = voices[i];
break;
}
} synthesizer.getSynthesizerProperties().setVoice(voice);
synthesizer.speakPlainText(speaktext, null); synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
synthesizer.deallocate();
}catch (Exception e)
{ String message = " missing speech.properties in " + System.getProperty("user.home") + "\n";
System.out.println(""+e);
System.out.println(message);}
}
public static void main(String[] args)
{
demojsapi obj=new demojsapi();
obj.despeak(args[0],” Akhilesh006”);
}

}

Running Procedure:
1) create a folder named texttospeech.

2) Copy all the Jars (jsapi.jar, freetts.jar, cmu_time_awb.jar, cmu_us_kal.jar, etc.) to that folder or C:\Program files\java\ jdk1.5.0\jre\lib\ext.
3) create the program named demojsapi.java

4) If all the jars files are in your working folder, then set the class path as

set classpath=cmu_us_kal.jar;en_us.jar;
freetts.jar;cmulex.jar;jsapi.jar;

If all the jar files do not exist in your working folder, set the class path as
(ex)
Set classpath=C:\Program files\java\ jdk1.5.0\jre\lib;C:\Program files\java\ jdk1.5.0\jre\lib\cmu_us_kal.jar; C:\Program files\java\jdk1.5.0\jre\lib\en_us.jar;C:\Program files\java\ jdk1.5.0\jre\lib\freetts.jar;C:\Program iles\java\ jdk1.5.0\lib\cmulex.jar;C:\Program files\java\ jdk1.5.0\jre\lib\jsapi.jar;

4)set the java path:
eg: set path=c:\windows\system32;c:\jdk1.4\bin

5) Compile the program
javac demojsapi.java

6) Run the program
java demojsapi “Welcome to ACCET”
(If you get the error : “missing speech.properties in user home: c:\documents and setting\user”, copy the speech.properties from freetts1.2.1 folder and paste it into user home(if we use windows xp, the user home is c:\documents and setting\user)

7)You will get the expected result.


2) demofreetts.java
import com.sun.speech.freetts.*;
import java.util.*;

public class demofreetts
{
private String speaktext;
public void dospeak(String speak,String
voice)
{
speaktext = speak;
try
{
VoiceManager voiceManager =
VoiceManager.getInstance();
Voice voices =
voiceManager.getVoice(voice);

Voice sp=null;
if(voices!=null)
{
sp=voices;

}
else
{
System.out.println("No Voice Available");
}

//========================
sp.allocate();
sp.speak(speaktext);
sp.deallocate();
//=========================
}catch(Exception e){e.printStackTrace();}
}
public static void main(String[] args)
{
demofreetts obj=new demofreetts();
obj.despeak(args[0],”Akhilesh006”)
}

}
The procedure for running the second program is the same as that of the first one.

About Author:
Akhilesh Dubey is final year MCA Student. You can reach him on 
akhil.bca08@gmail.com

Sunday 3 February 2013


Storage Class Variables:


Static storage class:
Storage: main memory & data segment.
Default value: zero.
Scope: local to the block in which the variable is defined.
Lifetime: till the value of the variable persists between different function calls.

Example:
#include <stdio.h>
//program in file f1.c
void count(void) {
static int count1 = 0;
int count2 = 0;
count1++;
count2++;
printf("\nValue of count1 is %d, Value of count2 is %d", count1, count2);
}
/*Main function*/
int main(){
count();
count();
count();
return 0;
}
Output:
Value of count1 is 1, Value of count2 is 1
Value of count1 is 2, Value of count2 is 1
Value of count1 is 3, Value of count2 is 1

Automatic storage class:
Storage : stack.
Default value: garbage value.
Scope: local to the block in which the variable is defined.
Lifetime: till the control remains within the block in which the variable is defined.
Example: auto int a = 5; int a = 5;
Register storage class:
Storage: CPU registers.
Default value: garbage value.
Scope: local to the block in which the variable is defined.
Lifetime: till the control remains within the block in which the variable is defined.

Example: register int x=5;

Automatic storage class:
Storage: main memory.
Default value: garbage value.
Scope: local to the block in which the variable is defined.
Lifetime: till the control remains within the block in which the variable is defined.


External storage class:
Storage: main memory.
Default value: zero.
Scope: global on other file in the same project
Lifetime: as long as the program execution doesn't come to an end.

Example:
/***************
Index: f1.c
****************/
#include <stdio.h>
extern int x;
int main() {
printf("value of x %d", x);
return 0;
}
Index: f2.c
****************/
int x = 3;

Global variables:
Storage: main memory.
Default value: zero.
Scope: throughout the program.
Lifetime: till the control remains within the block in which the variable is defined.
Note: declared above the main( ) function.

Bitwise operator:
Bitwise AND(&) operator.
ANDing operation :
________________________
10101101 original bit pattern
00001000 AND mask
----------------------------------------
00001000 resulting bit pattern

Left Shift or Right shift of number:

Left shift:

Eg1: 14<<1;
Consider a number 14-----00001110 (8+4+2)is its binary equivalent
left shift it by 1--------------00011100(16+8+4) which is 28.
Eg2: 1<<1;
consider the number as 1---00000001(0+0+1).
left shift that by 1------------00000010(0+2+0) which is 2.
left shift by 1 bit of a number=2*number
left shift by 1 bit of 2*number=2*2*number
left shift by n bits of number=(2^n)*number

Right Shift:

Eg1: 14>>1;
Consider a number 14-----00001110 (8+4+2)is its binary equivalent
right shift it by 1-----------------0000111(4+2+1) which is 7.
Eg2: 1<<1;
consider the number as 1---00000001(0+0+1).
right shift that by 1----------------0000000(0+0+0) which is 0.
right shift by 1 bit of a number=2/number
right shift by 1 bit of 2*number=2/2/number
right shift by n bits of number=(2^n)/number.





ExecuteDOSCommand.java

/*In the following program I'd tried to open a Notepad using DOS command.
In the same way you can execute other various commands like shutdown your computer or compile a java program.
Here it go:
 */

public class ExecuteDOSCommand {

    public static void main(String... s) {
        try {
            Thread.sleep(5000);//the main thread sleep for 5sec. then it execute
            Process p = Runtime.getRuntime().exec("Notepad");//command to open notepad
        } catch (Exception e) {
        }
    }
}
/*
Here are other codes you can try:

Process p=Runtime.getRuntime().exec("shutdown -s -t 10");//shutdown ur computer after 10sec.

Process p=Runtime.getRuntime().exec("javac <yourFileName>.java");
 */

PasswordGenerator.c:



#include<conio.h>
#include<stdio.h>
#include<ctype.h>

void main(){
char data,password[100]="\0";
int col=25,row=21;
int i=0;
clrscr();
gotoxy(25,20);
printf("Enter the password:\n");
while(1)
{
gotoxy(col,row);
flushall();
data=getch();
if(data!=8) //condition for digit
{
password[i]=data; //convert char to int
printf("*",data);
col++;
i++;
}
if(data==8) //condition for BACK_SPACE key
{
if(col<=25)
col=25;
else
{
col--;
printf("\b");
i--;
clreol();
}

}
else if(data==13) // condition for ENTER key
break;
else if(data==27) //condition for ESC key
break;
flushall();
}
password[i]='\0';
printf("\n\n%s",password); //display number
getch();
}

Print matrix diagonal and secondary Diagonal elements :

int mat[3][3]={
{1,2,3},
{4,5,6},
{7,8,9}
};
for(int i=0;i< 3;i++)
printf("%d",mat[i][i]);
printf("\n\n");
for(i=0;i< 3;i++)
printf("%d",mat[i][3-i-1]);

// somthing intreating of coding... lets C first.


Mouse programming in C:

#include<stdio.h>
#include<process.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
union REGS i,o;
int initmouse(void);
void showarrow(void);
void mousearea(int, int, int,int);
void m_pointerat(int *, int *, int *);
void main()
{
int gd=DETECT,gm,maxx,maxy,button,x,y;
clrscr();
initgraph(&gd,&gm,"");
maxx=getmaxx();
maxy=getmaxy();
rectangle(0,56,maxx,maxy);
setviewport(1,57,maxx-1,maxy-1,1);
gotoxy(26,1);
printf("mouse button pressed");


if(initmouse()==0)
{
closegraph();
restorecrtmode();
printf("\nmouse driver not found");
exit(1);
}
mousearea(0,100,maxx-50,maxy-10);
showarrow();
gotoxy(52,3);
printf("\nPress any key to exit");
while(!kbhit())
{
m_pointerat(&button,&x,&y);
gotoxy(5,3);
(button&1)==1?printf("LEFT"):printf("***");
gotoxy(20,3);
(button&2)==2?printf("RIGHT"):printf("****");
gotoxy(65,3);
printf("X=%03d,Y=%03d",x,y);
if(x==589&&y==100)
{
if((button&1)==1)
exit(1);
}
}
getch();
}


initmouse()
{
i.x.ax=0;
int86(0x33,&i,&o);
return(o.x.ax);
}


void showarrow()
{
i.x.ax=1;
int86(0x33,&i,&o);
}
void mousearea(int x1,int y1,int x2,int y2)
{
i.x.ax=7;
i.x.cx=x1;
i.x.dx=x2;
int86(0x33,&i,&o);
i.x.ax=8;
i.x.cx=y1;
i.x.dx=y2;
int86(0x33,&i,&o);
}
void m_pointerat(int *button,int *x,int *y)
{
i.x.ax=3;
int86(0x33,&i,&o);
*button=o.x.bx;
*x=o.x.cx;
*y=o.x.dx;
}

Note: first copy EGAVGA.BGI in bin folder, then run it....!!!

Waiting thread in C:

#include<dos.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int i=0;
clrscr();
printf("Look at number: ");
for(i=0;i<=100;i++)
{
gotoxy(20,1);
printf("%d",i);
delay(50);
}
getch();
}