Bläddra i källkod

Merge remote-tracking branch 'origin/master'

liweifan 3 år sedan
förälder
incheckning
2c05878bed

+ 158 - 0
audio-analysis/src/main/java/com/yonge/Main.java

@@ -0,0 +1,158 @@
+package com.yonge;
+
+import be.tarsos.dsp.AudioDispatcher;
+import be.tarsos.dsp.AudioEvent;
+import be.tarsos.dsp.AudioProcessor;
+import be.tarsos.dsp.io.jvm.JVMAudioInputStream;
+import be.tarsos.dsp.mfcc.MFCC;
+import be.tarsos.dsp.pitch.FastYin;
+import be.tarsos.dsp.pitch.PitchDetectionHandler;
+import be.tarsos.dsp.pitch.PitchDetectionResult;
+import be.tarsos.dsp.pitch.PitchProcessor;
+import com.yonge.audio.analysis.AudioFloatConverter;
+import com.yonge.audio.analysis.detector.YINPitchDetector;
+import com.yonge.audio.utils.ArrayUtil;
+import org.apache.commons.io.IOUtils;
+
+import javax.sound.sampled.AudioFormat;
+import javax.sound.sampled.AudioInputStream;
+import javax.sound.sampled.AudioSystem;
+import javax.sound.sampled.UnsupportedAudioFileException;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.net.URL;
+import java.util.Arrays;
+
+/**
+ * Description
+ *
+ * @author liujunchi
+ * @date 2022-06-24
+ */
+public class Main {
+    private final static int audioBufferSize = 2048;
+    private final static int bufferOverlap = 1024;
+    private final static int amountOfMelFilters = 20;
+    private final static int amountOfCepstrumCoef = 30;
+    private final static float lowerFilterFreq = 133.33f;
+    private final static float upperFilterFreq = 8000f;
+
+    private static AudioFormat audioFormat = new AudioFormat(44100, 16, 1, true, false);
+
+    // private AudioFloatConverter converter = AudioFloatConverter.getConverter(audioFormat);
+
+    public static void main(String[] args){
+        try{
+            float sampleRate = 44100;
+            int audioBufferSize = 2048;
+            int bufferOverlap = 0;
+            AudioFloatConverter converter = AudioFloatConverter.getConverter(audioFormat);
+            //Create an AudioInputStream from my .wav file
+            URL soundURL = Main.class.getResource("/300.wav");
+            AudioInputStream stream = AudioSystem.getAudioInputStream(soundURL);
+            final MFCC mfccProcessor = new MFCC(audioBufferSize, stream.getFormat().getSampleRate(),
+                                                amountOfCepstrumCoef, amountOfMelFilters, lowerFilterFreq, upperFilterFreq);
+
+            FastYin detector = new FastYin(sampleRate, audioBufferSize );
+            byte[] bytes = IOUtils.toByteArray(stream);
+            AudioFormat format = stream.getFormat();
+
+            int b = 0;
+            int frequency = 0;
+            while (bytes.length > 2048 *2) {
+
+                byte[] bufferData = ArrayUtil.extractByte(bytes, 0, 2048*2 - 1);
+
+                float[] sampleFloats = new float[1024*2];
+
+                converter.toFloatArray(bufferData, sampleFloats);
+                int playFrequency = (int)detector.getPitch(sampleFloats).getPitch();
+                System.out.println("play frequency is " +playFrequency);
+                // ArrayUtil.extractByte(channelContext.getChannelBufferBytes(), bufferSize, totalLength - 1)
+                bytes  = ArrayUtil.extractByte(bytes, 2048*2, bytes.length - 1);
+                // if (b == 1) {
+                //     frequency += playFrequency;
+                //     System.out.println("play frequency is " +frequency/2);
+                //     b = 0;
+                //     frequency = 0;
+                // } else {
+                //     frequency += playFrequency;
+                //     b ++;
+                // }
+            }
+
+
+            //Convert into TarsosDSP API
+            // JVMAudioInputStream audioStream = new JVMAudioInputStream(stream);
+            // AudioDispatcher dispatcher = new AudioDispatcher(audioStream, audioBufferSize, bufferOverlap);
+            // MyPitchDetector myPitchDetector = new MyPitchDetector();
+            // dispatcher.addAudioProcessor(mfccProcessor);
+            // dispatcher.addAudioProcessor(new AudioProcessor() {
+            //     @Override
+            //     public boolean process(AudioEvent audioEvent) {
+            //         float[] mfccs = mfccProcessor.getMFCC();
+            //
+            //         // System.out.println(Arrays.toString(mfccs));
+            //
+            //         YINPitchDetector frequencyDetector = new YINPitchDetector(mfccs.length, sampleRate);
+            //
+            //         int playFrequency = (int)detector.getPitch(audioEvent.getFloatBuffer()).getPitch();
+            //         // int playFrequency = (int) frequencyDetector.getFrequency(mfccs);
+            //         System.out.println("play frequency is " +playFrequency);
+            //         return true;
+            //     }
+            //
+            //     @Override
+            //     public void processingFinished() {
+            //
+            //     }
+            // });
+            // // dispatcher.addAudioProcessor(new MyPitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_YIN, sampleRate, audioBufferSize, myPitchDetector));
+            // dispatcher.run();
+
+
+        }
+        catch(FileNotFoundException fne){fne.printStackTrace();}
+        catch(UnsupportedAudioFileException uafe){uafe.printStackTrace();}
+        catch(IOException ie){ie.printStackTrace();}
+    }
+}
+
+class  MyPitchDetector implements PitchDetectionHandler {
+
+    //Here the result of pitch is always less than half.
+    @Override
+    public void handlePitch(PitchDetectionResult pitchDetectionResult,
+                            AudioEvent audioEvent) {
+        if(pitchDetectionResult.getPitch() != -1){
+            double timeStamp = audioEvent.getTimeStamp();
+            float pitch = pitchDetectionResult.getPitch();
+            float probability = pitchDetectionResult.getProbability();
+            double rms = audioEvent.getRMS() * 100;
+            String message = String.format("Pitch detected at %.2fs: %.2fHz ( %.2f probability, RMS: %.5f )\n", timeStamp,pitch,probability,rms);
+            System.out.println(message);
+        }
+    }
+}
+
+
+class  MyPitchProcessor extends PitchProcessor {
+
+    /**
+     * Initialize a new pitch processor.
+     *
+     * @param algorithm  An enum defining the algorithm.
+     * @param sampleRate The sample rate of the buffer (Hz).
+     * @param bufferSize The size of the buffer in samples.
+     * @param handler
+     */
+    public MyPitchProcessor(PitchEstimationAlgorithm algorithm, float sampleRate, int bufferSize, PitchDetectionHandler handler) {
+        super(algorithm, sampleRate, bufferSize, handler);
+    }
+
+
+    @Override
+    public boolean process(AudioEvent audioEvent) {
+        return super.process(audioEvent);
+    }
+}

