Forráskód Böngészése

声音控制音量

TitanWong 1 éve
szülő
commit
bdf86a8427

+ 5 - 1
pom.xml

@@ -40,7 +40,11 @@
             <artifactId>jna</artifactId>
             <version>5.12.1</version>
         </dependency>
-
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+            <version>1.2.79</version>
+        </dependency>
     </dependencies>
     <build>
         <plugins>

+ 11 - 7
src/main/java/com/zhili/stationcontrol/tts/component/XunFeiTextToSpeech.java

@@ -1,5 +1,6 @@
 package com.zhili.stationcontrol.tts.component;
 
+import com.alibaba.fastjson.JSON;
 import com.zhili.stationcontrol.tts.util.WavPlayUtil;
 import com.sun.jna.Library;
 import com.sun.jna.Native;
@@ -15,6 +16,7 @@ import org.springframework.stereotype.Component;
 import javax.annotation.PostConstruct;
 import java.io.IOException;
 import java.io.RandomAccessFile;
+import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
 /**
@@ -90,12 +92,14 @@ public class XunFeiTextToSpeech implements DisposableBean, Runnable {
             RandomAccessFile raf = null;
             String sessionId = null;
             try {
-                Object txtObj = redisTemplate.opsForList().leftPop("ttsList", 500l, TimeUnit.MILLISECONDS);
-                if (txtObj == null) {
+                Object voiceObj = redisTemplate.opsForList().leftPop("ttsList", 500l, TimeUnit.MILLISECONDS);
+                if (voiceObj == null) {
                     continue;
                 }
-                String text = txtObj.toString();
-                log.info("speak:{}", text);
+                Map<String, Object> voice = JSON.parseObject(voiceObj.toString(), Map.class);
+                String text = voice.get("txt").toString();
+                Integer vol = Integer.valueOf(voice.get("vol").toString());
+                log.info("speak:{},vol:{}", text, vol);
                 IntByReference errCode = new IntByReference();
                 sessionId = MscLibrary.INSTANCE.QTTSSessionBegin(sessionBeginParams, errCode);
                 if (errCode.getValue() != 0) {
@@ -136,10 +140,10 @@ public class XunFeiTextToSpeech implements DisposableBean, Runnable {
                 raf.seek(0);
                 //写入真实头格式
                 raf.write(getWavHeader(dataSize, 16000, 32000, 1, 16));
-                if(playWay.equals("java")){
+                if (playWay.equals("java")) {
                     WavPlayUtil.play(filename);
-                }else if(playWay.equals("sox")){
-                    WavPlayUtil.play1(filename);
+                } else if (playWay.equals("sox")) {
+                    WavPlayUtil.play2(filename, vol);
                 }
             } catch (Exception e) {
                 e.printStackTrace();

+ 16 - 1
src/main/java/com/zhili/stationcontrol/tts/controller/TtsController.java

@@ -1,5 +1,6 @@
 package com.zhili.stationcontrol.tts.controller;
 
+import com.alibaba.fastjson.JSON;
 import com.zhili.stationcontrol.tts.service.MusicService;
 import com.zhili.stationcontrol.tts.util.WavPlayUtil;
 import lombok.extern.slf4j.Slf4j;
@@ -11,6 +12,8 @@ import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RestController;
 
 import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * @author :HuangBin
@@ -31,7 +34,19 @@ public class TtsController {
 
     @GetMapping("/speak/{txt}")
     public String speak(@PathVariable String txt) {
-        redisTemplate.opsForList().rightPush("ttsList", txt);
+//        redisTemplate.opsForList().rightPush("ttsList", txt);
+        speak(txt,100);
+        return txt;
+    }
+
+    @GetMapping("/speak1/{txt}/{vol}")
+    public String speak(@PathVariable String txt,@PathVariable Integer vol) {
+        vol = vol > 1000 ? 1000 : vol;
+        vol = vol < 0 ? 0 : vol;
+        Map<String,Object> m = new HashMap();
+        m.put("txt",txt);
+        m.put("vol",vol);
+        redisTemplate.opsForList().rightPush("ttsList", JSON.toJSONString(m));
         return txt;
     }
 

+ 21 - 5
src/main/java/com/zhili/stationcontrol/tts/util/WavPlayUtil.java

@@ -1,5 +1,7 @@
 package com.zhili.stationcontrol.tts.util;
 
+import lombok.extern.slf4j.Slf4j;
+
 import javax.swing.*;
 import java.applet.Applet;
 import java.applet.AudioClip;
@@ -11,6 +13,7 @@ import java.net.URL;
  * @description:TODO
  * @date :2023/5/18 16:30
  */
+@Slf4j
 public class WavPlayUtil extends JFrame {
 
     public static void play(String path) {
@@ -21,18 +24,31 @@ public class WavPlayUtil extends JFrame {
             AudioClip aau;
             aau = Applet.newAudioClip(cb);//加载音频
             aau.play(); //播放音频
-            System.out.println(path+",播放完成");
+            System.out.println(path + ",播放完成");
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
 
-    public static void play1(String path){
-        try{
+    public static void play1(String path) {
+        try {
             Process exec = Runtime.getRuntime().exec("play " + path);
             exec.waitFor();
-            System.out.println(path+",播放完成");
-        }catch (Exception e){
+            System.out.println(path + ",播放完成");
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    public static void play2(String path, Integer vol) {
+//        "play -v " + Float.valueOf(new Integer(vol).toString()) / 100 + " " + path
+        Float v = Float.valueOf(new Integer(vol).toString()) / 100;
+        log.info("real v:{}", v);
+        try {
+            Process exec = Runtime.getRuntime().exec("play -v " + v.toString() + " " + path);
+            exec.waitFor();
+            System.out.println(path + ",播放完成");
+        } catch (Exception e) {
             e.printStackTrace();
         }
     }

+ 1 - 0
src/main/resources/application.yml

@@ -5,6 +5,7 @@ spring:
   redis:
     host: 127.0.0.1
     port: 6379
+#    password: 123456
 
 xunfei_tts:
   login_params: "appid = 9f2ce539, work_dir = ."