Explorar o código

feat:小节评分

Joburgess %!s(int64=4) %!d(string=hai) anos
pai
achega
6e92b6c732

+ 17 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/dto/MusicPitchDetailDto.java

@@ -22,6 +22,9 @@ public class MusicPitchDetailDto {
 
 
     private float avgFrequency;
     private float avgFrequency;
 
 
+    @ApiModelProperty("分贝")
+    private float decibel;
+
     @ApiModelProperty("小节数")
     @ApiModelProperty("小节数")
     private int measureIndex;
     private int measureIndex;
 
 
@@ -31,6 +34,14 @@ public class MusicPitchDetailDto {
     @ApiModelProperty("不需要评分")
     @ApiModelProperty("不需要评分")
     private Boolean dontEvaluating;
     private Boolean dontEvaluating;
 
 
+    public float getDecibel() {
+        return decibel;
+    }
+
+    public void setDecibel(float decibel) {
+        this.decibel = decibel;
+    }
+
     public float getAvgFrequency() {
     public float getAvgFrequency() {
         return avgFrequency;
         return avgFrequency;
     }
     }
@@ -82,6 +93,12 @@ public class MusicPitchDetailDto {
         this.frequency = frequency;
         this.frequency = frequency;
     }
     }
 
 
+    public MusicPitchDetailDto(int timeStamp, float frequency, double decibel) {
+        this.timeStamp = timeStamp;
+        this.frequency = frequency;
+        this.decibel = (float) decibel;
+    }
+
     public int getTimeStamp() {
     public int getTimeStamp() {
         return timeStamp;
         return timeStamp;
     }
     }

+ 45 - 23
mec-biz/src/main/java/com/ym/mec/biz/handler/WebSocketHandler.java

@@ -171,10 +171,10 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
                 cents = PitchConverter.hertzToAbsoluteCent(pitch);
                 cents = PitchConverter.hertzToAbsoluteCent(pitch);
             }
             }
 //            LOGGER.info("时间:{}, 频率:{}, 分贝:{}, 音分:{}", timeStamp, pitch, silenceDetecor.currentSPL(), cents);
 //            LOGGER.info("时间:{}, 频率:{}, 分贝:{}, 音分:{}", timeStamp, pitch, silenceDetecor.currentSPL(), cents);
-//            if (silenceDetecor.currentSPL() <= -60){
+//            if (silenceDetecor.currentSPL() < -66){
 //                pitch = -1;
 //                pitch = -1;
 //            }
 //            }
-            recordInfo.add(new MusicPitchDetailDto(timeStamp, pitch));
+            recordInfo.add(new MusicPitchDetailDto(timeStamp, pitch, silenceDetecor.currentSPL()));
         }));
         }));
         dispatcher.run();
         dispatcher.run();
         if(Objects.isNull(userSoundInfoMap.get(phone).getAccessFile())){
         if(Objects.isNull(userSoundInfoMap.get(phone).getAccessFile())){
@@ -309,13 +309,12 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
             for (int i = 0; i < userSoundInfoMap.get(phone).getMeasureXmlInfoMap().get(measureIndex).size(); i++) {
             for (int i = 0; i < userSoundInfoMap.get(phone).getMeasureXmlInfoMap().get(measureIndex).size(); i++) {
                 MusicPitchDetailDto musicXmlInfo = userSoundInfoMap.get(phone).getMeasureXmlInfoMap().get(measureIndex).get(i);
                 MusicPitchDetailDto musicXmlInfo = userSoundInfoMap.get(phone).getMeasureXmlInfoMap().get(measureIndex).get(i);
 
 
-                int ot5 = (int) (musicXmlInfo.getDuration()*0.05);
-                int halfOt = (int) (musicXmlInfo.getDuration()*0.35);
+                int ot5 = (int) (musicXmlInfo.getDuration()*0.1);
                 int startTimeStamp = musicXmlInfo.getTimeStamp() + userSoundInfoMap.get(phone).getOffsetTime() + ot5;
                 int startTimeStamp = musicXmlInfo.getTimeStamp() + userSoundInfoMap.get(phone).getOffsetTime() + ot5;
                 int endTimeStamp = musicXmlInfo.getTimeStamp()  + userSoundInfoMap.get(phone).getOffsetTime() + musicXmlInfo.getDuration() - ot5;
                 int endTimeStamp = musicXmlInfo.getTimeStamp()  + userSoundInfoMap.get(phone).getOffsetTime() + musicXmlInfo.getDuration() - ot5;
 
 
                 int preMeasureEndTimeStamp = startTimeStamp;
                 int preMeasureEndTimeStamp = startTimeStamp;
-                List<MusicPitchDetailDto> ms = userSoundInfoMap.get(phone).getMusicXmlInfos().stream().filter(m -> m.getMeasureIndex() == musicXmlInfo.getMeasureIndex() - 1).collect(Collectors.toList());
+                List<MusicPitchDetailDto> ms = userSoundInfoMap.get(phone).getMusicXmlInfos().stream().filter(m -> m.getMusicalNotesIndex() == musicXmlInfo.getMusicalNotesIndex() - 1).collect(Collectors.toList());
                 if(!CollectionUtils.isEmpty(ms)){
                 if(!CollectionUtils.isEmpty(ms)){
                     preMeasureEndTimeStamp = ms.get(0).getEndTimeStamp() + userSoundInfoMap.get(phone).getOffsetTime();
                     preMeasureEndTimeStamp = ms.get(0).getEndTimeStamp() + userSoundInfoMap.get(phone).getOffsetTime();
                 }
                 }
@@ -357,7 +356,9 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
                     }
                     }
                 }
                 }
 
 
