|
|
@@ -10,10 +10,12 @@ 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.service.SoundSocketService;
|
|
|
+import com.ym.mec.biz.service.SysMusicCompareRecordService;
|
|
|
import com.ym.mec.common.constant.CommonConstants;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
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.*;
|
|
|
@@ -52,6 +54,9 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
|
|
|
|
|
|
private Map<String, SoundCompareHelper> userSoundInfoMap = new ConcurrentHashMap<>();
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private SysMusicCompareRecordService sysMusicCompareRecordService;
|
|
|
+
|
|
|
public WebSocketHandler() {
|
|
|
super();
|
|
|
File soundDir = new File(tmpDir);
|
|
|
@@ -106,9 +111,15 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
|
|
|
if(!userSoundInfoMap.containsKey(phone)){
|
|
|
break;
|
|
|
}
|
|
|
-// if(!CollectionUtils.isEmpty(userSoundInfoMap.get(phone).getMeasureEndTime())){
|
|
|
-// measureCompare(phone, userSoundInfoMap.get(phone).getMeasureEndTime().keySet().stream().max(Integer::compareTo).get());
|
|
|
-// }
|
|
|
+ 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()/(audioFormat.getFrameSize()*audioFormat.getFrameRate())*1000;
|
|
|
+ //如果结束时时长大于某小节,则此小节需要评分
|
|
|
+ if(recordTime>userSoundInfoMap.get(phone).getMeasureEndTime().get(lastMeasureIndex)){
|
|
|
+ measureCompare(phone, lastMeasureIndex);
|
|
|
+ userSoundInfoMap.get(phone).getMeasureEndTime().remove(lastMeasureIndex);
|
|
|
+ }
|
|
|
+ }
|
|
|
calTotalScore(phone);
|
|
|
createHeader(phone);
|
|
|
break;
|
|
|
@@ -132,7 +143,6 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
|
|
|
int timeStamp = (int) (userSoundInfoMap.get(phone).getMeasureStartTime() + audioEvent.getTimeStamp()*1000);
|
|
|
float pitch = pitchDetectionResult.getPitch();
|
|
|
recordInfo.add(new MusicPitchDetailDto(timeStamp, pitch));
|
|
|
- LOGGER.info("频率:{}", pitch);
|
|
|
}));
|
|
|
dispatcher.run();
|
|
|
if(Objects.isNull(userSoundInfoMap.get(phone).getAccessFile())){
|
|
|
@@ -218,21 +228,21 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
|
|
|
//有效音频数量
|
|
|
float validNum = 0;
|
|
|
//音频有效阈值
|
|
|
- float validDuty = 0.7f;
|
|
|
+ float validDuty = 0.5f;
|
|
|
|
|
|
//音准匹配数量
|
|
|
float intonationNum = 0;
|
|
|
//音准匹配误差范围
|
|
|
- float intonationErrRange = 70;
|
|
|
+ float intonationErrRange = 30;
|
|
|
//音准有效阈值
|
|
|
- float intonationValidDuty = 0.7f;
|
|
|
+ float intonationValidDuty = 0.5f;
|
|
|
|
|
|
//节奏匹配数量
|
|
|
float cadenceNum = 0;
|
|
|
//节奏匹配误差范围
|
|
|
- float cadenceErrRange = 130;
|
|
|
+ float cadenceErrRange = 100;
|
|
|
//节奏有效阈值
|
|
|
- float cadenceValidDuty = 0.5f;
|
|
|
+ float cadenceValidDuty = 0.7f;
|
|
|
|
|
|
int totalCompareNum = userSoundInfoMap.get(phone).getMeasureXmlInfoMap().get(measureIndex).size();
|
|
|
|
|
|
@@ -249,6 +259,7 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
|
|
|
//时间范围内匹配次数
|
|
|
float compareNum = 0;
|
|
|
for (MusicPitchDetailDto recordInfo : userSoundInfoMap.get(phone).getRecordMeasurePithInfo()) {
|
|
|
+ LOGGER.info("频率:{}, {}", musicXmlInfo.getFrequency(), recordInfo.getFrequency());
|
|
|
//如果在时间范围之外直接跳过
|
|
|
if(recordInfo.getTimeStamp()<startTimeStamp||recordInfo.getTimeStamp()>endTimeStamp){
|
|
|
continue;
|
|
|
@@ -260,11 +271,11 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
|
|
|
}
|
|
|
recordValidNum++;
|
|
|
//如果频率差值在节奏误差范围内
|
|
|
- if(recordInfo.getFrequency()-musicXmlInfo.getFrequency()<=cadenceErrRange){
|
|
|
+ if(Math.abs(recordInfo.getFrequency()-musicXmlInfo.getFrequency())<=cadenceErrRange){
|
|
|
cadenceValidNum++;
|
|
|
}
|
|
|
//如果频率差值在音准误差范围内
|
|
|
- if(recordInfo.getFrequency()-musicXmlInfo.getFrequency()<=intonationErrRange){
|
|
|
+ if(Math.abs(recordInfo.getFrequency()-musicXmlInfo.getFrequency())<=intonationErrRange){
|
|
|
recordValidIntonationNum++;
|
|
|
}
|
|
|
}
|
|
|
@@ -310,17 +321,33 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
|
|
|
}else{
|
|
|
userSoundInfoMap.get(phone).getUserScoreMap().put("integrity", integrity);
|
|
|
}
|
|
|
+
|
|
|
+ Map<String, BigDecimal> scoreData = new HashMap<>();
|
|
|
+ scoreData.put("intonation", intonation);
|
|
|
+ scoreData.put("cadence", cadence);
|
|
|
+ scoreData.put("integrity", integrity);
|
|
|
+
|
|
|
+ userSoundInfoMap.get(phone).getUserMeasureScoreMap().put(measureIndex, scoreData);
|
|
|
+
|
|
|
WS_CLIENTS.get(phone).sendMessage(new TextMessage(JSON.toJSONString(createPushInfo("measureScore", measureIndex, intonation, cadence, integrity))));
|
|
|
-// LOGGER.info("推送评分结果:{}", phone);
|
|
|
}
|
|
|
|
|
|
private void calTotalScore(String phone) throws IOException {
|
|
|
- int totalCompareNum = userSoundInfoMap.get(phone).getMeasureXmlInfoMap().keySet().size();
|
|
|
- BigDecimal intonation = userSoundInfoMap.get(phone).getUserScoreMap().get("intonation").divide(new BigDecimal(totalCompareNum), 0, BigDecimal.ROUND_DOWN);
|
|
|
- BigDecimal cadence = userSoundInfoMap.get(phone).getUserScoreMap().get("cadence").divide(new BigDecimal(totalCompareNum), 0, BigDecimal.ROUND_DOWN);
|
|
|
- BigDecimal integrity = userSoundInfoMap.get(phone).getUserScoreMap().get("integrity").divide(new BigDecimal(totalCompareNum), 0, BigDecimal.ROUND_DOWN);
|
|
|
+ int totalCompareNum = userSoundInfoMap.get(phone).getMeasureXmlInfoMap().keySet().size()-userSoundInfoMap.get(phone).getMeasureEndTime().keySet().size();
|
|
|
+ BigDecimal intonation = BigDecimal.ZERO;
|
|
|
+ BigDecimal cadence = BigDecimal.ZERO;
|
|
|
+ BigDecimal integrity = BigDecimal.ZERO;
|
|
|
+
|
|
|
+ if(totalCompareNum>0){
|
|
|
+ intonation = userSoundInfoMap.get(phone).getUserScoreMap().get("intonation").divide(new BigDecimal(totalCompareNum), 0, BigDecimal.ROUND_DOWN);
|
|
|
+ cadence = userSoundInfoMap.get(phone).getUserScoreMap().get("cadence").divide(new BigDecimal(totalCompareNum), 0, BigDecimal.ROUND_DOWN);
|
|
|
+ integrity = userSoundInfoMap.get(phone).getUserScoreMap().get("integrity").divide(new BigDecimal(totalCompareNum), 0, BigDecimal.ROUND_DOWN);
|
|
|
+ }
|
|
|
|
|
|
WS_CLIENTS.get(phone).sendMessage(new TextMessage(JSON.toJSONString(createPushInfo("overall", -1, intonation, cadence, integrity))));
|
|
|
+
|
|
|
+ //存储评分数据
|
|
|
+ sysMusicCompareRecordService.saveMusicCompareData(phone, userSoundInfoMap.get(phone).getMusicScoreId(), userSoundInfoMap.get(phone).getUserMeasureScoreMap());
|
|
|
}
|
|
|
|
|
|
private WebSocketInfo createPushInfo(String commond, Integer measureIndex,
|
|
|
@@ -330,13 +357,14 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
|
|
|
header.put("commond", commond);
|
|
|
webSocketInfo.setHeader(header);
|
|
|
Map<String, Object> result = new HashMap<>();
|
|
|
- BigDecimal score = intonation.multiply(new BigDecimal(0.45)).add(cadence.multiply(new BigDecimal(0.45))).add(integrity.multiply(new BigDecimal(0.1))).setScale(0, BigDecimal.ROUND_HALF_UP);
|
|
|
+ BigDecimal score = intonation.multiply(new BigDecimal(0.5)).add(cadence.multiply(new BigDecimal(0.5))).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);
|
|
|
+ LOGGER.info("小节频分:{}", JSON.toJSONString(webSocketInfo));
|
|
|
return webSocketInfo;
|
|
|
}
|
|
|
}
|