Browse Source

Merge branch 'music_score'

Joburgess 4 years ago
parent
commit
84feb7ec0b
1 changed files with 29 additions and 8 deletions
  1. 29 8
      mec-biz/src/main/java/com/ym/mec/biz/handler/WebSocketHandler.java

+ 29 - 8
mec-biz/src/main/java/com/ym/mec/biz/handler/WebSocketHandler.java

@@ -46,17 +46,14 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
     public static final Map<String, WebSocketSession> WS_CLIENTS = new ConcurrentHashMap<>();
 
     private final BigDecimal oneHundred = new BigDecimal(100);
-
-    //检测偏移时长的最终时间
-    private final int endCheckOffsetTime = 500;
-
-    private final float simpleRate = 44100;
-    private int simpleSize = 512;
+    private final float simpleRate = 44200;
+    private final int simpleSize = 512;
+    private final int overlap = 256;
 
     private final AudioFormat audioFormat = new AudioFormat(simpleRate, 16, 1, true, false);
     private static final PitchProcessor.PitchEstimationAlgorithm algo = PitchProcessor.PitchEstimationAlgorithm.FFT_YIN;
 
-    private static final String tmpDir = FileUtils.getTempDirectoryPath() + "/soundCompare/";
+    private final String tmpDir = FileUtils.getTempDirectoryPath() + "/soundCompare/";
 
     //用户对应评分信息
     private Map<String, SoundCompareHelper> userSoundInfoMap = new ConcurrentHashMap<>();
@@ -160,7 +157,7 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
         }
 
         List<MusicPitchDetailDto> recordInfo = new ArrayList<>();
-        AudioDispatcher dispatcher = AudioDispatcherFactory.fromByteArray(message.getPayload().array(), audioFormat, simpleSize, 256);
+        AudioDispatcher dispatcher = AudioDispatcherFactory.fromByteArray(message.getPayload().array(), audioFormat, simpleSize, overlap);
         dispatcher.addAudioProcessor(new PitchProcessor(algo, simpleRate, simpleSize, (pitchDetectionResult, audioEvent) -> {
             int timeStamp = (int) (userSoundInfoMap.get(phone).getMeasureStartTime() + audioEvent.getTimeStamp()*1000);
             float pitch = pitchDetectionResult.getPitch();
@@ -381,11 +378,31 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
                     }
                 }
 
+                //非正常频率次数
+                int errPitchNum = 0;
+
                 if(CollectionUtils.isEmpty(musicalNotesPitchs)){
                     userSoundInfoMap.get(phone).getMusicalNotePitchMap().put(musicXmlInfo.getMusicalNotesIndex(), (float) 0);
                 }else{
                     Map<Integer, Long> collect = musicalNotesPitchs.stream().collect(Collectors.groupingBy(Integer::intValue, Collectors.counting()));
+                    //出现次数最多的频率
                     Integer pitch = collect.entrySet().stream().max(Comparator.comparing(e -> e.getValue())).get().getKey();
+                    boolean ing = false;
+                    int dnum = 0;
+                    for (Integer musicalNotesPitch : musicalNotesPitchs) {
+                        if (!ing && Math.abs(musicalNotesPitch - pitch) <= 20){
+                            ing = true;
+                            continue;
+                        }
+                        if (ing && Math.abs(musicalNotesPitch - pitch) > 20){
+                            dnum++;
+                        }
+                        if (dnum>=2){
+                            dnum = 0;
+                            errPitchNum++;
+                            ing  = false;
+                        }
+                    }
                     userSoundInfoMap.get(phone).getMusicalNotePitchMap().put(musicXmlInfo.getMusicalNotesIndex(), (float) pitch);
                 }
 
@@ -395,6 +412,10 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
                 float intonationDuty = recordValidIntonationNum/compareNum;
                 //有效节奏占比
                 float cadenceDuty = cadenceValidNum/compareNum;
+                //如果错误频率达到一定数值,则当前小节无效
+                if(errPitchNum>=3){
+                    cadenceDuty = 0;
+                }
                 //节奏
                 if(cadenceDuty>=cadenceValidDuty){
                     cadenceNum++;