|
@@ -0,0 +1,77 @@
|
|
|
+package com.ym.mec.biz.service.impl;
|
|
|
+
|
|
|
+import be.tarsos.dsp.AudioDispatcher;
|
|
|
+import be.tarsos.dsp.AudioEvent;
|
|
|
+import be.tarsos.dsp.io.jvm.AudioDispatcherFactory;
|
|
|
+import be.tarsos.dsp.pitch.PitchDetectionHandler;
|
|
|
+import be.tarsos.dsp.pitch.PitchDetectionResult;
|
|
|
+import be.tarsos.dsp.pitch.PitchProcessor;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.ym.mec.biz.dal.dto.MusicPitchDetailDto;
|
|
|
+import com.ym.mec.biz.dal.dto.SoundCheckInfoDto;
|
|
|
+import com.ym.mec.biz.dal.dto.WebSocketInfo;
|
|
|
+import com.ym.mec.biz.dal.enums.WebsocketTypeEnum;
|
|
|
+import com.ym.mec.biz.handler.WebSocketHandler;
|
|
|
+import com.ym.mec.biz.service.SoundSocketService;
|
|
|
+import com.ym.mec.biz.service.WebSocketEventHandler;
|
|
|
+import com.ym.mec.common.exception.BizException;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.socket.BinaryMessage;
|
|
|
+import org.springframework.web.socket.TextMessage;
|
|
|
+import org.springframework.web.socket.WebSocketSession;
|
|
|
+
|
|
|
+import javax.sound.sampled.UnsupportedAudioFileException;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Author Joburgess
|
|
|
+ * @Date 2021/8/19 0019
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class SoundCheckHandler implements WebSocketEventHandler {
|
|
|
+
|
|
|
+ private static final double COMPARE_FREQUENCY = 442;
|
|
|
+
|
|
|
+ /** 校音数据 */
|
|
|
+ private Map<String, Integer> userSoundCheckInfo = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ public SoundCheckHandler() {
|
|
|
+ WebSocketHandler.regist(WebsocketTypeEnum.SOUND_CHECK, this);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void afterConnectionEstablished(WebSocketSession session, String phone) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void receiveTextMessage(WebSocketSession session, String phone, TextMessage message) {
|
|
|
+ userSoundCheckInfo.put(phone, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void receiveBinaryMessage(WebSocketSession session, String phone, BinaryMessage message) {
|
|
|
+ if(!userSoundCheckInfo.containsKey(phone)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ AudioDispatcher dispatcher = AudioDispatcherFactory.fromByteArray(message.getPayload().array(), SoundCompareHandler.soundCompareConfig.audioFormat, SoundCompareHandler.soundCompareConfig.simpleSize, SoundCompareHandler.soundCompareConfig.overlap);
|
|
|
+ dispatcher.addAudioProcessor(new PitchProcessor(SoundCompareHandler.soundCompareConfig.algo, SoundCompareHandler.soundCompareConfig.simpleRate, SoundCompareHandler.soundCompareConfig.simpleSize, (pitchDetectionResult, audioEvent) -> {
|
|
|
+
|
|
|
+ }));
|
|
|
+ dispatcher.run();
|
|
|
+ } catch (UnsupportedAudioFileException e) {
|
|
|
+ throw new BizException("{}校音异常:{}", phone, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void afterConnectionClosed(WebSocketSession session, String phone) {
|
|
|
+
|
|
|
+ }
|
|
|
+}
|