yonge 3 years ago
parent
commit
80e1798308

+ 29 - 0
audio-analysis/src/main/java/com/yonge/netty/dto/NoteFrequencyRange.java

@@ -1,6 +1,8 @@
 package com.yonge.netty.dto;
 
 import java.math.BigDecimal;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * 一个音符的频率范围,包含最大值和最小值
@@ -70,9 +72,36 @@ public class NoteFrequencyRange {
 		return false;
 	}
 	
+	@Override
+	public int hashCode() {
+		int result = 17;
+		result = 31 * result + Double.hashCode(minFrequency);
+		result = 31 * result + Double.hashCode(maxFrequency);
+		return result;
+	}
+
 	public static void main(String[] args) {
+		
+		Map<NoteFrequencyRange,Integer> map = new HashMap<NoteFrequencyRange, Integer>();
+		
 		NoteFrequencyRange nfr = new NoteFrequencyRange(442,442);
 		System.out.println(nfr.getMinFrequency() + "-"+ nfr.getMaxFrequency());
+		
+		if(map.containsKey(nfr) == false){
+			map.put(nfr, 1);
+		}
+
+		NoteFrequencyRange nfr1 = new NoteFrequencyRange(442,430);
+		System.out.println(nfr1.getMinFrequency() + "-"+ nfr1.getMaxFrequency());
+		
+		if(map.containsKey(nfr1) == false){
+			map.put(nfr1, 1);
+		}else{
+			map.put(nfr, 2);
+		}
+		
+		System.out.println(nfr.equals(nfr1));
+		System.out.println(map.size());
 	}
 
 }

+ 44 - 2
audio-analysis/src/main/java/com/yonge/netty/dto/UserChannelContext.java

@@ -7,6 +7,7 @@ import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Optional;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -727,9 +728,48 @@ public class UserChannelContext {
 		NoteFrequencyRange noteFrequencyRange = null;
 		ChunkAnalysis chunkAnalysis = null;
 		boolean tempo = false;
-		boolean isContinue = true;
-		int unplayedSize = 0;
+		//boolean isContinue = true;
+		//int unplayedSize = 0;
 		int firstPeakIndex = -1;
+		
+		//将信号分堆归类
+		Map<NoteFrequencyRange, Integer> signalGrouping = new HashMap<NoteFrequencyRange, Integer>();
+		
+		for (int i = 0; i < chunkList.size(); i++) {
+			chunkAnalysis = chunkList.get(i);
+			if (chunkAnalysis != null) {
+				if (chunkAnalysis.getFrequency() > MIN_FREQUECY || firstPeakIndex > -1) {
+					
+					if (firstPeakIndex == -1) {
+						firstPeakIndex = i;
+					}
+					
+					noteFrequencyRange = new NoteFrequencyRange(standardFrequecy, chunkAnalysis.getFrequency());
+					
+					if (signalGrouping.containsKey(noteFrequencyRange)) {
+						signalGrouping.put(noteFrequencyRange, signalGrouping.get(noteFrequencyRange) + 1);
+					} else {
+						signalGrouping.put(noteFrequencyRange, 1);
+					}
+				}
+			}
+		}
+		
+		Integer maxTimes = 0, totalTimes = 0;
+		
+		for (Entry<NoteFrequencyRange, Integer> entry : signalGrouping.entrySet()) {
+			if (entry.getValue() > maxTimes) {
+				maxTimes = entry.getValue();
+			}
+			totalTimes = totalTimes + entry.getValue();
+		}
+		
+		if (maxTimes / totalTimes < hardLevel.getIntegrityRange()) {
+			tempo = false;
+			LOGGER.debug("节奏错误原因:不是同一个音");
+		}
+		
+		/**
 		for (int i = 0; i < chunkList.size(); i++) {
 			chunkAnalysis = chunkList.get(i);
 			if (chunkAnalysis != null) {
@@ -766,6 +806,7 @@ public class UserChannelContext {
 				}
 			}
 		}
+		*/
 		
 		if (tempo) {
 			// 判断进入时间点
@@ -939,6 +980,7 @@ public class UserChannelContext {
 		int standardPitch = 440; // a is 440 hz...
 		for (int x = 0; x < midi.length; ++x)
 		{
+			//转调
 		   midi[x] = new BigDecimal(standardPitch).multiply(new BigDecimal(Math.pow(2, new BigDecimal(x-69).divide(new BigDecimal(12),6,BigDecimal.ROUND_HALF_UP).doubleValue()))).doubleValue();
 		   System.out.println("x=" + x +"  "+ midi[x]);
 		}