Преглед изворни кода

feat:云教练音准与节奏计算逻辑调整

Joburgess пре 3 година
родитељ
комит
4ac114d914

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

@@ -17,7 +17,7 @@ public class SoundCompareConfig {
     /**
      * @describe 采样大小
      */
-    public int simpleSize = 512;
+    public int simpleSize = 1024;
     /**
      * @describe 帧覆盖大小
      */

+ 46 - 52
mec-biz/src/main/java/com/ym/mec/biz/dal/dto/SoundCompareHelper.java

@@ -83,6 +83,10 @@ public class SoundCompareHelper implements PitchDetectionHandler {
 
     private List<MusicPitchDetailDto> musicXmlInfos;
 
+    private long firstMeasureStartBytes = 0;
+
+    private long recordBytes = 0;
+
     private String clientId;
 
     private Integer detailId;
@@ -281,6 +285,22 @@ public class SoundCompareHelper implements PitchDetectionHandler {
         this.userScoreMap = userScoreMap;
     }
 
+    public long getFirstMeasureStartBytes() {
+        return firstMeasureStartBytes;
+    }
+
+    public void setFirstMeasureStartBytes(long firstMeasureStartBytes) {
+        this.firstMeasureStartBytes = firstMeasureStartBytes;
+    }
+
+    public long getRecordBytes() {
+        return recordBytes;
+    }
+
+    public void setRecordBytes(long recordBytes) {
+        this.recordBytes = recordBytes;
+    }
+
     public Map<Integer, Map<String, Object>> getUserMeasureScoreMap() {
         return userMeasureScoreMap;
     }
@@ -302,23 +322,6 @@ public class SoundCompareHelper implements PitchDetectionHandler {
         int timeStamp = (int) (measureStartTime + audioEvent.getTimeStamp()*1000);
         float pitch = pitchDetectionResult.getPitch();
         int decibel = (int) (100 - Math.abs(silenceDetector.currentSPL()));
-        //初始化偏移时间
-        if(offsetTime == -1 && decibel>SoundCompareHandler.soundCompareConfig.validDb){
-            offsetTime = timeStamp;
-            for (MusicPitchDetailDto musicXmlInfo : musicXmlInfos) {
-                if(!musicXmlInfo.getDontEvaluating()){
-                    if(offsetTime > musicXmlInfo.getTimeStamp()) {
-                        offsetTime = offsetTime - musicXmlInfo.getTimeStamp();
-                    }
-                    break;
-                }
-            }
-            musicXmlInfos.forEach(e->e.setTimeStamp(e.getTimeStamp()+offsetTime));
-            for (Map.Entry<Integer, MusicPitchDetailDto> musicPitchDetailDtoEntry : measureEndTime.entrySet()) {
-                musicPitchDetailDtoEntry.getValue().setTimeStamp(musicPitchDetailDtoEntry.getValue().getTimeStamp() + offsetTime);
-                musicPitchDetailDtoEntry.getValue().setEndTimeStamp(musicPitchDetailDtoEntry.getValue().getEndTimeStamp() + offsetTime);
-            }
-        }
 
         if(decibel <= SoundCompareHandler.soundCompareConfig.validDb){
             pitch = -1;
@@ -327,47 +330,38 @@ public class SoundCompareHelper implements PitchDetectionHandler {
 
         SoundCompareHandler.LOGGER.info("时间:{}, 频率:{}, 分贝:{}", timeStamp, pitch, decibel);
 
-//        if(pitch==-1){
-//            decibel = SoundCompareHandler.soundCompareConfig.validDb;
-//        }
-//        recordMeasurePithInfo.add(new MusicPitchDetailDto(timeStamp, pitch, silenceDetector.currentSPL()));
+        if(currPitchInfos.size()>0&&(Math.abs(currPitchInfos.get(currPitchInfos.size()-1).getFrequency()-pitch)>10||Math.abs(currPitchInfos.get(currPitchInfos.size()-1).getDecibel()-decibel)>10)){
+            Double avgPitch = currPitchInfos.stream().skip(1).collect(Collectors.averagingDouble(MusicPitchDetailDto::getFrequency));
+            Double avgDb = currPitchInfos.stream().skip(1).collect(Collectors.averagingDouble(MusicPitchDetailDto::getDecibel));
 
-        Double avgPitch = currPitchInfos.stream().skip(1).collect(Collectors.averagingDouble(MusicPitchDetailDto::getFrequency));
-        Double avgDb = currPitchInfos.stream().skip(1).collect(Collectors.averagingDouble(MusicPitchDetailDto::getDecibel));
+            MusicPitchDetailDto measureDetail = new MusicPitchDetailDto(currPitchInfos.get(0).getTimeStamp(), avgPitch.floatValue(), avgDb);
+            measureDetail.setEndTimeStamp(timeStamp);
+            measureDetail.setDuration(measureDetail.getEndTimeStamp()-measureDetail.getTimeStamp());
 
-        long count = currPitchInfos.stream().filter(p -> p.getFrequency() < 0).count();
-        if(currPitchInfos.size()>0&&count/currPitchInfos.size()<0.5){
-            avgPitch = currPitchInfos.stream().skip(1).filter(p->p.getFrequency()>0).collect(Collectors.averagingDouble(MusicPitchDetailDto::getFrequency));
-        }
-        if(Math.abs(avgPitch-pitch)>10||Math.abs(decibel-avgDb)>5){
-            obviousChangeNum++;
-        }else{
-            currTmpPitchInfos.clear();
-            obviousChangeNum=0;
-        }
-
-        if(obviousChangeNum>1){
-//            SoundCompareHandler.LOGGER.info("----------------------------{}---{}----{}", avgPitch, avgDb, recordMeasurePitchInfos.size());
-//            SoundCompareHandler.LOGGER.info("----------------------------{}", JSON.toJSONString(currPitchInfos.stream().map(MusicPitchDetailDto::getFrequency).collect(Collectors.toList())));
-//            SoundCompareHandler.LOGGER.info("----------------------------{}", JSON.toJSONString(currTmpPitchInfos.stream().map(MusicPitchDetailDto::getFrequency).collect(Collectors.toList())));
-
-            if(!CollectionUtils.isEmpty(currPitchInfos)&&(avgPitch>0||avgDb>SoundCompareHandler.soundCompareConfig.validDb)){
-                MusicPitchDetailDto measureDetail = new MusicPitchDetailDto(currPitchInfos.get(0).getTimeStamp(), avgPitch.floatValue(), avgDb);
-                measureDetail.setEndTimeStamp(currTmpPitchInfos.get(0).getTimeStamp());
-                measureDetail.setDuration(measureDetail.getEndTimeStamp()-measureDetail.getTimeStamp());
+            if(measureDetail.getDuration()>25&&(avgPitch>0||avgDb>SoundCompareHandler.soundCompareConfig.validDb)){
                 recordMeasurePitchInfos.add(measureDetail);
             }
 
             currPitchInfos.clear();
-            currPitchInfos.addAll(currTmpPitchInfos);
-            currTmpPitchInfos.clear();
-            obviousChangeNum = 0;
-        }
-        if(obviousChangeNum>0){
-            currTmpPitchInfos.add(new MusicPitchDetailDto(timeStamp, pitch, decibel));
-        }else{
-            currPitchInfos.add(new MusicPitchDetailDto(timeStamp, pitch, decibel));
-        }
 
+            //初始化偏移时间
+            if(offsetTime == -1 && recordMeasurePitchInfos.size() == 1){
+                offsetTime = recordMeasurePitchInfos.get(0).getTimeStamp();
+                for (MusicPitchDetailDto musicXmlInfo : musicXmlInfos) {
+                    if(!musicXmlInfo.getDontEvaluating()){
+                        if(offsetTime > musicXmlInfo.getTimeStamp()) {
+                            offsetTime = offsetTime - musicXmlInfo.getTimeStamp();
+                        }
+                        break;
+                    }
+                }
+                musicXmlInfos.forEach(e->e.setTimeStamp(e.getTimeStamp()+offsetTime));
+                for (Map.Entry<Integer, MusicPitchDetailDto> musicPitchDetailDtoEntry : measureEndTime.entrySet()) {
+                    musicPitchDetailDtoEntry.getValue().setTimeStamp(musicPitchDetailDtoEntry.getValue().getTimeStamp() + offsetTime);
+                    musicPitchDetailDtoEntry.getValue().setEndTimeStamp(musicPitchDetailDtoEntry.getValue().getEndTimeStamp() + offsetTime);
+                }
+            }
+        }
+        currPitchInfos.add(new MusicPitchDetailDto(timeStamp, pitch, decibel));
     }
 }

+ 17 - 14
mec-biz/src/main/java/com/ym/mec/biz/service/impl/SoundCompareHandler.java

@@ -96,6 +96,7 @@ public class SoundCompareHandler implements WebSocketEventHandler {
                 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).setFirstMeasureStartBytes((long) (0*(soundCompareConfig.audioFormat.getFrameSize()*soundCompareConfig.audioFormat.getFrameRate())));
                 userSoundInfoMap.get(phone).setMusicXmlInfos(musicXmlInfos);
                 musicXmlInfos = musicXmlInfos.stream().filter(m->!m.getDontEvaluating()).collect(Collectors.toList());
                 userSoundInfoMap.get(phone).setMusicScoreId(bodyObject.getInteger("id"));
@@ -208,22 +209,25 @@ public class SoundCompareHandler implements WebSocketEventHandler {
         if(!userSoundInfoMap.containsKey(phone)){
             return;
         }
+        userSoundInfoMap.get(phone).setRecordBytes(userSoundInfoMap.get(phone).getRecordBytes()+message.getPayloadLength());
+        if(userSoundInfoMap.get(phone).getRecordBytes()<userSoundInfoMap.get(phone).getFirstMeasureStartBytes()){
+            return;
+        }
         try {
             if(Objects.nonNull(userSoundInfoMap.get(phone).getAccessFile())){
                 userSoundInfoMap.get(phone).getAccessFile().write(message.getPayload().array());
+            }else{
+                return;
             }
 
             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()+100)){
                     if(userMeasureEndTimeMapEntry.getValue().getDontEvaluating()){
@@ -352,7 +356,7 @@ public class SoundCompareHandler implements WebSocketEventHandler {
                 int startTimeStamp = musicXmlInfo.getTimeStamp();
                 int endTimeStamp = musicXmlInfo.getTimeStamp() + musicXmlInfo.getDuration();
 
-                int ot5 = (int) (musicXmlInfo.getDuration()*0.23);
+                int ot5 = (int) (musicXmlInfo.getDuration()*0.22<70?70:musicXmlInfo.getDuration()*0.22);
 
                 List<MusicPitchDetailDto> recordPitchs = userSoundInfoMap.get(phone).getRecordMeasurePitchInfos().stream().filter(m -> m.getTimeStamp()>=startTimeStamp-ot5 && m.getTimeStamp() < endTimeStamp-ot5).collect(Collectors.toList());
 
@@ -392,12 +396,15 @@ public class SoundCompareHandler implements WebSocketEventHandler {
 
                     score = Math.pow(score/100f, userSoundInfoMap.get(phone).getHeardLevel().getIntonationCentsRange())*100;
 
+                    if(Objects.nonNull(userSoundInfoMap.get(phone).getSubjectId())&&userSoundInfoMap.get(phone).getSubjectId()==23){
+                        score = 100;
+                        integrityDuty = 1;
+                    }
+
                     intonationScore += score;
                     musicXmlInfo.setAvgFrequency(avgPitch);
                     intonationRight = score>70;
-                }
-                //完成度
-                if(cadenceRight){
+
                     integrityScore += integrityDuty;
                     integrityRight = integrityDuty>0.7;
                 }
@@ -505,12 +512,8 @@ public class SoundCompareHandler implements WebSocketEventHandler {
         WebSocketInfo webSocketInfo = new WebSocketInfo();
         webSocketInfo.setHeader(new WebSocketInfo.Head(command));
         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);
-        }
+        BigDecimal 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);