yonge 3 лет назад
Родитель
Сommit
ceec23fecb

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

@@ -69,5 +69,10 @@ public class NoteFrequencyRange {
 		}
 		return false;
 	}
+	
+	public static void main(String[] args) {
+		NoteFrequencyRange nfr = new NoteFrequencyRange(442,442);
+		System.out.println(nfr.getMinFrequency() + "-"+ nfr.getMaxFrequency());
+	}
 
 }

+ 34 - 19
audio-analysis/src/main/java/com/yonge/netty/dto/UserChannelContext.java

@@ -38,11 +38,17 @@ public class UserChannelContext {
 	//打击乐
 	private final static List<Integer> percussionList = Arrays.asList(23, 113);
 	
+	private FastYin detector;
+	
+	private float sampleRate;
+	
+	private int bitsPerSample;
+	
 	private String user;
 	
 	private double standardFrequecy = 442;
 	
-	private int offsetMS;
+	private float offsetMS;
 	
 	private double dynamicOffset;
 	
@@ -52,7 +58,7 @@ public class UserChannelContext {
 	
 	private Integer subjectId;
 	
-	private int beatDuration;
+	private float beatDuration;
 	
 	private int beatByteLength;
 	
@@ -77,8 +83,6 @@ public class UserChannelContext {
 	
 	private double playTime;
 	
-	private double receivedTime;
-	
 	private HardLevelEnum hardLevel = HardLevelEnum.ADVANCED;
 	
 	private boolean handlerSwitch;
@@ -118,12 +122,17 @@ public class UserChannelContext {
 		return result;
 	}
 	
-	public void init(String platform, String heardLevel, int subjectId, int beatDuration) {
+	public void init(String platform, String heardLevel, int subjectId, float beatDuration,float sampleRate, int bitsPerSample, int bufferSize) {
 		this.platform = platform;
 		this.subjectId = subjectId;
 		this.beatDuration = beatDuration;
-		this.beatByteLength = WaveformWriter.SAMPLE_RATE * WaveformWriter.BITS_PER_SAMPLE / 8 * beatDuration / 1000;
+		this.beatByteLength = (int) (sampleRate * bitsPerSample / 8 * beatDuration / 1000);
 		hardLevel = HardLevelEnum.valueOf(heardLevel);
+		if(detector == null){
+			detector = new FastYin(sampleRate, bufferSize);
+		}
+		this.sampleRate = sampleRate;
+		this.bitsPerSample = bitsPerSample;
 	}
 	
 	public byte[] skipMetronome(byte[] datas) {
@@ -132,9 +141,9 @@ public class UserChannelContext {
 				beatByteLength -= datas.length;
 				return new byte[0];
 			}
-			if(beatByteLength % 2 != 0){
+			/*if(beatByteLength % 2 != 0){
 				beatByteLength++;
-			}
+			}*/
 			datas = ArrayUtil.extractByte(datas, beatByteLength, datas.length - 1);
 			beatByteLength = 0;
 		}
@@ -161,14 +170,22 @@ public class UserChannelContext {
 		this.handlerSwitch = handlerSwitch;
 	}
 
-	public int getOffsetMS() {
+	public float getOffsetMS() {
 		return offsetMS;
 	}
 
-	public void setOffsetMS(int offsetMS) {
+	public void setOffsetMS(float offsetMS) {
 		this.offsetMS = offsetMS;
 	}
 
+	public float getBeatDuration() {
+		return beatDuration;
+	}
+
+	public void setBeatDuration(float beatDuration) {
+		this.beatDuration = beatDuration;
+	}
+
 	public HardLevelEnum getHardLevel() {
 		return hardLevel;
 	}
@@ -202,7 +219,7 @@ public class UserChannelContext {
 	}
 
 	public void resetUserInfo() {
-		beatByteLength = WaveformWriter.SAMPLE_RATE * WaveformWriter.BITS_PER_SAMPLE / 8 * beatDuration / 1000;
+		beatByteLength = (int) (sampleRate * bitsPerSample / 8 * beatDuration / 1000);
 		waveFileProcessor = null;
 		processingNote = new NoteAnalysis(0,0,-1);
 		evaluatingSectionIndex = new AtomicInteger(0);
@@ -212,7 +229,6 @@ public class UserChannelContext {
 		totalChunkAnalysisList = new ArrayList<ChunkAnalysis>();
 		recordId = null;
 		playTime = 0;
-		receivedTime = 0;
 		delayProcessed = false;
 		dynamicOffset = 0;
 		handlerSwitch = false;
@@ -330,10 +346,11 @@ public class UserChannelContext {
 		//YINPitchDetector frequencyDetector = new YINPitchDetector(samples.length , audioFormat.getSampleRate());
 		//int playFrequency = (int) frequencyDetector.getFrequency(samples);
 		
-		FastYin detector = new FastYin(audioFormat.getSampleRate(), samples.length);
 		int playFrequency = -1;
 		if(!percussionList.contains(subjectId)){
 			playFrequency = (int)detector.getPitch(samples).getPitch();
+		}else{
+			LOGGER.info("subjectId:{}", subjectId);
 		}
 		
 		int splDb = (int) Signals.soundPressureLevel(samples);
@@ -343,12 +360,6 @@ public class UserChannelContext {
 		
 		double durationTime = 1000 * (samples.length * 2) / audioFormat.getSampleRate() / (audioFormat.getSampleSizeInBits() / 8);
 		
-		/*receivedTime += durationTime;
-		
-		if(receivedTime < offsetMS){
-			return;
-		}*/
-		
 		playTime += durationTime;
 		
 		// 获取当前音符信息
@@ -641,6 +652,8 @@ public class UserChannelContext {
 		double endTime = musicXmlNote.getTimeStamp() + dynamicOffset + floatingRange;
 		double startTime = musicXmlNote.getTimeStamp() + dynamicOffset - floatingRange;
 		
+		LOGGER.info("------------floatingRange StartTime:{}  EndTime:{}------------", startTime, endTime);
+		
 		List<ChunkAnalysis> chunkAnalysisList = totalChunkAnalysisList.stream().filter(t -> Double.doubleToLongBits(t.getStartTime()) >= Double.doubleToLongBits(startTime) && Double.doubleToLongBits(t.getEndTime()) <= Double.doubleToLongBits(endTime)).collect(Collectors.toList());
 		
 		double correctedStartTime = queryFirstNoteStartTime(chunkAnalysisList, musicXmlNote);
@@ -648,6 +661,8 @@ public class UserChannelContext {
 		
 		chunkAnalysisList = totalChunkAnalysisList.stream().filter(t -> Double.doubleToLongBits(t.getStartTime()) >= Double.doubleToLongBits(correctedStartTime) && Double.doubleToLongBits(t.getEndTime()) <= Double.doubleToLongBits(correctedEndTime)).collect(Collectors.toList());
 
+		LOGGER.info("------------ correctedStartTime:{}  correctedEndTime:{}------------", correctedStartTime, correctedEndTime);
+		
 		//根据完整度取部分有效信号
 		int elementSize = chunkAnalysisList.size() * hardLevel.getIntegrityRange() / 100;
 		chunkAnalysisList = chunkAnalysisList.subList(0, elementSize);

+ 2 - 2
audio-analysis/src/main/java/com/yonge/netty/entity/MusicXmlBasicInfo.java

@@ -29,7 +29,7 @@ public class MusicXmlBasicInfo {
 
 	private String uuid;
 	
-	private int beatLength;
+	private float beatLength;
 
 	private List<MusicXmlNote> musicXmlInfos = new ArrayList<MusicXmlNote>();
 
@@ -115,7 +115,7 @@ public class MusicXmlBasicInfo {
 		this.uuid = uuid;
 	}
 
-	public int getBeatLength() {
+	public float getBeatLength() {
 		return beatLength;
 	}
 

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

@@ -121,7 +121,8 @@ public class AudioCompareHandler implements MessageHandler {
 			channelContext.setHandlerSwitch(false);
 
 			channelContext.getSongMusicXmlMap().put(musicXmlBasicInfo.getExamSongId(), musicXmlBasicInfo);
-			channelContext.init(musicXmlBasicInfo.getPlatform(), musicXmlBasicInfo.getHeardLevel(), musicXmlBasicInfo.getSubjectId(), musicXmlBasicInfo.getBeatLength());
+			channelContext.init(musicXmlBasicInfo.getPlatform(), musicXmlBasicInfo.getHeardLevel(), musicXmlBasicInfo.getSubjectId(),
+					musicXmlBasicInfo.getBeatLength(), audioFormat.getSampleRate(), audioFormat.getSampleSizeInBits(), bufferSize / 2);
 			channelContext.setUser(user);
 			
 			userChannelContextService.register(channel, channelContext);
@@ -280,11 +281,11 @@ public class AudioCompareHandler implements MessageHandler {
 		}
 		waveFileProcessor.process(datas);
 		
-		datas = channelContext.skipMetronome(datas);
+		/*datas = channelContext.skipMetronome(datas);
 
 		if (datas.length == 0) {
 			return false;
-		}
+		}*/
 
 		channelContext.setChannelBufferBytes(ArrayUtil.mergeByte(channelContext.getChannelBufferBytes(), datas));
 
@@ -294,14 +295,15 @@ public class AudioCompareHandler implements MessageHandler {
 			return false;
 		}
 		
-		if (channelContext.getOffsetMS() > 0) {
-			int beatByteLength = WaveformWriter.SAMPLE_RATE * WaveformWriter.BITS_PER_SAMPLE / 8 * channelContext.getOffsetMS() / 1000;
+		if (channelContext.getOffsetMS() + channelContext.getBeatDuration() > 0) {
+			int beatByteLength = (int) (audioFormat.getSampleRate() * audioFormat.getSampleSizeInBits() / 8 * (channelContext.getOffsetMS() + channelContext.getBeatDuration()) / 1000);
 			
 			if(totalLength > beatByteLength){
 				channelContext.setChannelBufferBytes(ArrayUtil.extractByte(channelContext.getChannelBufferBytes(), beatByteLength, totalLength - 1));
 				channelContext.setOffsetMS(0);
+				channelContext.setBeatDuration(0);
 				
-				LOGGER.info("--------Length:{}  Times:{}--------", waveFileProcessor.getFile().length() - channelContext.getChannelBufferBytes().length,(waveFileProcessor.getFile().length() - channelContext.getChannelBufferBytes().length) * 1000 /WaveformWriter.SAMPLE_RATE/2);
+				LOGGER.info("--------Length:{}  Times:{}--------", waveFileProcessor.getFile().length() - channelContext.getChannelBufferBytes().length,(waveFileProcessor.getFile().length() - channelContext.getChannelBufferBytes().length) * 1000 /audioFormat.getSampleRate()/2);
 			}else{
 				return false;
 			}