+ 115 - 112
cooleshow-common/src/main/java/com/yonge/cooleshow/common/controller/BaseController.java

@@ -33,91 +33,94 @@ import com.yonge.toolset.utils.http.HttpUtil;
 @ControllerAdvice
 public class BaseController {
 
-	private final static Logger logger = LoggerFactory.getLogger(BaseController.class);
-
-	public static <T> HttpResponseResult<T> succeed(T object) {
-		return getResponseData(true, HttpStatus.OK, object, "");
-	}
-
-	public static <T> HttpResponseResult<T> succeed() {
-		return getResponseData(true, HttpStatus.OK, null, "");
-	}
-
-	public static <T> HttpResponseResult<T> succeedData(T obj) {
-		return getResponseData(true, HttpStatus.OK, obj, "操作成功");
-	}
-
-	public static <T> HttpResponseResult<T> warned(String message) {
-		return failed(HttpStatus.MULTI_STATUS, message);
-	}
-
-	public static <T> HttpResponseResult<T> failed() {
-		return failed("");
-	}
-
-	public static <T> HttpResponseResult<T> failed(String msg) {
-		return failed(HttpStatus.INTERNAL_SERVER_ERROR, msg);
-	}
-
-	public static <T> HttpResponseResult<T> failed(HttpStatus statusCode, String msg) {
-		return getResponseData(false, statusCode, null, msg);
-	}
-	public static <T> HttpResponseResult<T> failed(int code, String msg) {
-		return getResponseData(false, code, null, msg);
-	}
-
-	public static <T> HttpResponseResult<T> status(boolean flag) {
-			return flag ? succeed() : failed("操作失败");
-	}
-
-	public static <T> HttpResponseResult<T> failed(HttpStatus statusCode, T data, String msg) {
-		return getResponseData(false, statusCode, data, msg);
-	}
-
-	private static <T> HttpResponseResult<T> getResponseData(boolean status, HttpStatus statusCode, T data, String message) {
-		HttpResponseResult<T> obj = new HttpResponseResult<T>();
-		obj.setStatus(status);
-		obj.setCode(statusCode.value());
-		obj.setData(data);
-		obj.setMsg(message);
-		return obj;
-	}
-
-	private static <T> HttpResponseResult<T> getResponseData(boolean status, int code, T data, String message) {
-		HttpResponseResult<T> obj = new HttpResponseResult<T>();
-		obj.setStatus(status);
-		obj.setCode(code);
-		obj.setData(data);
-		obj.setMsg(message);
-		return obj;
-	}
-
-	/**
-	 * 处理一般异常
-	 *
-	 * @param ex
-	 * @param request
-	 * @return
-	 */
-	@ExceptionHandler(Exception.class)
-	public HttpResponseResult<String> handleException(Exception ex, HttpServletRequest request) {
-		Throwable e = ExceptionUtils.getRootCause(ex);
-		if (e == null) {
-			e = ex;
-		}
-		logger.error("System Error", ex);
-		// return failed(e.getMessage());
-		if (e instanceof BizException || e instanceof ThirdpartyException) {
-			if(e.getMessage().equals("205")){
-				return failed(HttpStatus.RESET_CONTENT,e.getMessage());
-			}
-			return failed(e.getMessage());
-		} else if (e instanceof AccessDeniedException) {
-			return failed("禁止访问");
-		}else if(e instanceof BindException){
-			String errors = ((BindException) e).getFieldErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining("\n"));
-			return failed(errors);
-		} else if(e instanceof MethodArgumentNotValidException){
+    private final static Logger logger = LoggerFactory.getLogger(BaseController.class);
+
+    public static <T> HttpResponseResult<T> succeed(T object) {
+        return getResponseData(true, HttpStatus.OK, object, "");
+    }
+
+    public static <T> HttpResponseResult<T> succeed() {
+        return getResponseData(true, HttpStatus.OK, null, "");
+    }
+
+    public static <T> HttpResponseResult<T> succeedData(T obj) {
+        return getResponseData(true, HttpStatus.OK, obj, "操作成功");
+    }
+
+    public static <T> HttpResponseResult<T> warned(String message) {
+        return failed(HttpStatus.MULTI_STATUS, message);
+    }
+
+    public static <T> HttpResponseResult<T> failed() {
+        return failed("");
+    }
+
+    public static <T> HttpResponseResult<T> failed(String msg) {
+        return failed(HttpStatus.INTERNAL_SERVER_ERROR, msg);
+    }
+
+    public static <T> HttpResponseResult<T> failed(HttpStatus statusCode, String msg) {
+        return getResponseData(false, statusCode, null, msg);
+    }
+
+    public static <T> HttpResponseResult<T> failed(int code, String msg) {
+        return getResponseData(false, code, null, msg);
+    }
+
+    public static <T> HttpResponseResult<T> status(boolean flag) {
+        return flag ? succeed() : failed("操作失败");
+    }
+
+    public static <T> HttpResponseResult<T> failed(HttpStatus statusCode, T data, String msg) {
+        return getResponseData(false, statusCode, data, msg);
+    }
+
+    private static <T> HttpResponseResult<T> getResponseData(boolean status, HttpStatus statusCode, T data, String message) {
+        HttpResponseResult<T> obj = new HttpResponseResult<T>();
+        obj.setStatus(status);
+        obj.setCode(statusCode.value());
+        obj.setData(data);
+        obj.setMsg(message);
+        return obj;
+    }
+
+    private static <T> HttpResponseResult<T> getResponseData(boolean status, int code, T data, String message) {
+        HttpResponseResult<T> obj = new HttpResponseResult<T>();
+        obj.setStatus(status);
+        obj.setCode(code);
+        obj.setData(data);
+        obj.setMsg(message);
+        return obj;
+    }
+
+    /**
+     * 处理一般异常
+     *
+     * @param ex
+     * @param request
+     * @return
+     */
+    @ExceptionHandler(Exception.class)
+    public HttpResponseResult<String> handleException(Exception ex, HttpServletRequest request) {
+        Throwable e = ExceptionUtils.getRootCause(ex);
+        if (e == null) {
+            e = ex;
+        }
+        logger.error("System Error", ex);
+        // return failed(e.getMessage());
+        if (e instanceof BizException || e instanceof ThirdpartyException) {
+            if (e.getMessage().equals("205")) {
+                return failed(HttpStatus.RESET_CONTENT, e.getMessage());
+            }
+            return failed(e.getMessage());
+        } else if (e instanceof AccessDeniedException) {
+            return failed("禁止访问");
+        } else if (e instanceof IllegalMonitorStateException) {
+            return failed("操作失败");
+        } else if (e instanceof BindException) {
+            String errors = ((BindException) e).getFieldErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining("\n"));
+            return failed(errors);
+        } else if (e instanceof MethodArgumentNotValidException) {
             MethodArgumentNotValidException validException = (MethodArgumentNotValidException) ex;
             String errorMsg = validException.getBindingResult()
                     .getFieldErrors()
@@ -126,34 +129,34 @@ public class BaseController {
                     .collect(Collectors.joining());
             return failed(errorMsg);
         }
-		try {
-			Map<String,Object> paramMap = new HashMap<>(2);
-			JSONObject jsonObject = new JSONObject();
-			jsonObject.put("content","系统繁忙请及时处理: " + request.getRequestURL() + "   " + e);
-			paramMap.put("text",jsonObject.toJSONString());
-			paramMap.put("msgtype","text");
-			Map<String,String> headers = new HashMap<>(1);
-			headers.put("Content-Type","application/json");
-			HttpUtil.postForHttps(dingTalkRobotsSecurityParam(),JSON.toJSONString(paramMap),headers);
-		}catch (Exception exception){
-			logger.error("System Error", exception);
-		}
+        try {
+            Map<String, Object> paramMap = new HashMap<>(2);
+            JSONObject jsonObject = new JSONObject();
+            jsonObject.put("content", "系统繁忙请及时处理: " + request.getRequestURL() + "   " + e);
+            paramMap.put("text", jsonObject.toJSONString());
+            paramMap.put("msgtype", "text");
+            Map<String, String> headers = new HashMap<>(1);
+            headers.put("Content-Type", "application/json");
+            HttpUtil.postForHttps(dingTalkRobotsSecurityParam(), JSON.toJSONString(paramMap), headers);
+        } catch (Exception exception) {
+            logger.error("System Error", exception);
+        }
         if (StringUtils.isNotBlank(e.getMessage())) {
             return failed(e.getMessage());
         }
-		return failed("系统繁忙");
-	}
-
-	public String dingTalkRobotsSecurityParam() throws Exception{
-		Long timestamp = System.currentTimeMillis();
-		String secret = "SEC36b17ac2f4e201f962042fb05f4b6b827719bcf9b6bb7b672e61f99c752eb693";
-		String stringToSign = timestamp + "\n" + secret;
-		Mac mac = Mac.getInstance("HmacSHA256");
-		mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
-		byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
-		String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");
-		StringBuffer sb = new StringBuffer("https://api.dingtalk.com/robot/send?access_token=22d7b3b54ea7f1633c640dfdf17083d0731c3757719a84bd333740a8b18eb035&timestamp=");
-		sb.append(timestamp).append("&sign=").append(sign);
-		return sb.toString();
-	}
+        return failed("系统繁忙");
+    }
+
+    public String dingTalkRobotsSecurityParam() throws Exception {
+        Long timestamp = System.currentTimeMillis();
+        String secret = "SEC36b17ac2f4e201f962042fb05f4b6b827719bcf9b6bb7b672e61f99c752eb693";
+        String stringToSign = timestamp + "\n" + secret;
+        Mac mac = Mac.getInstance("HmacSHA256");
+        mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
+        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
+        String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
+        StringBuffer sb = new StringBuffer("https://api.dingtalk.com/robot/send?access_token=22d7b3b54ea7f1633c640dfdf17083d0731c3757719a84bd333740a8b18eb035&timestamp=");
+        sb.append(timestamp).append("&sign=").append(sign);
+        return sb.toString();
+    }
 }

