浏览代码

feat:小节评分

Joburgess 4 年之前
父节点
当前提交
d0b788beaf

+ 6 - 2
mec-biz/src/main/java/com/ym/mec/biz/dal/config/NioAudioInputStream.java

@@ -3,6 +3,7 @@ package com.ym.mec.biz.dal.config;
 import be.tarsos.dsp.io.TarsosDSPAudioFormat;
 import be.tarsos.dsp.io.TarsosDSPAudioInputStream;
 
+import javax.sound.sampled.AudioFormat;
 import java.io.IOException;
 import java.io.RandomAccessFile;
 
@@ -13,9 +14,11 @@ import java.io.RandomAccessFile;
 public class NioAudioInputStream implements TarsosDSPAudioInputStream {
 
     private final RandomAccessFile randomAccessFile;
+    private AudioFormat format;
 
-    public NioAudioInputStream(RandomAccessFile randomAccessFile) {
+    public NioAudioInputStream(RandomAccessFile randomAccessFile, AudioFormat audioFormat) {
         this.randomAccessFile = randomAccessFile;
+        this.format = audioFormat;
     }
 
     @Override
@@ -35,7 +38,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;

+ 29 - 2
mec-biz/src/main/java/com/ym/mec/biz/dal/dto/SoundCompareHelper.java

@@ -1,17 +1,25 @@
 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.handler.WebSocketHandler;
 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 Integer musicScoreId;
@@ -156,4 +164,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(pitch>0 && offsetTime == -1){
+            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()< WebSocketHandler.soundCompareConfig.validDb){
+            pitch = -1;
+        }
+//            LOGGER.info("时间:{}, 频率:{}, 分贝:{}", timeStamp, pitch, silenceDetecor.currentSPL());
+        recordMeasurePithInfo.add(new MusicPitchDetailDto(timeStamp, pitch, silenceDetector.currentSPL()));
+    }
 }

+ 3 - 14
mec-biz/src/main/java/com/ym/mec/biz/handler/WebSocketHandler.java

@@ -55,7 +55,7 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
     /**
      * @describe 音频处理参数
      */
-    private SoundCompareConfig soundCompareConfig = new SoundCompareConfig();
+    public static final SoundCompareConfig soundCompareConfig = new SoundCompareConfig();
 
     @Autowired
     private SysMusicCompareRecordService sysMusicCompareRecordService;
@@ -110,6 +110,7 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
                     musicPitchDetailDto.setDontEvaluating(dc == userMeasureXmlInfoEntry.getValue().size());
                     userSoundInfoMap.get(phone).getMeasureEndTime().put(userMeasureXmlInfoEntry.getKey(), musicPitchDetailDto);
                 }
+
                 break;
             case SoundSocketService.RECORD_START:
                 if(!userSoundInfoMap.containsKey(phone)){
@@ -161,19 +162,7 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
 
         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.addAudioProcessor(new PitchProcessor(soundCompareConfig.algo, soundCompareConfig.simpleRate, soundCompareConfig.simpleSize, userSoundInfoMap.get(phone)));
         dispatcher.run();
         if(Objects.isNull(userSoundInfoMap.get(phone).getAccessFile())){
             return;