Browse Source

feat:小节评分

Joburgess 4 năm trước cách đây
mục cha
commit
e9b95b289b

+ 0 - 87
mec-biz/src/main/java/com/ym/mec/biz/dal/dto/AudioRecordConfig.java

@@ -1,87 +0,0 @@
-package com.ym.mec.biz.dal.dto;
-
-/**
- * 录音参数配置
- *
- * @author maple
- * @time 2018/4/10.
- */
-public class AudioRecordConfig {
-    /**
-     * 音频源
-     */
-    private int audioSource = 1;
-
-    /**
-     * 采样率 赫兹
-     * 44100Hz 所有设备均可用
-     * 22050Hz  16000Hz  11025Hz
-     */
-    private int sampleRateInHz = 44100;
-
-    private int channelConfig = 0x10;
-
-    /**
-     * 音频数据格式
-     */
-    private int audioFormat = 2;
-
-
-    public AudioRecordConfig() {
-    }
-
-    public AudioRecordConfig(int audioSource, int sampleRateInHz, int channelConfig, int audioFormat) {
-        this.audioSource = audioSource;
-        this.sampleRateInHz = sampleRateInHz;
-        this.channelConfig = channelConfig;
-        this.audioFormat = audioFormat;
-    }
-
-    public byte bitsPerSample() {
-        return 16;
-    }
-
-    // -------------------------- get/set ----------------------------------
-
-    public int getChannelConfig() {
-        return channelConfig;
-    }
-
-    public void setChannelConfig(int channelConfig) {
-        this.channelConfig = channelConfig;
-    }
-
-    public int getAudioSource() {
-        return audioSource;
-    }
-
-    public void setAudioSource(int audioSource) {
-        this.audioSource = audioSource;
-    }
-
-    public int getSampleRateInHz() {
-        return sampleRateInHz;
-    }
-
-    public void setSampleRateInHz(int sampleRateInHz) {
-        this.sampleRateInHz = sampleRateInHz;
-    }
-
-    public int getAudioFormat() {
-        return audioFormat;
-    }
-
-    public void setAudioFormat(int audioFormat) {
-        this.audioFormat = audioFormat;
-    }
-
-    @Override
-    public String toString() {
-        return "录音参数配置: \n{" +
-                "audioSource=" + audioSource +
-                ", sampleRateInHz=" + sampleRateInHz +
-                ", channelConfig=" + channelConfig +
-                ", audioFormat=" + audioFormat +
-                '}';
-    }
-}

+ 6 - 16
mec-biz/src/main/java/com/ym/mec/biz/dal/dto/WavHeader.java

