Przeglądaj źródła

Merge branch 'online1' of http://git.dayaedu.com/yonge/mec into hr_728

yonge 4 lat temu
rodzic
commit
cb3c927206

+ 11 - 3
mec-biz/src/main/java/com/ym/mec/biz/dal/config/NioAudioInputStream.java

@@ -2,7 +2,9 @@ package com.ym.mec.biz.dal.config;
 
 import be.tarsos.dsp.io.TarsosDSPAudioFormat;
 import be.tarsos.dsp.io.TarsosDSPAudioInputStream;
+import org.springframework.stereotype.Component;
 
+import javax.sound.sampled.AudioFormat;
 import java.io.IOException;
 import java.io.RandomAccessFile;
 
@@ -12,10 +14,15 @@ import java.io.RandomAccessFile;
  */
 public class NioAudioInputStream implements TarsosDSPAudioInputStream {
 
-    private final RandomAccessFile randomAccessFile;
+    private RandomAccessFile randomAccessFile;
+    private AudioFormat format;
 
-    public NioAudioInputStream(RandomAccessFile randomAccessFile) {
+    public NioAudioInputStream() {
+    }
+
+    public NioAudioInputStream(RandomAccessFile randomAccessFile, AudioFormat audioFormat) {
         this.randomAccessFile = randomAccessFile;
+        this.format = audioFormat;
     }
 
     @Override
@@ -35,7 +42,8 @@ public class NioAudioInputStream implements TarsosDSPAudioInputStream {
 
     @Override
     public TarsosDSPAudioFormat getFormat() {
-        TarsosDSPAudioFormat tarsosDSPFormat = new TarsosDSPAudioFormat(44100, 16, 1, false, true);
+        boolean isSigned = format.getEncoding() == AudioFormat.Encoding.PCM_SIGNED;
+        TarsosDSPAudioFormat tarsosDSPFormat = new TarsosDSPAudioFormat(format.getSampleRate(), format.getSampleSizeInBits(), format.getChannels(), isSigned, format.isBigEndian());
         return tarsosDSPFormat;
     }
 

+ 0 - 1
mec-biz/src/main/java/com/ym/mec/biz/dal/config/SoundCompareConfig.java

@@ -1,6 +1,5 @@
 package com.ym.mec.biz.dal.config;
 
-import be.tarsos.dsp.SilenceDetector;
 import be.tarsos.dsp.pitch.PitchProcessor;
 
 import javax.sound.sampled.AudioFormat;

+ 2 - 2
mec-biz/src/main/java/com/ym/mec/biz/dal/dao/StudentExtracurricularExercisesSituationDao.java

@@ -108,10 +108,10 @@ public interface StudentExtracurricularExercisesSituationDao extends BaseDAO<Lon
      * @describe 统计指定星期的服务记录数量
      * @author Joburgess
      * @date 2020/12/25 0025
-     * @param sunday:
+     * @param monday:
      * @return int
      */
-    int countWeekServiceNum(@Param("sunday") String sunday);
+    int countWeekServiceNum(@Param("monday") String monday);
 
     /**
      * @describe 获取指定周指定学员的服务指标

+ 57 - 7
mec-biz/src/main/java/com/ym/mec/biz/dal/dto/SoundCompareHelper.java

@@ -1,21 +1,36 @@
 package com.ym.mec.biz.dal.dto;
 
+import be.tarsos.dsp.AudioEvent;
 import be.tarsos.dsp.SilenceDetector;
+import be.tarsos.dsp.pitch.PitchDetectionHandler;
+import be.tarsos.dsp.pitch.PitchDetectionResult;
+import com.ym.mec.biz.dal.enums.DeviceTypeEnum;
+import com.ym.mec.biz.service.impl.SoundCompareHandler;
 import io.swagger.annotations.ApiModelProperty;
+import org.springframework.util.CollectionUtils;
 
 import java.io.RandomAccessFile;
 import java.math.BigDecimal;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 /**
  * @Author Joburgess
  * @Date 2021/6/22 0022
  */
-public class SoundCompareHelper {
+public class SoundCompareHelper implements PitchDetectionHandler {
+
+    @ApiModelProperty(value = "设备类型")
+    private DeviceTypeEnum deviceType;
 
     @ApiModelProperty(value = "伴奏编号")
     private Integer musicScoreId;
 
+    @ApiModelProperty(value = "声部编号")
+    private Integer subjectId;
+
     @ApiModelProperty(value = "小节开始时间")
     private double measureStartTime = 0;
 
@@ -47,7 +62,7 @@ public class SoundCompareHelper {
 
     private List<MusicPitchDetailDto> musicXmlInfos;
 
-    private byte[] preDataArray = new byte[0];
+    private String clientId;
     /**
      * @describe 分贝检测器
      */
@@ -69,12 +84,20 @@ public class SoundCompareHelper {
         this.musicalNotePitchMap = musicalNotePitchMap;
     }
 
-    public byte[] getPreDataArray() {
-        return preDataArray;
+    public Integer getSubjectId() {
+        return subjectId;
+    }
+
+    public void setSubjectId(Integer subjectId) {
+        this.subjectId = subjectId;
+    }
+
+    public DeviceTypeEnum getDeviceType() {
+        return deviceType;
     }
 
-    public void setPreDataArray(byte[] preDataArray) {
-        this.preDataArray = preDataArray;
+    public void setDeviceType(DeviceTypeEnum deviceType) {
+        this.deviceType = deviceType;
     }
 
     public int getOffsetTime() {
@@ -109,6 +132,14 @@ public class SoundCompareHelper {
         this.accessFile = accessFile;
     }
 
+    public String getClientId() {
+        return clientId;
+    }
+
+    public void setClientId(String clientId) {
+        this.clientId = clientId;
+    }
+
     public double getMeasureStartTime() {
         return measureStartTime;
     }
@@ -156,4 +187,23 @@ public class SoundCompareHelper {
     public void setUserMeasureScoreMap(Map<Integer, Map<String, Object>> userMeasureScoreMap) {
         this.userMeasureScoreMap = userMeasureScoreMap;
     }
+
+    @Override
+    public void handlePitch(PitchDetectionResult pitchDetectionResult, AudioEvent audioEvent) {
+        int timeStamp = (int) (measureStartTime + audioEvent.getTimeStamp()*1000);
+        float pitch = pitchDetectionResult.getPitch();
+        if(offsetTime == -1 && !DeviceTypeEnum.IOS.equals(deviceType) && pitch>0){
+            int preTimeStamp = CollectionUtils.isEmpty(recordMeasurePithInfo)?0:recordMeasurePithInfo.get(recordMeasurePithInfo.size()-1).getTimeStamp();
+            offsetTime = timeStamp - (timeStamp - preTimeStamp)/2;
+            for (Map.Entry<Integer, MusicPitchDetailDto> musicPitchDetailDtoEntry : measureEndTime.entrySet()) {
+                musicPitchDetailDtoEntry.getValue().setTimeStamp(musicPitchDetailDtoEntry.getValue().getTimeStamp() + offsetTime);
+                musicPitchDetailDtoEntry.getValue().setEndTimeStamp(musicPitchDetailDtoEntry.getValue().getEndTimeStamp() + offsetTime);
+            }
+        }
+        if(silenceDetector.currentSPL()< SoundCompareHandler.soundCompareConfig.validDb){
+            pitch = -1;
+        }
+//            LOGGER.info("时间:{}, 频率:{}, 分贝:{}", timeStamp, pitch, silenceDetecor.currentSPL());
+        recordMeasurePithInfo.add(new MusicPitchDetailDto(timeStamp, pitch, silenceDetector.currentSPL()));
+    }
 }

+ 32 - 1
mec-biz/src/main/java/com/ym/mec/biz/dal/entity/SysMusicCompareRecord.java

@@ -1,5 +1,6 @@
 package com.ym.mec.biz.dal.entity;
 
+import com.ym.mec.biz.dal.enums.DeviceTypeEnum;
 import org.apache.commons.lang3.builder.ToStringBuilder;
 
 import java.math.BigDecimal;
@@ -38,6 +39,12 @@ public class SysMusicCompareRecord {
 	
 	/** 创建时间 */
 	private java.util.Date createTime;
+
+	private DeviceTypeEnum deviceType;
+
+	private float playTime = 0;
+
+	private String clientId;
 	
 	public void setId(Long id){
 		this.id = id;
@@ -54,7 +61,15 @@ public class SysMusicCompareRecord {
 	public Integer getUserId(){
 		return this.userId;
 	}
-			
+
+	public String getClientId() {
+		return clientId;
+	}
+
+	public void setClientId(String clientId) {
+		this.clientId = clientId;
+	}
+
 	public void setSysMusicScoreId(Integer sysMusicScoreId){
 		this.sysMusicScoreId = sysMusicScoreId;
 	}
@@ -111,6 +126,22 @@ public class SysMusicCompareRecord {
 		this.recordFilePath = recordFilePath;
 	}
 
+	public DeviceTypeEnum getDeviceType() {
+		return deviceType;
+	}
+
+	public void setDeviceType(DeviceTypeEnum deviceType) {
+		this.deviceType = deviceType;
+	}
+
+	public float getPlayTime() {
+		return playTime;
+	}
+
+	public void setPlayTime(float playTime) {
+		this.playTime = playTime;
+	}
+
 	public void setCreateTime(java.util.Date createTime){
 		this.createTime = createTime;
 	}

+ 35 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/enums/DeviceTypeEnum.java

@@ -0,0 +1,35 @@
+package com.ym.mec.biz.dal.enums;
+
+import com.ym.mec.common.enums.BaseEnum;
+
+public enum DeviceTypeEnum implements BaseEnum<String, DeviceTypeEnum> {
+	IOS("IOS", "苹果"),
+	ANDROID("ANDROID", "安卓"),
+	WEB("WEB", "浏览器");
+
+	private String code;
+
+	private String msg;
+
+	DeviceTypeEnum(String code, String msg) {
+		this.code = code;
+		this.msg = msg;
+	}
+
+	public void setCode(String code) {
+		this.code = code;
+	}
+
+	public String getMsg() {
+		return msg;
+	}
+
+	public void setMsg(String msg) {
+		this.msg = msg;
+	}
+
+	@Override
+	public String getCode() {
+		return this.code;
+	}
+}

+ 34 - 443
mec-biz/src/main/java/com/ym/mec/biz/handler/WebSocketHandler.java

@@ -1,34 +1,20 @@
 package com.ym.mec.biz.handler;
 
-import be.tarsos.dsp.AudioDispatcher;
-import be.tarsos.dsp.io.jvm.AudioDispatcherFactory;
-import be.tarsos.dsp.pitch.PitchProcessor;
-import be.tarsos.dsp.util.PitchConverter;
 import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONObject;
-import com.ym.mec.biz.dal.config.SoundCompareConfig;
-import com.ym.mec.biz.dal.dto.*;
+import com.ym.mec.biz.dal.dto.WebSocketClientDetail;
+import com.ym.mec.biz.dal.dto.WebSocketInfo;
 import com.ym.mec.biz.service.SoundSocketService;
-import com.ym.mec.biz.service.SysMusicCompareRecordService;
-import com.ym.mec.common.constant.CommonConstants;
-import org.apache.commons.io.FileUtils;
+import com.ym.mec.biz.service.WebSocketEventHandler;
+import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
-import org.springframework.util.CollectionUtils;
 import org.springframework.web.socket.*;
 import org.springframework.web.socket.handler.AbstractWebSocketHandler;
 
-import java.io.File;
-import java.io.IOException;
-import java.io.RandomAccessFile;
-import java.math.BigDecimal;
-import java.time.LocalDateTime;
-import java.time.format.DateTimeFormatter;
-import java.util.*;
+import java.util.Date;
+import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.stream.Collectors;
 
 /**
  * @Author Joburgess
@@ -37,35 +23,29 @@ import java.util.stream.Collectors;
 @Service
 public class WebSocketHandler extends AbstractWebSocketHandler {
 
-    private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketHandler.class);
+    private final Logger LOGGER = LoggerFactory.getLogger(WebSocketHandler.class);
 
     /**
      * @describe 存储客户端链接
      */
     public static final Map<String, WebSocketClientDetail> WS_CLIENTS = new ConcurrentHashMap<>();
 
-    private BigDecimal oneHundred = new BigDecimal(100);
+    private static Map<String, WebSocketEventHandler> appMap = new ConcurrentHashMap<>();
 
-    private final String tmpDir = FileUtils.getTempDirectoryPath() + "/soundCompare/";
-
-    /**
-     * @describe 用户对应评分信息
-     */
-    private Map<String, SoundCompareHelper> userSoundInfoMap = new ConcurrentHashMap<>();
     /**
-     * @describe 音频处理参数
+     * @describe 注册应用
+     * @author Joburgess
+     * @date 2021/8/5 0005
+     * @param tag:
+     * @param webSocketEventHandler:
+     * @return boolean
      */
-    private SoundCompareConfig soundCompareConfig = new SoundCompareConfig();
-
-    @Autowired
-    private SysMusicCompareRecordService sysMusicCompareRecordService;
-
-    public WebSocketHandler() {
-        super();
-        File soundDir = new File(tmpDir);
-        if(!soundDir.exists()){
-            soundDir.mkdir();
+    public static boolean regist(String tag, WebSocketEventHandler webSocketEventHandler){
+        if (appMap.containsKey(tag)){
+            return false;
         }
+        appMap.put(tag, webSocketEventHandler);
+        return true;
     }
 
     @Override
@@ -73,6 +53,7 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
         String phone = session.getPrincipal().getName().split(":")[1];
         LOGGER.info("{}上线", phone);
         WS_CLIENTS.put(phone, new WebSocketClientDetail(session, new Date()));
+        appMap.values().forEach(e->e.afterConnectionEstablished(session, phone));
         super.afterConnectionEstablished(session);
     }
 
@@ -82,116 +63,25 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
     }
 
     @Override
-    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
+    protected void handleTextMessage(WebSocketSession session, TextMessage message){
         String phone = session.getPrincipal().getName().split(":")[1];
         LOGGER.info("{}: {}", phone, message.getPayload());
         WebSocketInfo webSocketInfo = JSON.parseObject(message.getPayload(), WebSocketInfo.class);
-        JSONObject bodyObject = (JSONObject) webSocketInfo.getBody();
-
-        String commond = "";
-        if(webSocketInfo.getHeader().containsKey(SoundSocketService.COMMOND)){
-            commond = webSocketInfo.getHeader().get(SoundSocketService.COMMOND);
+        String tag = "";
+        if(webSocketInfo.getHeader().containsKey(SoundSocketService.TAG)){
+            tag = webSocketInfo.getHeader().get(SoundSocketService.TAG);
         }
-        switch (commond){
-            case SoundSocketService.MUSIC_XML:
-                userSoundInfoMap.put(phone, new SoundCompareHelper());
-                List<MusicPitchDetailDto> musicXmlInfos = JSON.parseArray(bodyObject.getString("musicXmlInfos"), MusicPitchDetailDto.class);
-                userSoundInfoMap.get(phone).setMusicXmlInfos(musicXmlInfos);
-                musicXmlInfos = musicXmlInfos.stream().filter(m->!m.getDontEvaluating()).collect(Collectors.toList());
-                userSoundInfoMap.get(phone).setMusicScoreId(bodyObject.getInteger("id"));
-                userSoundInfoMap.get(phone).setMeasureXmlInfoMap(musicXmlInfos.stream().collect(Collectors.groupingBy(MusicPitchDetailDto::getMeasureIndex)));
-                musicXmlInfos.forEach(e->userSoundInfoMap.get(phone).getMusicalNotePitchMap().put(e.getMusicalNotesIndex(), e.getFrequency()));
-                for (Map.Entry<Integer, List<MusicPitchDetailDto>> userMeasureXmlInfoEntry : userSoundInfoMap.get(phone).getMeasureXmlInfoMap().entrySet()) {
-                    MusicPitchDetailDto firstPitch = userMeasureXmlInfoEntry.getValue().stream().min(Comparator.comparing(MusicPitchDetailDto::getTimeStamp)).get();
-                    MusicPitchDetailDto lastPitch = userMeasureXmlInfoEntry.getValue().stream().max(Comparator.comparing(MusicPitchDetailDto::getTimeStamp)).get();
-                    long dc = userMeasureXmlInfoEntry.getValue().stream().filter(m -> m.getDontEvaluating()).count();
-                    MusicPitchDetailDto musicPitchDetailDto = new MusicPitchDetailDto(firstPitch.getTimeStamp(), lastPitch.getTimeStamp() + lastPitch.getDuration());
-                    musicPitchDetailDto.setDuration(musicPitchDetailDto.getEndTimeStamp()-musicPitchDetailDto.getTimeStamp());
-                    musicPitchDetailDto.setDontEvaluating(dc == userMeasureXmlInfoEntry.getValue().size());
-                    userSoundInfoMap.get(phone).getMeasureEndTime().put(userMeasureXmlInfoEntry.getKey(), musicPitchDetailDto);
-                }
-                break;
-            case SoundSocketService.RECORD_START:
-                if(!userSoundInfoMap.containsKey(phone)){
-                    break;
-                }
-                File file = new File(tmpDir+phone + "_"+ userSoundInfoMap.get(phone).getMusicScoreId() +"_"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) +".wav");
-                userSoundInfoMap.get(phone).setAccessFile(new RandomAccessFile(file, "rw"));
-                userSoundInfoMap.get(phone).setRecordFilePath(file.getAbsolutePath());
-                break;
-            case SoundSocketService.RECORD_END:
-                if(!userSoundInfoMap.containsKey(phone)){
-                    break;
-                }
-                if(!CollectionUtils.isEmpty(userSoundInfoMap.get(phone).getMeasureEndTime())){
-                    Integer lastMeasureIndex = userSoundInfoMap.get(phone).getMeasureEndTime().keySet().stream().min(Integer::compareTo).get();
-                    double recordTime = userSoundInfoMap.get(phone).getAccessFile().length()/(soundCompareConfig.audioFormat.getFrameSize()*soundCompareConfig.audioFormat.getFrameRate())*1000;
-                    //如果结束时时长大于某小节,则此小节需要评分
-                    if(recordTime>userSoundInfoMap.get(phone).getMeasureEndTime().get(lastMeasureIndex).getEndTimeStamp()){
-                        measureCompare(phone, lastMeasureIndex);
-                        userSoundInfoMap.get(phone).getMeasureEndTime().remove(lastMeasureIndex);
-                    }
-                }
-                calTotalScore(phone);
-                createHeader(phone);
-                break;
-            case SoundSocketService.RECORD_CANCEL:
-                createHeader(phone);
-                break;
-            case SoundSocketService.PROXY_MESSAGE:
-//                if(bodyObject.containsKey(SoundSocketService.OFFSET_TIME)){
-//                    int offsetTime = bodyObject.getIntValue(SoundSocketService.OFFSET_TIME);
-//                    calOffsetTime(phone, offsetTime);
-//                }
-                break;
-            default:
-                break;
+        if(StringUtils.isNotBlank(tag)){
+            appMap.get(tag).receiveTextMessage(session, phone, message);
+        }else{
+            appMap.values().forEach(e->e.receiveTextMessage(session, phone, message));
         }
     }
 
     @Override
-    protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
+    protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) {
         String phone = session.getPrincipal().getName().split(":")[1];
-        if(!userSoundInfoMap.containsKey(phone)){
-            return;
-        }
-        if(Objects.nonNull(userSoundInfoMap.get(phone).getAccessFile())){
-            userSoundInfoMap.get(phone).getAccessFile().write(message.getPayload().array());
-        }
-
-        AudioDispatcher dispatcher = AudioDispatcherFactory.fromByteArray(message.getPayload().array(), soundCompareConfig.audioFormat, soundCompareConfig.simpleSize, soundCompareConfig.overlap);
-        dispatcher.addAudioProcessor(userSoundInfoMap.get(phone).silenceDetector);
-        dispatcher.addAudioProcessor(new PitchProcessor(soundCompareConfig.algo, soundCompareConfig.simpleRate, soundCompareConfig.simpleSize, (pitchDetectionResult, audioEvent) -> {
-            int timeStamp = (int) (userSoundInfoMap.get(phone).getMeasureStartTime() + audioEvent.getTimeStamp()*1000);
-            float pitch = pitchDetectionResult.getPitch();
-            if(pitch>0 && userSoundInfoMap.get(phone).getOffsetTime() == -1){
-                int preTimeStamp = CollectionUtils.isEmpty(userSoundInfoMap.get(phone).getRecordMeasurePithInfo())?0:userSoundInfoMap.get(phone).getRecordMeasurePithInfo().get(userSoundInfoMap.get(phone).getRecordMeasurePithInfo().size()-1).getTimeStamp();
-                calOffsetTime(phone, timeStamp - (timeStamp - preTimeStamp)/2);
-            }
-            if(userSoundInfoMap.get(phone).silenceDetector.currentSPL()<soundCompareConfig.validDb){
-                pitch = -1;
-            }
-//            LOGGER.info("时间:{}, 频率:{}, 分贝:{}", timeStamp, pitch, silenceDetecor.currentSPL());
-            userSoundInfoMap.get(phone).getRecordMeasurePithInfo().add(new MusicPitchDetailDto(timeStamp, pitch, userSoundInfoMap.get(phone).silenceDetector.currentSPL()));
-        }));
-        dispatcher.run();
-        if(Objects.isNull(userSoundInfoMap.get(phone).getAccessFile())){
-            return;
-        }
-
-        double recordTime = userSoundInfoMap.get(phone).getAccessFile().length()/(soundCompareConfig.audioFormat.getFrameSize()*soundCompareConfig.audioFormat.getFrameRate())*1000;
-        userSoundInfoMap.get(phone).setMeasureStartTime(recordTime);
-        for (Map.Entry<Integer, MusicPitchDetailDto> userMeasureEndTimeMapEntry : userSoundInfoMap.get(phone).getMeasureEndTime().entrySet()) {
-            if(recordTime>(userMeasureEndTimeMapEntry.getValue().getEndTimeStamp())){
-                if(userMeasureEndTimeMapEntry.getValue().getDontEvaluating()){
-                    continue;
-                }else{
-                    measureCompare(phone, userMeasureEndTimeMapEntry.getKey());
-                }
-                userSoundInfoMap.get(phone).getMeasureEndTime().remove(userMeasureEndTimeMapEntry.getKey());
-                break;
-            }
-        }
+        appMap.values().forEach(e->e.receiveBinaryMessage(session, phone, message));
     }
 
     @Override
@@ -204,14 +94,13 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
     public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
         String phone = session.getPrincipal().getName().split(":")[1];
         session.close();
+        LOGGER.info("发生了错误,移除客户端: {}", phone);
+        appMap.values().forEach(e->e.afterConnectionClosed(session, phone));
+        exception.printStackTrace();
         if(!WS_CLIENTS.containsKey(phone)){
             return;
         }
-        exception.printStackTrace();
-        LOGGER.info("发生了错误,移除客户端: {}", phone);
         WS_CLIENTS.remove(phone);
-        userSoundInfoMap.remove(phone);
-        createHeader(phone);
     }
 
     @Override
@@ -219,310 +108,12 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
         super.afterConnectionClosed(session, status);
         String phone = session.getPrincipal().getName().split(":")[1];
         LOGGER.info("{}离线", phone);
-        createHeader(phone);
+        appMap.values().forEach(e->e.afterConnectionClosed(session, phone));
         WS_CLIENTS.remove(phone);
-        userSoundInfoMap.remove(phone);
     }
 
     @Override
     public boolean supportsPartialMessages() {
         return super.supportsPartialMessages();
     }
-
-    /**
-     * @describe 处理时间偏移
-     * @author Joburgess
-     * @date 2021/7/5 0005
-     * @param phone:
-     * @param offsetTime:
-     * @return void
-     */
-    private void calOffsetTime(String phone, int offsetTime){
-        userSoundInfoMap.get(phone).setOffsetTime(offsetTime);
-        for (Map.Entry<Integer, MusicPitchDetailDto> musicPitchDetailDtoEntry : userSoundInfoMap.get(phone).getMeasureEndTime().entrySet()) {
-            musicPitchDetailDtoEntry.getValue().setTimeStamp(musicPitchDetailDtoEntry.getValue().getTimeStamp() + offsetTime);
-            musicPitchDetailDtoEntry.getValue().setEndTimeStamp(musicPitchDetailDtoEntry.getValue().getEndTimeStamp() + offsetTime);
-        }
-    }
-
-    /**
-     * @describe 保存录音数据,并生成wav头信息
-     * @author Joburgess
-     * @date 2021/6/25 0025
-     * @param phone:
-     * @return void
-     */
-    private void createHeader(String phone) throws IOException {
-        if(!userSoundInfoMap.containsKey(phone)){
-            return;
-        }
-        if(Objects.nonNull(userSoundInfoMap.get(phone).getAccessFile())){
-            RandomAccessFile randomAccessFile = userSoundInfoMap.get(phone).getAccessFile();
-            LOGGER.info("音频时长:{}", randomAccessFile.length()/(soundCompareConfig.audioFormat.getFrameSize()*soundCompareConfig.audioFormat.getFrameRate())*1000);
-            randomAccessFile.seek(0);
-            randomAccessFile.write(WavHeader.getWaveHeader(randomAccessFile.length(), (long) soundCompareConfig.audioFormat.getFrameRate(), soundCompareConfig.audioFormat.getSampleSizeInBits()));
-            randomAccessFile.close();
-            userSoundInfoMap.get(phone).setAccessFile(null);
-        }
-//        userSoundInfoMap.get(phone).setRecordMeasurePithInfo(null);
-        userSoundInfoMap.remove(phone);
-    }
-
-    /**
-     * @describe 数据比对,生成分数
-     * @author Joburgess
-     * @date 2021/6/25 0025
-     * @param phone:
-     * @param measureIndex:
-     * @return void
-     */
-    private void measureCompare(String phone, int measureIndex) throws IOException {
-        if (userSoundInfoMap.get(phone).getOffsetTime() == -1){
-            userSoundInfoMap.get(phone).setOffsetTime(0);
-        }
-
-        //相似度
-        BigDecimal intonation = BigDecimal.ZERO;
-        //节奏
-        BigDecimal cadence = BigDecimal.ZERO;
-        //完整度
-        BigDecimal integrity = BigDecimal.ZERO;
-
-        try {
-            //音准分数
-            float intonationScore = 0;
-
-            //节奏匹配数量
-            float cadenceNum = 0;
-
-            //完整性分数
-            float integrityScore = 0;
-
-            int totalCompareNum = userSoundInfoMap.get(phone).getMeasureXmlInfoMap().get(measureIndex).size();
-
-            for (int i = 0; i < userSoundInfoMap.get(phone).getMeasureXmlInfoMap().get(measureIndex).size(); i++) {
-                MusicPitchDetailDto musicXmlInfo = userSoundInfoMap.get(phone).getMeasureXmlInfoMap().get(measureIndex).get(i);
-
-                int ot5 = (int) (musicXmlInfo.getDuration()*0.1);
-                int startTimeStamp = musicXmlInfo.getTimeStamp() + userSoundInfoMap.get(phone).getOffsetTime() + ot5;
-                int endTimeStamp = musicXmlInfo.getTimeStamp()  + userSoundInfoMap.get(phone).getOffsetTime() + musicXmlInfo.getDuration() - ot5;
-
-                //时间范围内有效节奏数量
-                float cadenceValidNum = 0;
-                //时间范围内有效音频数量
-                float integrityValidNum = 0;
-                //时间范围内匹配次数
-                float compareNum = 0;
-
-                List<MusicPitchDetailDto> measureSoundPitchInfos = new ArrayList<>();
-
-                for (int j = 0; j < userSoundInfoMap.get(phone).getRecordMeasurePithInfo().size(); j++) {
-                    MusicPitchDetailDto recordInfo = userSoundInfoMap.get(phone).getRecordMeasurePithInfo().get(j);
-                    //如果在时间范围之外直接跳过
-                    if(recordInfo.getTimeStamp()<startTimeStamp||recordInfo.getTimeStamp()>endTimeStamp){
-                        continue;
-                    }
-                    measureSoundPitchInfos.add(recordInfo);
-                    compareNum++;
-                    //如果在最低有效频率以下则跳过
-                    if(recordInfo.getFrequency()<soundCompareConfig.validFrequency&&musicXmlInfo.getFrequency()!=-1){
-                        continue;
-                    }
-                    cadenceValidNum++;
-                    //如果频率差值在节奏误差范围内
-                    if(Math.abs(recordInfo.getFrequency()-musicXmlInfo.getFrequency())<=soundCompareConfig.integrityFrequencyRange){
-                        integrityValidNum++;
-                    }
-                }
-
-                //非正常频率次数
-                int errPitchNum = 0;
-                //分贝变化次数
-                int decibelChangeNum = 0;
-
-                if(CollectionUtils.isEmpty(measureSoundPitchInfos)){
-                    userSoundInfoMap.get(phone).getMusicalNotePitchMap().put(musicXmlInfo.getMusicalNotesIndex(), (float) 0);
-                }else{
-                    Map<Integer, Long> collect = measureSoundPitchInfos.stream().map(pitch -> (int)pitch.getFrequency()).collect(Collectors.groupingBy(Integer::intValue, Collectors.counting()));
-                    //出现次数最多的频率
-                    Integer pitch = collect.entrySet().stream().max(Comparator.comparing(e -> e.getValue())).get().getKey();
-                    //当前频率
-                    double cf = -1;
-                    //频率持续数量
-                    int fnum = 0;
-                    //是否演奏中
-                    boolean ing = false;
-                    //当前分贝
-                    double cd = 0;
-                    //分贝变化方向,-1变小,1变大
-                    int dcd = -1;
-                    //分贝持续数量
-                    int dnum = 0;
-                    for (MusicPitchDetailDto musicalNotesPitch : measureSoundPitchInfos) {
-                        //计算频率断层次数
-                        if (Math.abs(musicalNotesPitch.getFrequency() - cf) > 20){
-                            fnum ++;
-                        }
-                        if (fnum>=5){
-                            cf = musicalNotesPitch.getFrequency();
-                            fnum = 0;
-                            if (cf != -1){
-                                errPitchNum ++;
-                                ing = true;
-                                cd = musicalNotesPitch.getDecibel();
-                            }
-                        }
-                        //计算声音大小断层册数
-                        if(ing && Math.abs(musicalNotesPitch.getDecibel() - cd) > 10){
-                            dnum ++;
-                        }
-                        if (dnum > 2){
-                            int tdcd = cd > musicalNotesPitch.getDecibel() ? -1 : 1;
-                            cd = musicalNotesPitch.getDecibel();
-                            dnum = 0;
-                            if (tdcd != dcd) {
-                                decibelChangeNum++;
-                            }
-                            dcd = tdcd;
-                        }
-                    }
-                    userSoundInfoMap.get(phone).getMusicalNotePitchMap().put(musicXmlInfo.getMusicalNotesIndex(), (float) pitch);
-                }
-
-                //有效节奏占比
-                float cadenceDuty = cadenceValidNum/compareNum;
-                //如果频率出现断层或这个音量出现断层,则当前音符节奏无效
-                if(errPitchNum>=2 || decibelChangeNum>1){
-                    cadenceDuty = 0;
-                }
-                //节奏
-                if(cadenceDuty>=soundCompareConfig.cadenceValidDuty){
-                    cadenceNum++;
-                }
-                //音准
-                if (!CollectionUtils.isEmpty(measureSoundPitchInfos)){
-                    Double avgPitch = measureSoundPitchInfos.stream().filter(pitch -> Math.abs((pitch.getFrequency()-musicXmlInfo.getFrequency()))<5).collect(Collectors.averagingDouble(pitch -> pitch.getFrequency()));
-                    //音分
-                    double recordCents = 0;
-                    if (avgPitch > 0){
-                        recordCents = PitchConverter.hertzToAbsoluteCent(avgPitch);
-                    }
-                    double cents = PitchConverter.hertzToAbsoluteCent(musicXmlInfo.getFrequency());
-                    double score = 100 - Math.round(Math.abs(cents - recordCents)) + soundCompareConfig.intonationCentsRange;
-                    if (score < 0){
-                        score = 0;
-                    }else if(score > 100){
-                        score = 100;
-                    }
-                    intonationScore += score;
-                    musicXmlInfo.setAvgFrequency(avgPitch.floatValue());
-                }
-                //完成度
-                if(integrityValidNum>0){
-                    integrityValidNum = integrityValidNum;
-                }
-                if(integrityValidNum > compareNum){
-                    integrityValidNum = compareNum;
-                }
-                float integrityDuty = integrityValidNum/compareNum;
-                integrityScore += integrityDuty;
-            }
-
-            BigDecimal measureNum = new BigDecimal(totalCompareNum);
-
-            intonation = new BigDecimal(intonationScore).divide(measureNum, CommonConstants.DECIMAL_PLACE, BigDecimal.ROUND_DOWN).setScale(0, BigDecimal.ROUND_UP);
-            cadence = new BigDecimal(cadenceNum).divide(measureNum, CommonConstants.DECIMAL_PLACE, BigDecimal.ROUND_DOWN).multiply(oneHundred).setScale(0, BigDecimal.ROUND_UP);
-            integrity = new BigDecimal(integrityScore).divide(measureNum, CommonConstants.DECIMAL_PLACE, BigDecimal.ROUND_DOWN).multiply(oneHundred).setScale(0, BigDecimal.ROUND_UP);
-
-        } catch (ArithmeticException e){
-            LOGGER.info("无musicXml信息");
-        }
-
-        if(userSoundInfoMap.get(phone).getUserScoreMap().containsKey("intonation")){
-            userSoundInfoMap.get(phone).getUserScoreMap().put("intonation", intonation.add(userSoundInfoMap.get(phone).getUserScoreMap().get("intonation")));
-        }else{
-            userSoundInfoMap.get(phone).getUserScoreMap().put("intonation", intonation);
-        }
-
-        if(userSoundInfoMap.get(phone).getUserScoreMap().containsKey("cadence")){
-            userSoundInfoMap.get(phone).getUserScoreMap().put("cadence", cadence.add(userSoundInfoMap.get(phone).getUserScoreMap().get("cadence")));
-        }else{
-            userSoundInfoMap.get(phone).getUserScoreMap().put("cadence", cadence);
-        }
-
-        if(userSoundInfoMap.get(phone).getUserScoreMap().containsKey("integrity")){
-            userSoundInfoMap.get(phone).getUserScoreMap().put("integrity", integrity.add(userSoundInfoMap.get(phone).getUserScoreMap().get("integrity")));
-        }else{
-            userSoundInfoMap.get(phone).getUserScoreMap().put("integrity", integrity);
-        }
-
-        //计算分数并推送
-        createPushInfo(phone, "measureScore", measureIndex, intonation, cadence, integrity);
-    }
-
-    /**
-     * @describe 计算最终评分
-     * @author Joburgess
-     * @date 2021/6/25 0025
-     * @param phone:
-     * @return void
-     */
-    private void calTotalScore(String phone) throws IOException {
-        int totalCompareNum = userSoundInfoMap.get(phone).getMeasureXmlInfoMap().keySet().size();
-        int currentCompareNum = totalCompareNum-userSoundInfoMap.get(phone).getMeasureEndTime().keySet().size();
-        BigDecimal intonation = BigDecimal.ZERO;
-        BigDecimal cadence = BigDecimal.ZERO;
-        BigDecimal integrity = BigDecimal.ZERO;
-
-        if(currentCompareNum>0){
-            intonation = userSoundInfoMap.get(phone).getUserScoreMap().get("intonation").divide(new BigDecimal(currentCompareNum), 0, BigDecimal.ROUND_DOWN);
-            cadence = userSoundInfoMap.get(phone).getUserScoreMap().get("cadence").divide(new BigDecimal(currentCompareNum), 0, BigDecimal.ROUND_DOWN);
-            integrity = userSoundInfoMap.get(phone).getUserScoreMap().get("integrity").divide(new BigDecimal(currentCompareNum), 0, BigDecimal.ROUND_DOWN);
-        }
-
-        //计算分数并推送
-        createPushInfo(phone, "overall", -1, intonation, cadence, integrity);
-
-        //存储评分数据
-        sysMusicCompareRecordService.saveMusicCompareData(phone, userSoundInfoMap.get(phone));
-
-        LOGGER.info("评分数据:{}", JSON.toJSONString(userSoundInfoMap.get(phone)));
-    }
-
-    /**
-     * @describe 生成评分结果
-     * @author Joburgess
-     * @date 2021/6/25 0025
-     * @param command:
-     * @param measureIndex:
-     * @param intonation:
-     * @param cadence:
-     * @param integrity:
-     * @return com.ym.mec.biz.dal.dto.WebSocketInfo
-     */
-    private WebSocketInfo createPushInfo(String phone, String command, Integer measureIndex,
-                                         BigDecimal intonation, BigDecimal cadence, BigDecimal integrity) throws IOException {
-        WebSocketInfo webSocketInfo = new WebSocketInfo();
-        HashMap<String, String> header = new HashMap<>();
-        header.put("commond", command);
-        webSocketInfo.setHeader(header);
-        Map<String, Object> result = new HashMap<>();
-//        BigDecimal score = intonation.multiply(new BigDecimal(0.5)).add(cadence.multiply(new BigDecimal(0.5))).setScale(0, BigDecimal.ROUND_HALF_UP);
-        BigDecimal score = intonation.add(cadence).add(integrity).divide(new BigDecimal(3), CommonConstants.DECIMAL_PLACE, BigDecimal.ROUND_DOWN).setScale(0, BigDecimal.ROUND_UP);
-//        BigDecimal score = integrity.setScale(0, BigDecimal.ROUND_HALF_UP);
-        result.put("score", score);
-        result.put("intonation", intonation);
-        result.put("cadence", cadence);
-        result.put("integrity", integrity);
-        result.put("measureIndex", measureIndex);
-        webSocketInfo.setBody(result);
-
-        userSoundInfoMap.get(phone).getUserMeasureScoreMap().put(measureIndex, result);
-
-        LOGGER.info("小节频分:{}", JSON.toJSONString(webSocketInfo));
-
-        //推送结果
-        WS_CLIENTS.get(phone).getSession().sendMessage(new TextMessage(JSON.toJSONString(webSocketInfo)));
-        return webSocketInfo;
-    }
 }

+ 1 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/SoundSocketService.java

@@ -6,6 +6,7 @@ package com.ym.mec.biz.service;
  */
 public interface SoundSocketService {
 
+    String TAG = "TAG";
     String COMMOND = "commond";
     String MUSIC_XML = "musicXml";
     String RECORD_START = "recordStart";

+ 53 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/WebSocketEventHandler.java

@@ -0,0 +1,53 @@
+package com.ym.mec.biz.service;
+
+import org.springframework.web.socket.BinaryMessage;
+import org.springframework.web.socket.TextMessage;
+import org.springframework.web.socket.WebSocketSession;
+
+/**
+ * @Author Joburgess
+ * @Date 2021/8/5 0005
+ **/
+public interface WebSocketEventHandler {
+
+
+
+    /**
+     * @describe 连接成功
+     * @author Joburgess
+     * @date 2021/8/5 0005
+     * @param session: 会话信息
+     * @return void
+     */
+    void afterConnectionEstablished(WebSocketSession session, String phone);
+
+    /**
+     * @describe 接受到文本信息
+     * @author Joburgess
+     * @date 2021/8/5 0005
+     * @param session: 会话信息
+     * @param message: 文本信息
+     * @return void
+     */
+    void receiveTextMessage(WebSocketSession session, String phone, TextMessage message);
+
+    /**
+     * @describe 接收到二进制数据
+     * @author Joburgess
+     * @date 2021/8/5 0005
+     * @param session: 会话信息
+     * @param message: 二进制数据
+     * @return void
+     */
+    void receiveBinaryMessage(WebSocketSession session, String phone, BinaryMessage message);
+
+    /**
+     * @describe 连接关闭
+     * @author Joburgess
+     * @date 2021/8/5 0005
+     * @param session: 会话信息
+     * @return void
+     */
+    void afterConnectionClosed(WebSocketSession session, String phone);
+
+}

+ 517 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/impl/SoundCompareHandler.java

@@ -0,0 +1,517 @@
+package com.ym.mec.biz.service.impl;
+
+import be.tarsos.dsp.AudioDispatcher;
+import be.tarsos.dsp.io.jvm.AudioDispatcherFactory;
+import be.tarsos.dsp.pitch.PitchProcessor;
+import be.tarsos.dsp.util.PitchConverter;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.ym.mec.biz.dal.config.SoundCompareConfig;
+import com.ym.mec.biz.dal.dao.SysMusicScoreAccompanimentDao;
+import com.ym.mec.biz.dal.dto.MusicPitchDetailDto;
+import com.ym.mec.biz.dal.dto.SoundCompareHelper;
+import com.ym.mec.biz.dal.dto.WavHeader;
+import com.ym.mec.biz.dal.dto.WebSocketInfo;
+import com.ym.mec.biz.dal.enums.DeviceTypeEnum;
+import com.ym.mec.biz.handler.WebSocketHandler;
+import com.ym.mec.biz.service.SoundSocketService;
+import com.ym.mec.biz.service.SysMusicCompareRecordService;
+import com.ym.mec.biz.service.WebSocketEventHandler;
+import com.ym.mec.common.constant.CommonConstants;
+import com.ym.mec.common.exception.BizException;
+import org.apache.commons.io.FileUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.oauth2.provider.OAuth2Authentication;
+import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
+import org.springframework.web.socket.BinaryMessage;
+import org.springframework.web.socket.TextMessage;
+import org.springframework.web.socket.WebSocketSession;
+
+import javax.sound.sampled.UnsupportedAudioFileException;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.math.BigDecimal;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Collectors;
+
+/**
+ * @Author Joburgess
+ * @Date 2021/8/5 0005
+ */
+@Service
+public class SoundCompareHandler implements WebSocketEventHandler {
+
+    private final Logger LOGGER = LoggerFactory.getLogger(SoundCompareHandler.class);
+
+    private BigDecimal oneHundred = new BigDecimal(100);
+
+    private final String tmpDir = FileUtils.getTempDirectoryPath() + "/soundCompare/";
+    /**
+     * @describe 用户对应评分信息
+     */
+    private Map<String, SoundCompareHelper> userSoundInfoMap = new ConcurrentHashMap<>();
+    /**
+     * @describe 音频处理参数
+     */
+    public static final SoundCompareConfig soundCompareConfig = new SoundCompareConfig();
+
+    @Autowired
+    private SysMusicCompareRecordService sysMusicCompareRecordService;
+    @Autowired
+    private SysMusicScoreAccompanimentDao sysMusicScoreAccompanimentDao;
+
+    public SoundCompareHandler() {
+        WebSocketHandler.regist("SOUND_COMPARE", this);
+        File soundDir = new File(tmpDir);
+        if(!soundDir.exists()){
+            soundDir.mkdir();
+        }
+    }
+
+    @Override
+    public void afterConnectionEstablished(WebSocketSession session, String phone) {
+
+    }
+
+    @Override
+    public void receiveTextMessage(WebSocketSession session, String phone, TextMessage message) {
+        WebSocketInfo webSocketInfo = JSON.parseObject(message.getPayload(), WebSocketInfo.class);
+        JSONObject bodyObject = (JSONObject) webSocketInfo.getBody();
+
+        String commond = "";
+        if(webSocketInfo.getHeader().containsKey(SoundSocketService.COMMOND)){
+            commond = webSocketInfo.getHeader().get(SoundSocketService.COMMOND);
+        }
+        switch (commond){
+            case SoundSocketService.MUSIC_XML:
+                userSoundInfoMap.put(phone, new SoundCompareHelper());
+                userSoundInfoMap.get(phone).setClientId(((OAuth2Authentication)session.getPrincipal()).getOAuth2Request().getClientId());
+                List<MusicPitchDetailDto> musicXmlInfos = JSON.parseArray(bodyObject.getString("musicXmlInfos"), MusicPitchDetailDto.class);
+                userSoundInfoMap.get(phone).setMusicXmlInfos(musicXmlInfos);
+                musicXmlInfos = musicXmlInfos.stream().filter(m->!m.getDontEvaluating()).collect(Collectors.toList());
+                userSoundInfoMap.get(phone).setMusicScoreId(bodyObject.getInteger("id"));
+                if(bodyObject.containsKey("platform")){
+                    userSoundInfoMap.get(phone).setDeviceType(DeviceTypeEnum.valueOf(bodyObject.getString("platform")));
+                }
+                List<Integer> subjectIds = sysMusicScoreAccompanimentDao.findSubjectByMusicScoreId(userSoundInfoMap.get(phone).getMusicScoreId(), null);
+                if(!CollectionUtils.isEmpty(subjectIds)){
+                    userSoundInfoMap.get(phone).setSubjectId(subjectIds.get(0));
+                }
+                userSoundInfoMap.get(phone).setMeasureXmlInfoMap(musicXmlInfos.stream().collect(Collectors.groupingBy(MusicPitchDetailDto::getMeasureIndex)));
+                musicXmlInfos.forEach(e->userSoundInfoMap.get(phone).getMusicalNotePitchMap().put(e.getMusicalNotesIndex(), e.getFrequency()));
+                for (Map.Entry<Integer, List<MusicPitchDetailDto>> userMeasureXmlInfoEntry : userSoundInfoMap.get(phone).getMeasureXmlInfoMap().entrySet()) {
+                    MusicPitchDetailDto firstPitch = userMeasureXmlInfoEntry.getValue().stream().min(Comparator.comparing(MusicPitchDetailDto::getTimeStamp)).get();
+                    MusicPitchDetailDto lastPitch = userMeasureXmlInfoEntry.getValue().stream().max(Comparator.comparing(MusicPitchDetailDto::getTimeStamp)).get();
+                    long dc = userMeasureXmlInfoEntry.getValue().stream().filter(m -> m.getDontEvaluating()).count();
+                    MusicPitchDetailDto musicPitchDetailDto = new MusicPitchDetailDto(firstPitch.getTimeStamp(), lastPitch.getTimeStamp() + lastPitch.getDuration());
+                    musicPitchDetailDto.setDuration(musicPitchDetailDto.getEndTimeStamp()-musicPitchDetailDto.getTimeStamp());
+                    musicPitchDetailDto.setDontEvaluating(dc == userMeasureXmlInfoEntry.getValue().size());
+                    userSoundInfoMap.get(phone).getMeasureEndTime().put(userMeasureXmlInfoEntry.getKey(), musicPitchDetailDto);
+                }
+
+                break;
+            case SoundSocketService.RECORD_START:
+                if(!userSoundInfoMap.containsKey(phone)){
+                    break;
+                }
+                try {
+                    File file = new File(tmpDir+phone + "_"+ userSoundInfoMap.get(phone).getMusicScoreId() +"_"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) +".wav");
+                    userSoundInfoMap.get(phone).setAccessFile(new RandomAccessFile(file, "rw"));
+                    userSoundInfoMap.get(phone).setRecordFilePath(file.getAbsolutePath());
+                } catch (FileNotFoundException e) {
+                    throw new BizException("文件创建失败:", e);
+                }
+                break;
+            case SoundSocketService.RECORD_END:
+                if(!userSoundInfoMap.containsKey(phone)){
+                    break;
+                }
+                try {
+                    if(!CollectionUtils.isEmpty(userSoundInfoMap.get(phone).getMeasureEndTime())){
+                        Integer lastMeasureIndex = userSoundInfoMap.get(phone).getMeasureEndTime().keySet().stream().min(Integer::compareTo).get();
+                        double recordTime = userSoundInfoMap.get(phone).getAccessFile().length()/(soundCompareConfig.audioFormat.getFrameSize()*soundCompareConfig.audioFormat.getFrameRate())*1000;
+                        //如果结束时时长大于某小节,则此小节需要评分
+                        if(recordTime>userSoundInfoMap.get(phone).getMeasureEndTime().get(lastMeasureIndex).getEndTimeStamp()){
+                            measureCompare(phone, lastMeasureIndex);
+                            userSoundInfoMap.get(phone).getMeasureEndTime().remove(lastMeasureIndex);
+                        }
+                    }
+                    calTotalScore(phone);
+                } catch (IOException e) {
+                    throw new BizException("评分错误:", e);
+                }
+                createHeader(phone);
+                break;
+            case SoundSocketService.RECORD_CANCEL:
+                createHeader(phone);
+                break;
+            case SoundSocketService.PROXY_MESSAGE:
+                if(DeviceTypeEnum.IOS.equals(userSoundInfoMap.get(phone).getDeviceType())&&bodyObject.containsKey(SoundSocketService.OFFSET_TIME)){
+                    int offsetTime = bodyObject.getIntValue(SoundSocketService.OFFSET_TIME);
+                    calOffsetTime(phone, offsetTime);
+                }
+                break;
+            default:
+                break;
+        }
+    }
+
+    @Override
+    public void receiveBinaryMessage(WebSocketSession session, String phone, BinaryMessage message) {
+        if(!userSoundInfoMap.containsKey(phone)){
+            return;
+        }
+        try {
+            if(Objects.nonNull(userSoundInfoMap.get(phone).getAccessFile())){
+                userSoundInfoMap.get(phone).getAccessFile().write(message.getPayload().array());
+            }
+
+            AudioDispatcher dispatcher = AudioDispatcherFactory.fromByteArray(message.getPayload().array(), soundCompareConfig.audioFormat, soundCompareConfig.simpleSize, soundCompareConfig.overlap);
+
+            dispatcher.addAudioProcessor(userSoundInfoMap.get(phone).silenceDetector);
+            dispatcher.addAudioProcessor(new PitchProcessor(soundCompareConfig.algo, soundCompareConfig.simpleRate, soundCompareConfig.simpleSize, userSoundInfoMap.get(phone)));
+            dispatcher.run();
+            if(Objects.isNull(userSoundInfoMap.get(phone).getAccessFile())){
+                return;
+            }
+
+            double recordTime = userSoundInfoMap.get(phone).getAccessFile().length()/(soundCompareConfig.audioFormat.getFrameSize()*soundCompareConfig.audioFormat.getFrameRate())*1000;
+            userSoundInfoMap.get(phone).setMeasureStartTime(recordTime);
+            for (Map.Entry<Integer, MusicPitchDetailDto> userMeasureEndTimeMapEntry : userSoundInfoMap.get(phone).getMeasureEndTime().entrySet()) {
+                if(recordTime>(userMeasureEndTimeMapEntry.getValue().getEndTimeStamp())){
+                    if(userMeasureEndTimeMapEntry.getValue().getDontEvaluating()){
+                        continue;
+                    }else{
+                        measureCompare(phone, userMeasureEndTimeMapEntry.getKey());
+                    }
+                    userSoundInfoMap.get(phone).getMeasureEndTime().remove(userMeasureEndTimeMapEntry.getKey());
+                    break;
+                }
+            }
+        } catch (UnsupportedAudioFileException | IOException e) {
+            throw new BizException("{}评分异常:{}", phone, e);
+        }
+    }
+
+    @Override
+    public void afterConnectionClosed(WebSocketSession session, String phone){
+        createHeader(phone);
+        userSoundInfoMap.remove(phone);
+    }
+
+    /**
+     * @describe 处理时间偏移
+     * @author Joburgess
+     * @date 2021/7/5 0005
+     * @param phone:
+     * @param offsetTime:
+     * @return void
+     */
+    private void calOffsetTime(String phone, int offsetTime){
+        userSoundInfoMap.get(phone).setOffsetTime(offsetTime);
+        for (Map.Entry<Integer, MusicPitchDetailDto> musicPitchDetailDtoEntry : userSoundInfoMap.get(phone).getMeasureEndTime().entrySet()) {
+            musicPitchDetailDtoEntry.getValue().setTimeStamp(musicPitchDetailDtoEntry.getValue().getTimeStamp() + offsetTime);
+            musicPitchDetailDtoEntry.getValue().setEndTimeStamp(musicPitchDetailDtoEntry.getValue().getEndTimeStamp() + offsetTime);
+        }
+    }
+
+    /**
+     * @describe 保存录音数据,并生成wav头信息
+     * @author Joburgess
+     * @date 2021/6/25 0025
+     * @param phone:
+     * @return void
+     */
+    private void createHeader(String phone){
+        if(!userSoundInfoMap.containsKey(phone)){
+            return;
+        }
+        if(Objects.nonNull(userSoundInfoMap.get(phone).getAccessFile())){
+            try {
+                RandomAccessFile randomAccessFile = userSoundInfoMap.get(phone).getAccessFile();
+                LOGGER.info("音频时长:{}", randomAccessFile.length()/(soundCompareConfig.audioFormat.getFrameSize()*soundCompareConfig.audioFormat.getFrameRate())*1000);
+                randomAccessFile.seek(0);
+                randomAccessFile.write(WavHeader.getWaveHeader(randomAccessFile.length(), (long) soundCompareConfig.audioFormat.getFrameRate(), soundCompareConfig.audioFormat.getSampleSizeInBits()));
+                randomAccessFile.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+            userSoundInfoMap.get(phone).setAccessFile(null);
+        }
+        userSoundInfoMap.remove(phone);
+    }
+
+    /**
+     * @describe 数据比对,生成分数
+     * @author Joburgess
+     * @date 2021/6/25 0025
+     * @param phone:
+     * @param measureIndex:
+     * @return void
+     */
+    private void measureCompare(String phone, int measureIndex){
+        if (userSoundInfoMap.get(phone).getOffsetTime() == -1){
+            userSoundInfoMap.get(phone).setOffsetTime(0);
+        }
+
+        //相似度
+        BigDecimal intonation = BigDecimal.ZERO;
+        //节奏
+        BigDecimal cadence = BigDecimal.ZERO;
+        //完整度
+        BigDecimal integrity = BigDecimal.ZERO;
+
+        try {
+            //音准分数
+            float intonationScore = 0;
+
+            //节奏匹配数量
+            float cadenceNum = 0;
+
+            //完整性分数
+            float integrityScore = 0;
+
+            int totalCompareNum = userSoundInfoMap.get(phone).getMeasureXmlInfoMap().get(measureIndex).size();
+
+            for (int i = 0; i < userSoundInfoMap.get(phone).getMeasureXmlInfoMap().get(measureIndex).size(); i++) {
+                MusicPitchDetailDto musicXmlInfo = userSoundInfoMap.get(phone).getMeasureXmlInfoMap().get(measureIndex).get(i);
+
+                int ot5 = (int) (musicXmlInfo.getDuration()*0.1);
+                int startTimeStamp = musicXmlInfo.getTimeStamp() + userSoundInfoMap.get(phone).getOffsetTime() + ot5;
+                int endTimeStamp = musicXmlInfo.getTimeStamp()  + userSoundInfoMap.get(phone).getOffsetTime() + musicXmlInfo.getDuration() - ot5;
+
+                //时间范围内有效节奏数量
+                float cadenceValidNum = 0;
+                //时间范围内有效音频数量
+                float integrityValidNum = 0;
+                //时间范围内匹配次数
+                float compareNum = 0;
+
+                List<MusicPitchDetailDto> measureSoundPitchInfos = new ArrayList<>();
+
+                for (int j = 0; j < userSoundInfoMap.get(phone).getRecordMeasurePithInfo().size(); j++) {
+                    MusicPitchDetailDto recordInfo = userSoundInfoMap.get(phone).getRecordMeasurePithInfo().get(j);
+                    //如果在时间范围之外直接跳过
+                    if(recordInfo.getTimeStamp()<startTimeStamp||recordInfo.getTimeStamp()>endTimeStamp){
+                        continue;
+                    }
+                    measureSoundPitchInfos.add(recordInfo);
+                    compareNum++;
+                    //如果在最低有效频率以下则跳过
+                    if(recordInfo.getFrequency()<soundCompareConfig.validFrequency&&musicXmlInfo.getFrequency()!=-1){
+                        continue;
+                    }
+                    cadenceValidNum++;
+                    //如果频率差值在节奏误差范围内
+                    if(Math.abs(recordInfo.getFrequency()-musicXmlInfo.getFrequency())<=soundCompareConfig.integrityFrequencyRange){
+                        integrityValidNum++;
+                    }
+                }
+
+                //非正常频率次数
+                int errPitchNum = 0;
+                //分贝变化次数
+                int decibelChangeNum = 0;
+
+                if(CollectionUtils.isEmpty(measureSoundPitchInfos)){
+                    userSoundInfoMap.get(phone).getMusicalNotePitchMap().put(musicXmlInfo.getMusicalNotesIndex(), (float) 0);
+                }else{
+                    Map<Integer, Long> collect = measureSoundPitchInfos.stream().map(pitch -> (int)pitch.getFrequency()).collect(Collectors.groupingBy(Integer::intValue, Collectors.counting()));
+                    //出现次数最多的频率
+                    Integer pitch = collect.entrySet().stream().max(Comparator.comparing(e -> e.getValue())).get().getKey();
+                    //当前频率
+                    double cf = -1;
+                    //频率持续数量
+                    int fnum = 0;
+                    //是否演奏中
+                    boolean ing = false;
+                    //当前分贝
+                    double cd = 0;
+                    //分贝变化方向,-1变小,1变大
+                    int dcd = -1;
+                    //分贝持续数量
+                    int dnum = 0;
+                    for (MusicPitchDetailDto musicalNotesPitch : measureSoundPitchInfos) {
+                        //计算频率断层次数
+                        if (Math.abs(musicalNotesPitch.getFrequency() - cf) > 20){
+                            fnum ++;
+                        }
+                        if (fnum>=5){
+                            cf = musicalNotesPitch.getFrequency();
+                            fnum = 0;
+                            if (cf != -1){
+                                errPitchNum ++;
+                                ing = true;
+                                cd = musicalNotesPitch.getDecibel();
+                            }
+                        }
+                        //计算声音大小断层册数
+                        if(ing && Math.abs(musicalNotesPitch.getDecibel() - cd) > 10){
+                            dnum ++;
+                        }
+                        if (dnum > 2){
+                            int tdcd = cd > musicalNotesPitch.getDecibel() ? -1 : 1;
+                            cd = musicalNotesPitch.getDecibel();
+                            dnum = 0;
+                            if (tdcd != dcd) {
+                                decibelChangeNum++;
+                            }
+                            dcd = tdcd;
+                        }
+                    }
+                    userSoundInfoMap.get(phone).getMusicalNotePitchMap().put(musicXmlInfo.getMusicalNotesIndex(), (float) pitch);
+                }
+
+                //有效节奏占比
+                float cadenceDuty = cadenceValidNum/compareNum;
+                //如果频率出现断层或这个音量出现断层,则当前音符节奏无效
+                if(errPitchNum>=2 || decibelChangeNum>1){
+                    cadenceDuty = 0;
+                }
+                //节奏
+                if(cadenceDuty>=soundCompareConfig.cadenceValidDuty){
+                    cadenceNum++;
+                }
+                //音准
+                if (!CollectionUtils.isEmpty(measureSoundPitchInfos)){
+                    Double avgPitch = measureSoundPitchInfos.stream().filter(pitch -> Math.abs((pitch.getFrequency()-musicXmlInfo.getFrequency()))<5).collect(Collectors.averagingDouble(pitch -> pitch.getFrequency()));
+                    //音分
+                    double recordCents = 0;
+                    if (avgPitch > 0){
+                        recordCents = PitchConverter.hertzToAbsoluteCent(avgPitch);
+                    }
+                    double cents = 0;
+                    if(musicXmlInfo.getFrequency()>0){
+                        cents =  PitchConverter.hertzToAbsoluteCent(musicXmlInfo.getFrequency());
+                    }
+                    double score = 100 - Math.round(Math.abs(cents - recordCents)) + soundCompareConfig.intonationCentsRange;
+                    if (score < 0){
+                        score = 0;
+                    }else if(score > 100){
+                        score = 100;
+                    }
+                    intonationScore += score;
+                    musicXmlInfo.setAvgFrequency(avgPitch.floatValue());
+                }
+                //完成度
+                if(integrityValidNum>0){
+                    integrityValidNum = integrityValidNum;
+                }
+                if(integrityValidNum > compareNum){
+                    integrityValidNum = compareNum;
+                }
+                float integrityDuty = integrityValidNum/compareNum;
+                integrityScore += integrityDuty;
+            }
+
+            BigDecimal measureNum = new BigDecimal(totalCompareNum);
+
+            intonation = new BigDecimal(intonationScore).divide(measureNum, CommonConstants.DECIMAL_PLACE, BigDecimal.ROUND_DOWN).setScale(0, BigDecimal.ROUND_UP);
+            cadence = new BigDecimal(cadenceNum).divide(measureNum, CommonConstants.DECIMAL_PLACE, BigDecimal.ROUND_DOWN).multiply(oneHundred).setScale(0, BigDecimal.ROUND_UP);
+            integrity = new BigDecimal(integrityScore).divide(measureNum, CommonConstants.DECIMAL_PLACE, BigDecimal.ROUND_DOWN).multiply(oneHundred).setScale(0, BigDecimal.ROUND_UP);
+
+        } catch (ArithmeticException e){
+            LOGGER.info("无musicXml信息");
+        }
+
+        if(userSoundInfoMap.get(phone).getUserScoreMap().containsKey("intonation")){
+            userSoundInfoMap.get(phone).getUserScoreMap().put("intonation", intonation.add(userSoundInfoMap.get(phone).getUserScoreMap().get("intonation")));
+        }else{
+            userSoundInfoMap.get(phone).getUserScoreMap().put("intonation", intonation);
+        }
+
+        if(userSoundInfoMap.get(phone).getUserScoreMap().containsKey("cadence")){
+            userSoundInfoMap.get(phone).getUserScoreMap().put("cadence", cadence.add(userSoundInfoMap.get(phone).getUserScoreMap().get("cadence")));
+        }else{
+            userSoundInfoMap.get(phone).getUserScoreMap().put("cadence", cadence);
+        }
+
+        if(userSoundInfoMap.get(phone).getUserScoreMap().containsKey("integrity")){
+            userSoundInfoMap.get(phone).getUserScoreMap().put("integrity", integrity.add(userSoundInfoMap.get(phone).getUserScoreMap().get("integrity")));
+        }else{
+            userSoundInfoMap.get(phone).getUserScoreMap().put("integrity", integrity);
+        }
+
+        //计算分数并推送
+        createPushInfo(phone, "measureScore", measureIndex, intonation, cadence, integrity);
+    }
+
+    /**
+     * @describe 计算最终评分
+     * @author Joburgess
+     * @date 2021/6/25 0025
+     * @param phone:
+     * @return void
+     */
+    private void calTotalScore(String phone) {
+        int totalCompareNum = userSoundInfoMap.get(phone).getMeasureXmlInfoMap().keySet().size();
+        int currentCompareNum = totalCompareNum-userSoundInfoMap.get(phone).getMeasureEndTime().keySet().size();
+        BigDecimal intonation = BigDecimal.ZERO;
+        BigDecimal cadence = BigDecimal.ZERO;
+        BigDecimal integrity = BigDecimal.ZERO;
+
+        if(currentCompareNum>0){
+            intonation = userSoundInfoMap.get(phone).getUserScoreMap().get("intonation").divide(new BigDecimal(currentCompareNum), 0, BigDecimal.ROUND_DOWN);
+            cadence = userSoundInfoMap.get(phone).getUserScoreMap().get("cadence").divide(new BigDecimal(currentCompareNum), 0, BigDecimal.ROUND_DOWN);
+            integrity = userSoundInfoMap.get(phone).getUserScoreMap().get("integrity").divide(new BigDecimal(currentCompareNum), 0, BigDecimal.ROUND_DOWN);
+        }
+
+        //计算分数并推送
+        createPushInfo(phone, "overall", -1, intonation, cadence, integrity);
+
+        //存储评分数据
+        sysMusicCompareRecordService.saveMusicCompareData(phone, userSoundInfoMap.get(phone));
+
+        LOGGER.info("评分数据:{}", JSON.toJSONString(userSoundInfoMap.get(phone)));
+    }
+
+    /**
+     * @describe 生成评分结果
+     * @author Joburgess
+     * @date 2021/6/25 0025
+     * @param command:
+     * @param measureIndex:
+     * @param intonation:
+     * @param cadence:
+     * @param integrity:
+     * @return com.ym.mec.biz.dal.dto.WebSocketInfo
+     */
+    private WebSocketInfo createPushInfo(String phone, String command, Integer measureIndex,
+                                         BigDecimal intonation, BigDecimal cadence, BigDecimal integrity){
+        WebSocketInfo webSocketInfo = new WebSocketInfo();
+        HashMap<String, String> header = new HashMap<>();
+        header.put("commond", command);
+        webSocketInfo.setHeader(header);
+        Map<String, Object> result = new HashMap<>(5);
+        BigDecimal score = cadence;
+        if(Objects.isNull(userSoundInfoMap.get(phone).getSubjectId())||userSoundInfoMap.get(phone).getSubjectId()!=23){
+            score = intonation.add(cadence).add(integrity).divide(new BigDecimal(3), CommonConstants.DECIMAL_PLACE, BigDecimal.ROUND_DOWN).setScale(0, BigDecimal.ROUND_UP);
+        }
+        result.put("score", score);
+        result.put("intonation", intonation);
+        result.put("cadence", cadence);
+        result.put("integrity", integrity);
+        result.put("measureIndex", measureIndex);
+        webSocketInfo.setBody(result);
+
+        userSoundInfoMap.get(phone).getUserMeasureScoreMap().put(measureIndex, result);
+
+        LOGGER.info("小节频分:{}", JSON.toJSONString(webSocketInfo));
+
+        //推送结果
+        try {
+            WebSocketHandler.WS_CLIENTS.get(phone).getSession().sendMessage(new TextMessage(JSON.toJSONString(webSocketInfo)));
+        } catch (IOException e) {
+            LOGGER.error("{}评分结果推送失败", phone);
+        }
+        return webSocketInfo;
+    }
+}

+ 2 - 1
mec-biz/src/main/java/com/ym/mec/biz/service/impl/StudentServeServiceImpl.java

@@ -299,8 +299,9 @@ public class StudentServeServiceImpl implements StudentServeService {
     public void exercisesSituationStatistics2(String monday, List<Integer> studentIds) {
         LocalDate nowDate = LocalDateTime.now(DateUtil.zoneId).toLocalDate();
 
+        int preWeekServiceNum = studentExtracurricularExercisesSituationDao.countWeekServiceNum(nowDate.with(DateUtil.weekFields.dayOfWeek(), DayOfWeek.MONDAY.getValue()).plusDays(-7).toString());
         int lastWeekTodayUpdateNum = studentExtracurricularExercisesSituationDao.findLastWeekTodayUpdateNum(nowDate.plusDays(-nowDate.getDayOfWeek().getValue()).toString());
-        if(lastWeekTodayUpdateNum<=0){
+        if(preWeekServiceNum>0 && lastWeekTodayUpdateNum<=0){
             nowDate = nowDate.plusDays(-nowDate.getDayOfWeek().getValue());
         }
 

+ 8 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/impl/SysMusicCompareRecordServiceImpl.java

@@ -12,6 +12,7 @@ import com.ym.mec.biz.service.SysMusicCompareRecordService;
 import com.ym.mec.biz.dal.dao.SysMusicCompareRecordDao;
 import org.springframework.stereotype.Service;
 
+import java.io.IOException;
 import java.math.BigDecimal;
 import java.util.Map;
 import java.util.Objects;
@@ -47,6 +48,13 @@ public class SysMusicCompareRecordServiceImpl extends BaseServiceImpl<Long, SysM
 			sysMusicCompareRecord.setIntegrity((BigDecimal) finalScore.get("integrity"));
 		}
 		sysMusicCompareRecord.setRecordFilePath(soundCompareInfo.getRecordFilePath());
+		sysMusicCompareRecord.setDeviceType(soundCompareInfo.getDeviceType());
+		sysMusicCompareRecord.setClientId(soundCompareInfo.getClientId());
+		try {
+			sysMusicCompareRecord.setPlayTime(soundCompareInfo.getAccessFile().length()/(SoundCompareHandler.soundCompareConfig.audioFormat.getFrameSize()*SoundCompareHandler.soundCompareConfig.audioFormat.getFrameRate()));
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
 		sysMusicCompareRecordDao.insert(sysMusicCompareRecord);
 	}
 }

+ 1 - 1
mec-biz/src/main/resources/config/mybatis/StudentExtracurricularExercisesSituationMapper.xml

@@ -471,7 +471,7 @@
 	</select>
 
 	<select id="countWeekServiceNum" resultType="int">
-		SELECT COUNT(id_) FROM student_extracurricular_exercises_situation_ WHERE monday_=#{sunday}
+		SELECT COUNT(id_) FROM student_extracurricular_exercises_situation_ WHERE monday_=#{monday}
 	</select>
 
 	<select id="findWeekServiceWithStudents" resultMap="StudentExtracurricularExercisesSituation">

+ 12 - 4
mec-biz/src/main/resources/config/mybatis/SysMusicCompareRecordMapper.xml

@@ -16,6 +16,9 @@
 		<result column="cadence_" property="cadence" />
 		<result column="integrity_" property="integrity" />
 		<result column="record_file_path_" property="recordFilePath" />
+		<result column="device_type_" property="deviceType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler" />
+		<result column="client_id_" property="clientId" />
+		<result column="play_time_" property="playTime"/>
 		<result column="create_time_" property="createTime" />
 	</resultMap>
 
@@ -36,8 +39,10 @@
 		SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL 
 		</selectKey>
 		-->
-		INSERT INTO sys_music_compare_record (id_,user_id_,sys_music_score_id_,score_data_,score_,intonation_,cadence_,integrity_,record_file_path_,create_time_)
-		VALUES(#{id},#{userId},#{sysMusicScoreId},#{scoreData},#{score},#{intonation},#{cadence},#{integrity},#{recordFilePath},NOW())
+		INSERT INTO sys_music_compare_record (id_,user_id_,sys_music_score_id_,score_data_,score_,intonation_,cadence_,integrity_,
+		                                      record_file_path_,device_type_,client_id_,play_time_,create_time_)
+		VALUES(#{id},#{userId},#{sysMusicScoreId},#{scoreData},#{score},#{intonation},#{cadence},#{integrity},
+		       #{recordFilePath},#{deviceType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{clientId},#{playTime},NOW())
 	</insert>
 
 	<!-- 根据主键查询一条记录 -->
@@ -70,8 +75,11 @@
 		<if test="recordFilePath != null">
 			record_file_path_ = #{recordFilePath},
 		</if>
-		<if test="createTime != null">
-			create_time_ = #{createTime},
+		<if test="clientId != null">
+			client_id_ = #{clientId},
+		</if>
+		<if test="playTime != null">
+			play_time_ = #{playTime},
 		</if>
 	</set> WHERE id_ = #{id}
 	</update>