Elettracompany.com

Компьютерный справочник
0 просмотров
Рейтинг статьи
1 звезда2 звезды3 звезды4 звезды5 звезд
Загрузка...

Java speech to text

Как добавить Text-to-Speech в свое приложение

Android предоставляет действительно огромные возможности для работы с устройствами. Например, он позволяет конвертировать текст в речь, о чём и будет говориться в этой статье. Это достигается благодаря использованию Синтеза речи (Text-to-Speech), который не только умеет конвертировать текст в речь, но также может говорить на разных языках.

Синтез речи может использоваться в самых разных областях, например, одно из наиболее частых применений это озвучивание текста для пользователей с нарушением зрения. Синтез речи также применяется для того, чтобы читать вслух книги или изучать языки.

В нашем приложении «Карточки для детей» с помощью Text-to-Speech, если активировать эту опцию в настройках, озвучивается содержимое выбранной карточки при нажатии на неё. В этой статье мы рассмотрим, как добавляется синтез речи в приложение.

Для того, чтобы использовать в своём приложении синтез речи, Android SDK предоставляет класс TextToSpeech, который и синтезирует речь из текста. Его реализация достаточно проста, для начала нужно инициализировать экземпляр TextToSpeech и слушатель onInitListener.

В этом слушателе мы должны определить свойства экземпляра TextToSpeech после его инициализации, такие как язык, тембр и так далее. Для этого добавим в onItit() следующий код.

Если инициализация завершена успешно, то с помощью метода isLanguageAvailable() проверяем доступность русского языка. В зависимости от того, доступен ли русский язык, методом setLanguage() устанавливаем экземпляру TextToSpeech или русский язык, или английский.

setLanguage() в качестве параметра принимает объект Locale, однако здесь есть один момент: TextToSpeech не знает локаль ru_RU, которая возвращается при вызове метода Locale.getDefault(). Поэтому при установке русского языка лучше явно задать локаль ru для корректной работы. Получить её можно, вызвав метод Locale.getDefault().getLanguage().

Метод setPitch() выставляет тембр голоса, который будет озвучивать текст.

Метод setSpeechRate() устанавливает скорость речи.

Теперь, когда объект инициализирован, нужно заставить его читать текст. Для этого при нажатии на карточку вызывается callback, передающий слушателю на активности с экземпляром TextToSpeech данные, которые нужно озвучить. Ключевым в процессе синтеза речи будет являться метод speak() класса TextToSpeech.

В метод speak() необходимо передать следующие параметры:

  • text — текст, который нужно воспроизвести;
  • TextToSpeech.QUEUE_FLUSH — флаг, обозначающий, что при добавлении новой фразы текущую необходимо прервать и начать воспроизведение добавленной. Если вы хотите составить очередь из фраз, следует использовать вместо этого флаг TextToSpeech.QUEUE_ADD;
  • Дополнительные параметры.

Главным отличием здесь является то, как формируются дополнительные параметры, в частности идентификатор фразы.

Для версий ниже Lolipop 5.0 (API 21) нужно создать объект HashMap , в который необходимо добавить пару ключ-значение, где ключ TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID обозначает, что мы хотим добавить в параметры идентификатор фразы, а значение — собственно идентификатор, например «MessageId».

В новых же версиях Android это можно сделать проще, без использования HashMap.

В результате мы добавили TextToSpeech в наше приложение и получили возможность воспроизводить название каждой карточки средствами устройства.

Using Google Cloud Text-to-Speech With Java

Google Cloud recently released a new text-to-speech service. Let’s take it for a test run with a Spring Boot app to see how to work with it in Java.

Join the DZone community and get the full member experience.

Google Cloud Text-to-Speech is a text-to-speech conversion service that got launched a few days back by Google Cloud. This was one of the most important services missing from Google Cloud’s AI portfolio, which is now available and completes the loop for text-to-speech and speech-to-text services by Google Cloud. In the next few weeks, you will learn about different usages of Google Cloud’s text-to-speech service with other Google cloud services.

In this post, you will learn about some of the following:

  • Set up an Eclipse IDE-based development environment
  • Create a Maven or Spring Boot (Spring Starter) Project

Set Up an Eclipse IDE-Based Development Environment

The following are some of the key aspects of setting up the development environment using Eclipse IDE:

