|
@@ -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);
|
|
|
}
|
|
|
}
|