+ 2 - 2
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/service/impl/CourseScheduleServiceImpl.java

@@ -1180,7 +1180,7 @@ public class CourseScheduleServiceImpl extends ServiceImpl<CourseScheduleDao, Co
                 .eq(CourseSchedule::getStatus, CourseScheduleEnum.NOT_START)
                 .eq(CourseSchedule::getType, CourseScheduleEnum.PRACTICE));
         if (ObjectUtil.isEmpty(schedule)) {
-            throw new BizException("无法调整该课程");
+            throw new BizException("无法调整该课程,课程不存在");
         }
 
         //校验系统配置上下课时间
@@ -1189,7 +1189,7 @@ public class CourseScheduleServiceImpl extends ServiceImpl<CourseScheduleDao, Co
         Date s = DateUtil.strToDate(DateUtil.dateToString(classDate) + " " + star + ":00");
         Date e = DateUtil.strToDate(DateUtil.dateToString(classDate) + " " + end + ":00");
         if (startTime.before(s) || endTime.after(e)) {
-            throw new BizException("无法调整该课程");
+            throw new BizException("调整时间必须在{}到{}之间",star,end);
         }
 
         //查询是否有人购买

+ 4 - 4
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/service/impl/ImUserFriendServiceImpl.java

@@ -56,10 +56,10 @@ public class ImUserFriendServiceImpl extends ServiceImpl<ImUserFriendDao, ImUser
             List<BasicUserInfo> basicUserInfos = basicUserInfoMap.get(studentId);
             if(CollectionUtils.isNotEmpty(basicUserInfos)){
                 BasicUserInfo info = basicUserInfos.get(0);
-                teacherFriend.setFriendAvatar(basicUserInfo.getAvatar());
-                teacherFriend.setFriendNickname(basicUserInfo.getUsername());
-                studentFriend.setFriendAvatar(info.getAvatar());
-                studentFriend.setFriendNickname(info.getUsername());
+                teacherFriend.setFriendAvatar(info.getAvatar());
+                teacherFriend.setFriendNickname(info.getUsername());
+                studentFriend.setFriendAvatar(basicUserInfo.getAvatar());
+                studentFriend.setFriendNickname(basicUserInfo.getUsername());
             }
             teacherFriend.setFriendId(studentId);
             teacherFriend.setUserId(teacherId);

+ 1 - 1
cooleshow-user/user-biz/src/main/resources/config/mybatis/CourseHomeworkMapper.xml

@@ -349,7 +349,7 @@
         s.avatar_ as studentAvatar,
         if(sch.id_ is not null,1,0) as submitHomework,
         (case when sch.id_ is null then 'NOTCOMMIT'
-        when sch.teacher_replied_ is not null or sch.teacher_replied_ != '' then 'NOTREVIEW'
+        when sch.teacher_replied_ is null or sch.teacher_replied_ = '' then 'NOTREVIEW'
         else 'REVIEWED' end ) as homeworkStatus,
         (select group_concat(s2.name_) from subject s2
         where find_in_set(s2.id_,st.subject_id_) and s2.del_flag_ = 0 ) as subjectName