Browse Source

feat:小节评分

Joburgess 4 năm trước cách đây
mục cha
commit
19432d133d

+ 46 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/config/NioAudioInputStream.java

@@ -0,0 +1,46 @@
+package com.ym.mec.biz.dal.config;
+
+import be.tarsos.dsp.io.TarsosDSPAudioFormat;
+import be.tarsos.dsp.io.TarsosDSPAudioInputStream;
+
+import java.io.IOException;
+import java.io.RandomAccessFile;
+
+/**
+ * @Author Joburgess
+ * @Date 2021/8/4 0004
+ */
+public class NioAudioInputStream implements TarsosDSPAudioInputStream {
+
+    private final RandomAccessFile randomAccessFile;
+
+    public NioAudioInputStream(RandomAccessFile randomAccessFile) {
+        this.randomAccessFile = randomAccessFile;
+    }
+
+    @Override
+    public long skip(long bytesToSkip) throws IOException {
+        return randomAccessFile.skipBytes((int) bytesToSkip);
+    }
+
+    @Override
+    public int read(byte[] b, int off, int len) throws IOException {
+        return randomAccessFile.read(b, off, len);
+    }
+
+    @Override
+    public void close() throws IOException {
+        randomAccessFile.close();
+    }
+
+    @Override
+    public TarsosDSPAudioFormat getFormat() {
+        TarsosDSPAudioFormat tarsosDSPFormat = new TarsosDSPAudioFormat(44100, 16, 1, false, true);
+        return tarsosDSPFormat;
+    }
+
+    @Override
+    public long getFrameLength() {
+        return 0;
+    }
+}

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

@@ -32,10 +32,6 @@ public class SoundCompareConfig {
      * @describe 音频解析算法
      */
     public PitchProcessor.PitchEstimationAlgorithm algo = PitchProcessor.PitchEstimationAlgorithm.FFT_YIN;
-    /**
-     * @describe 分贝检测器
-     */
-    public SilenceDetector silenceDetector = new SilenceDetector();
 
     /**
      * @describe 有效分贝大小

+ 5 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/dto/SoundCompareHelper.java

@@ -1,5 +1,6 @@
 package com.ym.mec.biz.dal.dto;
 
+import be.tarsos.dsp.SilenceDetector;
 import io.swagger.annotations.ApiModelProperty;
 
 import java.io.RandomAccessFile;
@@ -47,6 +48,10 @@ public class SoundCompareHelper {
     private List<MusicPitchDetailDto> musicXmlInfos;
 
     private byte[] preDataArray = new byte[0];
+    /**
+     * @describe 分贝检测器
+     */
+    public SilenceDetector silenceDetector = new SilenceDetector();
 
     public List<MusicPitchDetailDto> getMusicXmlInfos() {
         return musicXmlInfos;

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

@@ -160,7 +160,7 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
         }
 
         AudioDispatcher dispatcher = AudioDispatcherFactory.fromByteArray(message.getPayload().array(), soundCompareConfig.audioFormat, soundCompareConfig.simpleSize, soundCompareConfig.overlap);
-        dispatcher.addAudioProcessor(soundCompareConfig.silenceDetector);
+        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();
@@ -168,11 +168,11 @@ public class WebSocketHandler extends AbstractWebSocketHandler {
                 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(soundCompareConfig.silenceDetector.currentSPL()<soundCompareConfig.validDb){
+            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, soundCompareConfig.silenceDetector.currentSPL()));
+            userSoundInfoMap.get(phone).getRecordMeasurePithInfo().add(new MusicPitchDetailDto(timeStamp, pitch, userSoundInfoMap.get(phone).silenceDetector.currentSPL()));
         }));
         dispatcher.run();
         if(Objects.isNull(userSoundInfoMap.get(phone).getAccessFile())){