|
@@ -10,6 +10,7 @@ import be.tarsos.dsp.util.PitchConverter;
|
|
|
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.SoundCheckHelper;
|
|
|
import com.ym.mec.biz.dal.dto.SoundCheckInfoDto;
|
|
|
import com.ym.mec.biz.dal.dto.WebSocketInfo;
|
|
|
import com.ym.mec.biz.dal.enums.WebsocketTypeEnum;
|
|
@@ -17,6 +18,9 @@ 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.omg.PortableInterceptor.SYSTEM_EXCEPTION;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.socket.BinaryMessage;
|
|
|
import org.springframework.web.socket.TextMessage;
|
|
@@ -36,8 +40,10 @@ import java.util.concurrent.ConcurrentHashMap;
|
|
|
@Service
|
|
|
public class SoundCheckHandler implements WebSocketEventHandler {
|
|
|
|
|
|
+ private final Logger LOGGER = LoggerFactory.getLogger(SoundCheckHandler.class);
|
|
|
+
|
|
|
/** 校音数据 */
|
|
|
- private Map<String, Double> userSoundCheckInfo = new ConcurrentHashMap<>();
|
|
|
+ private Map<String, SoundCheckHelper> userSoundCheckInfo = new ConcurrentHashMap<>();
|
|
|
|
|
|
public SoundCheckHandler() {
|
|
|
WebSocketHandler.regist(WebsocketTypeEnum.SOUND_CHECK, this);
|
|
@@ -55,10 +61,10 @@ public class SoundCheckHandler implements WebSocketEventHandler {
|
|
|
userSoundCheckInfo.remove(phone);
|
|
|
break;
|
|
|
default:
|
|
|
- userSoundCheckInfo.put(phone, (double) 442);
|
|
|
+ userSoundCheckInfo.put(phone, new SoundCheckHelper(442, 0));
|
|
|
JSONObject body = (JSONObject) message.getBody();
|
|
|
if(body.containsKey("frequency")){
|
|
|
- userSoundCheckInfo.put(phone, body.getDoubleValue("frequency"));
|
|
|
+ userSoundCheckInfo.get(phone).setFrequency(body.getDoubleValue("frequency"));
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
@@ -78,14 +84,23 @@ public class SoundCheckHandler implements WebSocketEventHandler {
|
|
|
if(pitchDetectionResult.getPitch()<=0){
|
|
|
return;
|
|
|
}
|
|
|
- double normalCents = PitchConverter.hertzToAbsoluteCent(userSoundCheckInfo.get(phone));
|
|
|
+ double normalCents = PitchConverter.hertzToAbsoluteCent(userSoundCheckInfo.get(phone).getFrequency());
|
|
|
double recordCents = PitchConverter.hertzToAbsoluteCent(pitchDetectionResult.getPitch());
|
|
|
- if(Math.abs(normalCents - recordCents)<3){
|
|
|
- WebSocketHandler.sendTextMessage(phone, WebSocketInfo.success("checkDone"));
|
|
|
- userSoundCheckInfo.remove(phone);
|
|
|
+ if(Math.abs(normalCents - recordCents)<3 && userSoundCheckInfo.get(phone).getRightStartTime()<=0){
|
|
|
+ userSoundCheckInfo.get(phone).setRightStartTime(System.currentTimeMillis());
|
|
|
+ userSoundCheckInfo.get(phone).getErrorNum().set(0);
|
|
|
+ }else if(Math.abs(normalCents - recordCents)>=3){
|
|
|
+ userSoundCheckInfo.get(phone).getErrorNum().getAndIncrement();
|
|
|
}
|
|
|
}));
|
|
|
dispatcher.run();
|
|
|
+ LOGGER.info(JSONObject.toJSONString(userSoundCheckInfo.get(phone)));
|
|
|
+ if(userSoundCheckInfo.get(phone).getRightStartTime() >0 && System.currentTimeMillis() - userSoundCheckInfo.get(phone).getRightStartTime()>3000){
|
|
|
+ WebSocketHandler.sendTextMessage(phone, WebSocketInfo.success("checkDone"));
|
|
|
+ userSoundCheckInfo.remove(phone);
|
|
|
+ }else if(userSoundCheckInfo.get(phone).getErrorNum().get()>5){
|
|
|
+ userSoundCheckInfo.get(phone).setRightStartTime(0);
|
|
|
+ }
|
|
|
} catch (UnsupportedAudioFileException e) {
|
|
|
throw new BizException("{}校音异常:{}", phone, e);
|
|
|
}
|