liujunchi 3 gadi atpakaļ
vecāks
revīzija
f48731976e

+ 31 - 13
audio-analysis/src/main/java/com/yonge/netty/dto/UserChannelContext.java

@@ -15,6 +15,8 @@ import java.util.stream.Collectors;
 
 import javax.sound.sampled.AudioFormat;
 
+import com.yonge.audio.utils.ArrayUtil;
+import org.apache.commons.lang.ArrayUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -43,6 +45,9 @@ public class UserChannelContext {
 	private final static int MAX_FREQUECY = 2000;
 	
 	private FastYin detector;
+
+	// 	低频
+	private FastYin lowDetector;
 	
 	private String user;
 	
@@ -127,6 +132,7 @@ public class UserChannelContext {
 		hardLevel = HardLevelEnum.valueOf(heardLevel);
 		if(detector == null){
 			detector = new FastYin(sampleRate, bufferSize);
+			lowDetector = new FastYin(sampleRate,bufferSize * 4);
 		}
 	}
 	
@@ -325,28 +331,40 @@ public class UserChannelContext {
 		
 		//YINPitchDetector frequencyDetector = new YINPitchDetector(samples.length , audioFormat.getSampleRate());
 		//int playFrequency = (int) frequencyDetector.getFrequency(samples);
-		
+
+		double durationTime = 1000 * (samples.length * 2) / audioFormat.getSampleRate() / (audioFormat.getSampleSizeInBits() / 8);
+
+		playTime += durationTime;
+
+		// 获取当前音符信息
+		MusicXmlNote musicXmlNote = getCurrentMusicNote(null,null);
+
+		if (musicXmlNote == null) {
+			return;
+		}
 		int playFrequency = -1;
+
 		if(!percussionList.contains(subjectId)){
-			playFrequency = (int)detector.getPitch(samples).getPitch();
-			LOGGER.info("频率 {}",playFrequency);
+			if (musicXmlNote.getFrequency() < MIN_FREQUECY) {
+				float[] floats = ArrayUtils.addAll(samples, samples);
+				float[] lowSamples = ArrayUtils.addAll(floats,floats);
+
+				playFrequency = (int) lowDetector.getPitch(lowSamples).getPitch();
+
+			} else {
+				playFrequency = (int) detector.getPitch(samples).getPitch();
+			}
+			if (playFrequency != -1) {
+				LOGGER.info("频率 {}", playFrequency);
+			}
+
 		}
 		
 		int splDb = (int) Signals.soundPressureLevel(samples);
 		int power = (int) Signals.power(samples);
 		int amplitude = (int) Signals.norm(samples);
 		//float rms = Signals.rms(samples);
-		
-		double durationTime = 1000 * (samples.length * 2) / audioFormat.getSampleRate() / (audioFormat.getSampleSizeInBits() / 8);
-		
-		playTime += durationTime;
-		
-		// 获取当前音符信息
-		MusicXmlNote musicXmlNote = getCurrentMusicNote(null,null);
 
-		if (musicXmlNote == null) {
-			return;
-		}
 		
 		//取出当前处理中的音符信息
 		NoteAnalysis noteAnalysis = getProcessingNote();

+ 1 - 1
audio-analysis/src/main/java/com/yonge/netty/server/service/AudioCompareHandler.java

@@ -84,7 +84,7 @@ public class AudioCompareHandler implements MessageHandler {
 	/**
 	 * @describe 采样大小
 	 */
-	private int bufferSize = 1024 * 2 *2;
+	private int bufferSize = 1024 * 2;
 
 	private boolean signed = true;
 

+ 2 - 3
audio-analysis/src/main/java/com/yonge/netty/server/service/PitchDetectionHandler.java

@@ -75,9 +75,8 @@ public class PitchDetectionHandler implements MessageHandler {
 
 		int playFrequency = (int) frequencyDetector.getFrequency(samples);
 
-		if (playFrequency != 49) {
-			LOGGER.info("校音频率:{}", playFrequency);
-		}
+		LOGGER.info("校音频率:{}", playFrequency);
+
 
 		Map<String, Object> params = new HashMap<String, Object>();
 		params.put("frequency", playFrequency);