Читать еще:  Маткад ошибка значение должно быть скалярным

  • Select or create a Google Cloud project
  • Enable billing for the project
  • Enable Google Cloud’s Text-to-Speech Service; follow this page, Cloud Text-to-Speech API to enable the service. Do not forget to select the project you created in the above steps.
  • Set up authentication by creating credentials in the form of a service account key. The following represents the same:
  • Download the service account key; it gets downloaded as a JSON file.
  • Create a Spring Boot (Spring Starter) project or a Maven project from Eclipse IDE.
  • Right-click on the project. Click on Run As > Configurations and set the environment variable as shown in the next step.
  • Set the GOOGLE_APPLICATION_CREDENTIALS as an environment variable in Eclipse IDE. The following screenshot represents the same.

Create a Maven or Spring Boot (Spring Starter) Project

The following are two key steps that need to be taken to create a sample program/app for demonstrating Google Cloud text-to-speech services

  • Include Maven pom.xml artifacts for Text-to-Speech APIs
  • Create the demo app related to text-to-speech

Include Maven POM.xml Artifacts for Text-to-Speech APIs

The following are some of the artifacts that need to be included for working with Google Cloud Text-to-speech APIs

  • com.google.guava
  • org.threeten (threetenbp)
  • com.google.http-client (google-http-client)
  • com.google.cloud (google-cloud-texttospeech)

Create the Demo App Related to Text-to-Speech Conversion

Pay attention to some of the following aspects that needed to be done for achieving text-to-speech conversion:

  • Create an instance of TextToSpeechClient
  • Set the text input to be synthesized
  • Build the voice request. Set the voice type (male or female) and language code appropriately.
  • Select the type of audio file you want as an output based on audio encoding value. In the example below, MP3 is the type of audio encoding used. The following are some of the different audio encoding supported, the details of which could be found on the page Introduction to Audio Encoding
    • FLAC
    • Linear 16
    • MULAW
    • AMR_WB
    • OGG_OPUS
  • Process the text to speech conversion
  • Retrieve the audio output/content
  • Write the audio content to a file

The following is the code represents the steps above:

Further Reading/References

Summary

In this post, you learned about how to get started with Google Cloud’s Text-to-Speech Service using a Java/Sring Boot app.

Did you find this article useful? Do you have any questions or suggestions about this article? Leave a comment and ask your questions and I shall do my best to address your queries.

Android Speech to Text Tutorial

You might have already used this feature. We can use voice commands search on google. This feature is inbuilt in Android, and if you want you can use this feature to get voice input in your application as well. And this post will explain to you how to use android speech to text feature in your application.

Now let’s see how we use voice input using Android Speech to Text.

Android Speech to Text Tutorial

Creating a new Project

  • We will start by creating a new Android Studio Project. Here I have created a project named SpeechToText.
  • Now let’s begin with the UI design.

User Interface Design

  • We will try to create a beautiful UI as shown in the below image.

Android Speech to Text App

  • First, we will define the colors. And to do this go inside, res->values->colors.xml.
  • Change the style to NoActionBar as well (res->values->styles.xml).
  • As you see in the above image, we have a circular button with an audio recorder icon. You can use any image for the icon. But the tricky thing here is we are using both a custom background and a picture. So for this here we will create two more drawable XML files. One is for the default state of the button, and one is for the active state.
  • So first create an XML file named round_default.xml inside the drawable folder.
  • Now, create one more file named round_active.xml inside the drawable folder.
  • Remember, here we are using icon_record which is already in my project’s drawable folder. In your case, you need to paste an image for this inside your drawable folder.
  • Now for the button background, we will create one more drawable resource file inside the same directory. Create a file named button_background.xml and write the following code.
  • Now finally we will come inside activity_main.xml to create our User Interface. Open activity_main.xml and write the following code.
  • After writing the above-given code in your activity_main.xml you will see the following design.
Читать еще:  Ошибка 0xc000007b battlefield 4

Adding Permission

  • For this application, we need RECORD_AUDIO permission. So, lets first define it in the AndroidManifest.xml.

Checking Permission on Runtime

  • You might already be knowing that after Android Marshmallow we have to ask permission at runtime. So we need to check the permission on runtime if the device running our app is on the android marshmallow or ahead.
  • To do this come inside MainActivity.java and write the following method.
  • Here we are checking if the device is running android marshmallow or ahead. If this condition is true again, we are testing the permission granted or not. If the permission is not granted, we are opening the settings from where the user can allow the permission.
  • So, if the permission is not granted user will see the settings activity, from where the user can allow the permission needed. If the permission is already granted user will directly see our application screen.
  • To achieve our goal that the user should not see the app screen if the permission is not granted, we will first call the above method inside onCreate().

