Summary of the simple implementation of Java to read TXT document with voice

Time:2020-1-29

Recently, I was bored. I flipped through my blog and saw some people reading text content with VBS, that is, reading a few sentences of Chinese. Emmm is very interesting and easy to implement. I don’t need to install any environment. I just create a new TXT file, input some simple VBS code to read the text, then change the suffix of the new file to. VBS, and then double-click it to have an effect.. .

So I thought, is java OK? After checking some information, it’s really OK. I’ll tell you about the process of my experiment, which will be regarded as entertainment!

1. dependence

Create a maven project and import dependencies


<dependency>
  <groupId>com.hynnet</groupId>
  <artifactId>jacob</artifactId>
  <version>1.18</version>
</dependency>

It’s not enough to import only dependency, but also import a.dll file. Baidu cloud link: link: https://pan.baidu.com/s/1yyypiopxrtuykebjzabhlw extraction code: s62o, you can see that there are two DLL files. Since my computer is 64 bit, I copy the above DLL file to the bin directory where JDK is currently used

2. Java code implementation

A very simple java code implementation, after running will read out;

package com.wyq.day66;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class Speak02 {
 
 //Reading string STR with computer's own voice
 public static void main(String[] args) {
   String STR = "Hello, I'm a Java rookie! Please call me the most handsome pot ";
   
   ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
   Dispatch sapo = sap.getObject();
   try {
    //Volume 0-100
    sap.setProperty("Volume", new Variant(100));
    //Voice reading speed - 10 to + 10
    sap.setProperty("Rate", new Variant(0));
    //Read aloud 
    Dispatch.call(sapo, "Speak", new Variant(str));
    
   } catch (Exception e) {
    e.printStackTrace();
   } finally {
    sapo.safeRelease();
    sap.safeRelease();
   }
   
 }

}

3. Output audio file

Normally speaking, the above functions have been realized, but I still wonder if I can output the read audio file? The code is as follows:

package com.wyq.day66;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class JavaSpeak {

 public static void main(String[] args) {
  //Specify file audio output file location
  String output = "E:\test.wav";
  
  ActiveXComponent ax = null;
  String STR = "I'm a Java novice, I want to output the audio of this passage";
  try {
   ax = new ActiveXComponent("Sapi.SpVoice");

   //Output voice content at runtime
   Dispatch spVoice = ax.getObject();
   //Volume 0-100
   ax.setProperty("Volume", new Variant(100));
   //Voice reading speed - 10 to + 10
   ax.setProperty("Rate", new Variant(-3));
   //Read aloud
   Dispatch.call(spVoice, "Speak", new Variant(str));

   //Here is how to build a file stream to generate a voice file

   ax = new ActiveXComponent("Sapi.SpFileStream");
   Dispatch spFileStream = ax.getObject();

   ax = new ActiveXComponent("Sapi.SpAudioFormat");
   Dispatch spAudioFormat = ax.getObject();

   //Format audio stream
   Dispatch.put(spAudioFormat, "Type", new Variant(22));
   //Format file output stream
   Dispatch.putRef(spFileStream, "Format", spAudioFormat);
   //Call the output file stream open method to output a. Wav file at the specified location
   Dispatch.call(spFileStream, "Open", new Variant(output), new Variant(3), new Variant(true));
   //Set audio output stream of sound object as output file object
   Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);
   //Set volume 0 to 100
   Dispatch.put(spVoice, "Volume", new Variant(100));
   //Set reading speed
   Dispatch.put(spVoice, "Rate", new Variant(-2));
   //Start reading
   Dispatch.call(spVoice, "Speak", new Variant(str));

   //Close output file
   Dispatch.call(spFileStream, "Close");
   Dispatch.putRef(spVoice, "AudioOutputStream", null);

   spAudioFormat.safeRelease();
   spFileStream.safeRelease();
   spVoice.safeRelease();
   ax.safeRelease();

   } catch (Exception e) {
    e.printStackTrace();
   }
 
 }

}

Running directly, we can hear the sound of reading aloud, and audio files can be found in the specified directory;

4. Call Baidu AI to read the text

It should be more or less, but I always feel that the voice library of the computer doesn’t sound good. I want to use the lovely voice of Baidu AI. I still went to check the data, but it’s OK, and it’s easy!

4.1. Apply for Baidu voice API permission

Because we are going to call Baidu’s API for speech recognition, we need to apply for permission first, or we will always report an error (this place has been stuck for a long time, and finally I find out why the error was reported…) , link: http://ai.baidu.com/

