123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- import { Popup, Snackbar } from "@varlet/ui";
- import { Transition, defineComponent, onMounted, reactive, watch } from "vue";
- import {
- connectWebsocket,
- evaluatingData,
- handleEndBegin,
- handleEndSoundCheck,
- handlePerformDetection,
- handleStartBegin,
- handleStartEvaluat,
- handleViewReport,
- } from "/src/view/evaluating";
- import Earphone from "./earphone";
- import styles from "./index.module.less";
- import SoundEffect from "./sound-effect";
- import state from "/src/state";
- import { storeData } from "/src/store";
- import { browser } from "/src/utils";
- import { getNoteByMeasuresSlursStart } from "/src/helpers/formateMusic";
- import EvaluatResult from "./evaluat-result";
- import EvaluatAudio from "./evaluat-audio";
- import { api_openWebView, api_proxyServiceMessage, api_setCache, api_videoUpdate } from "/src/helpers/communication";
- import EvaluatShare from "./evaluat-share";
- import { Vue3Lottie } from "vue3-lottie";
- import startData from "./data/start.json";
- import startingData from "./data/starting.json";
- import iconEvaluat from "./icons/evaluating.json";
- import { getQuery } from "/src/utils/queryString";
- import { showToast } from "vant";
- // frequency 频率, amplitude 振幅, decibels 分贝
- type TCriteria = "frequency" | "amplitude" | "decibels";
- export default defineComponent({
- name: "evaluat-model",
- setup() {
- const query = getQuery();
- const evaluatModel = reactive({
- tips: true,
- evaluatUpdateAudio: false,
- shareMode: false,
- });
- const browserInfo = browser();
- /** 是否是节奏练习 */
- const isRhythmicExercises = () => {
- const examSongName = state.examSongName || "";
- return examSongName.indexOf("节奏练习") > -1;
- };
- /** 获取评测标准 */
- const getEvaluationCriteria = () => {
- let criteria: TCriteria = "frequency";
- // 声部打击乐
- if ([23, 113, 121].includes(state.subjectId)) {
- criteria = "amplitude";
- } else if (isRhythmicExercises()) {
- // 分类为节奏练习
- criteria = "decibels";
- }
- return criteria;
- };
- /** 生成评测曲谱数据 */
- const formatTimes = () => {
- const difftime = state.times[0]?.difftime || 0;
- let starTime = 0;
- let ListenMode = false;
- let dontEvaluatingMode = false;
- let skip = false;
- const datas = [];
- let times = state.times;
- /** 如果为选段模式,评测 */
- if (state.isSelectMeasureMode) {
- const startIndex = state.times.findIndex((n: any) => n.noteId == state.section[0].noteId);
- const endIndex = state.times.findIndex((n: any) => n.noteId == state.section[1].noteId);
- times = state.times.filter((n: any, index: number) => {
- return index >= startIndex && index <= endIndex;
- });
- starTime = times[0].sourceRelativeTime || times[0].relativeTime;
- }
- let measureIndex = -1;
- let recordMeasure = -1;
- for (let index = 0; index < times.length; index++) {
- const item = times[index];
- const note = getNoteByMeasuresSlursStart(item);
- const rate = state.speed / state.originSpeed;
- const start = difftime + (item.sourceRelativeTime || item.relativeTime) - starTime;
- const end = difftime + (item.sourceRelaEndtime || item.relaEndtime) - starTime;
- const isStaccato = note.noteElement.voiceEntry.isStaccato();
- const noteRate = isStaccato ? 0.5 : 1;
- if (note.formatLyricsEntries.contains("Play") || note.formatLyricsEntries.contains("Play...")) {
- ListenMode = false;
- }
- if (note.formatLyricsEntries.contains("Listen")) {
- ListenMode = true;
- }
- if (note.formatLyricsEntries.contains("纯律结束")) {
- dontEvaluatingMode = false;
- }
- if (note.formatLyricsEntries.contains("纯律")) {
- dontEvaluatingMode = true;
- }
- const nextNote = state.times[index + 1];
- // console.log("noteinfo", note.noteElement.isRestFlag && !!note.stave && !!nextNote)
- if (skip && (note.stave || !item.noteElement.isRestFlag || (nextNote && !nextNote.noteElement.isRestFlag))) {
- skip = false;
- }
- if (note.noteElement.isRestFlag && !!note.stave && !!nextNote && nextNote.noteElement.isRestFlag) {
- skip = true;
- }
- // console.log(note.measureOpenIndex, item.measureOpenIndex, note);
- // console.log("skip", skip)
- if (note.measureOpenIndex != recordMeasure) {
- measureIndex++;
- recordMeasure = note.measureOpenIndex;
- }
- const data = {
- timeStamp: (start * 1000) / rate,
- duration: ((end * 1000) / rate - (start * 1000) / rate) * noteRate,
- frequency: item.frequency,
- nextFrequency: item.nextFrequency,
- prevFrequency: item.prevFrequency,
- // 重复的情况index会自然累加,render的index是谱面渲染的index
- measureIndex: measureIndex,
- measureRenderIndex: item.measureListIndex,
- dontEvaluating: ListenMode || dontEvaluatingMode || item.skipMode,
- musicalNotesIndex: index,
- denominator: note.noteElement?.Length.denominator,
- isOrnament: !!note?.voiceEntry?.ornamentContainer,
- };
- datas.push(data);
- }
- return datas;
- };
- /** 生成数据 */
- const hanldeCreateData = async () => {
- const behaviorId = localStorage.getItem("behaviorId") || undefined;
- const rate = state.speed / state.originSpeed;
- const content = {
- musicXmlInfos: formatTimes(),
- subjectId: state.subjectId,
- detailId: state.detailId,
- examSongId: state.examSongId,
- xmlUrl: state.xmlUrl,
- partIndex: state.partIndex,
- behaviorId,
- platform: browserInfo.ios ? "IOS" : browserInfo.android ? "ANDROID" : "WEB",
- clientId: storeData.platformType === "STUDENT" ? "student" : storeData.platformType === "TEACHER" ? "teacher" : "education",
- hertz: state.setting.frequency,
- reactionTimeMs: state.setting.reactionTimeMs,
- speed: state.speed,
- heardLevel: state.setting.evaluationDifficulty,
- beatLength: Math.round((state.fixtime * 1000) / rate),
- practiceSource: query.unitId ? "UNIT_TEST" : "PRACTICE",
- feature: "EVALUATION",
- // evaluationCriteria: getEvaluationCriteria(),
- };
- await connectWebsocket(content);
- // 切换为伴奏
- if (state.accompany) {
- state.playSource = "background";
- }
- console.log("连接成功");
- };
- /** 评测结果按钮处理 */
- const handleEvaluatResult = (type: "practise" | "tryagain" | "look" | "share" | "update") => {
- if (type === "update") {
- // 上传云端
- evaluatModel.evaluatUpdateAudio = true;
- return;
- } else if (type === "share") {
- // 分享
- evaluatModel.shareMode = true;
- return;
- } else if (type === "look") {
- // 跳转
- handleViewReport("recordIdStr", "orchestra");
- return;
- } else if (type === "practise") {
- // 去练习
- handleStartEvaluat();
- } else if (type === "tryagain") {
- // 再来一次
- }
- evaluatingData.resulstMode = false;
- };
- /** 上传音视频 */
- const hanldeUpdateVideoAndAudio = async (update = false) => {
- if (!update) {
- evaluatModel.evaluatUpdateAudio = false;
- return;
- }
- if (state.setting.camera && state.setting.saveToAlbum) {
- evaluatModel.evaluatUpdateAudio = false;
- api_videoUpdate((res: any) => {
- if (res) {
- if (res?.content?.type === "success") {
- handleSaveResult({
- recordId: evaluatingData.resultData?.recordIdStr,
- filePath: res?.content?.filePath,
- });
- } else if (res?.content?.type === "error") {
- showToast({
- message: res.content?.message || "上传失败",
- });
- }
- }
- });
- return;
- }
- evaluatModel.evaluatUpdateAudio = false;
- handleSaveResult({
- recordId: evaluatingData.resultData?.recordId,
- });
- };
- const handleSaveResult = (_body: any) => {
- api_proxyServiceMessage({
- header: {
- commond: "videoUpload",
- status: 200,
- type: "SOUND_COMPARE",
- },
- body: _body,
- });
- showToast({
- message: "上传成功",
- });
- };
- onMounted(() => {
- handlePerformDetection();
- });
- watch(
- () => evaluatingData.checkEnd,
- () => {
- if (evaluatingData.checkEnd) {
- console.log("检测结束,生成评测数据");
- hanldeCreateData();
- }
- }
- );
- /** 监听评测结束 */
- watch(
- () => evaluatingData.resulstMode,
- () => {
- // 评测结束, 并且完整评测
- if (evaluatingData.resulstMode && evaluatingData.isComplete) {
- /** 有单元测验时,存储分数缓存 */
- api_setCache({
- key: "h5-orchestra-unit",
- value: JSON.stringify({
- musicId: query.id || "",
- unitId: query.unitId || "",
- questionId: query.questionId || "",
- score: evaluatingData.resultData?.score || 0,
- }),
- });
- }
- }
- );
- return () => (
- <div>
- <Transition name="pop-center">
- {evaluatingData.websocketState && !evaluatingData.startBegin && (
- <div class={styles.startBtn} onClick={handleStartBegin}>
- <img src={iconEvaluat.evaluatingStart} />
- </div>
- )}
- </Transition>
- <div style={{ display: !evaluatingData.startBegin ? "" : "none" }} class={styles.dialogueBox} key="start">
- <div class={styles.dialogue}>
- <img class={styles.dialoguebg} src={iconEvaluat["task-bg"]} />
- <div>演奏前请调整好乐器,保证最佳演奏状态。</div>
- </div>
- <Vue3Lottie class={styles.dialogueIcon} animationData={startData}></Vue3Lottie>
- </div>
- <div style={{ display: evaluatingData.startBegin ? "" : "none" }} class={styles.dialogueBox} key="start">
- <div class={styles.dialogueing}>收音中...</div>
- <Vue3Lottie class={styles.dialogueIcon} animationData={startingData}></Vue3Lottie>
- </div>
- <Popup teleport="body" closeOnClickOverlay={false} defaultStyle={false} v-model:show={evaluatingData.earphoneMode}>
- <Earphone
- onClose={() => {
- evaluatingData.earphoneMode = false;
- handlePerformDetection();
- }}
- />
- </Popup>
- <Popup teleport="body" closeOnClickOverlay={false} defaultStyle={false} v-model:show={evaluatingData.soundEffectMode}>
- <SoundEffect
- onClose={(value: any) => {
- evaluatingData.soundEffectMode = false;
- console.log('设置soundEffect')
- if (value) {
- state.setting.soundEffect = false;
- }
- handleEndSoundCheck();
- handlePerformDetection();
- }}
- />
- </Popup>
- <Popup teleport="body" closeOnClickOverlay={false} defaultStyle={false} v-model:show={evaluatingData.resulstMode}>
- <EvaluatResult onClose={handleEvaluatResult} />
- </Popup>
- <Popup teleport="body" closeOnClickOverlay={false} defaultStyle={false} v-model:show={evaluatModel.evaluatUpdateAudio}>
- <EvaluatAudio onClose={hanldeUpdateVideoAndAudio} />
- </Popup>
- <Popup teleport="body" defaultStyle={false} v-model:show={evaluatModel.shareMode}>
- <EvaluatShare onClose={() => (evaluatModel.shareMode = false)} />
- </Popup>
- </div>
- );
- },
- });
|