Creating a Text to Speech Application

Creating a Text to Speech Application
Creating a Text to Speech Application is surprisingly simple on android. I’m going to write a quick post with some great pointers on how to accomplish this. What you need: Android SDK Eclipse (I’m using Helios) Lets begin: First we need to create a new android project in Eclipse. In Eclipse Click File, New, and select android project. You will be created with the following screen. Enter your project information and click Next. Next we need to select a target platform. I have chosen 2.2, make your selection and click Next to continue. Now we need to name our package name, activity name, and set the minimum SDK. Enter your information and select Finish to complete creating the android project. Now that we have the project created lets get started with the good stuff. Start off by creating the layout. open /res/layout/main.xml This is the screen for the application, it is also by default created by eclipse. In this document we are going to create an EditText for user input and a button to start the text to speech. Lets add that EditText: delete the text view that eclipse adds by default. Add the EditText in between the LinearLayout
<EditText android:id=”@+id/Speech” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:hint=”Enter Text for Speech!” />
Alright now that we have the Edit Text added we can continue on to adding the button. Now add the following into your xml
    <Button android:id=”@+id/Speak” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Speak!” android:onClick=”onClick”/> The complete main.xml should look like the following <?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:orientation=”vertical” > <EditText android:id=”@+id/Speech” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:hint=”Enter Text for Speech!” /> <Button android:id=”@+id/Speak” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Speak!” android:onClick=”onClick”/> </LinearLayout>