Then I will let you log in. Just log in with QQ;

After creation, check the following application details:

4.2. Code implementation

So much is done to get these three strings. Now we need to import the dependency of Baidu voice:

<! -- Baidu voice broadcast SDK -- >
 <dependency>
  <groupId>com.baidu.aip</groupId>
  <artifactId>java-sdk</artifactId>
  <version>4.4.1</version>
 </dependency>
 
 <!-- https://mvnrepository.com/artifact/org.json/json -->
 <dependency>
  <groupId>org.json</groupId>
  <artifactId>json</artifactId>
  <version>20160810</version>
 </dependency>

Contents of notepad on the desktop:

The Java code is implemented as follows: in fact, baidu AI is used to read a TXT file in our computer, output MP3 file and save it to a specified location

package com.wyq.day66;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

import org.json.JSONObject;

import com.baidu.aip.speech.AipSpeech;
import com.baidu.aip.speech.TtsResponse;
import com.baidu.aip.util.Util;

public class Speak03 {
 //To set appid / AK / SK, we need to apply for the three parameters on Baidu AI platform (that is, the three strings mentioned above)
 public static final String APP_ID = "16447127";
 public static final String API_KEY = "8GO31sOIffR1oll5mPFKgtR9";
 public static final String SECRET_KEY = "jWsoNGlfzfRGSQ30****NOxz9ZpjMbc";
 
 //Readfile is our TXT document, WriteFile is the output MP3 format
 public static String readFile = "C:\Users\asus\Desktop\says.txt";
 public static String writeFile = "E:\output.mp3";


 public static void main(String[] args) {
  //It's OK to enter a string directly. It's better to use a TXT document if you have more content
  //Convertmp3 ("Hello! I'm Baidu AI intelligence, Java newcomer, glad to meet you, we will be very good friends. ";
  
  
  //Call the readtostring method to read the data in a TXT document into a string
  String string = readToString(readFile);
  //Use Baidu AI to read this string and output MP3 format
  convertMP3(string);

 }
  public static void convertMP3(String str) {
   AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
   //Optional: set network connection parameters, that is, timeout
   client.setConnectionTimeoutInMillis(2000);
   client.setSocketTimeoutInMillis(60000);

   //Set some optional parameters
   HashMap<String, Object> options = new HashMap<String, Object>();
   Options. Put ("SPD", "5"); // speech speed, 0-9 value, default to 5, not required
   Options. Put ("pit", "5"); // tone, value 0-9, default to 5, not required
   Options. Put ("per", "4"); // the pronunciation is selected by the speaker. 0 is female voice, 1 is male voice, 3 is emotional synthesis - duxiaoyao, 4 is emotional synthesis - duyaya. It is not required for ordinary female by default
   
   //Baidu AI starts to read the str string
   TtsResponse res = client.synthesis(str, "zh", 1, options);
   
   //The content returned by the server. It is null when the composition is successful, and contains information such as error no when it fails
   JSONObject result = res.getResult(); 
   if (result != null) {
    System.out.printf("error:" + result.toString()+"----------");
    return;
   }
   //Generated audio data
   byte[] data = res.getData();   
   JSONObject res1 = res.getResult();
   if (data != null) {
    try {
     //Output the generated audio to the specified location
     Util.writeBytesToFileSystem(data, writeFile);
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
   
   if (res1 != null) {
    System.out.println(res1.toString());
   }
  }
  
  //This method is to read the contents of the file and return a long string according to the input file path. Since TXT is GBK encoded, we also need to use GBK when we change it into a string
  //In fact, it is the most basic flow operation
  public static String readToString(String fileName) { 
   String encoding = "gbk"; 
   File file = new File(fileName); 
   Long filelength = file.length(); 
   byte[] filecontent = new byte[filelength.intValue()]; 
   
   try { 
    FileInputStream in = new FileInputStream(file); 
    in.read(filecontent); 
    in.close(); 
   } catch (FileNotFoundException e) { 
    e.printStackTrace(); 
   } catch (IOException e) { 
    e.printStackTrace(); 
   } 
   
   try { 
    return new String(filecontent, encoding); 
   } catch (UnsupportedEncodingException e) { 
    System.err.println("The OS does not support " + encoding); 
    e.printStackTrace(); 
    return null; 
   } 
  }
 

}

Output audio file:

5. summary

It’s a little interesting. When you’re free, you can play with java to kill time! Always look at some framework principles. It’s boring after a long time. It’s good to dig other Java functions!