index.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. import { Popup, Skeleton } from "vant";
  2. import { computed, defineComponent, nextTick, onBeforeMount, onBeforeUnmount, onMounted, reactive, Transition, watch, watchEffect } from "vue";
  3. import { formateTimes } from "../../helpers/formateMusic";
  4. import Metronome, { metronomeData } from "../../helpers/metronome";
  5. import state, { EnumMusicRenderType, evaluatCreateMusicPlayer, handleSetSpeed, IAudioState, IPlatform, isRhythmicExercises, resetPlaybackToStart, togglePlay } from "/src/state";
  6. import { browser, setGlobalData } from "../../utils";
  7. import AudioList from "../../view/audio-list";
  8. import MusicScore, { resetMusicScore } from "../../view/music-score";
  9. import { sysMusicScoreAccompanimentQueryPage } from "../api";
  10. import EvaluatModel from "../evaluat-model";
  11. import HeaderTop from "../header-top";
  12. import styles from "./index.module.less";
  13. import { api_cloudAccompanyMessage, api_cloudLoading, api_keepScreenLongLight, api_openCamera, api_openWebView, api_setEventTracking, api_setRequestedOrientation, api_setStatusBarVisibility, isSpecialShapedScreen } from "/src/helpers/communication";
  14. import { getQuery } from "/src/utils/queryString";
  15. import Evaluating, { evaluatingData } from "/src/view/evaluating";
  16. import MeasureSpeed from "/src/view/plugins/measure-speed";
  17. import { mappingVoicePart, subjectFingering } from "/src/view/fingering/fingering-config";
  18. import Fingering from "/src/view/fingering";
  19. import store from "store";
  20. import Tick, { handleInitTick } from "/src/view/tick";
  21. import FollowPractice, { followData } from "/src/view/follow-practice";
  22. import FollowModel from "../follow-model";
  23. import RecordingTime from "../custom-plugins/recording-time";
  24. import WorkIndex from "../custom-plugins/work-index";
  25. import TheMusicList from "../component/the-music-list";
  26. import { storeData } from "/src/store";
  27. import ViewFigner from "../view-figner";
  28. import { recalculateNoteData } from "/src/view/selection";
  29. import ToggleMusicSheet from "/src/view/plugins/toggleMusicSheet"
  30. const calcCeilFrequency = (frequency: number) => {
  31. if (frequency) return frequency * 1000 * 2 / 1000;
  32. return 0
  33. };
  34. /** 需要处理频率的乐器
  35. */
  36. const resetFrequency = (list: any[]) => {
  37. const instrumentNames = ["ocarina", "pan-flute", "piccolo", "hulusi-flute"];
  38. if (!state.fingeringInfo?.name || !instrumentNames.includes(state.fingeringInfo.name)) return list;
  39. console.log(state.subjectId, state.fingeringInfo.name, instrumentNames)
  40. for (let i = 0; i < list.length; i++) {
  41. if (list[i].prevFrequency) list[i].prevFrequency = calcCeilFrequency(list[i].prevFrequency);
  42. if (list[i].frequency) list[i].frequency = calcCeilFrequency(list[i].frequency);
  43. if (list[i].nextFrequency) list[i].nextFrequency = calcCeilFrequency(list[i].nextFrequency);
  44. }
  45. return list;
  46. };
  47. /**
  48. * 乐器指法处理
  49. */
  50. const setNoteHalfTone = (list: any[]) => {
  51. const instrumentNames = ["hulusi-flute"] // ["ocarina", "pan-flute", "piccolo"];
  52. if (!state.fingeringInfo?.name || !instrumentNames.includes(state.fingeringInfo.name)) return list;
  53. for (let i = 0; i < list.length; i++) {
  54. const note = list[i];
  55. if (note.realKey === 0) continue;
  56. note.realKey = note.realKey + 12;
  57. }
  58. return list;
  59. };
  60. export default defineComponent({
  61. name: "music-list",
  62. setup() {
  63. const query: any = getQuery();
  64. const detailData = reactive({
  65. isLoading: true,
  66. skeletonLoading: true,
  67. paddingLeft: "",
  68. headerHide: false,
  69. fingerPreView: false,
  70. orientation: 0,
  71. fingerPreViewGuide: false,
  72. });
  73. const getAPPData = async () => {
  74. const screenData = await isSpecialShapedScreen();
  75. if (screenData?.content) {
  76. // console.log("🚀 ~ screenData:", screenData.content);
  77. const { isSpecialShapedScreen, notchHeight } = screenData.content;
  78. if (isSpecialShapedScreen) {
  79. detailData.paddingLeft = 25 + "px";
  80. }
  81. }
  82. };
  83. onBeforeMount(() => {
  84. api_keepScreenLongLight();
  85. getAPPData();
  86. api_setStatusBarVisibility();
  87. const settting = store.get("musicscoresetting");
  88. if (settting) {
  89. state.setting = settting;
  90. if (state.setting.camera) {
  91. api_openCamera();
  92. }
  93. }
  94. });
  95. //给app传伴奏
  96. const pushAppMusic = () => {
  97. api_cloudAccompanyMessage(state.accompany);
  98. };
  99. // console.log(route.params, query)
  100. /** 获取曲谱数据 */
  101. const getMusicInfo = (res: any) => {
  102. const index = query["part-index"] ? parseInt(query["part-index"] as string) : 0;
  103. const musicData = res.data.background[index] || {};
  104. const musicInfo = {
  105. ...res.data,
  106. music: musicData.audioFileUrl || res.data.audioFileUrl,
  107. accompany: musicData.metronomeUrl || res.data.metronomeUrl,
  108. musicSheetId: musicData.musicSheetId || res.data.id,
  109. track: musicData.track || res.data.track,
  110. };
  111. console.log("🚀 ~ musicInfo:", musicInfo);
  112. setState(musicInfo, index);
  113. setCustom();
  114. detailData.isLoading = false;
  115. };
  116. const setState = (data: any, index: number) => {
  117. state.appName = "COLEXIU";
  118. state.detailId = data.id;
  119. state.xmlUrl = data.xmlFileUrl;
  120. state.partIndex = index;
  121. state.trackId = data.track;
  122. state.subjectId = data.musicSubject;
  123. state.categoriesId = data.categoriesId;
  124. state.categoriesName = data.musicTagNames;
  125. state.enableEvaluation = data.canEvaluate ? true : false;
  126. state.examSongId = data.id + "";
  127. state.examSongName = data.musicSheetName;
  128. state.coverImg = data.titleImg ?? "";
  129. // 解析扩展字段
  130. if (data.extConfigJson) {
  131. try {
  132. state.extConfigJson = JSON.parse(data.extConfigJson as string);
  133. } catch (error) {
  134. console.error("解析扩展字段错误:", error);
  135. }
  136. }
  137. state.isOpenMetronome = data.mp3Type === "MP3_METRONOME" ? true : false;
  138. // 曲子包含节拍器,就不开启节拍器
  139. state.needTick = data.mp3Type === "MP3_METRONOME" ? false : true;
  140. state.isShowFingering = data.showFingering ? true : false;
  141. state.music = data.music;
  142. state.accompany = data.accompany;
  143. state.midiUrl = data.midiUrl;
  144. state.parentCategoriesId = data.musicTag;
  145. state.musicSheetCategoriesId = data.musicSheetCategoriesId;
  146. state.playMode = data.audioType === "MP3" ? "MP3" : "MIDI";
  147. state.originSpeed = state.speed = data.playSpeed;
  148. const track = data.code || data.track;
  149. state.track = track ? track.replace(/ /g, "").toLocaleLowerCase() : "";
  150. state.enableNotation = data.notation ? true : false;
  151. // console.log("🚀 ~ state.subjectId:", state.subjectId, state.track as any , state.subjectId)
  152. // 是否打击乐
  153. // state.isPercussion =
  154. // state.subjectId == 23 ||
  155. // state.subjectId == 113 ||
  156. // state.subjectId == 121 ||
  157. // isRhythmicExercises();
  158. // 设置指法
  159. const code = mappingVoicePart(state.trackId, "INSTRUMENT");
  160. state.fingeringInfo = subjectFingering(code);
  161. console.log("🚀 ~ state.fingeringInfo:", state.fingeringInfo, state.trackId, state.track);
  162. // 检测是否原音和伴奏都有
  163. if (!state.music || !state.accompany) {
  164. state.playSource = state.music ? "music" : "background";
  165. }
  166. // 如果是PC端,放大曲谱
  167. state.platform = query.platform?.toLocaleUpperCase() || "";
  168. if (state.platform === IPlatform.PC) {
  169. state.zoom = query.zoom || 1.5;
  170. }
  171. //课堂乐器, 渲染类型: 五线谱, 简谱
  172. state.musicRenderType = query.musicRenderType || EnumMusicRenderType.firstTone;
  173. console.log('state对象', state)
  174. };
  175. const setCustom = () => {
  176. if (state.extConfigJson.multitrack) {
  177. setGlobalData("multitrack", state.extConfigJson.multitrack);
  178. }
  179. };
  180. onMounted(() => {
  181. (window as any).appName = "colexiu";
  182. const id = query.id || "43554";
  183. Promise.all([sysMusicScoreAccompanimentQueryPage(id)]).then((values) => {
  184. getMusicInfo(values[0]);
  185. });
  186. api_setEventTracking();
  187. });
  188. /** 渲染完成 */
  189. const handleRendered = (osmd: any) => {
  190. detailData.skeletonLoading = false;
  191. state.osmd = osmd;
  192. // 没有设置速度使用读取的速度
  193. if (state.originSpeed === 0) {
  194. state.originSpeed = state.speed = (osmd as any).bpm || osmd.Sheet.userStartTempoInBPM || 100;
  195. }
  196. const saveSpeed = (store.get("speeds") || {})[state.examSongId] || (osmd as any).bpm || osmd.Sheet.userStartTempoInBPM;
  197. // 加载本地缓存的速度
  198. if (saveSpeed) {
  199. handleSetSpeed(saveSpeed);
  200. }
  201. state.times = formateTimes(osmd);
  202. state.times = resetFrequency(state.times);
  203. state.times = setNoteHalfTone(state.times);
  204. console.log("🚀 ~ state.times:", state.times, state.subjectId, state);
  205. try {
  206. metronomeData.metro = new Metronome();
  207. metronomeData.metro.init(state.times);
  208. } catch (error) {}
  209. // 设置节拍器
  210. if (state.needTick) {
  211. const beatLengthInMilliseconds = (60 / state.speed) * 1000;
  212. // console.log(state.speed, osmd?.Sheet?.SheetPlaybackSetting?.beatLengthInMilliseconds , (60 / state.speed) * 1000)
  213. handleInitTick(beatLengthInMilliseconds, osmd?.Sheet?.SheetPlaybackSetting?.Rhythm?.Numerator || 4);
  214. }
  215. api_cloudLoading();
  216. state.musicRendered = true;
  217. evaluatCreateMusicPlayer();
  218. resetPlaybackToStart();
  219. pushAppMusic();
  220. };
  221. /** 指法配置 */
  222. const fingerConfig = computed<any>(() => {
  223. if (state.setting.displayFingering && state.fingeringInfo?.name) {
  224. if (state.fingeringInfo.direction === "transverse") {
  225. return {
  226. container: {
  227. paddingBottom: state.platform === IPlatform.PC ? `calc(${state.fingeringInfo.height} + ${state.attendHideMenu ? "0px" : "1.8rem"})` : state.fingeringInfo.height,
  228. },
  229. fingerBox: {
  230. height: state.fingeringInfo.height,
  231. },
  232. };
  233. } else {
  234. return {
  235. container: {
  236. paddingRight: state.fingeringInfo.width,
  237. },
  238. fingerBox: {
  239. position: "absolute",
  240. width: state.fingeringInfo.width,
  241. height: "100%",
  242. right: 0,
  243. top: 0,
  244. },
  245. };
  246. }
  247. }
  248. return {
  249. container: {},
  250. fingerBox: {},
  251. };
  252. });
  253. // 监听指法显示
  254. watch(
  255. () => state.setting.displayFingering,
  256. () => {
  257. if (state.fingeringInfo.direction === "vertical") {
  258. nextTick(() => {
  259. resetMusicScore();
  260. });
  261. }
  262. }
  263. );
  264. watch(
  265. () => state.setting.soundEffect,
  266. () => {
  267. store.set("musicscoresetting", state.setting);
  268. }
  269. );
  270. /**播放状态改变时,向父页面发送事件 */
  271. const sendParentMessage = (playState: IAudioState) => {
  272. window.parent.postMessage(
  273. {
  274. api: "headerTogge",
  275. playState: playState,
  276. },
  277. "*"
  278. );
  279. };
  280. // 监听播放状态
  281. watch(
  282. () => state.playState,
  283. () => {
  284. if (state.platform != IPlatform.PC) {
  285. detailData.headerHide = state.playState === "play" ? true : false;
  286. }
  287. sendParentMessage(state.playState);
  288. }
  289. );
  290. /** 指法预览切换 */
  291. watch(
  292. () => detailData.fingerPreView,
  293. () => {
  294. console.log(2342);
  295. window.parent.postMessage(
  296. {
  297. api: "api_fingerPreView",
  298. state: detailData.fingerPreView,
  299. },
  300. "*"
  301. );
  302. }
  303. );
  304. onMounted(() => {
  305. window.addEventListener("resize", resetMusicScore);
  306. });
  307. onBeforeUnmount(() => {
  308. window.removeEventListener("resize", resetMusicScore);
  309. });
  310. const browsInfo = browser();
  311. const handleOpenFignerView = () => {
  312. if (!query.modelType) {
  313. detailData.orientation = state.fingeringInfo.orientation || 0;
  314. api_setRequestedOrientation(detailData.orientation);
  315. }
  316. // const url = `${
  317. // /(192|localhost)/.test(location.origin)
  318. // ? "http://192.168.3.114:3000/instrument.html"
  319. // : location.origin + location.pathname
  320. // }#/view-figner`;
  321. // console.log("🚀 ~ url:", url);
  322. // api_openWebView({
  323. // url: url,
  324. // orientation: state.fingeringInfo.orientation || 0,
  325. // });
  326. if (state.playState === "play") {
  327. togglePlay("paused");
  328. setTimeout(() => {
  329. detailData.fingerPreView = true;
  330. }, 500);
  331. return;
  332. }
  333. detailData.fingerPreView = true;
  334. };
  335. const handleCloseFignerView = () => {
  336. if (!query.modelType && detailData.orientation == 1) {
  337. api_setRequestedOrientation(0);
  338. }
  339. detailData.fingerPreView = false;
  340. detailData.fingerPreViewGuide = false;
  341. };
  342. return () => (
  343. <div
  344. class={[styles.detail, state.setting.eyeProtection && "eyeProtection", state.platform === IPlatform.PC && styles.PC]}
  345. style={{
  346. paddingLeft: detailData.paddingLeft,
  347. background: state.setting.camera
  348. ? `rgba(${state.setting.eyeProtection ? '253,244,229' : '255,255,255'} ,${
  349. state.setting.cameraOpacity / 100
  350. }) !important`
  351. : '',
  352. }}
  353. >
  354. <Transition name="van-fade">
  355. {detailData.skeletonLoading && (
  356. <div class={styles.skeleton}>
  357. <Skeleton row={8} />
  358. </div>
  359. )}
  360. </Transition>
  361. <div class={[styles.headHeight, detailData.headerHide && styles.headHide]}>{state.musicRendered && <HeaderTop />}</div>
  362. <div
  363. id="scrollContainer"
  364. style={{ ...fingerConfig.value.container,
  365. height: detailData.headerHide ? "100vh" : "",
  366. }}
  367. class={[styles.container, !state.setting.displayCursor && "hideCursor", browsInfo.xiaomi && styles.xiaomi]}
  368. onClick={(e: Event) => {
  369. e.stopPropagation();
  370. if (state.playState === "play" && state.platform != IPlatform.PC) {
  371. detailData.headerHide = !detailData.headerHide;
  372. }
  373. }}
  374. >
  375. {/* 曲谱渲染 */}
  376. {!detailData.isLoading && <MusicScore onRendered={handleRendered} />}
  377. {/* 指法 */}
  378. {state.setting.displayFingering && state.fingeringInfo?.name && (
  379. <div style={{ ...fingerConfig.value.fingerBox }}>
  380. <Fingering
  381. style={{
  382. background: state.setting.camera
  383. ? `rgba(${state.setting.eyeProtection ? '253,244,229' : '255,255,255'} ,${
  384. state.setting.cameraOpacity / 100
  385. })`
  386. : '',
  387. }}
  388. onOpen={() => handleOpenFignerView()}
  389. />
  390. </div>
  391. )}
  392. </div>
  393. {/* 节拍器 */}
  394. {state.needTick && <Tick />}
  395. {/* 播放 */}
  396. {!detailData.isLoading && <AudioList />}
  397. {/* 评测 */}
  398. {state.modeType === "evaluating" && (
  399. <>
  400. <Evaluating />
  401. {evaluatingData.rendered && <EvaluatModel />}
  402. </>
  403. )}
  404. {/* 跟练模式 */}
  405. {state.modeType === "follow" && (
  406. <>
  407. <FollowPractice />
  408. <FollowModel />
  409. </>
  410. )}
  411. {/* 切换曲谱 */}
  412. {!query.lessonTrainingId && !query.questionId && (
  413. <ToggleMusicSheet />
  414. )}
  415. {state.musicRendered && (
  416. <>
  417. {/* 统计训练时长 */}
  418. {storeData.isApp && <RecordingTime />}
  419. {/* 作业 */}
  420. {query.workRecord && <WorkIndex />}
  421. {/* 曲谱列表 */}
  422. {state.playState == "play" || followData.start || evaluatingData.startBegin || query.workRecord || query.modelType || state.platform === IPlatform.PC ? null : <TheMusicList />}
  423. </>
  424. )}
  425. <Popup
  426. zIndex={5050}
  427. teleport="body"
  428. v-model:show={detailData.fingerPreView}
  429. position="bottom"
  430. onOpened={() => {
  431. detailData.fingerPreViewGuide = true;
  432. }}
  433. >
  434. <ViewFigner
  435. show={detailData.fingerPreViewGuide}
  436. subject={state.fingeringInfo.name}
  437. isComponent={true}
  438. onClose={handleCloseFignerView}
  439. />
  440. </Popup>
  441. </div>
  442. );
  443. },
  444. });