Lets take a look graphicaly at what we have created. Open your java file under /src/’your package name’/ Once opened you can see where eclipse has done some of the work for you. You content view has already been set to your main.xml Start off by adding the EditTex,Speech button, and The Text To Speech into the class. With the code below you have noticed that I have added ‘implements OnInitListener, OnUtteranceCompletedListener” You must add this as TextToSpeech uses this to check to see if the required data for TextToSpeech is installed. public class TextToSpeechExampleActivity extends Activity implements OnInitListener, OnUtteranceCompletedListener { /** Called when the activity is first created. */ Button Speak; EditText Speech; private TextToSpeech mTts; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Speak = (Button)this.findViewById(R.id.Speak); Speech = (EditText)this.findViewById(R.id.Speech); //initialize TTS mTts= new TextToSpeech(this, this); } public void onInit(int initStatus) { mTts.setOnUtteranceCompletedListener(this); } public void onUtteranceCompleted(String utteranceId) { mTts.shutdown(); } You will notice that you will have some errors once this code has been written. The error should be the following “Button cannot be resolved as a type” “EditText cannot be resolved as a type” “TextToSpeech cannot be resolved as a type” The fix for these errors are very simple. Simply go over each error and have eclipse import Button, EditText, and TextToSpeech. There is a even easier way to do this with eclipse. Press Ctrl+Shift+O. This will organize your imports for you and even delete unused ones. Once you have imported Button, EditText, and TextToSpeech we should be error free. So Lets keep trucking, the only thing we lack is what the button is finally going to do! I handle my button clicks using the onClick insert the code below into your class public void onClick(View v) { switch(v.getId()) { case R.id.Speech: //when Button Speak is clicked //grab text from the EditText Speech String TALK = Speech.getText().toString(); //Talk now has text from EditText mTts.speak(TALK, TextToSpeech.QUEUE_ADD, null); break; } } The complete class should look like the following “Your package name goes here”; import android.app.Activity; import android.os.Bundle; import android.speech.tts.TextToSpeech; import android.speech.tts.TextToSpeech.OnInitListener; import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener; import android.view.View; import android.widget.Button; import android.widget.EditText; public class TextToSpeechExampleActivity extends Activity implements OnInitListener, OnUtteranceCompletedListener { /** Called when the activity is first created. */ Button Speak; EditText Speech; private TextToSpeech mTts; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Speak = (Button)this.findViewById(R.id.Speak); Speech = (EditText)this.findViewById(R.id.Speech); //initialize TTS mTts= new TextToSpeech(this, this); } public void onInit(int initStatus) { mTts.setOnUtteranceCompletedListener(this); } public void onUtteranceCompleted(String utteranceId) { mTts.shutdown(); } public void onClick(View v) { switch(v.getId()) { case R.id.Speak: //when Button Speak is clicked //grab text from the EditText Speech String TALK = Speech.getText().toString(); //Talk now has text from EditText mTts.speak(TALK, TextToSpeech.QUEUE_ADD, null); break; } } } There you go! That ties up the Text to Speech Application if you have any questions about the posted code or have any suggestions. Feel free to contact us or leave a comment. Thanks
Posted in Uncategorized | Comments Off on Creating a Text to Speech Application

What’s wrong with straight-line coding?

The prevalent view of straight-line coding defines it as code with minimal branching, that is – based on certain conditions the execution will follow one path or another. I define straight-line coding as the method many developers follow by writing code from start to finish for each set of scenarios. Memories of attempting to plow a straight row in a field on a tractor involved picking an object at the end of the field and never taking your eye off it. Straight-line coders follow that same philosophy coding from point A to point B numerous times until all requirements are covered. One of the biggest problem with straight-line code is the maintenance.

Software maintenance can run as high as 50%-80% of development costs. In the early days of software development, emphasis was placed on making every single line of code count. The same was true for every I/O. This philosophy is not stressed with expanse of memory, megahertz, and DASD. There is an art to writing effective maintainable code. Before a program can be modified, it must be understood. In order for it to be understood, it should be readable and it should be designed such to withstand small changes.

The code shown in Figure 1 is taken from two software developers. At the time of this documentation, developer A had 5 years of experience and used straight-line coding. Developer B had 7 years and used a more structured approach to coding. The flowcharts depict software that was written to validate UPC/SCC for a national distribution chain. UPC is the Universal Product Code, the standard barcode used in identifying individual items. The SCC (Shipping Container Code) is the barcode used to identify shipping containers. Conditional branching, represented in the two flowcharts as diamonds, signifies a point in the program when a decision must be made. The rectangles in this figure represent calculations and data input/output processing. Execution of code flows from top down until a diamond is encountered; the program would branch based on the condition at the time of execution. The first diamond encountered in developer A code tests for the length of the UPC/SCC. If the length = 14, execution branches to the right, if not it continues to the lower diamond and tests for the length of 12. Developer A logic would have to increase by 20% in order to support all the scenarios as the code written by developer B.The most significant difference is that code generated by developer B is easier to read and maintain. This is not an issue of education or training; it is rooted in how developers have been geared to think.

Posted in Uncategorized | Comments Off on What’s wrong with straight-line coding?

Learning syntax and programming languages

Most introductory educational programming courses and textbooks focus on teaching the features of a specific language and how to use them. The volume of features in many languages requires such focus and acquiring such knowledge plays a large part of program development. Both individual laboratory-based programming tasks and collaborative work should be used in introductory courses. Problem solving should be at the heart of teaching programming. It is suggested that language features only be introduced as they become applicable to specific problems. However, problem solving in and of itself, it is not enough.

One of the biggest problems in some introductory programming courses in that the content is largely language specific. Students are presented with textbook problems and solutions and to a small extent are charged with the task of translating solutions into source code. This teaching technique bypasses the important process of comprehending, analyzing, strategizing solutions for real world problems.

M.C. Linn describes a chain of cognitive accomplishments by students in learning programming. The first link consists of acquiring language skills. Problem solving should be the vehicle used in learning language features. Learning features and learning problem solving should not be thought of as two distinct tasks. In earlier days, BASIC programming language was used in introductory courses, but since has changed to more challenging languages. This may be in part that educators recognized that syntax and semantic skills alone are not adequate in preparing for real world programming and that these skills do not translate well across languages for novice programmers.

A second link consists of design skills. The designing aspect of programming involves using computer language features to solve problems. There are two skill sets students use in program design: templates and procedural. Templates consist of code patterns that of a single language feature that can be drawn upon for reuse by students. “If programmers develop a repertoire of effective templates, then they are likely to program more effectively and to identify and reuse algorithms for problem solutions rather than reinvent them. Students can often employ templates they could not invent themselves. By using these templates they can ‘stand on the shoulders of giants’ and solve more interesting and complex problems than would be possible if they had to invent all their own templates. Even if they do not fully understand a template at first, the experience of using a well designed one will help students comprehend the technique in the template and the role of templates in general” (Linn, 1985). As students apply templates from existing repertoire, they advance beyond simply translating pseudo code to syntax, and become better equipped to solve programming problems. Procedural skills consist of planning, testing, and reformulation based on test results. This is accomplished by combining templates and language features.

Posted in Uncategorized | Comments Off on Learning syntax and programming languages

Left verses Right

One might question the effectiveness of one process over another in IT organizations. It cannot be argued the fact that IT process changes have made this industry the better. Integrated applications have become more and more complex as allowed by hardware advancements. Applications of today mandate varying models for process controls. There is one resource however that has remained constant – people. IT management must not lose sight that good personnel can make up for process shortcomings – good process cannot make up for personnel shortcomings. The most direct and lasting benefit IT management can give to an organization is to hire the most qualified people and find the best means of moving novice programmers to expert. Educational development plays an important first step in this process. </p><p>Software engineering is described as the process of applying tools, methods, and disciplines in an automated means to a real-world problem. Software engineering is about solving problems. Initially students acquire skills through education in programming languages and algorithmic development. Software engineering is applying those skills towards problems; it is not a predetermined set of practices. In his article “No Silver Bullet”, F P Brooks describe the difficulties in software engineering improvements dividing them into essence and accidents. “Good designers” is listed as one of the four solutions in overcoming difficulties. Good designs come from good designers. Good process cannot make up for personnel shortcomings – good personnel can make up for process shortcomings. </p><p>What causes such varying degrees of expertise among software developers? Education plays a vital role in the success of an individual in this industry. Environment and culture also can have an impact on how developers progress in an organization (Jin 2000). But what part do cognitive abilities play in the progression? </p><p>There have been numerous empirical studies that suggest that left hemisphere dominated people tend to pick up software development easier than people who are right hemisphere dominated. The left side of the brain deals more with rational and abstract thinking; while the right side controls creative, concrete, and deductive reasoning. However, both LH and RH play an important role in software development. The left side of the brain handles logical; the right side created. Different people process information in different ways based on the area of brain and their cognitive style. Procedural programming tends to favor left hemisphere dominate while object oriented is hemispheric friendly. Consensus is that regardless of one’s cognitive tendencies, anyone can be taught through repetition. After 20+ years of consulting with varying shops, I feel this community is best served when the differences of LH/RH dominant tendencies are recognized and both are strengthened through mentoring.
Posted in Uncategorized | Comments Off on Left verses Right

Conditionally display fields in a gridview (ASP.Net)

The following code will display one of two fields based on the value of the first field. Place the following vb code within the RowDataBound event.
If e.Row.Cells(0).Text &gt; "A" Then e.Row.Cells(7).Visible = True e.Row.Cells(8).Visible = False Else e.Row.Cells(7).Visible = False e.Row.Cells(8).Visible = True End If

e.Row.Cells(0).Visible = False

Posted in Uncategorized | Comments Off on Conditionally display fields in a gridview (ASP.Net)

Software Development – Science or Art?

Comparisons have been made between teaching software development to new students and learning a new foreign language. Acquiring software development skills in many cases has as many similarities to the school of arts as to the sciences. Composing a new application can be compared to composing a book or concerto. Suppose that arts curriculum took the same approach as do computer sciences? Consider for example a four-year undergraduate program for music composition. If syntax were considered the heart of curriculum, the focus would consist of teaching notes (the most rudimentary element of music), scales, arpeggios, timing, phrasing, harmony, and dynamics. Studies would primarily involve work done only by the student. Exposure to musical pieces of well known composers would be limited to small music snippets to demonstrate techniques. Suppose after four years of studying techniques and rules students then were asked to compose a piano concerto in E minor. It would be highly unlikely that any of the pieces would be robust and rich. These compositions would be limited by the degree of exposure the students had to outside compositions. The more exposure students have to outside compositions, the richer theirs will be. The same can be applied to software developers. Expectations are unrealistic for students to take code snippets and minimal application exposures and be able to build robust applications.

“Reading a well-written program can teach you techniques that help you write good programs. In fact, it’s difficult to imagine how anyone could become a good programmer without reading good programs”. Software engineering principles. Dale, N. (2003).

 

Posted in Uncategorized | Comments Off on Software Development – Science or Art?

How to process files in weighted order with RPGLE/SQL

Here is an easy way to read from a file using RPGLE/SQL and weight the sequence (sort them) based on data within a specific field.

CSR EXSR01 BEGSR C/EXEC SQL C+ DECLARE C1 CURSOR FOR
C+ SELECT DBFIELD1, DBFIELD2, DBFIELD3
C+ FROM DBFILE
C+ WHERE (DBFIELD1 = :RPGFIELD)
C+ ORDER BY DBFIELD1,
C+ CASE
C+ WHEN DBFILE.DBFIELD3 = ‘DEF’ THEN SMALLINT(10)
C+ WHEN DBFILE.DBFIELD3 = ‘ABC’ THEN SMALLINT(20)
C+ WHEN DBFILE.DBFIELD3 = ‘A01’ THEN SMALLINT(30)
C+ WHEN DBFILE.DBFIELD3 = ‘153’ THEN SMALLINT(40)
C+ ELSE SMALLINT(99)
C+ END DESC
C/END-EXEC
C/EXEC SQL
C+ OPEN C1
C/END-EXEC
C DOU SQLCODE <> *ZEROS
C/EXEC SQL
C+ FETCH C1 INTO :RPGFLDSTRUCT
C/END-EXEC
C IF SQLCODE = 100
/free
exec sql
CLOSE C1 ;
*INLR = *ON ;
/end-free
C LEAVESR . . . *exit loop
. . . DO SOMETHING
C ENDDO
C ENDSR


Posted in Uncategorized | Comments Off on How to process files in weighted order with RPGLE/SQL

Straight Line Coding

Straight-line coding can be described as the tendency of novice software developers to code from start to finish for each set of circumstances. The straight-line code employed by developers is in some ways similar to greedy algorithms. The developer has a vague picture of the finish line, but does not possess a plan. From the perspective of “where I am” and “where I think I need to be”, the best course of action is to take one step in this direction. The major difference between greedy algorithms and straight-line coding is that greedy algorithms analysis is based on short-term best solution, whereas straight-line coding focus is solely on a direct path from start to finish.

Novice SD developers are often overwhelmed with the information and cannot process effectively.  They do not decompose the requirements into smaller tasks and subtasks.  Straight-line programming does not incorporate decomposition in the thought process; it is simply the process of driving from point A to point B.   This process is repeated until the developer considers all viable contingencies are covered.

Posted in Uncategorized | Comments Off on Straight Line Coding