index.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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.subjectId = data.track;
  122. state.categoriesId = data.categoriesId;
  123. state.categoriesName = data.musicTagNames;
  124. state.enableEvaluation = data.canEvaluate ? true : false;
  125. state.examSongId = data.id + "";
  126. state.examSongName = data.musicSheetName;
  127. state.coverImg = data.titleImg ?? "";
  128. // 解析扩展字段
  129. if (data.extConfigJson) {
  130. try {
  131. state.extConfigJson = JSON.parse(data.extConfigJson as string);
  132. } catch (error) {
  133. console.error("解析扩展字段错误:", error);
  134. }
  135. }
  136. state.isOpenMetronome = data.mp3Type === "MP3_METRONOME" ? true : false;
  137. // 曲子包含节拍器,就不开启节拍器
  138. state.needTick = data.mp3Type === "MP3_METRONOME" ? false : true;
  139. state.isShowFingering = data.showFingering ? true : false;
  140. state.music = data.music;
  141. state.accompany = data.accompany;
  142. state.midiUrl = data.midiUrl;
  143. state.parentCategoriesId = data.musicTag;
  144. state.musicSheetCategoriesId = data.musicSheetCategoriesId;
  145. state.playMode = data.audioType === "MP3" ? "MP3" : "MIDI";
  146. state.originSpeed = state.speed = data.playSpeed;
  147. const track = data.code || data.track;
  148. state.track = track ? track.replace(/ /g, "").toLocaleLowerCase() : "";
  149. state.enableNotation = data.notation ? true : false;
  150. // console.log("🚀 ~ state.subjectId:", state.subjectId, state.track as any , state.subjectId)
  151. // 是否打击乐
  152. // state.isPercussion =
  153. // state.subjectId == 23 ||
  154. // state.subjectId == 113 ||
  155. // state.subjectId == 121 ||
  156. // isRhythmicExercises();
  157. // 设置指法
  158. const code = mappingVoicePart(state.subjectId, "INSTRUMENT");
  159. state.fingeringInfo = subjectFingering(code);
  160. console.log("🚀 ~ state.fingeringInfo:", state.fingeringInfo, state.subjectId, state.track);
  161. // 检测是否原音和伴奏都有
  162. if (!state.music || !state.accompany) {
  163. state.playSource = state.music ? "music" : "background";
  164. }
  165. // 如果是PC端,放大曲谱
  166. state.platform = query.platform?.toLocaleUpperCase() || "";
  167. if (state.platform === IPlatform.PC) {
  168. state.zoom = query.zoom || 1.5;
  169. }
  170. //课堂乐器,默认简谱
  171. state.musicRenderType = EnumMusicRenderType.firstTone;
  172. console.log(state,99999)
  173. };
  174. const setCustom = () => {
  175. if (state.extConfigJson.multitrack) {
  176. setGlobalData("multitrack", state.extConfigJson.multitrack);
  177. }
  178. };
  179. onMounted(() => {
  180. (window as any).appName = "colexiu";
  181. const id = query.id || "43554";
  182. Promise.all([sysMusicScoreAccompanimentQueryPage(id)]).then((values) => {
  183. getMusicInfo(values[0]);
  184. });
  185. api_setEventTracking();
  186. });
  187. /** 渲染完成 */
  188. const handleRendered = (osmd: any) => {
  189. detailData.skeletonLoading = false;
  190. state.osmd = osmd;
  191. // 没有设置速度使用读取的速度
  192. if (state.originSpeed === 0) {
  193. state.originSpeed = state.speed = (osmd as any).bpm || osmd.Sheet.userStartTempoInBPM || 100;
  194. }
  195. const saveSpeed = (store.get("speeds") || {})[state.examSongId] || (osmd as any).bpm || osmd.Sheet.userStartTempoInBPM;
  196. // 加载本地缓存的速度
  197. if (saveSpeed) {
  198. handleSetSpeed(saveSpeed);
  199. }
  200. state.times = formateTimes(osmd);
  201. state.times = resetFrequency(state.times);
  202. state.times = setNoteHalfTone(state.times);
  203. console.log("🚀 ~ state.times:", state.times, state.subjectId, state);
  204. try {
  205. metronomeData.metro = new Metronome();
  206. metronomeData.metro.init(state.times);
  207. } catch (error) {}
  208. // 设置节拍器
  209. if (state.needTick) {
  210. const beatLengthInMilliseconds = (60 / state.speed) * 1000;
  211. // console.log(state.speed, osmd?.Sheet?.SheetPlaybackSetting?.beatLengthInMilliseconds , (60 / state.speed) * 1000)
  212. handleInitTick(beatLengthInMilliseconds, osmd?.Sheet?.SheetPlaybackSetting?.Rhythm?.Numerator || 4);
  213. }
  214. api_cloudLoading();
  215. state.musicRendered = true;
  216. evaluatCreateMusicPlayer();
  217. resetPlaybackToStart();
  218. pushAppMusic();
  219. };
  220. /** 指法配置 */
  221. const fingerConfig = computed<any>(() => {
  222. if (state.setting.displayFingering && state.fingeringInfo?.name) {
  223. if (state.fingeringInfo.direction === "transverse") {
  224. return {
  225. container: {
  226. paddingBottom: state.platform === IPlatform.PC ? `calc(${state.fingeringInfo.height} + ${state.attendHideMenu ? "0px" : "1.8rem"})` : state.fingeringInfo.height,
  227. },
  228. fingerBox: {
  229. height: state.fingeringInfo.height,
  230. },
  231. };
  232. } else {
  233. return {
  234. container: {
  235. paddingRight: state.fingeringInfo.width,
  236. },
  237. fingerBox: {
  238. position: "absolute",
  239. width: state.fingeringInfo.width,
  240. height: "100%",
  241. right: 0,
  242. top: 0,
  243. },
  244. };
  245. }
  246. }
  247. return {
  248. container: {},
  249. fingerBox: {},
  250. };
  251. });
  252. // 监听指法显示
  253. watch(
  254. () => state.setting.displayFingering,
  255. () => {
  256. if (state.fingeringInfo.direction === "vertical") {
  257. nextTick(() => {
  258. resetMusicScore();
  259. });
  260. }
  261. }
  262. );
  263. watch(
  264. () => state.setting.soundEffect,
  265. () => {
  266. store.set("musicscoresetting", state.setting);
  267. }
  268. );
  269. /**播放状态改变时,向父页面发送事件 */
  270. const sendParentMessage = (playState: IAudioState) => {
  271. window.parent.postMessage(
  272. {
  273. api: "headerTogge",
  274. playState: playState,
  275. },
  276. "*"
  277. );
  278. };
  279. // 监听播放状态
  280. watch(
  281. () => state.playState,
  282. () => {
  283. if (state.platform != IPlatform.PC) {
  284. detailData.headerHide = state.playState === "play" ? true : false;
  285. }
  286. sendParentMessage(state.playState);
  287. }
  288. );
  289. /** 指法预览切换 */
  290. watch(
  291. () => detailData.fingerPreView,
  292. () => {
  293. console.log(2342);
  294. window.parent.postMessage(
  295. {
  296. api: "api_fingerPreView",
  297. state: detailData.fingerPreView,
  298. },
  299. "*"
  300. );
  301. }
  302. );
  303. onMounted(() => {
  304. window.addEventListener("resize", resetMusicScore);
  305. });
  306. onBeforeUnmount(() => {
  307. window.removeEventListener("resize", resetMusicScore);
  308. });
  309. const browsInfo = browser();
  310. const handleOpenFignerView = () => {
  311. if (!query.modelType) {
  312. detailData.orientation = state.fingeringInfo.orientation || 0;
  313. api_setRequestedOrientation(detailData.orientation);
  314. }
  315. // const url = `${
  316. // /(192|localhost)/.test(location.origin)
  317. // ? "http://192.168.3.114:3000/instrument.html"
  318. // : location.origin + location.pathname
  319. // }#/view-figner`;
  320. // console.log("🚀 ~ url:", url);
  321. // api_openWebView({
  322. // url: url,
  323. // orientation: state.fingeringInfo.orientation || 0,
  324. // });
  325. if (state.playState === "play") {
  326. togglePlay("paused");
  327. setTimeout(() => {
  328. detailData.fingerPreView = true;
  329. }, 500);
  330. return;
  331. }
  332. detailData.fingerPreView = true;
  333. };
  334. const handleCloseFignerView = () => {
  335. if (!query.modelType && detailData.orientation == 1) {
  336. api_setRequestedOrientation(0);
  337. }
  338. detailData.fingerPreView = false;
  339. detailData.fingerPreViewGuide = false;
  340. };
  341. return () => (
  342. <div
  343. class={[styles.detail, state.setting.eyeProtection && "eyeProtection", state.platform === IPlatform.PC && styles.PC]}
  344. style={{
  345. paddingLeft: detailData.paddingLeft,
  346. background: state.setting.camera
  347. ? `rgba(${state.setting.eyeProtection ? '253,244,229' : '255,255,255'} ,${
  348. state.setting.cameraOpacity / 100
  349. }) !important`
  350. : '',
  351. }}
  352. >
  353. <Transition name="van-fade">
  354. {detailData.skeletonLoading && (
  355. <div class={styles.skeleton}>
  356. <Skeleton row={8} />
  357. </div>
  358. )}
  359. </Transition>
  360. <div class={[styles.headHeight, detailData.headerHide && styles.headHide]}>{state.musicRendered && <HeaderTop />}</div>
  361. <div
  362. id="scrollContainer"
  363. style={{ ...fingerConfig.value.container,
  364. height: detailData.headerHide ? "100vh" : "",
  365. }}
  366. class={[styles.container, !state.setting.displayCursor && "hideCursor", browsInfo.xiaomi && styles.xiaomi]}
  367. onClick={(e: Event) => {
  368. e.stopPropagation();
  369. if (state.playState === "play" && state.platform != IPlatform.PC) {
  370. detailData.headerHide = !detailData.headerHide;
  371. }
  372. }}
  373. >
  374. {/* 曲谱渲染 */}
  375. {!detailData.isLoading && <MusicScore onRendered={handleRendered} />}
  376. {/* 指法 */}
  377. {state.setting.displayFingering && state.fingeringInfo?.name && (
  378. <div style={{ ...fingerConfig.value.fingerBox }}>
  379. <Fingering
  380. style={{
  381. background: state.setting.camera
  382. ? `rgba(${state.setting.eyeProtection ? '253,244,229' : '255,255,255'} ,${
  383. state.setting.cameraOpacity / 100
  384. })`
  385. : '',
  386. }}
  387. onOpen={() => handleOpenFignerView()}
  388. />
  389. </div>
  390. )}
  391. </div>
  392. {/* 节拍器 */}
  393. {state.needTick && <Tick />}
  394. {/* 播放 */}
  395. {!detailData.isLoading && <AudioList />}
  396. {/* 评测 */}
  397. {state.modeType === "evaluating" && (
  398. <>
  399. <Evaluating />
  400. {evaluatingData.rendered && <EvaluatModel />}
  401. </>
  402. )}
  403. {/* 跟练模式 */}
  404. {state.modeType === "follow" && (
  405. <>
  406. <FollowPractice />
  407. <FollowModel />
  408. </>
  409. )}
  410. {/* 切换曲谱 */}
  411. {!query.lessonTrainingId && !query.questionId && (
  412. <ToggleMusicSheet />
  413. )}
  414. {state.musicRendered && (
  415. <>
  416. {/* 统计训练时长 */}
  417. {storeData.isApp && <RecordingTime />}
  418. {/* 作业 */}
  419. {query.workRecord && <WorkIndex />}
  420. {/* 曲谱列表 */}
  421. {state.playState == "play" || followData.start || evaluatingData.startBegin || query.workRecord || query.modelType || state.platform === IPlatform.PC ? null : <TheMusicList />}
  422. </>
  423. )}
  424. <Popup
  425. zIndex={5050}
  426. teleport="body"
  427. v-model:show={detailData.fingerPreView}
  428. position="bottom"
  429. onOpened={() => {
  430. detailData.fingerPreViewGuide = true;
  431. }}
  432. >
  433. <ViewFigner
  434. show={detailData.fingerPreViewGuide}
  435. subject={state.fingeringInfo.name}
  436. isComponent={true}
  437. onClose={handleCloseFignerView}
  438. />
  439. </Popup>
  440. </div>
  441. );
  442. },
  443. });