IBM Developer Answers has been replaced

Please read our FAQ below.

IBM continues to transform your support experience. To help you more easily connect with IBM Support and the user community, many IBM business areas and products have consolidated their participation in the previously existing forums like IBM Developer Answers (developerWorks Answers) to the new IBM Support forum.

Some business areas and products, like IBM Z Support, have moved or are in the process of converting their IBM Developer Answers questions into technical documents. You can find these technical documents by using the search on the IBM Support Site.

IBM Z Clients:

For IBM Z products, IBM encourages clients to use the new IBM Support forum to communicate with each other. Z clients will need to open a case if they need to engage IBM support. There are many Preferred Care options for IBM Z that provide usage support to assist with non-defect installation and product-related questions.

SPSS Licensing:

  • For questions on SPSS license related topics, please visit IBM SPSS Licensing support.
  • IBM Statistics Subscription and Trial related questions can be raised on the IBM SPSS Statistics Subscription forum.
  • Other SPSS product questions can be raised in the IBM Support forum.

IBM Cloud:

If you use IBM Cloud, find answers in the IBM Cloud Support Center. There, you can view personalized topics, FAQs, troubleshooting, and community resources for your offering. And, check out the full FAQ library.

Other non-Support:

For all other non-support questions, the communities in the IBM Community ecosystem are available to assist you.

All questions that were migrated from IBM Developer Answers have a redirect in place to automatically take you to the new location.

Use the search bar to search for the same tags you used in the previous forum OR choose your product to sort the questions and narrow down what you’re looking for. You can also search for the tag or the title of the question in the forum search bar.

Читать еще:  Умножение матриц java

Not all questions were migrated. We kept our migration to only the newest and most relevant topics. Our Support and Development areas of the business chose the content to migrate. In general, content 18 months or newer and content important despite age were migrated.

Yes. Many of our Support and Development IBMers also participate in the forums to both ask and help answer questions. Some of our business areas such as ZSystems Software use forums as a client to client experience only.

Yes! Our development team for the IBM Support Site is working very hard to improve the functionality with the goal of bringing it to functional parity with the heritage forums.

Keep an eye on the IBM Support Insider page for updates as we continue to evolve your forums and IBM Support experience.

Android Speech To Text Tutorial

Android comes with an inbuilt feature speech to text through which you can provide speech input to your app. With this you can add some of the cool features to your app like adding voice navigation(Helpful when you are targeting disabled people), filling a form with voice input etc.,

In the background how voice input works is, the speech input will be streamed to a server, on the server voice will be converted to text and finally text will be sent back to our app.

If you want to do the other way i.e converting text to speech, follow my previous tutorial Android Text to Speech

I have created a simple app to demonstrate this tutorial. Below is the screenshot of the app which contains a simple button to invoke speech input and a TextView to display the converted speech text.

So let’s start by creating simple app.

Sample Application

1. Create a new project in Eclipse by going to File ⇒ New ⇒ Android Application Project and give required information.

2. Open strings.xml located under res ⇒ values and add below string values.

3. Open colors.xml located under res ⇒ values and add below colors. If you don’t see colors.xml, create a new file and add the values.

4. Now open the layout file for main activity(activity_main.xml) and add below code to create a simple layout.

5. Finally open your MainActivity.java and do the following changes. In simple adding speech input will be done in two steps.

Step 1: Starting RecognizerIntent
First we need to create a RecognizerIntent by setting necessary flags such as
ACTION_RECOGNIZE_SPEECH – Simply takes user’s speech input and returns it to same activity
LANGUAGE_MODEL_FREE_FORM – Considers input in free form English
EXTRA_PROMPT – Text prompt to show to the user when asking them to speak

Step 2: Receiving the speech response
Once the speech input is done we have to catch the response in onActivityResult and take appropriate action needed.

Run the app in a real device. Make sure that the device has good internet connectivity while you are testing.

Enabling Offline Mode

Right now all the devices are not supporting offline speech input. However you can follow this discussion to enable offline speech input for supported devices.

I have downloaded speech input packages on my Nexus 5 and offline speech is working fine.

1. On your device go to Settings -> Language and Input. Click on icon on Google voice input.
2. Under ALL tab select the language you want to download.
3. Once the language package downloaded, you can see it under INSTALLED tab.

Ссылка на основную публикацию
Adblock
detector