瀏覽代碼

feat:智能打分

Joburgess 4 年之前
父節點
當前提交
649d9c7c2d

+ 2 - 1
mec-biz/src/main/java/com/ym/mec/biz/service/SoundService.java

@@ -1,5 +1,6 @@
 package com.ym.mec.biz.service;
 
+import com.ym.mec.common.entity.HttpResponseResult;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -9,6 +10,6 @@ import org.springframework.web.multipart.MultipartFile;
  **/
 public interface SoundService {
 
-    void compare(MultipartFile record, Integer musicScoreId);
+    HttpResponseResult compare(MultipartFile record, Integer musicScoreId);
 
 }

+ 37 - 14
mec-biz/src/main/java/com/ym/mec/biz/service/impl/SoundServiceImpl.java

@@ -15,6 +15,9 @@ import com.alibaba.fastjson.JSON;
 import com.ym.mec.biz.dal.dao.SysMusicScoreDao;
 import com.ym.mec.biz.dal.entity.SysMusicScore;
 import com.ym.mec.biz.service.SoundService;
+import com.ym.mec.common.constant.CommonConstants;
+import com.ym.mec.common.controller.BaseController;
+import com.ym.mec.common.entity.HttpResponseResult;
 import com.ym.mec.common.exception.BizException;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -30,12 +33,11 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.math.BigDecimal;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Objects;
+import java.util.*;
 
 /**
  * @Author Joburgess
@@ -84,7 +86,7 @@ public class SoundServiceImpl implements SoundService {
      * @param bytes: 文件字节
      * @return java.util.List<java.lang.Double>
      */
-    private List<Double> soundPressureLevelExtractor(byte[] bytes, String url, Double duration) throws UnsupportedAudioFileException, IOException {
+    private List<Double> soundPressureLevelExtractor(byte[] bytes, String url, BigDecimal duration) throws UnsupportedAudioFileException, IOException {
         List<Double> pitchs = new ArrayList<>();
         int size = 2048;
         int overlap = 0;
@@ -106,7 +108,7 @@ public class SoundServiceImpl implements SoundService {
             }
         });
         dispatcher.run();
-        duration = t[0];
+        duration = new BigDecimal(t[0]);
         return pitchs;
     }
 
@@ -125,11 +127,17 @@ public class SoundServiceImpl implements SoundService {
     }
 
     @Override
-    public void compare(MultipartFile record, Integer musicScoreId) {
+    public HttpResponseResult compare(MultipartFile record, Integer musicScoreId) {
         SysMusicScore sysMusicScore = sysMusicScoreDao.get(musicScoreId);
         if(Objects.isNull(sysMusicScore)|| StringUtils.isBlank(sysMusicScore.getUrl())){
             throw new BizException("伴奏信息错误");
         }
+        Map<String, BigDecimal> result = new HashMap<>();
+
+        BigDecimal score = BigDecimal.ZERO;
+        BigDecimal intonation = BigDecimal.ZERO;
+        BigDecimal cadence = BigDecimal.ZERO;
+        BigDecimal integrity = BigDecimal.ZERO;
 
         try {
             URL url = new URL(sysMusicScore.getUrl());
@@ -137,10 +145,10 @@ public class SoundServiceImpl implements SoundService {
             File f = new File(filePath);
             FileUtils.copyURLToFile(url, f);
 
-//            AudioSystem.getAudioFileFormat(loadedFile);
+            BigDecimal oneHandred = new BigDecimal(100);
 
-            double l_s = 0;
-            double l_r = 0;
+            BigDecimal l_s = new BigDecimal(0);
+            BigDecimal l_r = new BigDecimal(0);
 
             //相似度
             List<Double> pitchs_s = soundPressureLevelExtractor(null, filePath, l_s);
@@ -163,8 +171,8 @@ public class SoundServiceImpl implements SoundService {
                 allPitchGap+=pitchGap;
                 pitchSize+=pitch1;
             }
-            Double intonation = 1-(allPitchGap/pitchSize);
-            System.out.printf("音准:%.2f \r\n", intonation);
+            Double intonation_d = 1-(allPitchGap/pitchSize);
+            intonation = new BigDecimal(intonation_d).multiply(oneHandred).setScale(0, BigDecimal.ROUND_HALF_UP);
 
             //节奏
             List<Double> times_s = beatExtractor(null, filePath);
@@ -178,14 +186,29 @@ public class SoundServiceImpl implements SoundService {
                     }
                 }
             }
-            Double cadence = Double.valueOf(sameTimes/times_s.size());
+            Double cadence_d = Double.valueOf(sameTimes/times_s.size());
+            cadence = new BigDecimal(cadence_d).multiply(oneHandred).setScale(0, BigDecimal.ROUND_HALF_UP);
             System.out.printf("节奏:%.2f", cadence);
+
+            integrity = new BigDecimal(1);
+            if(l_r.compareTo(l_s)<0){
+                integrity = l_r.divide(l_s, 0, BigDecimal.ROUND_HALF_UP);
+            }
+
+            score = intonation.add(cadence).add(integrity).divide(new BigDecimal(3), 0, BigDecimal.ROUND_HALF_UP);
+
+
         } catch (UnsupportedAudioFileException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
-        }finally {
-            return;
         }
+
+
+        result.put("score", score);
+        result.put("intonation", intonation);
+        result.put("cadence", cadence);
+        result.put("integrity", integrity);
+        return BaseController.succeed(result);
     }
 }

+ 1 - 32
mec-teacher/src/main/java/com/ym/mec/teacher/controller/SoundController.java

@@ -1,14 +1,5 @@
 package com.ym.mec.teacher.controller;
 
-import be.tarsos.dsp.AudioDispatcher;
-import be.tarsos.dsp.AudioEvent;
-import be.tarsos.dsp.AudioProcessor;
-import be.tarsos.dsp.SilenceDetector;
-import be.tarsos.dsp.beatroot.BeatRootOnsetEventHandler;
-import be.tarsos.dsp.io.jvm.AudioDispatcherFactory;
-import be.tarsos.dsp.onsets.ComplexOnsetDetector;
-import be.tarsos.dsp.onsets.OnsetHandler;
-import com.netflix.discovery.converters.Auto;
 import com.ym.mec.biz.service.SoundService;
 import com.ym.mec.common.controller.BaseController;
 import com.ym.mec.common.entity.HttpResponseResult;
@@ -23,16 +14,6 @@ import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 
-import javax.servlet.http.HttpServletRequest;
-import javax.sound.sampled.UnsupportedAudioFileException;
-import java.io.File;
-import java.io.IOException;
-import java.text.DecimalFormat;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
 /**
  * @Author Joburgess
  * @Date 2021/5/19 0019
@@ -50,20 +31,8 @@ public class SoundController extends BaseController {
     @ApiOperation(value = "评分")
     @PostMapping("compare")
     public HttpResponseResult compare(@RequestParam("record") MultipartFile record, Integer musicScoreId){
-        Map<String, String> result = new HashMap<>();
-
         LOGGER.info("文件名:{},伴奏编号:{}", record.getOriginalFilename(), musicScoreId);
-
-        DecimalFormat df   = new DecimalFormat("######0.00");
-
-        result.put("score", df.format(Math.random()*100));
-        result.put("intonation", df.format(Math.random()*100));
-        result.put("cadence", df.format(Math.random()*100));
-        result.put("integrity", df.format(Math.random()*100));
-
-        soundService.compare(record, musicScoreId);
-
-        return succeed(result);
+        return soundService.compare(record, musicScoreId);
     }
 
 }