@@ -7,28 +7,18 @@ package com.ym.mec.biz.dal.dto;
  * @time 2018/4/10.
  */
 public class WavHeader {
-    private AudioRecordConfig config;// wav录音配置参数
-    private long totalAudioLength;// 音频数据总长度
-
-    public WavHeader(AudioRecordConfig config, long totalAudioLength) {
-        this.config = config;
-        this.totalAudioLength = totalAudioLength;
-    }
 
     /**
      * 返回WAV文件头的byte数组
      */
-    public byte[] toBytes() {
-        long sampleRateInHz = config.getSampleRateInHz();
-        int channels = 1;
-        byte bitsPerSample = config.bitsPerSample();
+    public static byte[] getWaveHeader(long totalAudioLength, long frameRate, long frameRateSize) {
         return wavFileHeader(
                 totalAudioLength - 44,
                 totalAudioLength - 44 + 36,
-                sampleRateInHz,
-                channels,
-                bitsPerSample * sampleRateInHz * channels / 8,
-                bitsPerSample
+                frameRate,
+                1,
+                frameRateSize * frameRate * 1 / 8,
+                (byte) frameRateSize
         );
     }
 
@@ -43,7 +33,7 @@ public class WavHeader {
      * @param bitsPerSample  - 16/8 bit
      * @return
      */
-    private byte[] wavFileHeader(long totalAudioLen, long totalDataLen, long longSampleRate,
+    private static byte[] wavFileHeader(long totalAudioLen, long totalDataLen, long longSampleRate,
                                  int channels, long byteRate, byte bitsPerSample) {
         byte[] header = new byte[44];
         header[0] = 'R'; // RIFF/WAVE header

+ 5 - 9
mec-teacher/src/main/java/com/ym/mec/teacher/handler/WebSocketHandler.java

@@ -2,11 +2,9 @@ package com.ym.mec.teacher.handler;
 
 import be.tarsos.dsp.AudioDispatcher;
 import be.tarsos.dsp.io.jvm.AudioDispatcherFactory;
-import be.tarsos.dsp.io.jvm.JVMAudioInputStream;
 import be.tarsos.dsp.pitch.PitchProcessor;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
-import com.ym.mec.biz.dal.dto.AudioRecordConfig;
 import com.ym.mec.biz.dal.dto.MusicPitchDetailDto;
 import com.ym.mec.biz.dal.dto.WavHeader;
 import com.ym.mec.biz.dal.dto.WebSocketInfo;
@@ -19,15 +17,11 @@ import org.springframework.web.socket.*;
 import org.springframework.web.socket.handler.AbstractWebSocketHandler;
 
 import javax.sound.sampled.AudioFormat;
-import javax.sound.sampled.AudioInputStream;
-import javax.sound.sampled.DataLine;
-import javax.sound.sampled.TargetDataLine;
 import java.io.*;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.atomic.AtomicReference;
 
 /**
  * @Author Joburgess
@@ -104,11 +98,11 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
         dispatcher.addAudioProcessor(new PitchProcessor(algo, 44100, 256, (pitchDetectionResult, audioEvent) -> {
             double timeStamp = audioEvent.getTimeStamp();
             float pitch = pitchDetectionResult.getPitch();
-            LOGGER.info("时间:{},频率:{}", timeStamp, pitch);
+//            LOGGER.info("时间:{},频率:{}", timeStamp, pitch);
         }));
         dispatcher.run();
         double recordTime = userRandomAccessFileMap.get(phone).length()/(audioFormat.getFrameSize()*audioFormat.getFrameRate())*1000;
-        LOGGER.info("时长:{}", recordTime);
+//        LOGGER.info("时长:{}", recordTime);
         for (MusicPitchDetailDto musicPitchDetailDto : userXmlInfoMap.get(phone)) {
             if(!userMeasureMap.get(phone).contains(musicPitchDetailDto.getMeasureIndex())&&recordTime>musicPitchDetailDto.getTimeStamp()+musicPitchDetailDto.getDuration()){
                 WebSocketInfo webSocketInfo = new WebSocketInfo();
@@ -125,6 +119,7 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
                 webSocketInfo.setBody(result);
                 WS_CLIENTS.get(phone).sendMessage(new TextMessage(JSON.toJSONString(webSocketInfo)));
                 userMeasureMap.get(phone).add(musicPitchDetailDto.getMeasureIndex());
+                LOGGER.info("推送评分结果:{}", phone);
             }
         }
     }
@@ -165,8 +160,9 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
             return;
         }
         RandomAccessFile randomAccessFile = userRandomAccessFileMap.get(phone);
+        LOGGER.info("音频时长:{}", randomAccessFile.length()/(audioFormat.getFrameSize()*audioFormat.getFrameRate())*1000);
         randomAccessFile.seek(0);
-        randomAccessFile.write(new WavHeader(new AudioRecordConfig(), randomAccessFile.length()).toBytes());
+        randomAccessFile.write(WavHeader.getWaveHeader(randomAccessFile.length(), (long) audioFormat.getFrameRate(), audioFormat.getSampleSizeInBits()));
         randomAccessFile.close();
         userRandomAccessFileMap.remove(phone);
     }