Browse Source

声音控制音量

TitanWong 1 year ago
parent
commit
bdf86a8427

+ 5 - 1
pom.xml

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

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

@@ -1,5 +1,6 @@
 package com.zhili.stationcontrol.tts.component;
 package com.zhili.stationcontrol.tts.component;
 
 
+import com.alibaba.fastjson.JSON;
 import com.zhili.stationcontrol.tts.util.WavPlayUtil;
 import com.zhili.stationcontrol.tts.util.WavPlayUtil;
 import com.sun.jna.Library;
 import com.sun.jna.Library;
 import com.sun.jna.Native;
 import com.sun.jna.Native;
@@ -15,6 +16,7 @@ import org.springframework.stereotype.Component;
 import javax.annotation.PostConstruct;
 import javax.annotation.PostConstruct;
 import java.io.IOException;
 import java.io.IOException;
 import java.io.RandomAccessFile;
 import java.io.RandomAccessFile;
+import java.util.Map;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeUnit;
 
 
 /**
 /**
@@ -90,12 +92,14 @@ public class XunFeiTextToSpeech implements DisposableBean, Runnable {
             RandomAccessFile raf = null;
             RandomAccessFile raf = null;
             String sessionId = null;
             String sessionId = null;
             try {
             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;
                     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();
                 IntByReference errCode = new IntByReference();
                 sessionId = MscLibrary.INSTANCE.QTTSSessionBegin(sessionBeginParams, errCode);
                 sessionId = MscLibrary.INSTANCE.QTTSSessionBegin(sessionBeginParams, errCode);
                 if (errCode.getValue() != 0) {
                 if (errCode.getValue() != 0) {
@@ -136,10 +140,10 @@ public class XunFeiTextToSpeech implements DisposableBean, Runnable {
                 raf.seek(0);
                 raf.seek(0);
                 //写入真实头格式
                 //写入真实头格式
                 raf.write(getWavHeader(dataSize, 16000, 32000, 1, 16));
                 raf.write(getWavHeader(dataSize, 16000, 32000, 1, 16));
-                if(playWay.equals("java")){
+                if (playWay.equals("java")) {
                     WavPlayUtil.play(filename);
                     WavPlayUtil.play(filename);
-                }else if(playWay.equals("sox")){
-                    WavPlayUtil.play1(filename);
+                } else if (playWay.equals("sox")) {
+                    WavPlayUtil.play2(filename, vol);
                 }
                 }
             } catch (Exception e) {
             } catch (Exception e) {
                 e.printStackTrace();
                 e.printStackTrace();

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

@@ -1,5 +1,6 @@
 package com.zhili.stationcontrol.tts.controller;
 package com.zhili.stationcontrol.tts.controller;
 
 
+import com.alibaba.fastjson.JSON;
 import com.zhili.stationcontrol.tts.service.MusicService;
 import com.zhili.stationcontrol.tts.service.MusicService;
 import com.zhili.stationcontrol.tts.util.WavPlayUtil;
 import com.zhili.stationcontrol.tts.util.WavPlayUtil;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
@@ -11,6 +12,8 @@ import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.bind.annotation.RestController;
 
 
 import java.io.IOException;
 import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
 
 
 /**
 /**
  * @author :HuangBin
  * @author :HuangBin
@@ -31,7 +34,19 @@ public class TtsController {
 
 
     @GetMapping("/speak/{txt}")
     @GetMapping("/speak/{txt}")
     public String speak(@PathVariable String 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;
         return txt;
     }
     }
 
 

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

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

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

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