index.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import { Skeleton } from "vant";
  2. import { computed, defineComponent, nextTick, onBeforeMount, onBeforeUnmount, onMounted, reactive, Transition, watch, watchEffect } from "vue";
  3. import { useRoute } from "vue-router";
  4. import { formateTimes } from "../../helpers/formateMusic";
  5. import Metronome, { metronomeData } from "../../helpers/metronome";
  6. import state, { isRhythmicExercises } from "../../state";
  7. import { storeData } from "../../store";
  8. import { setGlobalData } from "../../utils";
  9. import AudioList from "../../view/audio-list";
  10. import MusicScore, { resetMusicScore } from "../../view/music-score";
  11. import { sysMusicScoreAccompanimentQueryPage } from "../api";
  12. import EvaluatModel from "../evaluat-model";
  13. import HeaderTop from "../header-top";
  14. import styles from "./index.module.less";
  15. import { api_cloudLoading, api_openCamera, api_setEventTracking, api_setStatusBarVisibility, isSpecialShapedScreen } from "/src/helpers/communication";
  16. import { getQuery } from "/src/utils/queryString";
  17. import Evaluating, { evaluatingData } from "/src/view/evaluating";
  18. import MeasureSpeed from "/src/view/plugins/measure-speed";
  19. import { mappingVoicePart, subjectFingering } from "/src/view/fingering/fingering-config";
  20. import Fingering from "/src/view/fingering";
  21. import store from "store";
  22. import Tick, { handleInitTick } from "/src/view/tick";
  23. import FollowPractice from "/src/view/follow-practice";
  24. import FollowModel from "../follow-model";
  25. import RecordingTime from "../custom-plugins/recording-time";
  26. export default defineComponent({
  27. name: "music-list",
  28. setup() {
  29. const query: any = getQuery();
  30. const detailData = reactive({
  31. isLoading: true,
  32. paddingLeft: "",
  33. headerHide: false,
  34. });
  35. const getAPPData = async () => {
  36. const screenData = await isSpecialShapedScreen();
  37. if (screenData?.content) {
  38. // console.log("🚀 ~ screenData:", screenData.content);
  39. const { isSpecialShapedScreen, notchHeight } = screenData.content;
  40. if (isSpecialShapedScreen) {
  41. detailData.paddingLeft = 25 + "px";
  42. }
  43. }
  44. };
  45. onBeforeMount(() => {
  46. getAPPData();
  47. api_setStatusBarVisibility();
  48. const settting = store.get("musicscoresetting");
  49. if (settting) {
  50. state.setting = settting;
  51. if (state.setting.camera) {
  52. api_openCamera();
  53. }
  54. // console.log("🚀 ~ settting:", settting)
  55. }
  56. });
  57. // console.log(route.params, query)
  58. /** 获取曲谱数据 */
  59. const getMusicInfo = (res: any) => {
  60. const index = query["part-index"] ? parseInt(query["part-index"] as string) : 0;
  61. const musicInfo = {
  62. ...res.data,
  63. accompany: res.data.audioFileUrl,
  64. ...res.data.background[index],
  65. };
  66. // console.log("🚀 ~ musicInfo:", musicInfo);
  67. setState(musicInfo, index);
  68. setCustom();
  69. detailData.isLoading = false;
  70. };
  71. const setState = (data: any, index: number) => {
  72. state.appName = "COLEXIU";
  73. state.detailId = data.id;
  74. state.xmlUrl = data.xmlFileUrl;
  75. state.partIndex = index;
  76. state.subjectId = data.musicSubject;
  77. state.categoriesId = data.categoriesId;
  78. state.categoriesName = data.musicTagNames;
  79. state.enableEvaluation = data.canEvaluate ? true : false;
  80. state.examSongId = data.id + "";
  81. state.examSongName = data.musicSheetName;
  82. // 解析扩展字段
  83. if (data.extConfigJson) {
  84. try {
  85. state.extConfigJson = JSON.parse(data.extConfigJson as string);
  86. } catch (error) {
  87. console.error("解析扩展字段错误:", error);
  88. }
  89. }
  90. state.isOpenMetronome = data.mp3Type === "MP3_METRONOME" ? true : false;
  91. state.needTick = true; // data.isOpenMetronome;
  92. state.isShowFingering = data.showFingering ? true : false;
  93. state.music = data.audioFileUrl;
  94. state.accompany = data.accompany;
  95. state.midiUrl = data.midiUrl;
  96. state.parentCategoriesId = data.musicTag;
  97. state.playMode = data.audioType === "MP3" ? "MP3" : "MIDI";
  98. // state.originSpeed = state.speed = parseFloat(data.playSpeed) || 0;
  99. state.originSpeed = state.speed = data.playSpeed;
  100. state.track = data.track;
  101. state.enableNotation = data.notation ? true : false;
  102. // 映射声部ID
  103. state.subjectId = mappingVoicePart(state.track as any, "COLEXIU");
  104. // 是否打击乐
  105. state.isPercussion = state.subjectId == 23 || state.subjectId == 113 || state.subjectId == 121 || isRhythmicExercises();
  106. // 设置指法
  107. state.fingeringInfo = subjectFingering(state.subjectId);
  108. // console.log("🚀 ~ state.fingeringInfo:", state.fingeringInfo, state.subjectId, state.track)
  109. // 检测是否原音和伴奏都有
  110. if (!state.music || !state.accompany) {
  111. state.playSource = state.music ? "music" : "background";
  112. }
  113. };
  114. const setCustom = () => {
  115. if (state.extConfigJson.multitrack) {
  116. setGlobalData("multitrack", state.extConfigJson.multitrack);
  117. }
  118. };
  119. onMounted(() => {
  120. (window as any).appName = "colexiu";
  121. Promise.all([sysMusicScoreAccompanimentQueryPage(query.id)]).then((values) => {
  122. getMusicInfo(values[0]);
  123. });
  124. api_setEventTracking();
  125. });
  126. /** 渲染完成 */
  127. const handleRendered = (osmd: any) => {
  128. state.musicRendered = true;
  129. state.osmd = osmd;
  130. const saveSpeed = (store.get("speeds") || {})[state.examSongId];
  131. const bpm = (osmd as any).bpm || osmd.Sheet.userStartTempoInBPM;
  132. state.originSpeed = state.speed = saveSpeed || bpm || 100;
  133. state.times = formateTimes(osmd);
  134. console.log("🚀 ~ state.times:", state.times);
  135. try {
  136. metronomeData.metro = new Metronome();
  137. metronomeData.metro.init(state.times);
  138. } catch (error) {}
  139. // 设置节拍器
  140. if (state.needTick) {
  141. const beatLengthInMilliseconds = osmd?.Sheet?.SheetPlaybackSetting?.beatLengthInMilliseconds || (60 / bpm) * 1000;
  142. handleInitTick(beatLengthInMilliseconds, osmd?.Sheet?.SheetPlaybackSetting?.Rhythm?.Numerator || 4);
  143. }
  144. api_cloudLoading();
  145. };
  146. /** 指法配置 */
  147. const fingerConfig = computed<any>(() => {
  148. if (state.setting.displayFingering && state.fingeringInfo?.name) {
  149. if (state.fingeringInfo.direction === "transverse") {
  150. return {
  151. container: {
  152. paddingBottom: state.fingeringInfo.height,
  153. },
  154. fingerBox: {
  155. height: state.fingeringInfo.height,
  156. },
  157. };
  158. } else {
  159. return {
  160. container: {
  161. paddingRight: state.fingeringInfo.width,
  162. },
  163. fingerBox: {
  164. position: "absolute",
  165. width: state.fingeringInfo.width,
  166. height: "100%",
  167. right: 0,
  168. top: 0,
  169. },
  170. };
  171. }
  172. }
  173. return {
  174. container: {},
  175. fingerBox: {},
  176. };
  177. });
  178. // 监听指法显示
  179. watch(
  180. () => state.setting.displayFingering,
  181. () => {
  182. if (state.fingeringInfo.direction === "vertical") {
  183. nextTick(() => {
  184. resetMusicScore();
  185. });
  186. }
  187. }
  188. );
  189. // 监听播放状态
  190. watch(
  191. () => state.playState,
  192. () => {
  193. detailData.headerHide = state.playState === "play" ? true : false;
  194. }
  195. );
  196. onMounted(() => {
  197. window.addEventListener("resize", resetMusicScore);
  198. });
  199. onBeforeUnmount(() => {
  200. window.removeEventListener("resize", resetMusicScore);
  201. });
  202. return () => (
  203. <div
  204. class={[styles.detail, state.setting.eyeProtection && "eyeProtection"]}
  205. style={{ paddingLeft: detailData.paddingLeft, opacity: state.setting.camera ? `${state.setting.cameraOpacity / 100}` : "" }}
  206. >
  207. {!state.musicRendered && (
  208. <div class={styles.skeleton}>
  209. <Skeleton class={styles.skeleton} row={8} />
  210. </div>
  211. )}
  212. <div class={[styles.headHeight, detailData.headerHide && styles.headHide]}>
  213. <Transition name="van-slide-down">{state.musicRendered && <HeaderTop />}</Transition>
  214. </div>
  215. <div
  216. id="scrollContainer"
  217. style={{ ...fingerConfig.value.container, height: detailData.headerHide ? "100vh" : "" }}
  218. class={[styles.container, !state.setting.displayCursor && "hideCursor"]}
  219. onClick={(e: Event) => {
  220. if (state.playState === "play") {
  221. detailData.headerHide = !detailData.headerHide;
  222. }
  223. }}
  224. >
  225. {/* 曲谱渲染 */}
  226. {!detailData.isLoading && <MusicScore onRendered={handleRendered} />}
  227. {/* 播放 */}
  228. {!detailData.isLoading && <AudioList />}
  229. {/* 评测 */}
  230. {state.modeType === "evaluating" && (
  231. <>
  232. <Evaluating />
  233. {evaluatingData.rendered && <EvaluatModel />}
  234. </>
  235. )}
  236. {/* 指法 */}
  237. {state.setting.displayFingering && state.fingeringInfo?.name && (
  238. <div style={{ ...fingerConfig.value.fingerBox }}>
  239. <Fingering />
  240. </div>
  241. )}
  242. </div>
  243. {/* 公用的插件 */}
  244. <div class={styles.plugins}>
  245. {state.musicRendered && (
  246. <>
  247. <MeasureSpeed />
  248. {/* 统计训练时长 */}
  249. <RecordingTime />
  250. </>
  251. )}
  252. </div>
  253. {/* 节拍器 */}
  254. {state.needTick && <Tick />}
  255. {/* 跟练模式 */}
  256. {state.modeType === "follow" && (
  257. <>
  258. <FollowPractice />
  259. <FollowModel />
  260. </>
  261. )}
  262. </div>
  263. );
  264. },
  265. });