-                List<Float> musicalNotesPitchs = new ArrayList<>();
+//                List<Float> musicalNotesPitchs = new ArrayList<>();
+//                List<Float> decibels = new ArrayList<>();
+                List<MusicPitchDetailDto> measureSoundPitchInfos = new ArrayList<>();
 
 
                 for (int j = 0; j < userSoundInfoMap.get(phone).getRecordMeasurePithInfo().size(); j++) {
                 for (int j = 0; j < userSoundInfoMap.get(phone).getRecordMeasurePithInfo().size(); j++) {
                     MusicPitchDetailDto recordInfo = userSoundInfoMap.get(phone).getRecordMeasurePithInfo().get(j);
                     MusicPitchDetailDto recordInfo = userSoundInfoMap.get(phone).getRecordMeasurePithInfo().get(j);
@@ -365,7 +366,9 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
                     if(recordInfo.getTimeStamp()<startTimeStamp||recordInfo.getTimeStamp()>endTimeStamp){
                     if(recordInfo.getTimeStamp()<startTimeStamp||recordInfo.getTimeStamp()>endTimeStamp){
                         continue;
                         continue;
                     }
                     }
-                    musicalNotesPitchs.add(recordInfo.getFrequency());
+//                    musicalNotesPitchs.add(recordInfo.getFrequency());
+//                    decibels.add(recordInfo.getDecibel());
+                    measureSoundPitchInfos.add(recordInfo);
                     compareNum++;
                     compareNum++;
                     if(!newMeasure){
                     if(!newMeasure){
                         continue;
                         continue;
@@ -387,28 +390,47 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
 
 
                 //非正常频率次数
                 //非正常频率次数
                 int errPitchNum = 0;
                 int errPitchNum = 0;
+                //分贝变化次数
+                int decibelChangeNum = 0;
 
 
-                if(CollectionUtils.isEmpty(musicalNotesPitchs)){
+                if(CollectionUtils.isEmpty(measureSoundPitchInfos)){
                     userSoundInfoMap.get(phone).getMusicalNotePitchMap().put(musicXmlInfo.getMusicalNotesIndex(), (float) 0);
                     userSoundInfoMap.get(phone).getMusicalNotePitchMap().put(musicXmlInfo.getMusicalNotesIndex(), (float) 0);
                 }else{
                 }else{
-                    Map<Integer, Long> collect = musicalNotesPitchs.stream().map(pitch -> pitch.intValue()).collect(Collectors.groupingBy(Integer::intValue, Collectors.counting()));
+                    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();
                     Integer pitch = collect.entrySet().stream().max(Comparator.comparing(e -> e.getValue())).get().getKey();
+                    //当前频率
+                    double cf = -1;
+                    //频率持续数量
+                    int fnum = 0;
+                    //是否演奏中
                     boolean ing = false;
                     boolean ing = false;
+                    //当前分贝
+                    double cd = 0;
+                    //分贝持续数量
                     int dnum = 0;
                     int dnum = 0;
-                    for (Float musicalNotesPitch : musicalNotesPitchs) {
-                        if (Math.abs(musicalNotesPitch - pitch) <= 10){
-                            dnum = 0;
-                            ing = true;
-                            continue;
+                    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 - pitch) > 10){
-                            dnum++;
+                        //计算声音大小断层册数
+                        if(ing && Math.abs(musicalNotesPitch.getDecibel() - cd) > 5){
+                            dnum ++;
                         }
                         }
-                        if (dnum>1){
+                        if (dnum > 2){
+                            cd = musicalNotesPitch.getDecibel();
                             dnum = 0;
                             dnum = 0;
-                            errPitchNum++;
-                            ing  = false;
+                            decibelChangeNum++;
                         }
                         }
                     }
                     }
                     userSoundInfoMap.get(phone).getMusicalNotePitchMap().put(musicXmlInfo.getMusicalNotesIndex(), (float) pitch);
                     userSoundInfoMap.get(phone).getMusicalNotePitchMap().put(musicXmlInfo.getMusicalNotesIndex(), (float) pitch);
@@ -416,8 +438,8 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
 
 
                 //有效节奏占比
                 //有效节奏占比
                 float cadenceDuty = cadenceValidNum/compareNum;
                 float cadenceDuty = cadenceValidNum/compareNum;
-                //如果错误频率达到一定数值,则当前小节无效
-                if(errPitchNum>=2){
+                //如果频率出现断层或这个音量出现断层,则当前音符节奏无效
+                if(errPitchNum>=2 || decibelChangeNum>1){
                     cadenceDuty = 0;
                     cadenceDuty = 0;
                 }
                 }
                 //节奏
                 //节奏
@@ -425,8 +447,8 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
                     cadenceNum++;
                     cadenceNum++;
                 }
                 }
                 //音准
                 //音准
-                if (!CollectionUtils.isEmpty(musicalNotesPitchs)){
-                    Double avgPitch = musicalNotesPitchs.stream().filter(pitch -> Math.abs((pitch-musicXmlInfo.getFrequency()))<5).collect(Collectors.averagingDouble(pitch -> pitch));
+                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;
                     double recordCents = 0;
                     if (avgPitch > 0){
                     if (avgPitch > 0){